mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
DevTools+LibGUI: Make ProcessChooser a general Dialog in LibGUI
Moves ProcessChooser and RunningProcessesModel to LibGUI and generalizes their construction for use by other apps. Updates Profiler to reflect the change and use its new icons.
This commit is contained in:
parent
5cfbf88b4d
commit
6448f94372
7 changed files with 57 additions and 26 deletions
|
@ -51,9 +51,11 @@ set(SOURCES
|
|||
MultiView.cpp
|
||||
Notification.cpp
|
||||
Painter.cpp
|
||||
ProcessChooser.cpp
|
||||
ProgressBar.cpp
|
||||
RadioButton.cpp
|
||||
ResizeCorner.cpp
|
||||
RunningProcessesModel.cpp
|
||||
ScrollableWidget.cpp
|
||||
ScrollBar.cpp
|
||||
Shortcut.cpp
|
||||
|
|
100
Libraries/LibGUI/ProcessChooser.cpp
Normal file
100
Libraries/LibGUI/ProcessChooser.cpp
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/Desktop.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/ProcessChooser.h>
|
||||
#include <LibGUI/RunningProcessesModel.h>
|
||||
#include <LibGUI/SortingProxyModel.h>
|
||||
#include <LibGUI/TableView.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
ProcessChooser::ProcessChooser(const StringView& window_title, const StringView& button_label, const Gfx::Bitmap* window_icon, GUI::Window* parent_window)
|
||||
: Dialog(parent_window)
|
||||
, m_window_title(window_title)
|
||||
, m_button_label(button_label)
|
||||
, m_window_icon(window_icon)
|
||||
{
|
||||
set_title(m_window_title);
|
||||
|
||||
if (m_window_icon)
|
||||
set_icon(m_window_icon);
|
||||
else if (parent_window)
|
||||
set_icon(parent_window->icon());
|
||||
|
||||
Gfx::IntRect window_rect { 0, 0, 300, 340 };
|
||||
window_rect.center_within(GUI::Desktop::the().rect());
|
||||
set_rect(window_rect);
|
||||
|
||||
auto& widget = set_main_widget<GUI::Widget>();
|
||||
widget.set_fill_with_background_color(true);
|
||||
widget.set_layout<GUI::VerticalBoxLayout>();
|
||||
widget.layout()->set_margins({ 0, 0, 0, 2 });
|
||||
|
||||
auto& table_view = widget.add<GUI::TableView>();
|
||||
auto sorting_model = GUI::SortingProxyModel::create(RunningProcessesModel::create());
|
||||
sorting_model->set_sort_role(GUI::Model::Role::Display);
|
||||
sorting_model->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
|
||||
table_view.set_model(sorting_model);
|
||||
|
||||
auto& button_container = widget.add<GUI::Widget>();
|
||||
button_container.set_preferred_size(0, 30);
|
||||
button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
|
||||
button_container.set_layout<GUI::HorizontalBoxLayout>();
|
||||
button_container.layout()->set_margins({ 0, 0, 4, 0 });
|
||||
button_container.layout()->add_spacer();
|
||||
|
||||
auto& select_button = button_container.add<GUI::Button>(m_button_label);
|
||||
select_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||
select_button.set_preferred_size(80, 24);
|
||||
select_button.on_click = [&](auto) {
|
||||
if (table_view.selection().is_empty()) {
|
||||
GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
auto index = table_view.selection().first();
|
||||
auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom);
|
||||
m_pid = pid_as_variant.as_i32();
|
||||
done(ExecOK);
|
||||
};
|
||||
auto& cancel_button = button_container.add<GUI::Button>("Cancel");
|
||||
cancel_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||
cancel_button.set_preferred_size(80, 24);
|
||||
cancel_button.on_click = [this](auto) {
|
||||
done(ExecCancel);
|
||||
};
|
||||
|
||||
table_view.model()->update();
|
||||
}
|
||||
|
||||
ProcessChooser::~ProcessChooser()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
51
Libraries/LibGUI/ProcessChooser.h
Normal file
51
Libraries/LibGUI/ProcessChooser.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibGUI/Dialog.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
class ProcessChooser final : public GUI::Dialog {
|
||||
C_OBJECT(ProcessChooser);
|
||||
|
||||
public:
|
||||
virtual ~ProcessChooser() override;
|
||||
|
||||
pid_t pid() const { return m_pid; }
|
||||
|
||||
private:
|
||||
ProcessChooser(const StringView& window_title = "Process Chooser", const StringView& button_label = "Select", const Gfx::Bitmap* window_icon = nullptr, GUI::Window* parent_window = nullptr);
|
||||
|
||||
pid_t m_pid { 0 };
|
||||
|
||||
String m_window_title;
|
||||
String m_button_label;
|
||||
RefPtr<Gfx::Bitmap> m_window_icon;
|
||||
};
|
||||
|
||||
}
|
121
Libraries/LibGUI/RunningProcessesModel.cpp
Normal file
121
Libraries/LibGUI/RunningProcessesModel.cpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/SharedBuffer.h>
|
||||
#include <LibCore/ProcessStatisticsReader.h>
|
||||
#include <LibGUI/RunningProcessesModel.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
NonnullRefPtr<RunningProcessesModel> RunningProcessesModel::create()
|
||||
{
|
||||
return adopt(*new RunningProcessesModel);
|
||||
}
|
||||
|
||||
RunningProcessesModel::RunningProcessesModel()
|
||||
{
|
||||
}
|
||||
|
||||
RunningProcessesModel::~RunningProcessesModel()
|
||||
{
|
||||
}
|
||||
|
||||
void RunningProcessesModel::update()
|
||||
{
|
||||
m_processes.clear();
|
||||
|
||||
Core::ProcessStatisticsReader reader;
|
||||
auto processes = reader.get_all();
|
||||
|
||||
for (auto& it : processes) {
|
||||
Process process;
|
||||
process.pid = it.value.pid;
|
||||
process.uid = it.value.uid;
|
||||
if (it.value.icon_id != -1) {
|
||||
if (auto icon_buffer = SharedBuffer::create_from_shbuf_id(it.value.icon_id)) {
|
||||
process.icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *icon_buffer, { 16, 16 });
|
||||
}
|
||||
}
|
||||
process.name = it.value.name;
|
||||
|
||||
m_processes.append(move(process));
|
||||
}
|
||||
|
||||
did_update();
|
||||
}
|
||||
|
||||
int RunningProcessesModel::row_count(const GUI::ModelIndex&) const
|
||||
{
|
||||
return m_processes.size();
|
||||
}
|
||||
|
||||
int RunningProcessesModel::column_count(const GUI::ModelIndex&) const
|
||||
{
|
||||
return Column::__Count;
|
||||
}
|
||||
|
||||
String RunningProcessesModel::column_name(int column_index) const
|
||||
{
|
||||
switch (column_index) {
|
||||
case Column::Icon:
|
||||
return {};
|
||||
case Column::PID:
|
||||
return "PID";
|
||||
case Column::UID:
|
||||
return "UID";
|
||||
case Column::Name:
|
||||
return "Name";
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
GUI::Variant RunningProcessesModel::data(const GUI::ModelIndex& index, Role role) const
|
||||
{
|
||||
auto& process = m_processes[index.row()];
|
||||
|
||||
if (role == Role::Custom) {
|
||||
return process.pid;
|
||||
}
|
||||
|
||||
if (role == Role::Display) {
|
||||
switch (index.column()) {
|
||||
case Column::Icon:
|
||||
if (!process.icon)
|
||||
return GUI::Icon();
|
||||
return GUI::Icon(*process.icon);
|
||||
case Column::PID:
|
||||
return process.pid;
|
||||
case Column::UID:
|
||||
return process.uid;
|
||||
case Column::Name:
|
||||
return process.name;
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
65
Libraries/LibGUI/RunningProcessesModel.h
Normal file
65
Libraries/LibGUI/RunningProcessesModel.h
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
class RunningProcessesModel final : public GUI::Model {
|
||||
public:
|
||||
static NonnullRefPtr<RunningProcessesModel> create();
|
||||
virtual ~RunningProcessesModel() override;
|
||||
|
||||
enum Column {
|
||||
Icon,
|
||||
PID,
|
||||
UID,
|
||||
Name,
|
||||
__Count,
|
||||
};
|
||||
|
||||
virtual int row_count(const GUI::ModelIndex&) const override;
|
||||
virtual int column_count(const GUI::ModelIndex&) const override;
|
||||
virtual String column_name(int column_index) const override;
|
||||
virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override;
|
||||
virtual void update() override;
|
||||
|
||||
private:
|
||||
RunningProcessesModel();
|
||||
|
||||
struct Process {
|
||||
pid_t pid;
|
||||
uid_t uid;
|
||||
RefPtr<Gfx::Bitmap> icon;
|
||||
String name;
|
||||
};
|
||||
Vector<Process> m_processes;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue