Splitter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_INT_PROPERTY("first_resizee_minimum_size", first_resizee_minimum_size, set_first_resizee_minimum_size);
  20. REGISTER_INT_PROPERTY("second_resizee_minimum_size", second_resizee_minimum_size, set_second_resizee_minimum_size);
  21. REGISTER_ENUM_PROPERTY("fixed_resizee", fixed_resizee, set_fixed_resizee, FixedResizee,
  22. { FixedResizee::First, "First" },
  23. { FixedResizee::Second, "Second" });
  24. set_background_role(ColorRole::Button);
  25. set_layout<BoxLayout>(orientation);
  26. set_fill_with_background_color(true);
  27. if (m_orientation == Gfx::Orientation::Horizontal)
  28. layout()->set_spacing(3);
  29. else
  30. layout()->set_spacing(4);
  31. }
  32. void Splitter::paint_event(PaintEvent& event)
  33. {
  34. Painter painter(*this);
  35. painter.add_clip_rect(event.rect());
  36. auto palette = this->palette();
  37. auto paint_knurl = [&](int x, int y) {
  38. painter.set_pixel(x, y, palette.threed_shadow1());
  39. painter.set_pixel(x + 1, y, palette.threed_shadow1());
  40. painter.set_pixel(x, y + 1, palette.threed_shadow1());
  41. painter.set_pixel(x + 1, y + 1, palette.threed_highlight());
  42. };
  43. constexpr size_t knurl_width = 2;
  44. constexpr size_t knurl_spacing = 1;
  45. constexpr size_t knurl_count = 10;
  46. constexpr size_t total_knurling_width = knurl_count * (knurl_width + knurl_spacing);
  47. if (m_hovered_index.has_value())
  48. painter.fill_rect(m_grabbables[m_hovered_index.value()].paint_rect, palette.hover_highlight());
  49. for (auto& grabbable : m_grabbables) {
  50. for (size_t i = 0; i < knurl_count; ++i) {
  51. auto& rect = grabbable.paint_rect;
  52. int primary = rect.center().primary_offset_for_orientation(m_orientation) - 1;
  53. int secondary = rect.center().secondary_offset_for_orientation(m_orientation) - (total_knurling_width / 2) + (i * (knurl_width + knurl_spacing));
  54. if (m_orientation == Gfx::Orientation::Vertical)
  55. paint_knurl(secondary, primary);
  56. else
  57. paint_knurl(primary, secondary);
  58. }
  59. }
  60. }
  61. void Splitter::resize_event(ResizeEvent& event)
  62. {
  63. Widget::resize_event(event);
  64. set_hovered_grabbable(nullptr);
  65. }
  66. void Splitter::set_hovered_grabbable(Grabbable* grabbable)
  67. {
  68. if (m_hovered_index.has_value()) {
  69. if (grabbable && grabbable->index == m_hovered_index.value())
  70. return;
  71. update(m_grabbables[m_hovered_index.value()].paint_rect);
  72. }
  73. if (grabbable) {
  74. m_hovered_index = grabbable->index;
  75. update(grabbable->paint_rect);
  76. } else {
  77. m_hovered_index = {};
  78. }
  79. }
  80. void Splitter::override_cursor(bool do_override)
  81. {
  82. if (do_override) {
  83. if (!m_overriding_cursor) {
  84. set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
  85. m_overriding_cursor = true;
  86. }
  87. } else {
  88. if (m_overriding_cursor) {
  89. set_override_cursor(Gfx::StandardCursor::None);
  90. m_overriding_cursor = false;
  91. }
  92. }
  93. }
  94. void Splitter::leave_event(Core::Event&)
  95. {
  96. if (!m_resizing)
  97. override_cursor(false);
  98. set_hovered_grabbable(nullptr);
  99. }
  100. Splitter::Grabbable* Splitter::grabbable_at(Gfx::IntPoint const& position)
  101. {
  102. for (auto& grabbable : m_grabbables) {
  103. if (grabbable.grabbable_rect.contains(position))
  104. return &grabbable;
  105. }
  106. return nullptr;
  107. }
  108. void Splitter::mousedown_event(MouseEvent& event)
  109. {
  110. if (event.button() != MouseButton::Primary)
  111. return;
  112. auto* grabbable = grabbable_at(event.position());
  113. if (!grabbable)
  114. return;
  115. m_resizing = true;
  116. m_first_resizee = *grabbable->first_widget;
  117. m_second_resizee = *grabbable->second_widget;
  118. m_first_resizee_start_size = m_first_resizee->size();
  119. m_second_resizee_start_size = m_second_resizee->size();
  120. m_resize_origin = event.position();
  121. }
  122. Gfx::IntRect Splitter::rect_between_widgets(GUI::Widget const& first_widget, GUI::Widget const& second_widget, bool honor_grabbable_margins) const
  123. {
  124. auto first_widget_rect = honor_grabbable_margins ? first_widget.relative_non_grabbable_rect() : first_widget.relative_rect();
  125. auto second_widget_rect = honor_grabbable_margins ? second_widget.relative_non_grabbable_rect() : second_widget.relative_rect();
  126. auto first_edge = first_widget_rect.last_edge_for_orientation(m_orientation);
  127. auto second_edge = second_widget_rect.first_edge_for_orientation(m_orientation);
  128. Gfx::IntRect rect;
  129. rect.set_primary_offset_for_orientation(m_orientation, first_edge + 1);
  130. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge - 1);
  131. rect.set_secondary_offset_for_orientation(m_orientation, 0);
  132. rect.set_secondary_size_for_orientation(m_orientation, relative_rect().secondary_size_for_orientation(m_orientation));
  133. return rect;
  134. }
  135. void Splitter::recompute_grabbables()
  136. {
  137. auto old_grabbables_count = m_grabbables.size();
  138. m_grabbables.clear();
  139. auto old_hovered_index = m_hovered_index;
  140. m_hovered_index = {};
  141. auto child_widgets = this->child_widgets();
  142. child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });
  143. m_last_child_count = child_widgets.size();
  144. if (child_widgets.size() < 2)
  145. return;
  146. size_t start_index = 0;
  147. size_t end_index = 1;
  148. while (end_index < child_widgets.size()) {
  149. auto const& first_widget = child_widgets[start_index];
  150. auto const& second_widget = child_widgets[end_index];
  151. m_grabbables.append(Grabbable {
  152. .index = m_grabbables.size(),
  153. .grabbable_rect = rect_between_widgets(first_widget, second_widget, true),
  154. .paint_rect = rect_between_widgets(first_widget, second_widget, false),
  155. .first_widget = first_widget,
  156. .second_widget = second_widget,
  157. });
  158. ++start_index;
  159. ++end_index;
  160. }
  161. if (old_hovered_index.has_value() && old_grabbables_count == m_grabbables.size())
  162. set_hovered_grabbable(&m_grabbables[old_hovered_index.value()]);
  163. }
  164. // FIXME: Respect the child widgets min and max sizes
  165. void Splitter::mousemove_event(MouseEvent& event)
  166. {
  167. auto* grabbable = grabbable_at(event.position());
  168. set_hovered_grabbable(grabbable);
  169. if (!m_resizing) {
  170. override_cursor(grabbable != nullptr);
  171. return;
  172. }
  173. auto delta = event.position() - m_resize_origin;
  174. if (!m_first_resizee || !m_second_resizee) {
  175. // One or both of the resizees were deleted during an ongoing resize, screw this.
  176. m_resizing = false;
  177. return;
  178. }
  179. auto new_first_resizee_size = m_first_resizee_start_size;
  180. auto new_second_resizee_size = m_second_resizee_start_size;
  181. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + delta.primary_offset_for_orientation(m_orientation));
  182. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - delta.primary_offset_for_orientation(m_orientation));
  183. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < m_first_resizee_minimum_size) {
  184. int correction = m_first_resizee_minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  185. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  186. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  187. }
  188. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < m_second_resizee_minimum_size) {
  189. int correction = m_second_resizee_minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  190. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  191. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  192. }
  193. if (m_orientation == Orientation::Horizontal) {
  194. if (fixed_resizee() == FixedResizee::First) {
  195. m_first_resizee->set_fixed_width(new_first_resizee_size.width());
  196. m_second_resizee->set_min_width(SpecialDimension::Shrink);
  197. m_second_resizee->set_max_width(SpecialDimension::Grow);
  198. } else {
  199. VERIFY(fixed_resizee() == FixedResizee::Second);
  200. m_second_resizee->set_fixed_width(new_second_resizee_size.width());
  201. m_first_resizee->set_min_width(SpecialDimension::Shrink);
  202. m_first_resizee->set_max_width(SpecialDimension::Grow);
  203. }
  204. } else {
  205. if (fixed_resizee() == FixedResizee::First) {
  206. m_first_resizee->set_fixed_height(new_first_resizee_size.height());
  207. m_second_resizee->set_min_height(SpecialDimension::Shrink);
  208. m_second_resizee->set_max_height(SpecialDimension::Grow);
  209. } else {
  210. VERIFY(fixed_resizee() == FixedResizee::Second);
  211. m_second_resizee->set_fixed_height(new_second_resizee_size.height());
  212. m_first_resizee->set_min_height(SpecialDimension::Shrink);
  213. m_first_resizee->set_max_height(SpecialDimension::Grow);
  214. }
  215. }
  216. invalidate_layout();
  217. }
  218. void Splitter::did_layout()
  219. {
  220. recompute_grabbables();
  221. }
  222. void Splitter::custom_layout()
  223. {
  224. auto child_widgets = this->child_widgets();
  225. child_widgets.remove_all_matching([&](auto& widget) { return !widget.is_visible(); });
  226. if (!child_widgets.size())
  227. return;
  228. if (m_last_child_count > child_widgets.size()) {
  229. bool has_child_to_fill_space = false;
  230. for (auto& child : child_widgets) {
  231. if (child.max_size().primary_size_for_orientation(m_orientation).is_grow()) {
  232. has_child_to_fill_space = true;
  233. break;
  234. }
  235. }
  236. if (!has_child_to_fill_space)
  237. child_widgets.last().set_preferred_size(SpecialDimension::Grow);
  238. }
  239. }
  240. void Splitter::mouseup_event(MouseEvent& event)
  241. {
  242. if (event.button() != MouseButton::Primary)
  243. return;
  244. m_resizing = false;
  245. m_first_resizee = nullptr;
  246. m_second_resizee = nullptr;
  247. if (!rect().contains(event.position()))
  248. set_override_cursor(Gfx::StandardCursor::None);
  249. }
  250. }