Splitter.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/Splitter.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Palette.h>
  31. namespace GUI {
  32. Splitter::Splitter(Orientation orientation)
  33. : m_orientation(orientation)
  34. {
  35. set_background_role(ColorRole::Button);
  36. set_layout<BoxLayout>(orientation);
  37. set_fill_with_background_color(true);
  38. layout()->set_spacing(3);
  39. }
  40. Splitter::~Splitter()
  41. {
  42. }
  43. void Splitter::paint_event(PaintEvent& event)
  44. {
  45. Painter painter(*this);
  46. painter.add_clip_rect(event.rect());
  47. painter.fill_rect(m_grabbable_rect, palette().hover_highlight());
  48. }
  49. void Splitter::resize_event(ResizeEvent& event)
  50. {
  51. Widget::resize_event(event);
  52. m_grabbable_rect = {};
  53. }
  54. void Splitter::override_cursor(bool do_override)
  55. {
  56. if (do_override) {
  57. if (!m_overriding_cursor) {
  58. set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
  59. m_overriding_cursor = true;
  60. }
  61. } else {
  62. if (m_overriding_cursor) {
  63. set_override_cursor(Gfx::StandardCursor::None);
  64. m_overriding_cursor = false;
  65. }
  66. }
  67. }
  68. void Splitter::leave_event(Core::Event&)
  69. {
  70. if (!m_resizing)
  71. override_cursor(false);
  72. if (!m_grabbable_rect.is_empty()) {
  73. m_grabbable_rect = {};
  74. update();
  75. }
  76. }
  77. bool Splitter::get_resize_candidates_at(const Gfx::IntPoint& position, Widget*& first, Widget*& second)
  78. {
  79. int x_or_y = position.primary_offset_for_orientation(m_orientation);
  80. Widget* previous_widget = nullptr;
  81. bool found_candidates = false;
  82. for_each_child_widget([&](auto& child_widget) {
  83. if (!child_widget.is_visible()) {
  84. // We need to skip over widgets that are not visible as they
  85. // are not necessarily in the correct location (anymore)
  86. return IterationDecision::Continue;
  87. }
  88. if (!previous_widget) {
  89. previous_widget = &child_widget;
  90. return IterationDecision::Continue;
  91. }
  92. if (x_or_y > previous_widget->content_rect().last_edge_for_orientation(m_orientation)
  93. && x_or_y <= child_widget.content_rect().first_edge_for_orientation(m_orientation)) {
  94. first = previous_widget;
  95. second = &child_widget;
  96. found_candidates = true;
  97. return IterationDecision::Break;
  98. }
  99. previous_widget = &child_widget;
  100. return IterationDecision::Continue;
  101. });
  102. return found_candidates;
  103. }
  104. void Splitter::mousedown_event(MouseEvent& event)
  105. {
  106. if (event.button() != MouseButton::Left)
  107. return;
  108. m_resizing = true;
  109. Widget* first { nullptr };
  110. Widget* second { nullptr };
  111. if (!get_resize_candidates_at(event.position(), first, second))
  112. return;
  113. m_first_resizee = *first;
  114. m_second_resizee = *second;
  115. m_first_resizee_start_size = first->size();
  116. m_second_resizee_start_size = second->size();
  117. m_resize_origin = event.position();
  118. }
  119. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  120. {
  121. auto first_edge = first.content_rect().primary_offset_for_orientation(m_orientation) + first.content_rect().primary_size_for_orientation(m_orientation);
  122. auto second_edge = second.content_rect().primary_offset_for_orientation(m_orientation);
  123. Gfx::IntRect rect;
  124. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  125. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  126. rect.set_secondary_offset_for_orientation(m_orientation, first.content_rect().secondary_offset_for_orientation(m_orientation));
  127. rect.set_secondary_size_for_orientation(m_orientation, first.content_rect().secondary_size_for_orientation(m_orientation));
  128. if (m_grabbable_rect != rect) {
  129. m_grabbable_rect = rect;
  130. update();
  131. }
  132. }
  133. void Splitter::mousemove_event(MouseEvent& event)
  134. {
  135. if (!m_resizing) {
  136. Widget* first { nullptr };
  137. Widget* second { nullptr };
  138. if (!get_resize_candidates_at(event.position(), first, second)) {
  139. override_cursor(false);
  140. return;
  141. }
  142. recompute_grabbable_rect(*first, *second);
  143. override_cursor(m_grabbable_rect.contains(event.position()));
  144. return;
  145. }
  146. auto delta = event.position() - m_resize_origin;
  147. if (!m_first_resizee || !m_second_resizee) {
  148. // One or both of the resizees were deleted during an ongoing resize, screw this.
  149. m_resizing = false;
  150. return;
  151. }
  152. int minimum_size = 0;
  153. auto new_first_resizee_size = m_first_resizee_start_size;
  154. auto new_second_resizee_size = m_second_resizee_start_size;
  155. 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));
  156. 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));
  157. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  158. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  159. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  160. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  161. }
  162. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  163. int correction = minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  164. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  165. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  166. }
  167. m_first_resizee->set_preferred_size(new_first_resizee_size);
  168. m_second_resizee->set_preferred_size(new_second_resizee_size);
  169. m_first_resizee->set_size_policy(m_orientation, SizePolicy::Fixed);
  170. m_second_resizee->set_size_policy(m_orientation, SizePolicy::Fill);
  171. invalidate_layout();
  172. }
  173. void Splitter::did_layout()
  174. {
  175. if (m_first_resizee && m_second_resizee)
  176. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  177. }
  178. void Splitter::mouseup_event(MouseEvent& event)
  179. {
  180. if (event.button() != MouseButton::Left)
  181. return;
  182. m_resizing = false;
  183. m_first_resizee = nullptr;
  184. m_second_resizee = nullptr;
  185. if (!rect().contains(event.position()))
  186. set_override_cursor(Gfx::StandardCursor::None);
  187. }
  188. }