Splitter.cpp 8.7 KB

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