ProcessChooser.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Desktop.h>
  29. #include <LibGUI/MessageBox.h>
  30. #include <LibGUI/ProcessChooser.h>
  31. #include <LibGUI/RunningProcessesModel.h>
  32. #include <LibGUI/SortingProxyModel.h>
  33. #include <LibGUI/TableView.h>
  34. namespace GUI {
  35. ProcessChooser::ProcessChooser(const StringView& window_title, const StringView& button_label, const Gfx::Bitmap* window_icon, GUI::Window* parent_window)
  36. : Dialog(parent_window)
  37. , m_window_title(window_title)
  38. , m_button_label(button_label)
  39. , m_window_icon(window_icon)
  40. {
  41. set_title(m_window_title);
  42. if (m_window_icon)
  43. set_icon(m_window_icon);
  44. else if (parent_window)
  45. set_icon(parent_window->icon());
  46. Gfx::IntRect window_rect { 0, 0, 300, 340 };
  47. window_rect.center_within(GUI::Desktop::the().rect());
  48. set_rect(window_rect);
  49. auto& widget = set_main_widget<GUI::Widget>();
  50. widget.set_fill_with_background_color(true);
  51. widget.set_layout<GUI::VerticalBoxLayout>();
  52. widget.layout()->set_margins({ 0, 0, 0, 2 });
  53. auto& table_view = widget.add<GUI::TableView>();
  54. auto sorting_model = GUI::SortingProxyModel::create(RunningProcessesModel::create());
  55. sorting_model->set_sort_role(GUI::Model::Role::Display);
  56. sorting_model->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
  57. table_view.set_model(sorting_model);
  58. auto& button_container = widget.add<GUI::Widget>();
  59. button_container.set_preferred_size(0, 30);
  60. button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  61. button_container.set_layout<GUI::HorizontalBoxLayout>();
  62. button_container.layout()->set_margins({ 0, 0, 4, 0 });
  63. button_container.layout()->add_spacer();
  64. auto& select_button = button_container.add<GUI::Button>(m_button_label);
  65. select_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  66. select_button.set_preferred_size(80, 24);
  67. select_button.on_click = [&](auto) {
  68. if (table_view.selection().is_empty()) {
  69. GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
  70. return;
  71. }
  72. auto index = table_view.selection().first();
  73. auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom);
  74. m_pid = pid_as_variant.as_i32();
  75. done(ExecOK);
  76. };
  77. auto& cancel_button = button_container.add<GUI::Button>("Cancel");
  78. cancel_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  79. cancel_button.set_preferred_size(80, 24);
  80. cancel_button.on_click = [this](auto) {
  81. done(ExecCancel);
  82. };
  83. table_view.model()->update();
  84. m_refresh_timer = add<Core::Timer>();
  85. m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes
  86. m_refresh_timer->on_timeout = [&table_view] {
  87. auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
  88. if (!table_view.selection().is_empty()) {
  89. auto pid_as_variant = table_view.model()->data(table_view.selection().first(), GUI::Model::Role::Custom);
  90. previous_selected_pid = pid_as_variant.as_i32();
  91. }
  92. table_view.model()->update();
  93. if (previous_selected_pid == -1) {
  94. return;
  95. }
  96. auto model = table_view.model();
  97. auto row_count = model->row_count();
  98. auto column_index = 1; // Corresponds to PID column in the table_view.
  99. for (int row_index = 0; row_index < row_count; ++row_index) {
  100. auto cell_index = model->index(row_index, column_index);
  101. auto pid_as_variant = model->data(cell_index, GUI::Model::Role::Custom);
  102. if (previous_selected_pid == pid_as_variant.as_i32()) {
  103. table_view.selection().set(cell_index); // Set only if PIDs are matched.
  104. }
  105. }
  106. };
  107. }
  108. ProcessChooser::~ProcessChooser()
  109. {
  110. }
  111. }