Serenity Operating System
at master 107 lines 4.0 kB view raw
1/* 2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <LibGUI/BoxLayout.h> 9#include <LibGUI/Button.h> 10#include <LibGUI/MessageBox.h> 11#include <LibGUI/ProcessChooser.h> 12#include <LibGUI/RunningProcessesModel.h> 13#include <LibGUI/SortingProxyModel.h> 14#include <LibGUI/TableView.h> 15 16namespace GUI { 17 18ProcessChooser::ProcessChooser(StringView window_title, String button_label, Gfx::Bitmap const* window_icon, GUI::Window* parent_window) 19 : Dialog(parent_window) 20 , m_window_title(window_title) 21 , m_button_label(move(button_label)) 22 , m_window_icon(window_icon) 23{ 24 set_title(m_window_title); 25 26 if (m_window_icon) 27 set_icon(m_window_icon); 28 else if (parent_window) 29 set_icon(parent_window->icon()); 30 31 resize(300, 340); 32 center_on_screen(); 33 34 auto widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors(); 35 widget->set_fill_with_background_color(true); 36 widget->set_layout<GUI::VerticalBoxLayout>(); 37 38 m_table_view = widget->add<GUI::TableView>(); 39 auto process_model = RunningProcessesModel::create(); 40 auto sorting_model = MUST(GUI::SortingProxyModel::create(process_model)); 41 sorting_model->set_sort_role(GUI::ModelRole::Display); 42 m_table_view->set_model(sorting_model); 43 m_table_view->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending); 44 45 m_process_model = process_model; 46 47 m_table_view->on_activation = [this](ModelIndex const& index) { set_pid_from_index_and_close(index); }; 48 49 auto& button_container = widget->add<GUI::Widget>(); 50 button_container.set_fixed_height(30); 51 button_container.set_layout<GUI::HorizontalBoxLayout>(GUI::Margins { 0, 4, 0 }); 52 button_container.add_spacer().release_value_but_fixme_should_propagate_errors(); 53 54 auto& select_button = button_container.add<GUI::Button>(m_button_label); 55 select_button.set_fixed_width(80); 56 select_button.on_click = [this](auto) { 57 if (m_table_view->selection().is_empty()) { 58 GUI::MessageBox::show(this, "No process selected!"sv, m_window_title, GUI::MessageBox::Type::Error); 59 return; 60 } 61 auto index = m_table_view->selection().first(); 62 set_pid_from_index_and_close(index); 63 }; 64 auto& cancel_button = button_container.add<GUI::Button>("Cancel"_short_string); 65 cancel_button.set_fixed_width(80); 66 cancel_button.on_click = [this](auto) { 67 done(ExecResult::Cancel); 68 }; 69 70 m_process_model->update(); 71 72 m_refresh_timer = add<Core::Timer>(); 73 74 m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes 75 m_refresh_timer->on_timeout = [this] { 76 auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update. 77 if (!m_table_view->selection().is_empty()) { 78 auto pid_as_variant = m_table_view->selection().first().data(GUI::ModelRole::Custom); 79 previous_selected_pid = pid_as_variant.as_i32(); 80 } 81 82 m_process_model->update(); 83 84 if (previous_selected_pid == -1) { 85 return; 86 } 87 88 auto model = m_table_view->model(); 89 auto row_count = model->row_count(); 90 auto column_index = 1; // Corresponds to PID column in the m_table_view. 91 for (int row_index = 0; row_index < row_count; ++row_index) { 92 auto cell_index = model->index(row_index, column_index); 93 auto pid_as_variant = cell_index.data(GUI::ModelRole::Custom); 94 if (previous_selected_pid == pid_as_variant.as_i32()) { 95 m_table_view->selection().set(cell_index); // Set only if PIDs are matched. 96 } 97 } 98 }; 99} 100 101void ProcessChooser::set_pid_from_index_and_close(ModelIndex const& index) 102{ 103 m_pid = index.data(GUI::ModelRole::Custom).as_i32(); 104 done(ExecResult::OK); 105} 106 107}