AbstractScrollableWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2018-2021, 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/AbstractScrollableWidget.h>
  9. #include <LibGUI/Scrollbar.h>
  10. namespace GUI {
  11. AbstractScrollableWidget::AbstractScrollableWidget()
  12. {
  13. m_vertical_scrollbar = add<AbstractScrollableWidgetScrollbar>(*this, Orientation::Vertical);
  14. m_vertical_scrollbar->set_step(4);
  15. m_vertical_scrollbar->on_change = [this](int) {
  16. did_scroll();
  17. update();
  18. };
  19. m_horizontal_scrollbar = add<AbstractScrollableWidgetScrollbar>(*this, Orientation::Horizontal);
  20. m_horizontal_scrollbar->set_step(4);
  21. m_horizontal_scrollbar->set_page_step(30);
  22. m_horizontal_scrollbar->on_change = [this](int) {
  23. did_scroll();
  24. update();
  25. };
  26. m_corner_widget = add<Widget>();
  27. m_corner_widget->set_fill_with_background_color(true);
  28. m_automatic_scrolling_timer = add<Core::Timer>();
  29. m_automatic_scrolling_timer->set_interval(50);
  30. m_automatic_scrolling_timer->on_timeout = [this] {
  31. on_automatic_scrolling_timer_fired();
  32. };
  33. }
  34. void AbstractScrollableWidget::handle_wheel_event(MouseEvent& event, Widget& event_source)
  35. {
  36. if (!m_scrollbars_enabled) {
  37. event.ignore();
  38. return;
  39. }
  40. int wheel_delta_x { 0 };
  41. bool vertical_scroll_hijacked { false };
  42. if (event.shift() || &event_source == m_horizontal_scrollbar.ptr()) {
  43. wheel_delta_x = event.wheel_delta_y();
  44. vertical_scroll_hijacked = true;
  45. }
  46. if (event.wheel_delta_x() != 0) {
  47. wheel_delta_x = event.wheel_delta_x();
  48. }
  49. if (wheel_delta_x != 0) {
  50. // FIXME: The wheel delta multiplier should probably come from... somewhere?
  51. horizontal_scrollbar().increase_slider_by(wheel_delta_x * 60);
  52. }
  53. if (!vertical_scroll_hijacked && event.wheel_delta_y() != 0) {
  54. vertical_scrollbar().increase_slider_by(event.wheel_delta_y() * 20);
  55. }
  56. }
  57. void AbstractScrollableWidget::mousewheel_event(MouseEvent& event)
  58. {
  59. handle_wheel_event(event, *this);
  60. }
  61. void AbstractScrollableWidget::custom_layout()
  62. {
  63. auto inner_rect = frame_inner_rect_for_size(size());
  64. int height_wanted_by_horizontal_scrollbar = m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->min_height() : 0;
  65. int width_wanted_by_vertical_scrollbar = m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->min_width() : 0;
  66. m_vertical_scrollbar->set_relative_rect(
  67. inner_rect.right() + 1 - m_vertical_scrollbar->min_width(),
  68. inner_rect.top(),
  69. m_vertical_scrollbar->min_width(),
  70. inner_rect.height() - height_wanted_by_horizontal_scrollbar);
  71. m_horizontal_scrollbar->set_relative_rect(
  72. inner_rect.left(),
  73. inner_rect.bottom() + 1 - m_horizontal_scrollbar->min_height(),
  74. inner_rect.width() - width_wanted_by_vertical_scrollbar,
  75. m_horizontal_scrollbar->min_height());
  76. m_corner_widget->set_visible(m_vertical_scrollbar->is_visible() && m_horizontal_scrollbar->is_visible());
  77. if (m_corner_widget->is_visible()) {
  78. Gfx::IntRect corner_rect { m_horizontal_scrollbar->relative_rect().right() + 1, m_vertical_scrollbar->relative_rect().bottom() + 1, width_occupied_by_vertical_scrollbar(), height_occupied_by_horizontal_scrollbar() };
  79. m_corner_widget->set_relative_rect(corner_rect);
  80. }
  81. }
  82. void AbstractScrollableWidget::resize_event(ResizeEvent& event)
  83. {
  84. Frame::resize_event(event);
  85. update_scrollbar_ranges();
  86. }
  87. Gfx::IntSize AbstractScrollableWidget::available_size() const
  88. {
  89. auto inner_size = Widget::content_size();
  90. unsigned available_width = max(inner_size.width() - m_size_occupied_by_fixed_elements.width(), 0);
  91. unsigned available_height = max(inner_size.height() - m_size_occupied_by_fixed_elements.height(), 0);
  92. return { available_width, available_height };
  93. }
  94. Gfx::IntSize AbstractScrollableWidget::excess_size() const
  95. {
  96. auto available_size = this->available_size();
  97. int excess_height = max(0, m_content_size.height() - available_size.height());
  98. int excess_width = max(0, m_content_size.width() - available_size.width());
  99. return { excess_width, excess_height };
  100. }
  101. void AbstractScrollableWidget::update_scrollbar_ranges()
  102. {
  103. if (should_hide_unnecessary_scrollbars()) {
  104. if (excess_size().height() - height_occupied_by_horizontal_scrollbar() <= 0 && excess_size().width() - width_occupied_by_vertical_scrollbar() <= 0) {
  105. m_horizontal_scrollbar->set_visible(false);
  106. m_vertical_scrollbar->set_visible(false);
  107. } else {
  108. auto vertical_initial_visibility = m_vertical_scrollbar->is_visible();
  109. auto horizontal_initial_visibility = m_horizontal_scrollbar->is_visible();
  110. m_vertical_scrollbar->set_visible(excess_size().height() > 0);
  111. m_horizontal_scrollbar->set_visible(excess_size().width() > 0);
  112. if (m_vertical_scrollbar->is_visible() != vertical_initial_visibility)
  113. m_horizontal_scrollbar->set_visible(excess_size().width() > 0);
  114. if (m_horizontal_scrollbar->is_visible() != horizontal_initial_visibility)
  115. m_vertical_scrollbar->set_visible(excess_size().height() > 0);
  116. }
  117. }
  118. m_horizontal_scrollbar->set_range(0, excess_size().width());
  119. m_horizontal_scrollbar->set_page_step(visible_content_rect().width() - m_horizontal_scrollbar->step());
  120. m_vertical_scrollbar->set_range(0, excess_size().height());
  121. m_vertical_scrollbar->set_page_step(visible_content_rect().height() - m_vertical_scrollbar->step());
  122. }
  123. void AbstractScrollableWidget::set_content_size(Gfx::IntSize const& size)
  124. {
  125. if (m_content_size == size)
  126. return;
  127. m_content_size = size;
  128. update_scrollbar_ranges();
  129. }
  130. void AbstractScrollableWidget::set_size_occupied_by_fixed_elements(Gfx::IntSize const& size)
  131. {
  132. if (m_size_occupied_by_fixed_elements == size)
  133. return;
  134. m_size_occupied_by_fixed_elements = size;
  135. update_scrollbar_ranges();
  136. }
  137. int AbstractScrollableWidget::height_occupied_by_horizontal_scrollbar() const
  138. {
  139. return m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->height() : 0;
  140. }
  141. int AbstractScrollableWidget::width_occupied_by_vertical_scrollbar() const
  142. {
  143. return m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->width() : 0;
  144. }
  145. Margins AbstractScrollableWidget::content_margins() const
  146. {
  147. return Frame::content_margins() + Margins { 0, width_occupied_by_vertical_scrollbar(), height_occupied_by_horizontal_scrollbar(), 0 };
  148. }
  149. Gfx::IntRect AbstractScrollableWidget::visible_content_rect() const
  150. {
  151. auto inner_size = Widget::content_size();
  152. Gfx::IntRect rect {
  153. m_horizontal_scrollbar->value(),
  154. m_vertical_scrollbar->value(),
  155. min(m_content_size.width(), inner_size.width() - m_size_occupied_by_fixed_elements.width()),
  156. min(m_content_size.height(), inner_size.height() - m_size_occupied_by_fixed_elements.height())
  157. };
  158. if (rect.is_empty())
  159. return {};
  160. return rect;
  161. }
  162. void AbstractScrollableWidget::scroll_into_view(Gfx::IntRect const& rect, Orientation orientation)
  163. {
  164. if (orientation == Orientation::Vertical)
  165. return scroll_into_view(rect, false, true);
  166. return scroll_into_view(rect, true, false);
  167. }
  168. void AbstractScrollableWidget::scroll_into_view(Gfx::IntRect const& rect, bool scroll_horizontally, bool scroll_vertically)
  169. {
  170. auto visible_content_rect = this->visible_content_rect();
  171. if (visible_content_rect.contains(rect))
  172. return;
  173. if (scroll_vertically) {
  174. if (rect.top() < visible_content_rect.top()) {
  175. m_vertical_scrollbar->set_value(rect.top());
  176. } else if (rect.top() > visible_content_rect.top() && rect.bottom() > visible_content_rect.bottom()) {
  177. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height() + 1);
  178. }
  179. }
  180. if (scroll_horizontally) {
  181. if (rect.left() < visible_content_rect.left()) {
  182. m_horizontal_scrollbar->set_value(rect.left());
  183. } else if (rect.left() > visible_content_rect.left() && rect.right() > visible_content_rect.right()) {
  184. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width() + 1);
  185. }
  186. }
  187. }
  188. void AbstractScrollableWidget::set_scrollbars_enabled(bool scrollbars_enabled)
  189. {
  190. if (m_scrollbars_enabled == scrollbars_enabled)
  191. return;
  192. m_scrollbars_enabled = scrollbars_enabled;
  193. m_vertical_scrollbar->set_visible(m_scrollbars_enabled);
  194. m_horizontal_scrollbar->set_visible(m_scrollbars_enabled);
  195. m_corner_widget->set_visible(m_scrollbars_enabled);
  196. }
  197. void AbstractScrollableWidget::scroll_to_top()
  198. {
  199. scroll_into_view({}, Orientation::Vertical);
  200. }
  201. void AbstractScrollableWidget::scroll_to_bottom()
  202. {
  203. scroll_into_view({ 0, content_height(), 0, 0 }, Orientation::Vertical);
  204. }
  205. void AbstractScrollableWidget::set_automatic_scrolling_timer(bool active)
  206. {
  207. if (active == m_active_scrolling_enabled)
  208. return;
  209. m_active_scrolling_enabled = active;
  210. if (active) {
  211. on_automatic_scrolling_timer_fired();
  212. m_automatic_scrolling_timer->start();
  213. } else {
  214. m_automatic_scrolling_timer->stop();
  215. }
  216. }
  217. Gfx::IntPoint AbstractScrollableWidget::automatic_scroll_delta_from_position(Gfx::IntPoint const& pos) const
  218. {
  219. Gfx::IntPoint delta { 0, 0 };
  220. if (pos.y() < m_autoscroll_threshold)
  221. delta.set_y(clamp(-(m_autoscroll_threshold - pos.y()), -m_autoscroll_threshold, 0));
  222. else if (pos.y() > widget_inner_rect().height() - m_autoscroll_threshold)
  223. delta.set_y(clamp(m_autoscroll_threshold - (widget_inner_rect().height() - pos.y()), 0, m_autoscroll_threshold));
  224. if (pos.x() < m_autoscroll_threshold)
  225. delta.set_x(clamp(-(m_autoscroll_threshold - pos.x()), -m_autoscroll_threshold, 0));
  226. else if (pos.x() > widget_inner_rect().width() - m_autoscroll_threshold)
  227. delta.set_x(clamp(m_autoscroll_threshold - (widget_inner_rect().width() - pos.x()), 0, m_autoscroll_threshold));
  228. return delta;
  229. }
  230. Gfx::IntRect AbstractScrollableWidget::widget_inner_rect() const
  231. {
  232. auto rect = frame_inner_rect();
  233. rect.set_width(rect.width() - width_occupied_by_vertical_scrollbar());
  234. rect.set_height(rect.height() - height_occupied_by_horizontal_scrollbar());
  235. return rect;
  236. }
  237. Gfx::IntPoint AbstractScrollableWidget::to_content_position(Gfx::IntPoint const& widget_position) const
  238. {
  239. auto content_position = widget_position;
  240. content_position.translate_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  241. content_position.translate_by(-frame_thickness(), -frame_thickness());
  242. return content_position;
  243. }
  244. Gfx::IntPoint AbstractScrollableWidget::to_widget_position(Gfx::IntPoint const& content_position) const
  245. {
  246. auto widget_position = content_position;
  247. widget_position.translate_by(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  248. widget_position.translate_by(frame_thickness(), frame_thickness());
  249. return widget_position;
  250. }
  251. }