Splitter.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <LibGUI/BoxLayout.h>
  8. #include <LibGUI/Painter.h>
  9. #include <LibGUI/Splitter.h>
  10. #include <LibGUI/UIDimensions.h>
  11. #include <LibGUI/Window.h>
  12. #include <LibGfx/Palette.h>
  13. REGISTER_WIDGET(GUI, HorizontalSplitter)
  14. REGISTER_WIDGET(GUI, VerticalSplitter)
  15. namespace GUI {
  16. Splitter::Splitter(Orientation orientation)
  17. : m_orientation(orientation)
  18. {
  19. REGISTER_ENUM_PROPERTY("opportunistic_resizee", opportunisitic_resizee, set_opportunisitic_resizee, OpportunisticResizee,
  20. { OpportunisticResizee::First, "First" },
  21. { OpportunisticResizee::Second, "Second" });
  22. set_background_role(ColorRole::Button);
  23. set_layout<BoxLayout>(orientation);
  24. set_fill_with_background_color(true);
  25. if (m_orientation == Gfx::Orientation::Horizontal)
  26. layout()->set_spacing(3);
  27. else
  28. layout()->set_spacing(4);
  29. }
  30. void Splitter::paint_event(PaintEvent& event)
  31. {
  32. Painter painter(*this);
  33. painter.add_clip_rect(event.rect());
  34. auto palette = this->palette();
  35. auto paint_knurl = [&](int x, int y) {
  36. painter.set_pixel(x, y, palette.threed_shadow1());
  37. painter.set_pixel(x + 1, y, palette.threed_shadow1());
  38. painter.set_pixel(x, y + 1, palette.threed_shadow1());
  39. painter.set_pixel(x + 1, y + 1, palette.threed_highlight());
  40. };
  41. constexpr size_t knurl_width = 2;
  42. constexpr size_t knurl_spacing = 1;
  43. constexpr size_t knurl_count = 10;
  44. constexpr size_t total_knurling_width = knurl_count * (knurl_width + knurl_spacing);
  45. if (m_hovered_index.has_value())
  46. painter.fill_rect(m_grabbables[m_hovered_index.value()].paint_rect, palette.hover_highlight());
  47. for (auto& grabbable : m_grabbables) {
  48. for (size_t i = 0; i < knurl_count; ++i) {
  49. auto& rect = grabbable.paint_rect;
  50. int primary = rect.center().primary_offset_for_orientation(m_orientation) - 1;
  51. int secondary = rect.center().secondary_offset_for_orientation(m_orientation) - (total_knurling_width / 2) + (i * (knurl_width + knurl_spacing));
  52. if (m_orientation == Gfx::Orientation::Vertical)
  53. paint_knurl(secondary, primary);
  54. else
  55. paint_knurl(primary, secondary);
  56. }
  57. }
  58. }
  59. void Splitter::resize_event(ResizeEvent& event)
  60. {
  61. Widget::resize_event(event);
  62. set_hovered_grabbable(nullptr);
  63. }
  64. void Splitter::set_hovered_grabbable(Grabbable* grabbable)
  65. {
  66. if (m_hovered_index.has_value()) {
  67. if (grabbable && grabbable->index == m_hovered_index.value())
  68. return;
  69. update(m_grabbables[m_hovered_index.value()].paint_rect);
  70. }
  71. if (grabbable) {
  72. m_hovered_index = grabbable->index;
  73. update(grabbable->paint_rect);
  74. } else {
  75. m_hovered_index = {};
  76. }
  77. }
  78. void Splitter::override_cursor(bool do_override)
  79. {
  80. if (do_override) {
  81. if (!m_overriding_cursor) {
  82. set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
  83. m_overriding_cursor = true;
  84. }
  85. } else {
  86. if (m_overriding_cursor) {
  87. set_override_cursor(Gfx::StandardCursor::None);
  88. m_overriding_cursor = false;
  89. }
  90. }
  91. }
  92. void Splitter::leave_event(Core::Event&)
  93. {
  94. if (!m_resizing)
  95. override_cursor(false);
  96. set_hovered_grabbable(nullptr);
  97. }
  98. Splitter::Grabbable* Splitter::grabbable_at(Gfx::IntPoint const& position)
  99. {
  100. for (auto& grabbable : m_grabbables) {
  101. if (grabbable.grabbable_rect.contains(position))
  102. return &grabbable;
  103. }
  104. return nullptr;
  105. }
  106. void Splitter::mousedown_event(MouseEvent& event)
  107. {
  108. if (event.button() != MouseButton::Primary)
  109. return;
  110. auto* grabbable = grabbable_at(event.position());
  111. if (!grabbable)
  112. return;
  113. m_resizing = true;
  114. m_first_resizee = *grabbable->first_widget;
  115. m_second_resizee = *grabbable->second_widget;
  116. m_first_resizee_start_size = m_first_resizee->size();
  117. m_second_resizee_start_size = m_second_resizee->size();
  118. m_resize_origin = event.position();
  119. VERIFY(layout());
  120. auto spacer = layout()->spacing();
  121. auto splitter = size().primary_size_for_orientation(m_orientation);
  122. m_first_resizee_max_size = splitter - spacer - m_second_resizee->calculated_min_size().value_or({ 0, 0 }).primary_size_for_orientation(m_orientation).as_int();
  123. m_second_resizee_max_size = splitter - spacer - m_first_resizee->calculated_min_size().value_or({ 0, 0 }).primary_size_for_orientation(m_orientation).as_int();
  124. }
  125. Gfx::IntRect Splitter::rect_between_widgets(GUI::Widget const& first_widget, GUI::Widget const& second_widget, bool honor_grabbable_margins) const
  126. {
  127. auto first_widget_rect = honor_grabbable_margins ? first_widget.relative_non_grabbable_rect() : first_widget.relative_rect();
  128. auto second_widget_rect = honor_grabbable_margins ? second_widget.relative_non_grabbable_rect() : second_widget.relative_rect();
  129. auto first_edge = first_widget_rect.last_edge_for_orientation(m_orientation);
  130. auto second_edge = second_widget_rect.first_edge_for_orientation(m_orientation);
  131. Gfx::IntRect rect;
  132. rect.set_primary_offset_for_orientation(m_orientation, first_edge + 1);
  133. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge - 1);
  134. rect.set_secondary_offset_for_orientation(m_orientation, 0);
  135. rect.set_secondary_size_for_orientation(m_orientation, relative_rect().secondary_size_for_orientation(m_orientation));
  136. return rect;
  137. }
  138. void Splitter::recompute_grabbables()
  139. {
  140. auto old_grabbables_count = m_grabbables.size();
  141. m_grabbables.clear();
  142. auto old_hovered_index = m_hovered_index;
  143. m_hovered_index = {};
  144. auto child_widgets = this->child_widgets();
  145. child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });
  146. m_last_child_count = child_widgets.size();
  147. if (child_widgets.size() < 2)
  148. return;
  149. size_t start_index = 0;
  150. size_t end_index = 1;
  151. while (end_index < child_widgets.size()) {
  152. auto const& first_widget = child_widgets[start_index];
  153. auto const& second_widget = child_widgets[end_index];
  154. m_grabbables.append(Grabbable {
  155. .index = m_grabbables.size(),
  156. .grabbable_rect = rect_between_widgets(first_widget, second_widget, true),
  157. .paint_rect = rect_between_widgets(first_widget, second_widget, false),
  158. .first_widget = first_widget,
  159. .second_widget = second_widget,
  160. });
  161. ++start_index;
  162. ++end_index;
  163. }
  164. if (old_hovered_index.has_value() && old_grabbables_count == m_grabbables.size())
  165. set_hovered_grabbable(&m_grabbables[old_hovered_index.value()]);
  166. }
  167. void Splitter::mousemove_event(MouseEvent& event)
  168. {
  169. auto* grabbable = grabbable_at(event.position());
  170. set_hovered_grabbable(grabbable);
  171. if (!m_resizing) {
  172. override_cursor(grabbable != nullptr);
  173. return;
  174. }
  175. if (!m_first_resizee || !m_second_resizee) {
  176. m_resizing = false;
  177. return;
  178. }
  179. auto delta = (event.position() - m_resize_origin).primary_offset_for_orientation(m_orientation);
  180. auto new_first_resizee_size = clamp(m_first_resizee_start_size.primary_size_for_orientation(m_orientation) + delta, 0, m_first_resizee_max_size);
  181. auto new_second_resizee_size = clamp(m_second_resizee_start_size.primary_size_for_orientation(m_orientation) - delta, 0, m_second_resizee_max_size);
  182. if (m_orientation == Orientation::Horizontal) {
  183. if (opportunisitic_resizee() == OpportunisticResizee::First) {
  184. m_first_resizee->set_preferred_width(SpecialDimension::OpportunisticGrow);
  185. m_second_resizee->set_preferred_width(new_second_resizee_size);
  186. } else {
  187. VERIFY(opportunisitic_resizee() == OpportunisticResizee::Second);
  188. m_second_resizee->set_preferred_width(SpecialDimension::OpportunisticGrow);
  189. m_first_resizee->set_preferred_width(new_first_resizee_size);
  190. }
  191. } else {
  192. if (opportunisitic_resizee() == OpportunisticResizee::First) {
  193. m_first_resizee->set_preferred_height(SpecialDimension::OpportunisticGrow);
  194. m_second_resizee->set_preferred_height(new_second_resizee_size);
  195. } else {
  196. VERIFY(opportunisitic_resizee() == OpportunisticResizee::Second);
  197. m_second_resizee->set_preferred_height(SpecialDimension::OpportunisticGrow);
  198. m_first_resizee->set_preferred_height(new_first_resizee_size);
  199. }
  200. }
  201. invalidate_layout();
  202. }
  203. void Splitter::did_layout()
  204. {
  205. recompute_grabbables();
  206. }
  207. void Splitter::custom_layout()
  208. {
  209. auto child_widgets = this->child_widgets();
  210. child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });
  211. if (!child_widgets.size())
  212. return;
  213. if (m_last_child_count > child_widgets.size()) {
  214. bool has_child_to_fill_space = false;
  215. for (auto& child : child_widgets) {
  216. if (child.preferred_size().primary_size_for_orientation(m_orientation).is_opportunistic_grow()) {
  217. has_child_to_fill_space = true;
  218. break;
  219. }
  220. }
  221. if (!has_child_to_fill_space)
  222. child_widgets.last().set_preferred_size(SpecialDimension::OpportunisticGrow);
  223. }
  224. }
  225. void Splitter::mouseup_event(MouseEvent& event)
  226. {
  227. if (event.button() != MouseButton::Primary)
  228. return;
  229. m_resizing = false;
  230. m_first_resizee = nullptr;
  231. m_second_resizee = nullptr;
  232. if (!rect().contains(event.position()))
  233. set_override_cursor(Gfx::StandardCursor::None);
  234. }
  235. }