Slider.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2018-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 <AK/Assertions.h>
  27. #include <AK/StdLibExtras.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Slider.h>
  30. #include <LibGfx/Palette.h>
  31. #include <LibGfx/StylePainter.h>
  32. namespace GUI {
  33. Slider::Slider(Orientation orientation)
  34. : AbstractSlider(orientation)
  35. {
  36. REGISTER_ENUM_PROPERTY("knob_size_mode", knob_size_mode, set_knob_size_mode, KnobSizeMode,
  37. { KnobSizeMode::Fixed, "Fixed" },
  38. { KnobSizeMode::Proportional, "Proportional" });
  39. }
  40. Slider::~Slider()
  41. {
  42. }
  43. void Slider::paint_event(PaintEvent& event)
  44. {
  45. Painter painter(*this);
  46. painter.add_clip_rect(event.rect());
  47. Gfx::IntRect track_rect;
  48. if (orientation() == Orientation::Horizontal) {
  49. track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() };
  50. track_rect.center_vertically_within(inner_rect());
  51. } else {
  52. track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() };
  53. track_rect.center_horizontally_within(inner_rect());
  54. }
  55. Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, 1);
  56. if (is_enabled())
  57. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered);
  58. else
  59. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, true, m_knob_hovered);
  60. }
  61. Gfx::IntRect Slider::knob_rect() const
  62. {
  63. auto inner_rect = this->inner_rect();
  64. Gfx::IntRect rect;
  65. rect.set_secondary_offset_for_orientation(orientation(), 0);
  66. rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
  67. if (knob_size_mode() == KnobSizeMode::Fixed) {
  68. if (max() - min()) {
  69. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(max() - min());
  70. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)((value() - min()) * scale)) - (knob_fixed_primary_size() / 2));
  71. } else
  72. rect.set_primary_size_for_orientation(orientation(), 0);
  73. rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
  74. } else {
  75. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(max() - min() + 1);
  76. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)((value() - min()) * scale)));
  77. if (max() - min())
  78. rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
  79. else
  80. rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
  81. }
  82. if (orientation() == Orientation::Horizontal)
  83. rect.center_vertically_within(inner_rect);
  84. else
  85. rect.center_horizontally_within(inner_rect);
  86. return rect;
  87. }
  88. void Slider::mousedown_event(MouseEvent& event)
  89. {
  90. if (event.button() == MouseButton::Left) {
  91. if (knob_rect().contains(event.position())) {
  92. m_dragging = true;
  93. m_drag_origin = event.position();
  94. m_drag_origin_value = value();
  95. return;
  96. } else {
  97. if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation()))
  98. set_value(value() + page_step());
  99. else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation()))
  100. set_value(value() - page_step());
  101. }
  102. }
  103. return Widget::mousedown_event(event);
  104. }
  105. void Slider::mousemove_event(MouseEvent& event)
  106. {
  107. set_knob_hovered(knob_rect().contains(event.position()));
  108. if (m_dragging) {
  109. float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
  110. float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
  111. float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range;
  112. float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
  113. set_value((int)new_value);
  114. return;
  115. }
  116. return Widget::mousemove_event(event);
  117. }
  118. void Slider::mouseup_event(MouseEvent& event)
  119. {
  120. if (event.button() == MouseButton::Left) {
  121. m_dragging = false;
  122. return;
  123. }
  124. return Widget::mouseup_event(event);
  125. }
  126. void Slider::mousewheel_event(MouseEvent& event)
  127. {
  128. auto acceleration_modifier = step();
  129. if (event.modifiers() == KeyModifier::Mod_Ctrl && knob_size_mode() == KnobSizeMode::Fixed)
  130. acceleration_modifier *= 6;
  131. if (orientation() == Orientation::Horizontal)
  132. set_value(value() - event.wheel_delta() * acceleration_modifier);
  133. else
  134. set_value(value() + event.wheel_delta() * acceleration_modifier);
  135. Widget::mousewheel_event(event);
  136. }
  137. void Slider::leave_event(Core::Event& event)
  138. {
  139. if (!is_enabled())
  140. return;
  141. set_knob_hovered(false);
  142. Widget::leave_event(event);
  143. }
  144. void Slider::change_event(Event& event)
  145. {
  146. if (event.type() == Event::Type::EnabledChange) {
  147. if (!is_enabled())
  148. m_dragging = false;
  149. }
  150. Widget::change_event(event);
  151. }
  152. void Slider::set_knob_hovered(bool hovered)
  153. {
  154. if (m_knob_hovered == hovered)
  155. return;
  156. m_knob_hovered = hovered;
  157. update(knob_rect());
  158. }
  159. }