2020-07-02 10:12:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-02 10:12:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-07-28 10:09:19 +00:00
|
|
|
#include <LibGUI/ProcessChooser.h>
|
|
|
|
#include <LibGUI/RunningProcessesModel.h>
|
2020-07-02 10:12:16 +00:00
|
|
|
#include <LibGUI/SortingProxyModel.h>
|
|
|
|
#include <LibGUI/TableView.h>
|
|
|
|
|
2020-07-28 10:09:19 +00:00
|
|
|
namespace GUI {
|
2020-07-02 10:12:16 +00:00
|
|
|
|
2020-07-28 10:09:19 +00:00
|
|
|
ProcessChooser::ProcessChooser(const StringView& window_title, const StringView& button_label, const Gfx::Bitmap* window_icon, GUI::Window* parent_window)
|
2020-07-02 10:12:16 +00:00
|
|
|
: Dialog(parent_window)
|
2020-07-28 10:09:19 +00:00
|
|
|
, m_window_title(window_title)
|
|
|
|
, m_button_label(button_label)
|
|
|
|
, m_window_icon(window_icon)
|
2020-07-02 10:12:16 +00:00
|
|
|
{
|
2020-07-28 10:09:19 +00:00
|
|
|
set_title(m_window_title);
|
2020-07-02 10:12:16 +00:00
|
|
|
|
2020-07-28 10:09:19 +00:00
|
|
|
if (m_window_icon)
|
|
|
|
set_icon(m_window_icon);
|
|
|
|
else if (parent_window)
|
|
|
|
set_icon(parent_window->icon());
|
|
|
|
|
2020-08-15 14:32:11 +00:00
|
|
|
resize(300, 340);
|
|
|
|
center_on_screen();
|
2020-07-02 10:12:16 +00:00
|
|
|
|
|
|
|
auto& widget = set_main_widget<GUI::Widget>();
|
|
|
|
widget.set_fill_with_background_color(true);
|
|
|
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
2020-07-28 10:09:19 +00:00
|
|
|
widget.layout()->set_margins({ 0, 0, 0, 2 });
|
|
|
|
|
2020-08-15 16:54:20 +00:00
|
|
|
m_table_view = widget.add<GUI::TableView>();
|
2020-07-28 10:09:19 +00:00
|
|
|
auto sorting_model = GUI::SortingProxyModel::create(RunningProcessesModel::create());
|
2020-08-16 14:00:07 +00:00
|
|
|
sorting_model->set_sort_role(GUI::ModelRole::Display);
|
2020-08-15 16:54:20 +00:00
|
|
|
m_table_view->set_model(sorting_model);
|
2020-09-17 14:17:14 +00:00
|
|
|
m_table_view->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
|
2020-07-28 10:09:19 +00:00
|
|
|
|
2020-08-15 17:01:58 +00:00
|
|
|
m_table_view->on_activation = [this](const ModelIndex& index) { set_pid_from_index_and_close(index); };
|
|
|
|
|
2020-07-02 10:12:16 +00:00
|
|
|
auto& button_container = widget.add<GUI::Widget>();
|
2020-12-30 00:23:32 +00:00
|
|
|
button_container.set_fixed_height(30);
|
2020-07-02 10:12:16 +00:00
|
|
|
button_container.set_layout<GUI::HorizontalBoxLayout>();
|
2020-07-28 10:09:19 +00:00
|
|
|
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);
|
2020-12-30 00:23:32 +00:00
|
|
|
select_button.set_fixed_size(80, 24);
|
2020-08-15 16:54:20 +00:00
|
|
|
select_button.on_click = [this](auto) {
|
|
|
|
if (m_table_view->selection().is_empty()) {
|
2020-07-28 10:09:19 +00:00
|
|
|
GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
|
2020-07-02 10:12:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-15 16:54:20 +00:00
|
|
|
auto index = m_table_view->selection().first();
|
2020-08-15 17:01:58 +00:00
|
|
|
set_pid_from_index_and_close(index);
|
2020-07-02 10:12:16 +00:00
|
|
|
};
|
|
|
|
auto& cancel_button = button_container.add<GUI::Button>("Cancel");
|
2020-12-30 00:23:32 +00:00
|
|
|
cancel_button.set_fixed_size(80, 24);
|
2020-07-02 10:12:16 +00:00
|
|
|
cancel_button.on_click = [this](auto) {
|
|
|
|
done(ExecCancel);
|
|
|
|
};
|
|
|
|
|
2020-08-15 16:54:20 +00:00
|
|
|
m_table_view->model()->update();
|
2020-08-14 17:18:33 +00:00
|
|
|
|
|
|
|
m_refresh_timer = add<Core::Timer>();
|
|
|
|
|
|
|
|
m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes
|
2020-08-15 16:54:20 +00:00
|
|
|
m_refresh_timer->on_timeout = [this] {
|
2020-08-14 17:18:33 +00:00
|
|
|
auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
|
2020-08-15 16:54:20 +00:00
|
|
|
if (!m_table_view->selection().is_empty()) {
|
2020-08-16 14:14:39 +00:00
|
|
|
auto pid_as_variant = m_table_view->selection().first().data(GUI::ModelRole::Custom);
|
2020-08-14 17:18:33 +00:00
|
|
|
previous_selected_pid = pid_as_variant.as_i32();
|
|
|
|
}
|
|
|
|
|
2020-08-15 16:54:20 +00:00
|
|
|
m_table_view->model()->update();
|
2020-08-14 17:18:33 +00:00
|
|
|
|
|
|
|
if (previous_selected_pid == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-15 16:54:20 +00:00
|
|
|
auto model = m_table_view->model();
|
2020-08-14 17:18:33 +00:00
|
|
|
auto row_count = model->row_count();
|
2020-08-15 16:54:20 +00:00
|
|
|
auto column_index = 1; // Corresponds to PID column in the m_table_view.
|
2020-08-14 17:18:33 +00:00
|
|
|
for (int row_index = 0; row_index < row_count; ++row_index) {
|
|
|
|
auto cell_index = model->index(row_index, column_index);
|
2020-08-16 14:14:39 +00:00
|
|
|
auto pid_as_variant = cell_index.data(GUI::ModelRole::Custom);
|
2020-08-14 17:18:33 +00:00
|
|
|
if (previous_selected_pid == pid_as_variant.as_i32()) {
|
2020-08-15 16:54:20 +00:00
|
|
|
m_table_view->selection().set(cell_index); // Set only if PIDs are matched.
|
2020-08-14 17:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-02 10:12:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 17:01:58 +00:00
|
|
|
void ProcessChooser::set_pid_from_index_and_close(const ModelIndex& index)
|
|
|
|
{
|
2020-08-16 14:14:39 +00:00
|
|
|
m_pid = index.data(GUI::ModelRole::Custom).as_i32();
|
2020-08-15 17:01:58 +00:00
|
|
|
done(ExecOK);
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:09:19 +00:00
|
|
|
ProcessChooser::~ProcessChooser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-02 10:12:16 +00:00
|
|
|
}
|