Scrollbar.cpp 16 KB

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