Splitter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. Frame::resize_event(event);
  52. m_grabbable_rect = {};
  53. }
  54. void Splitter::enter_event(Core::Event&)
  55. {
  56. window()->set_override_cursor(m_orientation == Orientation::Horizontal ? StandardCursor::ResizeHorizontal : StandardCursor::ResizeVertical);
  57. }
  58. void Splitter::leave_event(Core::Event&)
  59. {
  60. if (!m_resizing)
  61. window()->set_override_cursor(StandardCursor::None);
  62. if (!m_grabbable_rect.is_empty()) {
  63. m_grabbable_rect = {};
  64. update();
  65. }
  66. }
  67. bool Splitter::get_resize_candidates_at(const Gfx::Point& position, Widget*& first, Widget*& second)
  68. {
  69. int x_or_y = position.primary_offset_for_orientation(m_orientation);
  70. auto child_widgets = this->child_widgets();
  71. if (child_widgets.size() < 2)
  72. return false;
  73. for (size_t i = 0; i < child_widgets.size() - 1; ++i) {
  74. auto* first_candidate = child_widgets[i];
  75. auto* second_candidate = child_widgets[i + 1];
  76. if (x_or_y > first_candidate->content_rect().last_edge_for_orientation(m_orientation)
  77. && x_or_y <= second_candidate->content_rect().first_edge_for_orientation(m_orientation)) {
  78. first = first_candidate;
  79. second = second_candidate;
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. void Splitter::mousedown_event(MouseEvent& event)
  86. {
  87. if (event.button() != MouseButton::Left)
  88. return;
  89. m_resizing = true;
  90. Widget* first { nullptr };
  91. Widget* second { nullptr };
  92. if (!get_resize_candidates_at(event.position(), first, second))
  93. return;
  94. m_first_resizee = first->make_weak_ptr();
  95. m_second_resizee = second->make_weak_ptr();
  96. m_first_resizee_start_size = first->size();
  97. m_second_resizee_start_size = second->size();
  98. m_resize_origin = event.position();
  99. }
  100. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  101. {
  102. auto first_edge = first.content_rect().primary_offset_for_orientation(m_orientation) + first.content_rect().primary_size_for_orientation(m_orientation);
  103. auto second_edge = second.content_rect().primary_offset_for_orientation(m_orientation);
  104. Gfx::Rect rect;
  105. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  106. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  107. rect.set_secondary_offset_for_orientation(m_orientation, first.content_rect().secondary_offset_for_orientation(m_orientation));
  108. rect.set_secondary_size_for_orientation(m_orientation, first.content_rect().secondary_size_for_orientation(m_orientation));
  109. if (m_grabbable_rect != rect) {
  110. m_grabbable_rect = rect;
  111. update();
  112. }
  113. }
  114. void Splitter::mousemove_event(MouseEvent& event)
  115. {
  116. if (!m_resizing) {
  117. Widget* first { nullptr };
  118. Widget* second { nullptr };
  119. if (!get_resize_candidates_at(event.position(), first, second))
  120. return;
  121. recompute_grabbable_rect(*first, *second);
  122. return;
  123. }
  124. auto delta = event.position() - m_resize_origin;
  125. if (!m_first_resizee || !m_second_resizee) {
  126. // One or both of the resizees were deleted during an ongoing resize, screw this.
  127. m_resizing = false;
  128. return;
  129. }
  130. int minimum_size = 0;
  131. auto new_first_resizee_size = m_first_resizee_start_size;
  132. auto new_second_resizee_size = m_second_resizee_start_size;
  133. 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));
  134. 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));
  135. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  136. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  137. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  138. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  139. }
  140. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  141. int correction = minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  142. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  143. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  144. }
  145. m_first_resizee->set_preferred_size(new_first_resizee_size);
  146. m_second_resizee->set_preferred_size(new_second_resizee_size);
  147. m_first_resizee->set_size_policy(m_orientation, SizePolicy::Fixed);
  148. m_second_resizee->set_size_policy(m_orientation, SizePolicy::Fill);
  149. invalidate_layout();
  150. }
  151. void Splitter::did_layout()
  152. {
  153. if (m_first_resizee && m_second_resizee)
  154. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  155. }
  156. void Splitter::mouseup_event(MouseEvent& event)
  157. {
  158. if (event.button() != MouseButton::Left)
  159. return;
  160. m_resizing = false;
  161. m_first_resizee = nullptr;
  162. m_second_resizee = nullptr;
  163. if (!rect().contains(event.position()))
  164. window()->set_override_cursor(StandardCursor::None);
  165. }
  166. }