Slider.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StdLibExtras.h>
  8. #include <LibGUI/Painter.h>
  9. #include <LibGUI/Slider.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibGfx/StylePainter.h>
  12. REGISTER_WIDGET(GUI, HorizontalSlider)
  13. REGISTER_WIDGET(GUI, Slider)
  14. REGISTER_WIDGET(GUI, VerticalSlider)
  15. namespace GUI {
  16. Slider::Slider(Orientation orientation)
  17. : AbstractSlider(orientation)
  18. {
  19. REGISTER_ENUM_PROPERTY("knob_size_mode", knob_size_mode, set_knob_size_mode, KnobSizeMode,
  20. { KnobSizeMode::Fixed, "Fixed" },
  21. { KnobSizeMode::Proportional, "Proportional" });
  22. }
  23. void Slider::paint_event(PaintEvent& event)
  24. {
  25. Painter painter(*this);
  26. painter.add_clip_rect(event.rect());
  27. Gfx::IntRect track_rect;
  28. if (orientation() == Orientation::Horizontal) {
  29. track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() };
  30. track_rect.center_vertically_within(inner_rect());
  31. } else {
  32. track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() };
  33. track_rect.center_horizontally_within(inner_rect());
  34. }
  35. Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, 1);
  36. if (is_enabled())
  37. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered);
  38. else
  39. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, true, m_knob_hovered);
  40. }
  41. Gfx::IntRect Slider::knob_rect() const
  42. {
  43. auto inner_rect = this->inner_rect();
  44. Gfx::IntRect rect;
  45. rect.set_secondary_offset_for_orientation(orientation(), 0);
  46. rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
  47. if (knob_size_mode() == KnobSizeMode::Fixed) {
  48. if (max() - min()) {
  49. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(max() - min());
  50. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)((value() - min()) * scale)) - (knob_fixed_primary_size() / 2));
  51. } else
  52. rect.set_primary_size_for_orientation(orientation(), 0);
  53. rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
  54. } else {
  55. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(max() - min() + 1);
  56. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)((value() - min()) * scale)));
  57. if (max() - min())
  58. rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
  59. else
  60. rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
  61. }
  62. if (orientation() == Orientation::Horizontal)
  63. rect.center_vertically_within(inner_rect);
  64. else
  65. rect.center_horizontally_within(inner_rect);
  66. return rect;
  67. }
  68. void Slider::mousedown_event(MouseEvent& event)
  69. {
  70. if (event.button() == MouseButton::Primary) {
  71. if (knob_rect().contains(event.position())) {
  72. m_dragging = true;
  73. m_drag_origin = event.position();
  74. m_drag_origin_value = value();
  75. return;
  76. }
  77. auto const mouse_offset = event.position().primary_offset_for_orientation(orientation());
  78. if (jump_to_cursor()) {
  79. float normalized_mouse_offset = 0.0f;
  80. if (orientation() == Orientation::Vertical) {
  81. normalized_mouse_offset = static_cast<float>(mouse_offset - track_margin()) / static_cast<float>(inner_rect().height());
  82. } else {
  83. normalized_mouse_offset = static_cast<float>(mouse_offset - track_margin()) / static_cast<float>(inner_rect().width());
  84. }
  85. int new_value = static_cast<int>(min() + ((max() - min()) * normalized_mouse_offset));
  86. set_value(new_value);
  87. } else {
  88. auto knob_first_edge = knob_rect().first_edge_for_orientation(orientation());
  89. auto knob_last_edge = knob_rect().last_edge_for_orientation(orientation());
  90. if (mouse_offset > knob_last_edge)
  91. increase_slider_by_page_steps(1);
  92. else if (mouse_offset < knob_first_edge)
  93. decrease_slider_by_page_steps(1);
  94. }
  95. }
  96. return Widget::mousedown_event(event);
  97. }
  98. void Slider::mousemove_event(MouseEvent& event)
  99. {
  100. set_knob_hovered(knob_rect().contains(event.position()));
  101. if (m_dragging) {
  102. float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
  103. float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
  104. float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range;
  105. float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
  106. set_value((int)new_value);
  107. return;
  108. }
  109. return Widget::mousemove_event(event);
  110. }
  111. void Slider::mouseup_event(MouseEvent& event)
  112. {
  113. if (event.button() == MouseButton::Primary) {
  114. m_dragging = false;
  115. return;
  116. }
  117. return Widget::mouseup_event(event);
  118. }
  119. void Slider::mousewheel_event(MouseEvent& event)
  120. {
  121. auto acceleration_modifier = step();
  122. auto wheel_delta = event.wheel_delta_y();
  123. if (event.modifiers() == KeyModifier::Mod_Ctrl)
  124. acceleration_modifier *= 6;
  125. if (knob_size_mode() == KnobSizeMode::Proportional)
  126. wheel_delta /= abs(wheel_delta);
  127. if (orientation() == Orientation::Horizontal)
  128. decrease_slider_by(wheel_delta * acceleration_modifier);
  129. else
  130. increase_slider_by(wheel_delta * acceleration_modifier);
  131. Widget::mousewheel_event(event);
  132. }
  133. void Slider::leave_event(Core::Event& event)
  134. {
  135. if (!is_enabled())
  136. return;
  137. set_knob_hovered(false);
  138. Widget::leave_event(event);
  139. }
  140. void Slider::change_event(Event& event)
  141. {
  142. if (event.type() == Event::Type::EnabledChange) {
  143. if (!is_enabled())
  144. m_dragging = false;
  145. }
  146. Widget::change_event(event);
  147. }
  148. void Slider::set_knob_hovered(bool hovered)
  149. {
  150. if (m_knob_hovered == hovered)
  151. return;
  152. m_knob_hovered = hovered;
  153. update(knob_rect());
  154. }
  155. }