ProcessChooser.cpp 3.8 KB

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