ProcessChooser.cpp 3.8 KB

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