Scrollbar.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. Gfx::IntRect Scrollbar::decrement_gutter_rect() const
  99. {
  100. if (orientation() == Orientation::Vertical)
  101. return { 0, button_height(), button_width(), scrubber_rect().top() - button_height() };
  102. else
  103. return { button_width(), 0, scrubber_rect().x() - button_width(), button_height() };
  104. }
  105. Gfx::IntRect Scrollbar::increment_gutter_rect() const
  106. {
  107. auto scrubber_rect = this->scrubber_rect();
  108. if (orientation() == Orientation::Vertical)
  109. return { 0, scrubber_rect.bottom() + 1, button_width(), height() - button_height() - scrubber_rect.bottom() - 1 };
  110. else
  111. return { scrubber_rect.right() + 1, 0, width() - button_width() - scrubber_rect.right() - 1, button_width() };
  112. }
  113. int Scrollbar::scrubbable_range_in_pixels() const
  114. {
  115. if (orientation() == Orientation::Vertical)
  116. return height() - button_height() * 2 - visible_scrubber_size();
  117. else
  118. return width() - button_width() * 2 - visible_scrubber_size();
  119. }
  120. bool Scrollbar::has_scrubber() const
  121. {
  122. return max() != min();
  123. }
  124. int Scrollbar::unclamped_scrubber_size() const
  125. {
  126. int pixel_range = length(orientation()) - button_size() * 2;
  127. int value_range = max() - min();
  128. int scrubber_size = 0;
  129. if (value_range > 0) {
  130. // Scrubber size should be proportional to the visible portion
  131. // (page) in relation to the content (value range + page)
  132. scrubber_size = (page_step() * pixel_range) / (value_range + page_step());
  133. }
  134. return scrubber_size;
  135. }
  136. int Scrollbar::visible_scrubber_size() const
  137. {
  138. return ::max(unclamped_scrubber_size(), button_size());
  139. }
  140. Gfx::IntRect Scrollbar::scrubber_rect() const
  141. {
  142. if (!has_scrubber() || length(orientation()) <= (button_size() * 2) + visible_scrubber_size())
  143. return {};
  144. float x_or_y;
  145. if (value() == min())
  146. x_or_y = button_size();
  147. else if (value() == max())
  148. x_or_y = (length(orientation()) - button_size() - visible_scrubber_size()) + 1;
  149. else {
  150. float range_size = max() - min();
  151. float available = scrubbable_range_in_pixels();
  152. float step = available / range_size;
  153. x_or_y = (button_size() + (step * value()));
  154. }
  155. if (orientation() == Orientation::Vertical)
  156. return { 0, (int)x_or_y, button_width(), visible_scrubber_size() };
  157. else
  158. return { (int)x_or_y, 0, visible_scrubber_size(), button_height() };
  159. }
  160. void Scrollbar::paint_event(PaintEvent& event)
  161. {
  162. Painter painter(*this);
  163. painter.add_clip_rect(event.rect());
  164. Component hovered_component_for_painting = m_hovered_component;
  165. if (!has_scrubber() || (m_pressed_component != Component::None && m_hovered_component != m_pressed_component))
  166. hovered_component_for_painting = Component::None;
  167. painter.fill_rect_with_dither_pattern(rect(), palette().button().lightened(1.3f), palette().button());
  168. bool decrement_pressed = (m_pressed_component == Component::DecrementButton) && (m_pressed_component == m_hovered_component);
  169. bool increment_pressed = (m_pressed_component == Component::IncrementButton) && (m_pressed_component == m_hovered_component);
  170. Gfx::StylePainter::paint_button(painter, decrement_button_rect(), palette(), Gfx::ButtonStyle::Normal, decrement_pressed, hovered_component_for_painting == Component::DecrementButton);
  171. Gfx::StylePainter::paint_button(painter, increment_button_rect(), palette(), Gfx::ButtonStyle::Normal, increment_pressed, hovered_component_for_painting == Component::IncrementButton);
  172. if (length(orientation()) > default_button_size()) {
  173. auto decrement_location = decrement_button_rect().location().translated(3, 3);
  174. if (decrement_pressed)
  175. decrement_location.move_by(1, 1);
  176. if (!has_scrubber() || !is_enabled())
  177. painter.draw_bitmap(decrement_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, palette().threed_highlight());
  178. 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());
  179. auto increment_location = increment_button_rect().location().translated(3, 3);
  180. if (increment_pressed)
  181. increment_location.move_by(1, 1);
  182. if (!has_scrubber() || !is_enabled())
  183. painter.draw_bitmap(increment_location.translated(1, 1), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, palette().threed_highlight());
  184. 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());
  185. }
  186. if (has_scrubber())
  187. Gfx::StylePainter::paint_button(painter, scrubber_rect(), palette(), Gfx::ButtonStyle::Normal, false, hovered_component_for_painting == Component::Scrubber || m_pressed_component == Component::Scrubber);
  188. }
  189. void Scrollbar::on_automatic_scrolling_timer_fired()
  190. {
  191. if (m_pressed_component == Component::DecrementButton && component_at_position(m_last_mouse_position) == Component::DecrementButton) {
  192. set_value(value() - step());
  193. return;
  194. }
  195. if (m_pressed_component == Component::IncrementButton && component_at_position(m_last_mouse_position) == Component::IncrementButton) {
  196. set_value(value() + step());
  197. return;
  198. }
  199. if (m_pressed_component == Component::Gutter && component_at_position(m_last_mouse_position) == Component::Gutter) {
  200. scroll_by_page(m_last_mouse_position);
  201. m_hovered_component = component_at_position(m_last_mouse_position);
  202. return;
  203. }
  204. }
  205. void Scrollbar::mousedown_event(MouseEvent& event)
  206. {
  207. if (event.button() != MouseButton::Left)
  208. return;
  209. if (!has_scrubber())
  210. return;
  211. m_last_mouse_position = event.position();
  212. m_pressed_component = component_at_position(m_last_mouse_position);
  213. if (m_pressed_component == Component::DecrementButton) {
  214. set_automatic_scrolling_active(true, Component::DecrementButton);
  215. update();
  216. return;
  217. }
  218. if (m_pressed_component == Component::IncrementButton) {
  219. set_automatic_scrolling_active(true, Component::IncrementButton);
  220. update();
  221. return;
  222. }
  223. if (event.shift()) {
  224. scroll_to_position(event.position());
  225. m_pressed_component = component_at_position(event.position());
  226. VERIFY(m_pressed_component == Component::Scrubber);
  227. }
  228. if (m_pressed_component == Component::Scrubber) {
  229. m_scrub_start_value = value();
  230. m_scrub_origin = event.position();
  231. update();
  232. return;
  233. }
  234. VERIFY(!event.shift());
  235. VERIFY(m_pressed_component == Component::Gutter);
  236. set_automatic_scrolling_active(true, Component::Gutter);
  237. update();
  238. }
  239. void Scrollbar::mouseup_event(MouseEvent& event)
  240. {
  241. if (event.button() != MouseButton::Left)
  242. return;
  243. set_automatic_scrolling_active(false, Component::None);
  244. update();
  245. }
  246. void Scrollbar::mousewheel_event(MouseEvent& event)
  247. {
  248. if (!is_scrollable())
  249. return;
  250. set_value(value() + event.wheel_delta() * step());
  251. Widget::mousewheel_event(event);
  252. }
  253. void Scrollbar::set_automatic_scrolling_active(bool active, Component pressed_component)
  254. {
  255. m_pressed_component = pressed_component;
  256. if (m_pressed_component == Component::Gutter)
  257. m_automatic_scrolling_timer->set_interval(200);
  258. else
  259. m_automatic_scrolling_timer->set_interval(100);
  260. if (active) {
  261. on_automatic_scrolling_timer_fired();
  262. m_automatic_scrolling_timer->start();
  263. } else {
  264. m_automatic_scrolling_timer->stop();
  265. }
  266. }
  267. void Scrollbar::scroll_by_page(const Gfx::IntPoint& click_position)
  268. {
  269. float range_size = max() - min();
  270. float available = scrubbable_range_in_pixels();
  271. float rel_scrubber_size = unclamped_scrubber_size() / available;
  272. float page_increment = range_size * rel_scrubber_size;
  273. if (click_position.primary_offset_for_orientation(orientation()) < scrubber_rect().primary_offset_for_orientation(orientation()))
  274. set_value(value() - page_increment);
  275. else
  276. set_value(value() + page_increment);
  277. }
  278. void Scrollbar::scroll_to_position(const Gfx::IntPoint& click_position)
  279. {
  280. float range_size = max() - min();
  281. float available = scrubbable_range_in_pixels();
  282. float x_or_y = ::max(0, click_position.primary_offset_for_orientation(orientation()) - button_width() - button_width() / 2);
  283. float rel_x_or_y = x_or_y / available;
  284. set_value(min() + rel_x_or_y * range_size);
  285. }
  286. Scrollbar::Component Scrollbar::component_at_position(const Gfx::IntPoint& position)
  287. {
  288. if (scrubber_rect().contains(position))
  289. return Component::Scrubber;
  290. if (decrement_button_rect().contains(position))
  291. return Component::DecrementButton;
  292. if (increment_button_rect().contains(position))
  293. return Component::IncrementButton;
  294. if (rect().contains(position))
  295. return Component::Gutter;
  296. return Component::None;
  297. }
  298. void Scrollbar::mousemove_event(MouseEvent& event)
  299. {
  300. m_last_mouse_position = event.position();
  301. auto old_hovered_component = m_hovered_component;
  302. m_hovered_component = component_at_position(m_last_mouse_position);
  303. if (old_hovered_component != m_hovered_component) {
  304. update();
  305. }
  306. if (m_pressed_component != Component::Scrubber)
  307. return;
  308. float delta = orientation() == Orientation::Vertical ? (event.y() - m_scrub_origin.y()) : (event.x() - m_scrub_origin.x());
  309. float scrubbable_range = scrubbable_range_in_pixels();
  310. float value_steps_per_scrubbed_pixel = (max() - min()) / scrubbable_range;
  311. float new_value = m_scrub_start_value + (value_steps_per_scrubbed_pixel * delta);
  312. set_value(new_value);
  313. }
  314. void Scrollbar::leave_event(Core::Event&)
  315. {
  316. if (m_hovered_component != Component::None) {
  317. m_hovered_component = Component::None;
  318. update();
  319. }
  320. }
  321. void Scrollbar::change_event(Event& event)
  322. {
  323. if (event.type() == Event::Type::EnabledChange) {
  324. if (!is_enabled())
  325. set_automatic_scrolling_active(false, Component::None);
  326. }
  327. return Widget::change_event(event);
  328. }
  329. }