MouseWidget.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MouseWidget.h"
  7. #include <Applications/MouseSettings/MouseWidgetGML.h>
  8. #include <LibGUI/ConnectionToWindowServer.h>
  9. #include <LibGUI/Label.h>
  10. #include <LibGUI/Slider.h>
  11. #include <LibGUI/SpinBox.h>
  12. #include <WindowServer/Screen.h>
  13. #include <WindowServer/WindowManager.h>
  14. constexpr double speed_slider_scale = 100.0;
  15. constexpr int default_scroll_length = 4;
  16. constexpr int double_click_speed_default = 250;
  17. MouseWidget::MouseWidget()
  18. {
  19. load_from_gml(mouse_widget_gml);
  20. m_speed_label = *find_descendant_of_type_named<GUI::Label>("speed_label");
  21. m_speed_slider = *find_descendant_of_type_named<GUI::HorizontalSlider>("speed_slider");
  22. m_speed_slider->set_range(WindowServer::mouse_accel_min * speed_slider_scale, WindowServer::mouse_accel_max * speed_slider_scale);
  23. m_speed_slider->on_change = [&](int value) {
  24. m_speed_label->set_text(String::formatted("{} %", value));
  25. };
  26. int const slider_value = float { speed_slider_scale } * GUI::ConnectionToWindowServer::the().get_mouse_acceleration();
  27. m_speed_slider->set_value(slider_value);
  28. m_scroll_length_spinbox = *find_descendant_of_type_named<GUI::SpinBox>("scroll_length_spinbox");
  29. m_scroll_length_spinbox->set_min(WindowServer::scroll_step_size_min);
  30. m_scroll_length_spinbox->set_value(GUI::ConnectionToWindowServer::the().get_scroll_step_size());
  31. m_double_click_arrow_widget = *find_descendant_of_type_named<MouseSettings::DoubleClickArrowWidget>("double_click_arrow_widget");
  32. m_double_click_speed_label = *find_descendant_of_type_named<GUI::Label>("double_click_speed_label");
  33. m_double_click_speed_slider = *find_descendant_of_type_named<GUI::HorizontalSlider>("double_click_speed_slider");
  34. m_double_click_speed_slider->set_min(WindowServer::double_click_speed_min);
  35. m_double_click_speed_slider->set_max(WindowServer::double_click_speed_max);
  36. m_double_click_speed_slider->on_change = [&](int speed) {
  37. m_double_click_arrow_widget->set_double_click_speed(speed);
  38. m_double_click_speed_label->set_text(String::formatted("{} ms", speed));
  39. };
  40. m_double_click_speed_slider->set_value(GUI::ConnectionToWindowServer::the().get_double_click_speed());
  41. m_switch_buttons_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("switch_buttons_input");
  42. m_switch_buttons_checkbox->set_checked(GUI::ConnectionToWindowServer::the().get_buttons_switched());
  43. }
  44. void MouseWidget::apply_settings()
  45. {
  46. float const factor = m_speed_slider->value() / speed_slider_scale;
  47. GUI::ConnectionToWindowServer::the().async_set_mouse_acceleration(factor);
  48. GUI::ConnectionToWindowServer::the().async_set_scroll_step_size(m_scroll_length_spinbox->value());
  49. GUI::ConnectionToWindowServer::the().async_set_double_click_speed(m_double_click_speed_slider->value());
  50. GUI::ConnectionToWindowServer::the().async_set_buttons_switched(m_switch_buttons_checkbox->is_checked());
  51. }
  52. void MouseWidget::reset_default_values()
  53. {
  54. m_speed_slider->set_value(speed_slider_scale);
  55. m_scroll_length_spinbox->set_value(default_scroll_length);
  56. m_double_click_speed_slider->set_value(double_click_speed_default);
  57. m_switch_buttons_checkbox->set_checked(false);
  58. }