Scrollbar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/Timer.h>
  7. #include <LibGUI/Painter.h>
  8. #include <LibGUI/Scrollbar.h>
  9. #include <LibGfx/CharacterBitmap.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibGfx/StylePainter.h>
  12. REGISTER_WIDGET(GUI, Scrollbar)
  13. namespace GUI {
  14. static const char* s_up_arrow_bitmap_data = {
  15. " "
  16. " # "
  17. " ### "
  18. " ##### "
  19. " ####### "
  20. " ### "
  21. " ### "
  22. " ### "
  23. " "
  24. };
  25. static const char* s_down_arrow_bitmap_data = {
  26. " "
  27. " ### "
  28. " ### "
  29. " ### "
  30. " ####### "
  31. " ##### "
  32. " ### "
  33. " # "
  34. " "
  35. };
  36. static const char* s_left_arrow_bitmap_data = {
  37. " "
  38. " # "
  39. " ## "
  40. " ###### "
  41. " ####### "
  42. " ###### "
  43. " ## "
  44. " # "
  45. " "
  46. };
  47. static const char* s_right_arrow_bitmap_data = {
  48. " "
  49. " # "
  50. " ## "
  51. " ###### "
  52. " ####### "
  53. " ###### "
  54. " ## "
  55. " # "
  56. " "
  57. };
  58. static Gfx::CharacterBitmap* s_up_arrow_bitmap;
  59. static Gfx::CharacterBitmap* s_down_arrow_bitmap;
  60. static Gfx::CharacterBitmap* s_left_arrow_bitmap;
  61. static Gfx::CharacterBitmap* s_right_arrow_bitmap;
  62. Scrollbar::Scrollbar(Orientation orientation)
  63. : AbstractSlider(orientation)
  64. {
  65. m_automatic_scrolling_timer = add<Core::Timer>();
  66. if (!s_up_arrow_bitmap)
  67. s_up_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref();
  68. if (!s_down_arrow_bitmap)
  69. s_down_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref();
  70. if (!s_left_arrow_bitmap)
  71. s_left_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref();
  72. if (!s_right_arrow_bitmap)
  73. s_right_arrow_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref();
  74. if (orientation == Orientation::Vertical) {
  75. set_fixed_width(16);
  76. } else {
  77. set_fixed_height(16);
  78. }
  79. m_automatic_scrolling_timer->set_interval(100);
  80. m_automatic_scrolling_timer->on_timeout = [this] {
  81. on_automatic_scrolling_timer_fired();
  82. };
  83. }
  84. Scrollbar::~Scrollbar()
  85. {
  86. }
  87. Gfx::IntRect Scrollbar::decrement_button_rect() const
  88. {
  89. return { 0, 0, button_width(), button_height() };
  90. }
  91. Gfx::IntRect Scrollbar::increment_button_rect() const
  92. {
  93. if (orientation() == Orientation::Vertical)
  94. return { 0, height() - button_height(), button_width(), button_height() };
  95. else
  96. return { width() - button_width(), 0, button_width(), button_height() };
  97. }
  98. int Scrollbar::scrubbable_range_in_pixels() const
  99. {
  100. if (orientation() == Orientation::Vertical)
  101. return height() - button_height() * 2 - visible_scrubber_size();
  102. else
  103. return width() - button_width() * 2 - visible_scrubber_size();
  104. }
  105. bool Scrollbar::has_scrubber() const
  106. {
  107. return max() != min();
  108. }
  109. int Scrollbar::unclamped_scrubber_size() const
  110. {
  111. int pixel_range = length(orientation()) - button_size() * 2;
  112. int value_range = max() - min();
  113. int scrubber_size = 0;
  114. if (value_range > 0) {
  115. // Scrubber size should be proportional to the visible portion
  116. // (page) in relation to the content (value range + page)
  117. scrubber_size = (page_step() * pixel_range) / (value_range + page_step());
  118. }
  119. return scrubber_size;
  120. }
  121. int Scrollbar::visible_scrubber_size() const
  122. {
  123. return ::max(unclamped_scrubber_size(), button_size());
  124. }
  125. Gfx::IntRect Scrollbar::scrubber_rect() const
  126. {
  127. if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + visible_scrubber_size())
  128. return {};
  129. float x_or_y;
  130. if (value() == min())
  131. x_or_y = button_size();
  132. else if (value() == max())
  133. x_or_y = length(orientation()) - button_size() - visible_scrubber_size();
  134. else {
  135. float range_size = max() - min();
  136. float available = scrubbable_range_in_pixels();
  137. float step = available / range_size;
  138. x_or_y = (button_size() + (step * value()));
  139. }
  140. if (orientation() == Orientation::Vertical)
  141. return { 0, (int)x_or_y, button_width(), visible_scrubber_size() };
  142. else
  143. return { (int)x_or_y, 0, visible_scrubber_size(), button_height() };
  144. }
  145. void Scrollbar::paint_event(PaintEvent& event)
  146. {
  147. Painter painter(*this);
  148. painter.add_clip_rect(event.rect());
  149. Component hovered_component_for_painting = m_hovered_component;
  150. if (!has_scrubber() || (m_pressed_component != Component::None && m_hovered_component != m_pressed_component))
  151. hovered_component_for_painting = Component::None;
  152. painter.fill_rect_with_dither_pattern(rect(), palette().button().lightened(1.3f), palette().button());
  153. bool decrement_pressed = (m_pressed_component == Component::DecrementButton) && (m_pressed_component == m_hovered_component);
  154. bool increment_pressed = (m_pressed_component == Component::IncrementButton) && (m_pressed_component == m_hovered_component);
  155. Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, decrement_pressed, hovered_component_for_painting == Component::DecrementButton);
  156. Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::Normal, increment_pressed, hovered_component_for_painting == Component::IncrementButton);
  157. if (length(orientation()) > default_button_size()) {
  158. auto decrement_location = decrement_button_rect().location().translated(3, 3);
  159. if (decrement_pressed)
  160. decrement_location.translate_by(1, 1);
  161. if (!has_scrubber() || !is_enabled())
  162. painter.draw_bitmap(decrement_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, palette().threed_highlight());
  163. 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());
  164. auto increment_location = increment_button_rect().location().translated(3, 3);
  165. if (increment_pressed)
  166. increment_location.translate_by(1, 1);
  167. if (!has_scrubber() || !is_enabled())
  168. painter.draw_bitmap(increment_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, palette().threed_highlight());
  169. 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());
  170. }
  171. if (has_scrubber())
  172. Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, hovered_component_for_painting == Component::Scrubber || m_pressed_component == Component::Scrubber);
  173. }
  174. void Scrollbar::on_automatic_scrolling_timer_fired()
  175. {
  176. if (m_pressed_component == Component::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) {
  177. set_value(value() - step());
  178. return;
  179. }
  180. if (m_pressed_component == Component::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) {
  181. set_value(value() + step());
  182. return;
  183. }
  184. if (m_pressed_component == Component::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) {
  185. scroll_by_page(m_last_mouse_position);
  186. m_hovered_component = component_at_position(m_last_mouse_position);
  187. return;
  188. }
  189. }
  190. void Scrollbar::mousedown_event(MouseEvent& event)
  191. {
  192. if (event.button() != MouseButton::Left)
  193. return;
  194. if (!has_scrubber())
  195. return;
  196. m_last_mouse_position = event.position();
  197. m_pressed_component = component_at_position(m_last_mouse_position);
  198. if (m_pressed_component == Component::DecrementButton) {
  199. set_automatic_scrolling_active(true, Component::DecrementButton);
  200. update();
  201. return;
  202. }
  203. if (m_pressed_component == Component::IncrementButton) {
  204. set_automatic_scrolling_active(true, Component::IncrementButton);
  205. update();
  206. return;
  207. }
  208. if (event.shift()) {
  209. scroll_to_position(event.position());
  210. m_pressed_component = component_at_position(event.position());
  211. VERIFY(m_pressed_component == Component::Scrubber);
  212. }
  213. if (m_pressed_component == Component::Scrubber) {
  214. m_scrub_start_value = value();
  215. m_scrub_origin = event.position();
  216. update();
  217. return;
  218. }
  219. VERIFY(!event.shift());
  220. VERIFY(m_pressed_component == Component::Gutter);
  221. set_automatic_scrolling_active(true, Component::Gutter);
  222. update();
  223. }
  224. void Scrollbar::mouseup_event(MouseEvent& event)
  225. {
  226. if (event.button() != MouseButton::Left)
  227. return;
  228. set_automatic_scrolling_active(false, Component::None);
  229. update();
  230. }
  231. void Scrollbar::mousewheel_event(MouseEvent& event)
  232. {
  233. if (!is_scrollable())
  234. return;
  235. set_value(value() + event.wheel_delta() * step());
  236. Widget::mousewheel_event(event);
  237. }
  238. void Scrollbar::set_automatic_scrolling_active(bool active, Component pressed_component)
  239. {
  240. m_pressed_component = pressed_component;
  241. if (m_pressed_component == Component::Gutter)
  242. m_automatic_scrolling_timer->set_interval(200);
  243. else
  244. m_automatic_scrolling_timer->set_interval(100);
  245. if (active) {
  246. on_automatic_scrolling_timer_fired();
  247. m_automatic_scrolling_timer->start();
  248. } else {
  249. m_automatic_scrolling_timer->stop();
  250. }
  251. }
  252. void Scrollbar::scroll_by_page(const Gfx::IntPoint& click_position)
  253. {
  254. float range_size = max() - min();
  255. float available = scrubbable_range_in_pixels();
  256. float rel_scrubber_size = unclamped_scrubber_size() / available;
  257. float page_increment = range_size * rel_scrubber_size;
  258. if (click_position.primary_offset_for_orientation(orientation()) < scrubber_rect().primary_offset_for_orientation(orientation()))
  259. set_value(value() - page_increment);
  260. else
  261. set_value(value() + page_increment);
  262. }
  263. void Scrollbar::scroll_to_position(const Gfx::IntPoint& click_position)
  264. {
  265. float range_size = max() - min();
  266. float available = scrubbable_range_in_pixels();
  267. float x_or_y = ::max(0, click_position.primary_offset_for_orientation(orientation()) - button_width() - button_width() / 2);
  268. float rel_x_or_y = x_or_y / available;
  269. set_value(min() + rel_x_or_y * range_size);
  270. }
  271. Scrollbar::Component Scrollbar::component_at_position(const Gfx::IntPoint& position)
  272. {
  273. if (scrubber_rect().contains(position))
  274. return Component::Scrubber;
  275. if (decrement_button_rect().contains(position))
  276. return Component::DecrementButton;
  277. if (increment_button_rect().contains(position))
  278. return Component::IncrementButton;
  279. if (rect().contains(position))
  280. return Component::Gutter;
  281. return Component::None;
  282. }
  283. void Scrollbar::mousemove_event(MouseEvent& event)
  284. {
  285. m_last_mouse_position = event.position();
  286. auto old_hovered_component = m_hovered_component;
  287. m_hovered_component = component_at_position(m_last_mouse_position);
  288. if (old_hovered_component != m_hovered_component) {
  289. if (is_enabled())
  290. update();
  291. }
  292. if (m_pressed_component != Component::Scrubber)
  293. return;
  294. float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x());
  295. float scrubbable_range = scrubbable_range_in_pixels();
  296. float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range;
  297. float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta);
  298. set_value(new_value);
  299. }
  300. void Scrollbar::leave_event(Core::Event&)
  301. {
  302. if (m_hovered_component != Component::None) {
  303. m_hovered_component = Component::None;
  304. if (is_enabled())
  305. update();
  306. }
  307. }
  308. void Scrollbar::change_event(Event& event)
  309. {
  310. if (event.type() == Event::Type::EnabledChange) {
  311. if (!is_enabled())
  312. set_automatic_scrolling_active(false, Component::None);
  313. }
  314. return Widget::change_event(event);
  315. }
  316. }