Slider.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. REGISTER_BOOL_PROPERTY("jump_to_cursor", jump_to_cursor, set_jump_to_cursor);
  23. set_preferred_size(SpecialDimension::Fit);
  24. }
  25. void Slider::paint_event(PaintEvent& event)
  26. {
  27. Painter painter(*this);
  28. painter.add_clip_rect(event.rect());
  29. Gfx::IntRect track_rect;
  30. // To avoid drop shadow peeking out behind the slider knob, we paint the track slightly shorter
  31. int shadow_thickness = 1;
  32. if (orientation() == Orientation::Horizontal) {
  33. track_rect = { inner_rect().x(), 0, inner_rect().width() - shadow_thickness, track_size() };
  34. track_rect.center_vertically_within(inner_rect());
  35. } else {
  36. track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() - shadow_thickness };
  37. track_rect.center_horizontally_within(inner_rect());
  38. }
  39. Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, shadow_thickness);
  40. if (is_enabled())
  41. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered);
  42. else
  43. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, true, m_knob_hovered);
  44. }
  45. Gfx::IntRect Slider::knob_rect() const
  46. {
  47. auto inner_rect = this->inner_rect();
  48. Gfx::IntRect rect;
  49. rect.set_secondary_offset_for_orientation(orientation(), 0);
  50. rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
  51. if (knob_size_mode() == KnobSizeMode::Fixed) {
  52. if (max() - min()) {
  53. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(max() - min());
  54. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)((value() - min()) * scale)) - (knob_fixed_primary_size() / 2));
  55. } else
  56. rect.set_primary_size_for_orientation(orientation(), 0);
  57. rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
  58. } else {
  59. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(max() - min() + 1);
  60. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)((value() - min()) * scale)));
  61. if (max() - min())
  62. rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
  63. else
  64. rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
  65. }
  66. if (orientation() == Orientation::Horizontal)
  67. rect.center_vertically_within(inner_rect);
  68. else
  69. rect.center_horizontally_within(inner_rect);
  70. return rect;
  71. }
  72. void Slider::start_drag(Gfx::IntPoint start_position)
  73. {
  74. VERIFY(!m_dragging);
  75. m_dragging = true;
  76. m_drag_origin = start_position;
  77. m_drag_origin_value = value();
  78. if (on_drag_start)
  79. on_drag_start();
  80. }
  81. void Slider::mousedown_event(MouseEvent& event)
  82. {
  83. if (event.button() == MouseButton::Primary) {
  84. auto const mouse_offset = event.position().primary_offset_for_orientation(orientation());
  85. if (jump_to_cursor()) {
  86. float normalized_mouse_offset = 0.0f;
  87. if (orientation() == Orientation::Vertical) {
  88. normalized_mouse_offset = static_cast<float>(mouse_offset - track_margin()) / static_cast<float>(inner_rect().height());
  89. } else {
  90. normalized_mouse_offset = static_cast<float>(mouse_offset - track_margin()) / static_cast<float>(inner_rect().width());
  91. }
  92. int new_value = static_cast<int>(min() + ((max() - min()) * normalized_mouse_offset));
  93. set_value(new_value, AllowCallback::No);
  94. start_drag(event.position());
  95. // Delay the callback to make it aware that a drag has started.
  96. if (on_change)
  97. on_change(value());
  98. return;
  99. }
  100. if (knob_rect().contains(event.position())) {
  101. start_drag(event.position());
  102. return;
  103. }
  104. auto knob_first_edge = knob_rect().first_edge_for_orientation(orientation());
  105. auto knob_last_edge = knob_rect().last_edge_for_orientation(orientation());
  106. if (mouse_offset > knob_last_edge)
  107. increase_slider_by_page_steps(1);
  108. else if (mouse_offset < knob_first_edge)
  109. decrease_slider_by_page_steps(1);
  110. }
  111. return Widget::mousedown_event(event);
  112. }
  113. void Slider::mousemove_event(MouseEvent& event)
  114. {
  115. set_knob_hovered(knob_rect().contains(event.position()));
  116. if (m_dragging) {
  117. float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
  118. float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
  119. float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range;
  120. float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
  121. set_value((int)roundf(new_value));
  122. return;
  123. }
  124. return Widget::mousemove_event(event);
  125. }
  126. void Slider::end_drag()
  127. {
  128. if (m_dragging) {
  129. m_dragging = false;
  130. if (on_drag_end)
  131. on_drag_end();
  132. }
  133. }
  134. void Slider::mouseup_event(MouseEvent& event)
  135. {
  136. if (event.button() == MouseButton::Primary) {
  137. end_drag();
  138. return;
  139. }
  140. return Widget::mouseup_event(event);
  141. }
  142. void Slider::mousewheel_event(MouseEvent& event)
  143. {
  144. auto acceleration_modifier = step();
  145. auto wheel_delta = event.wheel_delta_y();
  146. if (event.modifiers() == KeyModifier::Mod_Ctrl)
  147. acceleration_modifier *= 6;
  148. if (knob_size_mode() == KnobSizeMode::Proportional)
  149. wheel_delta /= abs(wheel_delta);
  150. if (orientation() == Orientation::Horizontal)
  151. decrease_slider_by(wheel_delta * acceleration_modifier);
  152. else
  153. increase_slider_by(wheel_delta * acceleration_modifier);
  154. Widget::mousewheel_event(event);
  155. }
  156. void Slider::leave_event(Core::Event& event)
  157. {
  158. if (!is_enabled())
  159. return;
  160. set_knob_hovered(false);
  161. Widget::leave_event(event);
  162. }
  163. void Slider::change_event(Event& event)
  164. {
  165. if (event.type() == Event::Type::EnabledChange) {
  166. if (!is_enabled())
  167. m_dragging = false;
  168. }
  169. Widget::change_event(event);
  170. }
  171. void Slider::set_knob_hovered(bool hovered)
  172. {
  173. if (m_knob_hovered == hovered)
  174. return;
  175. m_knob_hovered = hovered;
  176. update(knob_rect());
  177. }
  178. Optional<UISize> Slider::calculated_min_size() const
  179. {
  180. if (orientation() == Gfx::Orientation::Vertical)
  181. return { { knob_secondary_size(), knob_fixed_primary_size() * 2 + track_margin() * 2 } };
  182. return { { knob_fixed_primary_size() * 2 + track_margin() * 2, knob_secondary_size() } };
  183. }
  184. Optional<UISize> Slider::calculated_preferred_size() const
  185. {
  186. if (orientation() == Gfx::Orientation::Vertical)
  187. return { { SpecialDimension::Shrink, SpecialDimension::OpportunisticGrow } };
  188. return { { SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink } };
  189. }
  190. }