ProcessChooser.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(StringView window_title, 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 = MUST(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.layout()->set_margins({ 0, 4, 0 });
  42. button_container.layout()->add_spacer();
  43. auto& select_button = button_container.add<GUI::Button>(m_button_label);
  44. select_button.set_fixed_width(80);
  45. select_button.on_click = [this](auto) {
  46. if (m_table_view->selection().is_empty()) {
  47. GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
  48. return;
  49. }
  50. auto index = m_table_view->selection().first();
  51. set_pid_from_index_and_close(index);
  52. };
  53. auto& cancel_button = button_container.add<GUI::Button>("Cancel");
  54. cancel_button.set_fixed_width(80);
  55. cancel_button.on_click = [this](auto) {
  56. done(ExecCancel);
  57. };
  58. m_process_model->update();
  59. m_refresh_timer = add<Core::Timer>();
  60. m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes
  61. m_refresh_timer->on_timeout = [this] {
  62. auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
  63. if (!m_table_view->selection().is_empty()) {
  64. auto pid_as_variant = m_table_view->selection().first().data(GUI::ModelRole::Custom);
  65. previous_selected_pid = pid_as_variant.as_i32();
  66. }
  67. m_process_model->update();
  68. if (previous_selected_pid == -1) {
  69. return;
  70. }
  71. auto model = m_table_view->model();
  72. auto row_count = model->row_count();
  73. auto column_index = 1; // Corresponds to PID column in the m_table_view.
  74. for (int row_index = 0; row_index < row_count; ++row_index) {
  75. auto cell_index = model->index(row_index, column_index);
  76. auto pid_as_variant = cell_index.data(GUI::ModelRole::Custom);
  77. if (previous_selected_pid == pid_as_variant.as_i32()) {
  78. m_table_view->selection().set(cell_index); // Set only if PIDs are matched.
  79. }
  80. }
  81. };
  82. }
  83. void ProcessChooser::set_pid_from_index_and_close(const ModelIndex& index)
  84. {
  85. m_pid = index.data(GUI::ModelRole::Custom).as_i32();
  86. done(ExecOK);
  87. }
  88. ProcessChooser::~ProcessChooser()
  89. {
  90. }
  91. }