ProcessChooser.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/MessageBox.h>
  29. #include <LibGUI/ProcessChooser.h>
  30. #include <LibGUI/RunningProcessesModel.h>
  31. #include <LibGUI/SortingProxyModel.h>
  32. #include <LibGUI/TableView.h>
  33. namespace GUI {
  34. ProcessChooser::ProcessChooser(const StringView& window_title, const StringView& button_label, const Gfx::Bitmap* window_icon, GUI::Window* parent_window)
  35. : Dialog(parent_window)
  36. , m_window_title(window_title)
  37. , m_button_label(button_label)
  38. , m_window_icon(window_icon)
  39. {
  40. set_title(m_window_title);
  41. if (m_window_icon)
  42. set_icon(m_window_icon);
  43. else if (parent_window)
  44. set_icon(parent_window->icon());
  45. resize(300, 340);
  46. center_on_screen();
  47. auto& widget = set_main_widget<GUI::Widget>();
  48. widget.set_fill_with_background_color(true);
  49. widget.set_layout<GUI::VerticalBoxLayout>();
  50. widget.layout()->set_margins({ 0, 0, 0, 2 });
  51. m_table_view = widget.add<GUI::TableView>();
  52. auto sorting_model = GUI::SortingProxyModel::create(RunningProcessesModel::create());
  53. sorting_model->set_sort_role(GUI::ModelRole::Display);
  54. m_table_view->set_model(sorting_model);
  55. m_table_view->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
  56. m_table_view->on_activation = [this](const ModelIndex& index) { set_pid_from_index_and_close(index); };
  57. auto& button_container = widget.add<GUI::Widget>();
  58. button_container.set_fixed_height(30);
  59. button_container.set_layout<GUI::HorizontalBoxLayout>();
  60. button_container.layout()->set_margins({ 0, 0, 4, 0 });
  61. button_container.layout()->add_spacer();
  62. auto& select_button = button_container.add<GUI::Button>(m_button_label);
  63. select_button.set_fixed_size(80, 24);
  64. select_button.on_click = [this](auto) {
  65. if (m_table_view->selection().is_empty()) {
  66. GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
  67. return;
  68. }
  69. auto index = m_table_view->selection().first();
  70. set_pid_from_index_and_close(index);
  71. };
  72. auto& cancel_button = button_container.add<GUI::Button>("Cancel");
  73. cancel_button.set_fixed_size(80, 24);
  74. cancel_button.on_click = [this](auto) {
  75. done(ExecCancel);
  76. };
  77. m_table_view->model()->update();
  78. m_refresh_timer = add<Core::Timer>();
  79. m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes
  80. m_refresh_timer->on_timeout = [this] {
  81. auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
  82. if (!m_table_view->selection().is_empty()) {
  83. auto pid_as_variant = m_table_view->selection().first().data(GUI::ModelRole::Custom);
  84. previous_selected_pid = pid_as_variant.as_i32();
  85. }
  86. m_table_view->model()->update();
  87. if (previous_selected_pid == -1) {
  88. return;
  89. }
  90. auto model = m_table_view->model();
  91. auto row_count = model->row_count();
  92. auto column_index = 1; // Corresponds to PID column in the m_table_view.
  93. for (int row_index = 0; row_index < row_count; ++row_index) {
  94. auto cell_index = model->index(row_index, column_index);
  95. auto pid_as_variant = cell_index.data(GUI::ModelRole::Custom);
  96. if (previous_selected_pid == pid_as_variant.as_i32()) {
  97. m_table_view->selection().set(cell_index); // Set only if PIDs are matched.
  98. }
  99. }
  100. };
  101. }
  102. void ProcessChooser::set_pid_from_index_and_close(const ModelIndex& index)
  103. {
  104. m_pid = index.data(GUI::ModelRole::Custom).as_i32();
  105. done(ExecOK);
  106. }
  107. ProcessChooser::~ProcessChooser()
  108. {
  109. }
  110. }