Scrollbar.cpp 15 KB

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