Splitter.cpp 10 KB

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