AbstractScrollableWidget.cpp 11 KB

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