Serenity Operating System
at master 442 lines 16 kB view raw
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 8#include <LibCore/Timer.h> 9#include <LibGUI/Desktop.h> 10#include <LibGUI/Painter.h> 11#include <LibGUI/Scrollbar.h> 12#include <LibGfx/CharacterBitmap.h> 13#include <LibGfx/Palette.h> 14#include <LibGfx/StylePainter.h> 15 16static constexpr int ANIMATION_INTERVAL = 16; // Milliseconds 17static constexpr double ANIMATION_TIME = 0.18; // Seconds 18 19REGISTER_WIDGET(GUI, Scrollbar) 20 21namespace GUI { 22 23static constexpr AK::Array<Gfx::IntPoint, 3> s_up_arrow_coords = { 24 Gfx::IntPoint { 4, 2 }, 25 Gfx::IntPoint { 1, 5 }, 26 Gfx::IntPoint { 7, 5 }, 27}; 28 29static constexpr AK::Array<Gfx::IntPoint, 3> s_down_arrow_coords = { 30 Gfx::IntPoint { 1, 3 }, 31 Gfx::IntPoint { 7, 3 }, 32 Gfx::IntPoint { 4, 6 }, 33}; 34 35static constexpr AK::Array<Gfx::IntPoint, 3> s_left_arrow_coords = { 36 Gfx::IntPoint { 5, 1 }, 37 Gfx::IntPoint { 2, 4 }, 38 Gfx::IntPoint { 5, 7 }, 39}; 40 41static constexpr AK::Array<Gfx::IntPoint, 3> s_right_arrow_coords = { 42 Gfx::IntPoint { 3, 1 }, 43 Gfx::IntPoint { 6, 4 }, 44 Gfx::IntPoint { 3, 7 }, 45}; 46 47Scrollbar::Scrollbar(Orientation orientation) 48 : AbstractSlider(orientation) 49{ 50 m_automatic_scrolling_timer = add<Core::Timer>(); 51 52 set_preferred_size({ SpecialDimension::Fit }); 53 54 m_automatic_scrolling_timer->set_interval(100); 55 m_automatic_scrolling_timer->on_timeout = [this] { 56 automatic_scrolling_timer_did_fire(); 57 }; 58} 59 60Gfx::IntRect Scrollbar::decrement_button_rect() const 61{ 62 return { 0, 0, button_width(), button_height() }; 63} 64 65Gfx::IntRect Scrollbar::increment_button_rect() const 66{ 67 if (orientation() == Orientation::Vertical) 68 return { 0, height() - button_height(), button_width(), button_height() }; 69 else 70 return { width() - button_width(), 0, button_width(), button_height() }; 71} 72 73int Scrollbar::scrubbable_range_in_pixels() const 74{ 75 if (orientation() == Orientation::Vertical) 76 return height() - button_height() * 2 - visible_scrubber_size(); 77 else 78 return width() - button_width() * 2 - visible_scrubber_size(); 79} 80 81bool Scrollbar::has_scrubber() const 82{ 83 return max() != min(); 84} 85 86void Scrollbar::set_scroll_animation(Animation scroll_animation) 87{ 88 m_scroll_animation = scroll_animation; 89} 90 91void Scrollbar::set_value(int value, AllowCallback allow_callback, DoClamp do_clamp) 92{ 93 m_target_value = value; 94 if (!(m_animated_scrolling_timer.is_null())) 95 m_animated_scrolling_timer->stop(); 96 97 AbstractSlider::set_value(value, allow_callback, do_clamp); 98} 99 100void Scrollbar::set_target_value(int new_target_value) 101{ 102 new_target_value = clamp(new_target_value, min(), max()); 103 104 // If we are already at or scrolling to the new target then don't touch anything 105 if (m_target_value == new_target_value) 106 return; 107 108 if (m_scroll_animation == Animation::CoarseScroll || !Desktop::the().system_effects().smooth_scrolling()) 109 return set_value(new_target_value); 110 111 m_animation_time_elapsed = 0; 112 m_start_value = value(); 113 m_target_value = new_target_value; 114 115 if (m_animated_scrolling_timer.is_null()) { 116 m_animated_scrolling_timer = add<Core::Timer>(); 117 m_animated_scrolling_timer->set_interval(ANIMATION_INTERVAL); 118 m_animated_scrolling_timer->on_timeout = [this]() { 119 m_animation_time_elapsed += (double)ANIMATION_INTERVAL / 1'000; // ms -> sec 120 update_animated_scroll(); 121 }; 122 } 123 124 m_animated_scrolling_timer->start(); 125} 126 127float Scrollbar::unclamped_scrubber_size() const 128{ 129 float pixel_range = length(orientation()) - button_size() * 2; 130 float value_range = max() - min(); 131 132 float scrubber_size { 0 }; 133 if (value_range > 0) { 134 // Scrubber size should be proportional to the visible portion 135 // (page) in relation to the content (value range + page) 136 scrubber_size = (page_step() * pixel_range) / (value_range + page_step()); 137 } 138 return scrubber_size; 139} 140 141int Scrollbar::visible_scrubber_size() const 142{ 143 return ::max(unclamped_scrubber_size(), button_size()); 144} 145 146Gfx::IntRect Scrollbar::scrubber_rect() const 147{ 148 if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + visible_scrubber_size()) 149 return {}; 150 float x_or_y; 151 if (value() == min()) 152 x_or_y = button_size(); 153 else if (value() == max()) 154 x_or_y = length(orientation()) - button_size() - visible_scrubber_size(); 155 else { 156 float range_size = max() - min(); 157 float available = scrubbable_range_in_pixels(); 158 float step = available / range_size; 159 x_or_y = (button_size() + (step * value())); 160 } 161 162 if (orientation() == Orientation::Vertical) 163 return { 0, (int)x_or_y, button_width(), visible_scrubber_size() }; 164 else 165 return { (int)x_or_y, 0, visible_scrubber_size(), button_height() }; 166} 167 168void Scrollbar::paint_event(PaintEvent& event) 169{ 170 Painter painter(*this); 171 painter.add_clip_rect(event.rect()); 172 173 Component hovered_component_for_painting = m_hovered_component; 174 if (!has_scrubber() || (m_pressed_component != Component::None && m_hovered_component != m_pressed_component)) 175 hovered_component_for_painting = Component::None; 176 177 painter.fill_rect_with_dither_pattern(rect(), palette().button().lightened(1.3f), palette().button()); 178 if (m_gutter_click_state != GutterClickState::NotPressed && has_scrubber() && !scrubber_rect().is_empty() && hovered_component_for_painting == Component::Gutter) { 179 Gfx::IntRect rect_to_fill = rect(); 180 if (orientation() == Orientation::Vertical) { 181 if (m_gutter_click_state == GutterClickState::BeforeScrubber) { 182 rect_to_fill.set_top(decrement_button_rect().bottom()); 183 rect_to_fill.set_bottom(scrubber_rect().top()); 184 } else { 185 VERIFY(m_gutter_click_state == GutterClickState::AfterScrubber); 186 rect_to_fill.set_top(scrubber_rect().bottom()); 187 rect_to_fill.set_bottom(increment_button_rect().top()); 188 } 189 } else { 190 if (m_gutter_click_state == GutterClickState::BeforeScrubber) { 191 rect_to_fill.set_left(decrement_button_rect().right()); 192 rect_to_fill.set_right(scrubber_rect().left()); 193 } else { 194 VERIFY(m_gutter_click_state == GutterClickState::AfterScrubber); 195 rect_to_fill.set_left(scrubber_rect().right()); 196 rect_to_fill.set_right(increment_button_rect().left()); 197 } 198 } 199 painter.fill_rect_with_dither_pattern(rect_to_fill, palette().button(), palette().button().lightened(0.77f)); 200 } 201 202 bool decrement_pressed = (m_pressed_component == Component::DecrementButton) && (m_pressed_component == m_hovered_component) && !is_min(); 203 bool increment_pressed = (m_pressed_component == Component::IncrementButton) && (m_pressed_component == m_hovered_component) && !is_max(); 204 205 Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::ThickCap, decrement_pressed, hovered_component_for_painting == Component::DecrementButton && !is_min()); 206 Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::ThickCap, increment_pressed, hovered_component_for_painting == Component::IncrementButton && !is_max()); 207 208 if (length(orientation()) >= default_button_size() * 2) { 209 auto decrement_location = decrement_button_rect().location().translated(3, 3); 210 if (decrement_pressed) 211 decrement_location.translate_by(1, 1); 212 if (!has_scrubber() || !is_enabled()) 213 painter.draw_triangle(decrement_location + Gfx::IntPoint { 1, 1 }, orientation() == Orientation::Vertical ? s_up_arrow_coords : s_left_arrow_coords, palette().threed_highlight()); 214 painter.draw_triangle(decrement_location, orientation() == Orientation::Vertical ? s_up_arrow_coords : s_left_arrow_coords, (has_scrubber() && is_enabled() && !is_min()) ? palette().button_text() : palette().threed_shadow1()); 215 216 auto increment_location = increment_button_rect().location().translated(3, 3); 217 if (increment_pressed) 218 increment_location.translate_by(1, 1); 219 if (!has_scrubber() || !is_enabled()) 220 painter.draw_triangle(increment_location + Gfx::IntPoint { 1, 1 }, orientation() == Orientation::Vertical ? s_down_arrow_coords : s_right_arrow_coords, palette().threed_highlight()); 221 painter.draw_triangle(increment_location, orientation() == Orientation::Vertical ? s_down_arrow_coords : s_right_arrow_coords, (has_scrubber() && is_enabled() && !is_max()) ? palette().button_text() : palette().threed_shadow1()); 222 } 223 224 if (has_scrubber() && !scrubber_rect().is_empty()) 225 Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::ThickCap, false, hovered_component_for_painting == Component::Scrubber || m_pressed_component == Component::Scrubber); 226} 227 228void Scrollbar::automatic_scrolling_timer_did_fire() 229{ 230 if (m_pressed_component == Component::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) { 231 decrease_slider_by_steps(1); 232 return; 233 } 234 if (m_pressed_component == Component::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) { 235 increase_slider_by_steps(1); 236 return; 237 } 238 if (m_pressed_component == Component::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) { 239 scroll_by_page(m_last_mouse_position); 240 if (m_hovered_component != component_at_position(m_last_mouse_position)) { 241 m_hovered_component = component_at_position(m_last_mouse_position); 242 if (m_hovered_component != Component::Gutter) 243 m_gutter_click_state = GutterClickState::NotPressed; 244 update(); 245 } 246 return; 247 } 248 if (m_gutter_click_state != GutterClickState::NotPressed) { 249 m_gutter_click_state = GutterClickState::NotPressed; 250 update(); 251 return; 252 } 253} 254 255void Scrollbar::mousedown_event(MouseEvent& event) 256{ 257 if (event.button() != MouseButton::Primary) 258 return; 259 if (!has_scrubber()) 260 return; 261 262 m_last_mouse_position = event.position(); 263 m_pressed_component = component_at_position(m_last_mouse_position); 264 265 if (m_pressed_component == Component::DecrementButton) { 266 if (is_min()) 267 return; 268 set_automatic_scrolling_timer_active(true, Component::DecrementButton); 269 update(); 270 return; 271 } 272 if (m_pressed_component == Component::IncrementButton) { 273 if (is_max()) 274 return; 275 set_automatic_scrolling_timer_active(true, Component::IncrementButton); 276 update(); 277 return; 278 } 279 280 if (event.shift()) { 281 scroll_to_position(event.position()); 282 m_pressed_component = component_at_position(event.position()); 283 return; 284 } 285 if (m_pressed_component == Component::Scrubber) { 286 m_scrub_start_value = value(); 287 m_scrub_origin = event.position(); 288 update(); 289 return; 290 } 291 VERIFY(!event.shift()); 292 293 VERIFY(m_pressed_component == Component::Gutter); 294 set_automatic_scrolling_timer_active(true, Component::Gutter); 295 update(); 296} 297 298void Scrollbar::mouseup_event(MouseEvent& event) 299{ 300 if (event.button() != MouseButton::Primary) 301 return; 302 set_automatic_scrolling_timer_active(false, Component::None); 303 update(); 304} 305 306void Scrollbar::mousewheel_event(MouseEvent& event) 307{ 308 if (!is_scrollable()) 309 return; 310 increase_slider_by_steps(event.wheel_delta_y()); 311 Widget::mousewheel_event(event); 312} 313 314void Scrollbar::set_automatic_scrolling_timer_active(bool active, Component pressed_component) 315{ 316 m_pressed_component = pressed_component; 317 if (m_pressed_component == Component::Gutter) 318 m_automatic_scrolling_timer->set_interval(200); 319 else 320 m_automatic_scrolling_timer->set_interval(100); 321 322 if (active) { 323 automatic_scrolling_timer_did_fire(); 324 m_automatic_scrolling_timer->start(); 325 } else { 326 m_automatic_scrolling_timer->stop(); 327 m_gutter_click_state = GutterClickState::NotPressed; 328 } 329} 330 331void Scrollbar::scroll_by_page(Gfx::IntPoint click_position) 332{ 333 float range_size = max() - min(); 334 float available = scrubbable_range_in_pixels(); 335 float rel_scrubber_size = unclamped_scrubber_size() / available; 336 float page_increment = range_size * rel_scrubber_size; 337 338 if (click_position.primary_offset_for_orientation(orientation()) < scrubber_rect().primary_offset_for_orientation(orientation())) { 339 m_gutter_click_state = GutterClickState::BeforeScrubber; 340 decrease_slider_by(page_increment); 341 } else { 342 m_gutter_click_state = GutterClickState::AfterScrubber; 343 increase_slider_by(page_increment); 344 } 345} 346 347void Scrollbar::scroll_to_position(Gfx::IntPoint click_position) 348{ 349 float range_size = max() - min(); 350 float available = scrubbable_range_in_pixels(); 351 352 float x_or_y = ::max(0, click_position.primary_offset_for_orientation(orientation()) - button_width() - button_width() / 2); 353 float rel_x_or_y = x_or_y / available; 354 set_target_value(min() + rel_x_or_y * range_size); 355} 356 357Scrollbar::Component Scrollbar::component_at_position(Gfx::IntPoint position) 358{ 359 if (scrubber_rect().contains(position)) 360 return Component::Scrubber; 361 if (decrement_button_rect().contains(position)) 362 return Component::DecrementButton; 363 if (increment_button_rect().contains(position)) 364 return Component::IncrementButton; 365 if (rect().contains(position)) 366 return Component::Gutter; 367 return Component::None; 368} 369 370void Scrollbar::mousemove_event(MouseEvent& event) 371{ 372 if (!is_scrollable()) 373 return; 374 375 m_last_mouse_position = event.position(); 376 377 auto old_hovered_component = m_hovered_component; 378 m_hovered_component = component_at_position(m_last_mouse_position); 379 if (old_hovered_component != m_hovered_component) { 380 if (is_enabled()) 381 update(); 382 } 383 if (m_pressed_component != Component::Scrubber) 384 return; 385 float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x()); 386 float scrubbable_range = scrubbable_range_in_pixels(); 387 float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range; 388 float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta); 389 set_value(new_value); 390} 391 392void Scrollbar::leave_event(Core::Event&) 393{ 394 if (m_hovered_component != Component::None) { 395 m_hovered_component = Component::None; 396 if (is_enabled()) 397 update(); 398 } 399} 400 401void Scrollbar::change_event(Event& event) 402{ 403 if (event.type() == Event::Type::EnabledChange) { 404 if (!is_enabled()) 405 set_automatic_scrolling_timer_active(false, Component::None); 406 } 407 return Widget::change_event(event); 408} 409 410void Scrollbar::update_animated_scroll() 411{ 412 if (value() == m_target_value) { 413 m_animated_scrolling_timer->stop(); 414 return; 415 } 416 417 double time_percent = m_animation_time_elapsed / ANIMATION_TIME; 418 double ease_percent = 1.0 - pow(1.0 - time_percent, 5.0); // Ease out quint 419 double initial_distance = m_target_value - m_start_value; 420 double new_distance = initial_distance * ease_percent; 421 int new_value = m_start_value + (int)round(new_distance); 422 AbstractSlider::set_value(new_value); 423} 424 425Optional<UISize> Scrollbar::calculated_min_size() const 426{ 427 auto scrubber_and_gutter = default_button_size() + 1; 428 if (orientation() == Gfx::Orientation::Vertical) 429 return { { default_button_size(), 2 * default_button_size() + scrubber_and_gutter } }; 430 else 431 return { { 2 * default_button_size() + scrubber_and_gutter, default_button_size() } }; 432} 433 434Optional<UISize> Scrollbar::calculated_preferred_size() const 435{ 436 if (orientation() == Gfx::Orientation::Vertical) 437 return { { SpecialDimension::Shrink, SpecialDimension::Grow } }; 438 else 439 return { { SpecialDimension::Grow, SpecialDimension::Shrink } }; 440} 441 442}