Serenity Operating System
at portability 202 lines 6.9 kB view raw
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 27#include <AK/Assertions.h> 28#include <AK/StdLibExtras.h> 29#include <LibGUI/Painter.h> 30#include <LibGUI/Slider.h> 31#include <LibGfx/Palette.h> 32#include <LibGfx/StylePainter.h> 33 34namespace GUI { 35 36Slider::Slider(Orientation orientation) 37 : m_orientation(orientation) 38{ 39} 40 41Slider::~Slider() 42{ 43} 44 45void Slider::set_range(int min, int max) 46{ 47 ASSERT(min <= max); 48 if (m_min == min && m_max == max) 49 return; 50 m_min = min; 51 m_max = max; 52 m_value = clamp(m_value, m_min, m_max); 53 update(); 54} 55 56void Slider::set_value(int value) 57{ 58 value = clamp(value, m_min, m_max); 59 if (m_value == value) 60 return; 61 m_value = value; 62 update(); 63 64 if (on_value_changed) 65 on_value_changed(m_value); 66} 67 68void Slider::paint_event(PaintEvent& event) 69{ 70 Painter painter(*this); 71 painter.add_clip_rect(event.rect()); 72 73 Gfx::Rect track_rect; 74 75 if (orientation() == Orientation::Horizontal) { 76 track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() }; 77 track_rect.center_vertically_within(inner_rect()); 78 } else { 79 track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() }; 80 track_rect.center_horizontally_within(inner_rect()); 81 } 82 83 Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, 1); 84 Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered); 85} 86 87Gfx::Rect Slider::knob_rect() const 88{ 89 auto inner_rect = this->inner_rect(); 90 Gfx::Rect rect; 91 rect.set_secondary_offset_for_orientation(orientation(), 0); 92 rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size()); 93 94 if (knob_size_mode() == KnobSizeMode::Fixed) { 95 if (m_max - m_min) { 96 float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min); 97 rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)) - (knob_fixed_primary_size() / 2)); 98 } else 99 rect.set_primary_size_for_orientation(orientation(), 0); 100 rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size()); 101 } else { 102 float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min + 1); 103 rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale))); 104 if (m_max - m_min) 105 rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size())); 106 else 107 rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation())); 108 } 109 if (orientation() == Orientation::Horizontal) 110 rect.center_vertically_within(inner_rect); 111 else 112 rect.center_horizontally_within(inner_rect); 113 return rect; 114} 115 116void Slider::mousedown_event(MouseEvent& event) 117{ 118 if (!is_enabled()) 119 return; 120 if (event.button() == MouseButton::Left) { 121 if (knob_rect().contains(event.position())) { 122 m_dragging = true; 123 m_drag_origin = event.position(); 124 m_drag_origin_value = m_value; 125 return; 126 } else { 127 if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation())) 128 set_value(m_value + 1); 129 else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation())) 130 set_value(m_value - 1); 131 } 132 } 133 return Widget::mousedown_event(event); 134} 135 136void Slider::mousemove_event(MouseEvent& event) 137{ 138 if (!is_enabled()) 139 return; 140 set_knob_hovered(knob_rect().contains(event.position())); 141 if (m_dragging) { 142 float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation()); 143 float scrubbable_range = inner_rect().primary_size_for_orientation(orientation()); 144 float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range; 145 float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta); 146 set_value((int)new_value); 147 return; 148 } 149 return Widget::mousemove_event(event); 150} 151 152void Slider::mouseup_event(MouseEvent& event) 153{ 154 if (!is_enabled()) 155 return; 156 if (event.button() == MouseButton::Left) { 157 m_dragging = false; 158 return; 159 } 160 161 return Widget::mouseup_event(event); 162} 163 164void Slider::mousewheel_event(MouseEvent& event) 165{ 166 if (!is_enabled()) 167 return; 168 169 if (orientation() == Orientation::Horizontal) 170 set_value(value() - event.wheel_delta() * m_step); 171 else 172 set_value(value() + event.wheel_delta() * m_step); 173 174 Widget::mousewheel_event(event); 175} 176 177void Slider::leave_event(Core::Event& event) 178{ 179 if (!is_enabled()) 180 return; 181 set_knob_hovered(false); 182 Widget::leave_event(event); 183} 184 185void Slider::change_event(Event& event) 186{ 187 if (event.type() == Event::Type::EnabledChange) { 188 if (!is_enabled()) 189 m_dragging = false; 190 } 191 Widget::change_event(event); 192} 193 194void Slider::set_knob_hovered(bool hovered) 195{ 196 if (m_knob_hovered == hovered) 197 return; 198 m_knob_hovered = hovered; 199 update(knob_rect()); 200} 201 202}