ProcessChooser.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
  55. m_table_view->set_model(sorting_model);
  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_preferred_size(0, 30);
  59. button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  60. button_container.set_layout<GUI::HorizontalBoxLayout>();
  61. button_container.layout()->set_margins({ 0, 0, 4, 0 });
  62. button_container.layout()->add_spacer();
  63. auto& select_button = button_container.add<GUI::Button>(m_button_label);
  64. select_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  65. select_button.set_preferred_size(80, 24);
  66. select_button.on_click = [this](auto) {
  67. if (m_table_view->selection().is_empty()) {
  68. GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
  69. return;
  70. }
  71. auto index = m_table_view->selection().first();
  72. set_pid_from_index_and_close(index);
  73. };
  74. auto& cancel_button = button_container.add<GUI::Button>("Cancel");
  75. cancel_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  76. cancel_button.set_preferred_size(80, 24);
  77. cancel_button.on_click = [this](auto) {
  78. done(ExecCancel);
  79. };
  80. m_table_view->model()->update();
  81. m_refresh_timer = add<Core::Timer>();
  82. m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes
  83. m_refresh_timer->on_timeout = [this] {
  84. auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
  85. if (!m_table_view->selection().is_empty()) {
  86. auto pid_as_variant = m_table_view->selection().first().data(GUI::ModelRole::Custom);
  87. previous_selected_pid = pid_as_variant.as_i32();
  88. }
  89. m_table_view->model()->update();
  90. if (previous_selected_pid == -1) {
  91. return;
  92. }
  93. auto model = m_table_view->model();
  94. auto row_count = model->row_count();
  95. auto column_index = 1; // Corresponds to PID column in the m_table_view.
  96. for (int row_index = 0; row_index < row_count; ++row_index) {
  97. auto cell_index = model->index(row_index, column_index);
  98. auto pid_as_variant = cell_index.data(GUI::ModelRole::Custom);
  99. if (previous_selected_pid == pid_as_variant.as_i32()) {
  100. m_table_view->selection().set(cell_index); // Set only if PIDs are matched.
  101. }
  102. }
  103. };
  104. }
  105. void ProcessChooser::set_pid_from_index_and_close(const ModelIndex& index)
  106. {
  107. m_pid = index.data(GUI::ModelRole::Custom).as_i32();
  108. done(ExecOK);
  109. }
  110. ProcessChooser::~ProcessChooser()
  111. {
  112. }
  113. }