ProcessChooser.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. auto& table_view = widget.add<GUI::TableView>();
  52. auto sorting_model = GUI::SortingProxyModel::create(RunningProcessesModel::create());
  53. sorting_model->set_sort_role(GUI::Model::Role::Display);
  54. sorting_model->set_key_column_and_sort_order(RunningProcessesModel::Column::PID, GUI::SortOrder::Descending);
  55. table_view.set_model(sorting_model);
  56. auto& button_container = widget.add<GUI::Widget>();
  57. button_container.set_preferred_size(0, 30);
  58. button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  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_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  64. select_button.set_preferred_size(80, 24);
  65. select_button.on_click = [&](auto) {
  66. if (table_view.selection().is_empty()) {
  67. GUI::MessageBox::show(this, "No process selected!", m_window_title, GUI::MessageBox::Type::Error);
  68. return;
  69. }
  70. auto index = table_view.selection().first();
  71. auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom);
  72. m_pid = pid_as_variant.as_i32();
  73. done(ExecOK);
  74. };
  75. auto& cancel_button = button_container.add<GUI::Button>("Cancel");
  76. cancel_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  77. cancel_button.set_preferred_size(80, 24);
  78. cancel_button.on_click = [this](auto) {
  79. done(ExecCancel);
  80. };
  81. table_view.model()->update();
  82. m_refresh_timer = add<Core::Timer>();
  83. m_refresh_timer->start(m_refresh_interval); // Start the timer to update the processes
  84. m_refresh_timer->on_timeout = [&table_view] {
  85. auto previous_selected_pid = -1; // Store the selection index to not to clear the selection upon update.
  86. if (!table_view.selection().is_empty()) {
  87. auto pid_as_variant = table_view.model()->data(table_view.selection().first(), GUI::Model::Role::Custom);
  88. previous_selected_pid = pid_as_variant.as_i32();
  89. }
  90. table_view.model()->update();
  91. if (previous_selected_pid == -1) {
  92. return;
  93. }
  94. auto model = table_view.model();
  95. auto row_count = model->row_count();
  96. auto column_index = 1; // Corresponds to PID column in the table_view.
  97. for (int row_index = 0; row_index < row_count; ++row_index) {
  98. auto cell_index = model->index(row_index, column_index);
  99. auto pid_as_variant = model->data(cell_index, GUI::Model::Role::Custom);
  100. if (previous_selected_pid == pid_as_variant.as_i32()) {
  101. table_view.selection().set(cell_index); // Set only if PIDs are matched.
  102. }
  103. }
  104. };
  105. }
  106. ProcessChooser::~ProcessChooser()
  107. {
  108. }
  109. }