MouseSettingsWindow.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2020, Idan Horowitz <idan.horowitz@gmail.com>
  3. * Copyright (c) 2021, the SerenityOS developers
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "MouseSettingsWindow.h"
  28. #include <Applications/MouseSettings/MouseSettingsWindowGML.h>
  29. #include <LibGUI/Application.h>
  30. #include <LibGUI/Button.h>
  31. #include <LibGUI/Event.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/Widget.h>
  34. #include <LibGUI/Window.h>
  35. #include <LibGUI/WindowServerConnection.h>
  36. #include <WindowServer/Screen.h>
  37. #include <WindowServer/WindowManager.h>
  38. constexpr double speed_slider_scale = 100.0;
  39. constexpr int default_scroll_length = 4;
  40. constexpr int double_click_speed_default = 250;
  41. void MouseSettingsWindow::update_window_server()
  42. {
  43. const float factor = m_speed_slider->value() / speed_slider_scale;
  44. GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetMouseAcceleration>(factor);
  45. GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetScrollStepSize>(m_scroll_length_spinbox->value());
  46. GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetDoubleClickSpeed>(m_double_click_speed_slider->value());
  47. }
  48. void MouseSettingsWindow::reset_default_values()
  49. {
  50. m_speed_slider->set_value(speed_slider_scale);
  51. m_scroll_length_spinbox->set_value(default_scroll_length);
  52. m_double_click_speed_slider->set_value(double_click_speed_default);
  53. update_window_server();
  54. }
  55. MouseSettingsWindow::MouseSettingsWindow()
  56. {
  57. auto& main_widget = set_main_widget<GUI::Widget>();
  58. main_widget.load_from_gml(mouse_settings_window_gml);
  59. m_speed_label = *main_widget.find_descendant_of_type_named<GUI::Label>("speed_label");
  60. m_speed_slider = *main_widget.find_descendant_of_type_named<GUI::HorizontalSlider>("speed_slider");
  61. m_speed_slider->set_range(WindowServer::mouse_accel_min * speed_slider_scale, WindowServer::mouse_accel_max * speed_slider_scale);
  62. m_speed_slider->on_change = [&](const int value) {
  63. m_speed_label->set_text(String::formatted("{} %", value));
  64. };
  65. const int slider_value = float { speed_slider_scale } * GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::GetMouseAcceleration>()->factor();
  66. m_speed_slider->set_value(slider_value);
  67. m_scroll_length_spinbox = *main_widget.find_descendant_of_type_named<GUI::SpinBox>("scroll_length_spinbox");
  68. m_scroll_length_spinbox->set_min(WindowServer::scroll_step_size_min);
  69. m_scroll_length_spinbox->set_value(GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::GetScrollStepSize>()->step_size());
  70. m_double_click_speed_label = *main_widget.find_descendant_of_type_named<GUI::Label>("double_click_speed_label");
  71. m_double_click_speed_slider = *main_widget.find_descendant_of_type_named<GUI::HorizontalSlider>("double_click_speed_slider");
  72. m_double_click_speed_slider->set_min(WindowServer::double_click_speed_min);
  73. m_double_click_speed_slider->set_max(WindowServer::double_click_speed_max);
  74. m_double_click_speed_slider->on_change = [&](const int value) {
  75. m_double_click_speed_label->set_text(String::formatted("{} ms", value));
  76. };
  77. m_double_click_speed_slider->set_value(GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::GetDoubleClickSpeed>()->speed());
  78. m_ok_button = *main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  79. m_ok_button->on_click = [this](auto) {
  80. update_window_server();
  81. GUI::Application::the()->quit();
  82. };
  83. m_apply_button = *main_widget.find_descendant_of_type_named<GUI::Button>("apply_button");
  84. m_apply_button->on_click = [this](auto) {
  85. update_window_server();
  86. };
  87. m_reset_button = *main_widget.find_descendant_of_type_named<GUI::Button>("reset_button");
  88. m_reset_button->on_click = [this](auto) {
  89. reset_default_values();
  90. };
  91. }
  92. MouseSettingsWindow::~MouseSettingsWindow()
  93. {
  94. }