AbstractScrollableWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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->effective_min_size().height().as_int() : 0;
  65. int width_wanted_by_vertical_scrollbar = m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->effective_min_size().width().as_int() : 0;
  66. {
  67. int vertical_scrollbar_width = m_vertical_scrollbar->effective_min_size().width().as_int();
  68. m_vertical_scrollbar->set_relative_rect(
  69. inner_rect.right() + 1 - vertical_scrollbar_width,
  70. inner_rect.top(),
  71. vertical_scrollbar_width,
  72. inner_rect.height() - height_wanted_by_horizontal_scrollbar);
  73. }
  74. {
  75. int horizontal_scrollbar_height = m_horizontal_scrollbar->effective_min_size().height().as_int();
  76. m_horizontal_scrollbar->set_relative_rect(
  77. inner_rect.left(),
  78. inner_rect.bottom() + 1 - horizontal_scrollbar_height,
  79. inner_rect.width() - width_wanted_by_vertical_scrollbar,
  80. horizontal_scrollbar_height);
  81. }
  82. m_corner_widget->set_visible(m_vertical_scrollbar->is_visible() && m_horizontal_scrollbar->is_visible());
  83. if (m_corner_widget->is_visible()) {
  84. 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() };
  85. m_corner_widget->set_relative_rect(corner_rect);
  86. }
  87. }
  88. void AbstractScrollableWidget::resize_event(ResizeEvent& event)
  89. {
  90. Frame::resize_event(event);
  91. update_scrollbar_ranges();
  92. }
  93. Gfx::IntSize AbstractScrollableWidget::available_size() const
  94. {
  95. auto inner_size = Widget::content_size();
  96. int available_width = max(inner_size.width() - m_size_occupied_by_fixed_elements.width(), 0);
  97. int available_height = max(inner_size.height() - m_size_occupied_by_fixed_elements.height(), 0);
  98. return { available_width, available_height };
  99. }
  100. Gfx::IntSize AbstractScrollableWidget::excess_size() const
  101. {
  102. auto available_size = this->available_size();
  103. int excess_height = max(0, m_content_size.height() - available_size.height());
  104. int excess_width = max(0, m_content_size.width() - available_size.width());
  105. return { excess_width, excess_height };
  106. }
  107. void AbstractScrollableWidget::update_scrollbar_ranges()
  108. {
  109. if (should_hide_unnecessary_scrollbars()) {
  110. if (excess_size().height() - height_occupied_by_horizontal_scrollbar() <= 0 && excess_size().width() - width_occupied_by_vertical_scrollbar() <= 0) {
  111. m_horizontal_scrollbar->set_visible(false);
  112. m_vertical_scrollbar->set_visible(false);
  113. } else {
  114. auto vertical_initial_visibility = m_vertical_scrollbar->is_visible();
  115. auto horizontal_initial_visibility = m_horizontal_scrollbar->is_visible();
  116. m_vertical_scrollbar->set_visible(excess_size().height() > 0);
  117. m_horizontal_scrollbar->set_visible(excess_size().width() > 0);
  118. if (m_vertical_scrollbar->is_visible() != vertical_initial_visibility)
  119. m_horizontal_scrollbar->set_visible(excess_size().width() > 0);
  120. if (m_horizontal_scrollbar->is_visible() != horizontal_initial_visibility)
  121. m_vertical_scrollbar->set_visible(excess_size().height() > 0);
  122. }
  123. }
  124. m_horizontal_scrollbar->set_range(0, excess_size().width());
  125. m_horizontal_scrollbar->set_page_step(visible_content_rect().width() - m_horizontal_scrollbar->step());
  126. m_vertical_scrollbar->set_range(0, excess_size().height());
  127. m_vertical_scrollbar->set_page_step(visible_content_rect().height() - m_vertical_scrollbar->step());
  128. }
  129. void AbstractScrollableWidget::set_content_size(Gfx::IntSize const& size)
  130. {
  131. if (m_content_size == size)
  132. return;
  133. m_content_size = size;
  134. update_scrollbar_ranges();
  135. }
  136. void AbstractScrollableWidget::set_size_occupied_by_fixed_elements(Gfx::IntSize const& size)
  137. {
  138. if (m_size_occupied_by_fixed_elements == size)
  139. return;
  140. m_size_occupied_by_fixed_elements = size;
  141. update_scrollbar_ranges();
  142. }
  143. int AbstractScrollableWidget::height_occupied_by_horizontal_scrollbar() const
  144. {
  145. return m_horizontal_scrollbar->is_visible() ? m_horizontal_scrollbar->height() : 0;
  146. }
  147. int AbstractScrollableWidget::width_occupied_by_vertical_scrollbar() const
  148. {
  149. return m_vertical_scrollbar->is_visible() ? m_vertical_scrollbar->width() : 0;
  150. }
  151. Margins AbstractScrollableWidget::content_margins() const
  152. {
  153. return Frame::content_margins() + Margins { 0, width_occupied_by_vertical_scrollbar(), height_occupied_by_horizontal_scrollbar(), 0 };
  154. }
  155. Gfx::IntRect AbstractScrollableWidget::visible_content_rect() const
  156. {
  157. auto inner_size = Widget::content_size();
  158. Gfx::IntRect rect {
  159. m_horizontal_scrollbar->value(),
  160. m_vertical_scrollbar->value(),
  161. min(m_content_size.width(), inner_size.width() - m_size_occupied_by_fixed_elements.width()),
  162. min(m_content_size.height(), inner_size.height() - m_size_occupied_by_fixed_elements.height())
  163. };
  164. if (rect.is_empty())
  165. return {};
  166. return rect;
  167. }
  168. void AbstractScrollableWidget::scroll_into_view(Gfx::IntRect const& rect, Orientation orientation)
  169. {
  170. if (orientation == Orientation::Vertical)
  171. return scroll_into_view(rect, false, true);
  172. return scroll_into_view(rect, true, false);
  173. }
  174. void AbstractScrollableWidget::scroll_into_view(Gfx::IntRect const& rect, bool scroll_horizontally, bool scroll_vertically)
  175. {
  176. auto visible_content_rect = this->visible_content_rect();
  177. if (visible_content_rect.contains(rect))
  178. return;
  179. if (scroll_vertically) {
  180. if (rect.top() < visible_content_rect.top()) {
  181. m_vertical_scrollbar->set_value(rect.top());
  182. } else if (rect.top() > visible_content_rect.top() && rect.bottom() > visible_content_rect.bottom()) {
  183. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height() + 1);
  184. }
  185. }
  186. if (scroll_horizontally) {
  187. if (rect.left() < visible_content_rect.left()) {
  188. m_horizontal_scrollbar->set_value(rect.left());
  189. } else if (rect.left() > visible_content_rect.left() && rect.right() > visible_content_rect.right()) {
  190. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width() + 1);
  191. }
  192. }
  193. }
  194. void AbstractScrollableWidget::set_scrollbars_enabled(bool scrollbars_enabled)
  195. {
  196. if (m_scrollbars_enabled == scrollbars_enabled)
  197. return;
  198. m_scrollbars_enabled = scrollbars_enabled;
  199. m_vertical_scrollbar->set_visible(m_scrollbars_enabled);
  200. m_horizontal_scrollbar->set_visible(m_scrollbars_enabled);
  201. m_corner_widget->set_visible(m_scrollbars_enabled);
  202. }
  203. void AbstractScrollableWidget::scroll_to_top()
  204. {
  205. scroll_into_view({}, Orientation::Vertical);
  206. }
  207. void AbstractScrollableWidget::scroll_to_bottom()
  208. {
  209. scroll_into_view({ 0, content_height(), 0, 0 }, Orientation::Vertical);
  210. }
  211. void AbstractScrollableWidget::set_automatic_scrolling_timer(bool active)
  212. {
  213. if (active == m_active_scrolling_enabled)
  214. return;
  215. m_active_scrolling_enabled = active;
  216. if (active) {
  217. on_automatic_scrolling_timer_fired();
  218. m_automatic_scrolling_timer->start();
  219. } else {
  220. m_automatic_scrolling_timer->stop();
  221. }
  222. }
  223. Gfx::IntPoint AbstractScrollableWidget::automatic_scroll_delta_from_position(Gfx::IntPoint const& pos) const
  224. {
  225. Gfx::IntPoint delta { 0, 0 };
  226. if (pos.y() < m_autoscroll_threshold)
  227. delta.set_y(clamp(-(m_autoscroll_threshold - pos.y()), -m_autoscroll_threshold, 0));
  228. else if (pos.y() > widget_inner_rect().height() - m_autoscroll_threshold)
  229. delta.set_y(clamp(m_autoscroll_threshold - (widget_inner_rect().height() - pos.y()), 0, m_autoscroll_threshold));
  230. if (pos.x() < m_autoscroll_threshold)
  231. delta.set_x(clamp(-(m_autoscroll_threshold - pos.x()), -m_autoscroll_threshold, 0));
  232. else if (pos.x() > widget_inner_rect().width() - m_autoscroll_threshold)
  233. delta.set_x(clamp(m_autoscroll_threshold - (widget_inner_rect().width() - pos.x()), 0, m_autoscroll_threshold));
  234. return delta;
  235. }
  236. Gfx::IntRect AbstractScrollableWidget::widget_inner_rect() const
  237. {
  238. auto rect = frame_inner_rect();
  239. rect.set_width(rect.width() - width_occupied_by_vertical_scrollbar());
  240. rect.set_height(rect.height() - height_occupied_by_horizontal_scrollbar());
  241. return rect;
  242. }
  243. Gfx::IntPoint AbstractScrollableWidget::to_content_position(Gfx::IntPoint const& widget_position) const
  244. {
  245. auto content_position = widget_position;
  246. content_position.translate_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  247. content_position.translate_by(-frame_thickness(), -frame_thickness());
  248. return content_position;
  249. }
  250. Gfx::IntPoint AbstractScrollableWidget::to_widget_position(Gfx::IntPoint const& content_position) const
  251. {
  252. auto widget_position = content_position;
  253. widget_position.translate_by(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  254. widget_position.translate_by(frame_thickness(), frame_thickness());
  255. return widget_position;
  256. }
  257. }