ProcessChooser.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <LibGUI/BoxLayout.h>
  8. #include <LibGUI/Button.h>
  9. #include <LibGUI/MessageBox.h>
  10. #include <LibGUI/ProcessChooser.h>
  11. #include <LibGUI/RunningProcessesModel.h>
  12. #include <LibGUI/SortingProxyModel.h>
  13. #include <LibGUI/TableView.h>
  14. namespace GUI {
  15. ProcessChooser::ProcessChooser(StringView window_title, StringView button_label, Gfx::Bitmap const* window_icon, GUI::Window* parent_window)
  16. : Dialog(parent_window)
  17. , m_window_title(window_title)
  18. , m_button_label(button_label)
  19. , m_window_icon(window_icon)
  20. {
  21. set_title(m_window_title);
  22. if (m_window_icon)
  23. set_icon(m_window_icon);
  24. else if (parent_window)
  25. set_icon(parent_window->icon());
  26. resize(300, 340);
  27. center_on_screen();
  28. auto& widget = set_main_widget<GUI::Widget>();
  29. widget.set_fill_with_background_color(true);
  30. widget.set_layout<GUI::VerticalBoxLayout>();
  31. m_table_view = widget.add<GUI::TableView>();
  32. auto process_model = RunningProcessesModel::create();
  33. auto sorting_model = MUST(GUI::SortingProxyModel::create(process_model));
  34. sorting_model->set_sort_role(GUI::ModelRole::Display);
  35. m_table_view->set_model(sorting_model);
  36. m_table_view->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
  37. m_process_model = process_model;
  38. m_table_view->on_activation = [this](ModelIndex const& index) { set_pid_from_index_and_close(index); };
  39. auto& button_container = widget.add<GUI::Widget>();
  40. button_container.set_fixed_height(30);
  41. button_container.set_layout<GUI::HorizontalBoxLayout>();
  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(ExecResult::Cancel);
  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(ModelIndex const& index)
  85. {
  86. m_pid = index.data(GUI::ModelRole::Custom).as_i32();
  87. done(ExecResult::OK);
  88. }
  89. }