Scrollbar.cpp 15 KB

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