Splitter.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. window()->set_override_cursor(m_orientation == Orientation::Horizontal ? StandardCursor::ResizeColumn : StandardCursor::ResizeRow);
  59. m_overriding_cursor = true;
  60. }
  61. } else {
  62. if (m_overriding_cursor) {
  63. window()->set_override_cursor(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. auto child_widgets = this->child_widgets();
  81. if (child_widgets.size() < 2)
  82. return false;
  83. for (size_t i = 0; i < child_widgets.size() - 1; ++i) {
  84. auto* first_candidate = child_widgets[i];
  85. auto* second_candidate = child_widgets[i + 1];
  86. if (x_or_y > first_candidate->content_rect().last_edge_for_orientation(m_orientation)
  87. && x_or_y <= second_candidate->content_rect().first_edge_for_orientation(m_orientation)) {
  88. first = first_candidate;
  89. second = second_candidate;
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. void Splitter::mousedown_event(MouseEvent& event)
  96. {
  97. if (event.button() != MouseButton::Left)
  98. return;
  99. m_resizing = true;
  100. Widget* first { nullptr };
  101. Widget* second { nullptr };
  102. if (!get_resize_candidates_at(event.position(), first, second))
  103. return;
  104. m_first_resizee = first->make_weak_ptr();
  105. m_second_resizee = second->make_weak_ptr();
  106. m_first_resizee_start_size = first->size();
  107. m_second_resizee_start_size = second->size();
  108. m_resize_origin = event.position();
  109. }
  110. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  111. {
  112. auto first_edge = first.content_rect().primary_offset_for_orientation(m_orientation) + first.content_rect().primary_size_for_orientation(m_orientation);
  113. auto second_edge = second.content_rect().primary_offset_for_orientation(m_orientation);
  114. Gfx::IntRect rect;
  115. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  116. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  117. rect.set_secondary_offset_for_orientation(m_orientation, first.content_rect().secondary_offset_for_orientation(m_orientation));
  118. rect.set_secondary_size_for_orientation(m_orientation, first.content_rect().secondary_size_for_orientation(m_orientation));
  119. if (m_grabbable_rect != rect) {
  120. m_grabbable_rect = rect;
  121. update();
  122. }
  123. }
  124. void Splitter::mousemove_event(MouseEvent& event)
  125. {
  126. if (!m_resizing) {
  127. Widget* first { nullptr };
  128. Widget* second { nullptr };
  129. if (!get_resize_candidates_at(event.position(), first, second)) {
  130. override_cursor(false);
  131. return;
  132. }
  133. recompute_grabbable_rect(*first, *second);
  134. override_cursor(m_grabbable_rect.contains(event.position()));
  135. return;
  136. }
  137. auto delta = event.position() - m_resize_origin;
  138. if (!m_first_resizee || !m_second_resizee) {
  139. // One or both of the resizees were deleted during an ongoing resize, screw this.
  140. m_resizing = false;
  141. return;
  142. }
  143. int minimum_size = 0;
  144. auto new_first_resizee_size = m_first_resizee_start_size;
  145. auto new_second_resizee_size = m_second_resizee_start_size;
  146. 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));
  147. 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));
  148. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  149. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  150. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  151. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  152. }
  153. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  154. int correction = minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  155. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  156. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  157. }
  158. m_first_resizee->set_preferred_size(new_first_resizee_size);
  159. m_second_resizee->set_preferred_size(new_second_resizee_size);
  160. m_first_resizee->set_size_policy(m_orientation, SizePolicy::Fixed);
  161. m_second_resizee->set_size_policy(m_orientation, SizePolicy::Fill);
  162. invalidate_layout();
  163. }
  164. void Splitter::did_layout()
  165. {
  166. if (m_first_resizee && m_second_resizee)
  167. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  168. }
  169. void Splitter::mouseup_event(MouseEvent& event)
  170. {
  171. if (event.button() != MouseButton::Left)
  172. return;
  173. m_resizing = false;
  174. m_first_resizee = nullptr;
  175. m_second_resizee = nullptr;
  176. if (!rect().contains(event.position()))
  177. window()->set_override_cursor(StandardCursor::None);
  178. }
  179. }