Splitter.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2018-2020, 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. painter.fill_rect(m_grabbable_rect, palette().hover_highlight());
  32. }
  33. void Splitter::resize_event(ResizeEvent& event)
  34. {
  35. Widget::resize_event(event);
  36. m_grabbable_rect = {};
  37. }
  38. void Splitter::override_cursor(bool do_override)
  39. {
  40. if (do_override) {
  41. if (!m_overriding_cursor) {
  42. set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
  43. m_overriding_cursor = true;
  44. }
  45. } else {
  46. if (m_overriding_cursor) {
  47. set_override_cursor(Gfx::StandardCursor::None);
  48. m_overriding_cursor = false;
  49. }
  50. }
  51. }
  52. void Splitter::leave_event(Core::Event&)
  53. {
  54. if (!m_resizing)
  55. override_cursor(false);
  56. if (!m_grabbable_rect.is_empty()) {
  57. m_grabbable_rect = {};
  58. update();
  59. }
  60. }
  61. bool Splitter::get_resize_candidates_at(const Gfx::IntPoint& position, Widget*& first, Widget*& second)
  62. {
  63. int x_or_y = position.primary_offset_for_orientation(m_orientation);
  64. Widget* previous_widget = nullptr;
  65. bool found_candidates = false;
  66. for_each_child_widget([&](auto& child_widget) {
  67. if (!child_widget.is_visible()) {
  68. // We need to skip over widgets that are not visible as they
  69. // are not necessarily in the correct location (anymore)
  70. return IterationDecision::Continue;
  71. }
  72. if (!previous_widget) {
  73. previous_widget = &child_widget;
  74. return IterationDecision::Continue;
  75. }
  76. if (x_or_y > previous_widget->content_rect().last_edge_for_orientation(m_orientation)
  77. && x_or_y <= child_widget.content_rect().first_edge_for_orientation(m_orientation)) {
  78. first = previous_widget;
  79. second = &child_widget;
  80. found_candidates = true;
  81. return IterationDecision::Break;
  82. }
  83. previous_widget = &child_widget;
  84. return IterationDecision::Continue;
  85. });
  86. return found_candidates;
  87. }
  88. void Splitter::mousedown_event(MouseEvent& event)
  89. {
  90. if (event.button() != MouseButton::Left)
  91. return;
  92. m_resizing = true;
  93. Widget* first { nullptr };
  94. Widget* second { nullptr };
  95. if (!get_resize_candidates_at(event.position(), first, second))
  96. return;
  97. m_first_resizee = *first;
  98. m_second_resizee = *second;
  99. m_first_resizee_start_size = first->size();
  100. m_second_resizee_start_size = second->size();
  101. m_resize_origin = event.position();
  102. }
  103. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  104. {
  105. auto first_edge = first.content_rect().primary_offset_for_orientation(m_orientation) + first.content_rect().primary_size_for_orientation(m_orientation);
  106. auto second_edge = second.content_rect().primary_offset_for_orientation(m_orientation);
  107. Gfx::IntRect rect;
  108. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  109. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  110. rect.set_secondary_offset_for_orientation(m_orientation, first.content_rect().secondary_offset_for_orientation(m_orientation));
  111. rect.set_secondary_size_for_orientation(m_orientation, first.content_rect().secondary_size_for_orientation(m_orientation));
  112. if (m_grabbable_rect != rect) {
  113. m_grabbable_rect = rect;
  114. update();
  115. }
  116. }
  117. void Splitter::mousemove_event(MouseEvent& event)
  118. {
  119. if (!m_resizing) {
  120. Widget* first { nullptr };
  121. Widget* second { nullptr };
  122. if (!get_resize_candidates_at(event.position(), first, second)) {
  123. override_cursor(false);
  124. return;
  125. }
  126. recompute_grabbable_rect(*first, *second);
  127. override_cursor(m_grabbable_rect.contains(event.position()));
  128. return;
  129. }
  130. auto delta = event.position() - m_resize_origin;
  131. if (!m_first_resizee || !m_second_resizee) {
  132. // One or both of the resizees were deleted during an ongoing resize, screw this.
  133. m_resizing = false;
  134. return;
  135. }
  136. auto new_first_resizee_size = m_first_resizee_start_size;
  137. auto new_second_resizee_size = m_second_resizee_start_size;
  138. 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));
  139. 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));
  140. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < m_first_resizee_minimum_size) {
  141. int correction = m_first_resizee_minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  142. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  143. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  144. }
  145. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < m_second_resizee_minimum_size) {
  146. int correction = m_second_resizee_minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  147. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  148. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  149. }
  150. if (m_orientation == Orientation::Horizontal) {
  151. m_first_resizee->set_fixed_width(new_first_resizee_size.width());
  152. m_second_resizee->set_fixed_width(-1);
  153. } else {
  154. m_first_resizee->set_fixed_height(new_first_resizee_size.height());
  155. m_second_resizee->set_fixed_height(-1);
  156. }
  157. invalidate_layout();
  158. }
  159. void Splitter::did_layout()
  160. {
  161. if (m_first_resizee && m_second_resizee)
  162. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  163. }
  164. void Splitter::mouseup_event(MouseEvent& event)
  165. {
  166. if (event.button() != MouseButton::Left)
  167. return;
  168. m_resizing = false;
  169. m_first_resizee = nullptr;
  170. m_second_resizee = nullptr;
  171. if (!rect().contains(event.position()))
  172. set_override_cursor(Gfx::StandardCursor::None);
  173. }
  174. }