Splitter.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(make<BoxLayout>(orientation));
  37. set_fill_with_background_color(true);
  38. layout()->set_spacing(4);
  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. int fudge = layout()->spacing();
  71. for_each_child_widget([&](auto& child) {
  72. int child_start = child.relative_rect().first_edge_for_orientation(m_orientation);
  73. int child_end = child.relative_rect().last_edge_for_orientation(m_orientation);
  74. if (x_or_y > child_end && (x_or_y - fudge) <= child_end)
  75. first = &child;
  76. if (x_or_y < child_start && (x_or_y + fudge) >= child_start)
  77. second = &child;
  78. return IterationDecision::Continue;
  79. });
  80. return first && second;
  81. }
  82. void Splitter::mousedown_event(MouseEvent& event)
  83. {
  84. if (event.button() != MouseButton::Left)
  85. return;
  86. m_resizing = true;
  87. Widget* first { nullptr };
  88. Widget* second { nullptr };
  89. if (!get_resize_candidates_at(event.position(), first, second))
  90. return;
  91. m_first_resizee = first->make_weak_ptr();
  92. m_second_resizee = second->make_weak_ptr();
  93. m_first_resizee_start_size = first->size();
  94. m_second_resizee_start_size = second->size();
  95. m_resize_origin = event.position();
  96. }
  97. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  98. {
  99. auto first_edge = first.relative_rect().primary_offset_for_orientation(m_orientation) + first.relative_rect().primary_size_for_orientation(m_orientation);
  100. auto second_edge = second.relative_rect().primary_offset_for_orientation(m_orientation);
  101. Gfx::Rect rect;
  102. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  103. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  104. rect.set_secondary_offset_for_orientation(m_orientation, first.relative_rect().secondary_offset_for_orientation(m_orientation));
  105. rect.set_secondary_size_for_orientation(m_orientation, first.relative_rect().secondary_size_for_orientation(m_orientation));
  106. if (m_grabbable_rect != rect) {
  107. m_grabbable_rect = rect;
  108. update();
  109. }
  110. }
  111. void Splitter::mousemove_event(MouseEvent& event)
  112. {
  113. if (!m_resizing) {
  114. Widget* first { nullptr };
  115. Widget* second { nullptr };
  116. if (!get_resize_candidates_at(event.position(), first, second))
  117. return;
  118. recompute_grabbable_rect(*first, *second);
  119. return;
  120. }
  121. auto delta = event.position() - m_resize_origin;
  122. if (!m_first_resizee || !m_second_resizee) {
  123. // One or both of the resizees were deleted during an ongoing resize, screw this.
  124. m_resizing = false;
  125. return;
  126. }
  127. int minimum_size = 0;
  128. auto new_first_resizee_size = m_first_resizee_start_size;
  129. auto new_second_resizee_size = m_second_resizee_start_size;
  130. 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));
  131. 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));
  132. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  133. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  134. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  135. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  136. }
  137. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  138. int correction = minimum_size - new_second_resizee_size.primary_size_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) + correction);
  140. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  141. }
  142. m_first_resizee->set_preferred_size(new_first_resizee_size);
  143. m_second_resizee->set_preferred_size(new_second_resizee_size);
  144. m_first_resizee->set_size_policy(m_orientation, SizePolicy::Fixed);
  145. m_second_resizee->set_size_policy(m_orientation, SizePolicy::Fill);
  146. invalidate_layout();
  147. }
  148. void Splitter::did_layout()
  149. {
  150. if (m_first_resizee && m_second_resizee)
  151. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  152. }
  153. void Splitter::mouseup_event(MouseEvent& event)
  154. {
  155. if (event.button() != MouseButton::Left)
  156. return;
  157. m_resizing = false;
  158. m_first_resizee = nullptr;
  159. m_second_resizee = nullptr;
  160. if (!rect().contains(event.position()))
  161. window()->set_override_cursor(StandardCursor::None);
  162. }
  163. }