Splitter.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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, Widget* parent)
  33. : Frame(parent)
  34. , m_orientation(orientation)
  35. {
  36. set_background_role(ColorRole::Button);
  37. set_layout(make<BoxLayout>(orientation));
  38. set_fill_with_background_color(true);
  39. layout()->set_spacing(4);
  40. }
  41. Splitter::~Splitter()
  42. {
  43. }
  44. void Splitter::paint_event(PaintEvent& event)
  45. {
  46. Painter painter(*this);
  47. painter.add_clip_rect(event.rect());
  48. painter.fill_rect(m_grabbable_rect, palette().hover_highlight());
  49. }
  50. void Splitter::resize_event(ResizeEvent& event)
  51. {
  52. Frame::resize_event(event);
  53. m_grabbable_rect = {};
  54. }
  55. void Splitter::enter_event(Core::Event&)
  56. {
  57. window()->set_override_cursor(m_orientation == Orientation::Horizontal ? StandardCursor::ResizeHorizontal : StandardCursor::ResizeVertical);
  58. }
  59. void Splitter::leave_event(Core::Event&)
  60. {
  61. if (!m_resizing)
  62. window()->set_override_cursor(StandardCursor::None);
  63. if (!m_grabbable_rect.is_empty()) {
  64. m_grabbable_rect = {};
  65. update();
  66. }
  67. }
  68. bool Splitter::get_resize_candidates_at(const Gfx::Point& position, Widget*& first, Widget*& second)
  69. {
  70. int x_or_y = position.primary_offset_for_orientation(m_orientation);
  71. int fudge = layout()->spacing();
  72. for_each_child_widget([&](auto& child) {
  73. int child_start = child.relative_rect().first_edge_for_orientation(m_orientation);
  74. int child_end = child.relative_rect().last_edge_for_orientation(m_orientation);
  75. if (x_or_y > child_end && (x_or_y - fudge) <= child_end)
  76. first = &child;
  77. if (x_or_y < child_start && (x_or_y + fudge) >= child_start)
  78. second = &child;
  79. return IterationDecision::Continue;
  80. });
  81. return first && second;
  82. }
  83. void Splitter::mousedown_event(MouseEvent& event)
  84. {
  85. if (event.button() != MouseButton::Left)
  86. return;
  87. m_resizing = true;
  88. Widget* first { nullptr };
  89. Widget* second { nullptr };
  90. if (!get_resize_candidates_at(event.position(), first, second))
  91. return;
  92. m_first_resizee = first->make_weak_ptr();
  93. m_second_resizee = second->make_weak_ptr();
  94. m_first_resizee_start_size = first->size();
  95. m_second_resizee_start_size = second->size();
  96. m_resize_origin = event.position();
  97. }
  98. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  99. {
  100. auto first_edge = first.relative_rect().primary_offset_for_orientation(m_orientation) + first.relative_rect().primary_size_for_orientation(m_orientation);
  101. auto second_edge = second.relative_rect().primary_offset_for_orientation(m_orientation);
  102. Gfx::Rect rect;
  103. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  104. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  105. rect.set_secondary_offset_for_orientation(m_orientation, first.relative_rect().secondary_offset_for_orientation(m_orientation));
  106. rect.set_secondary_size_for_orientation(m_orientation, first.relative_rect().secondary_size_for_orientation(m_orientation));
  107. if (m_grabbable_rect != rect) {
  108. m_grabbable_rect = rect;
  109. update();
  110. }
  111. }
  112. void Splitter::mousemove_event(MouseEvent& event)
  113. {
  114. if (!m_resizing) {
  115. Widget* first { nullptr };
  116. Widget* second { nullptr };
  117. if (!get_resize_candidates_at(event.position(), first, second))
  118. return;
  119. recompute_grabbable_rect(*first, *second);
  120. return;
  121. }
  122. auto delta = event.position() - m_resize_origin;
  123. if (!m_first_resizee || !m_second_resizee) {
  124. // One or both of the resizees were deleted during an ongoing resize, screw this.
  125. m_resizing = false;
  126. return;
  127. }
  128. int minimum_size = 0;
  129. auto new_first_resizee_size = m_first_resizee_start_size;
  130. auto new_second_resizee_size = m_second_resizee_start_size;
  131. 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));
  132. 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));
  133. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  134. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  135. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  136. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  137. }
  138. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  139. int correction = minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  140. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  141. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  142. }
  143. m_first_resizee->set_preferred_size(new_first_resizee_size);
  144. m_second_resizee->set_preferred_size(new_second_resizee_size);
  145. m_first_resizee->set_size_policy(m_orientation, SizePolicy::Fixed);
  146. m_second_resizee->set_size_policy(m_orientation, SizePolicy::Fill);
  147. invalidate_layout();
  148. }
  149. void Splitter::did_layout()
  150. {
  151. if (m_first_resizee && m_second_resizee)
  152. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  153. }
  154. void Splitter::mouseup_event(MouseEvent& event)
  155. {
  156. if (event.button() != MouseButton::Left)
  157. return;
  158. m_resizing = false;
  159. m_first_resizee = nullptr;
  160. m_second_resizee = nullptr;
  161. if (!rect().contains(event.position()))
  162. window()->set_override_cursor(StandardCursor::None);
  163. }
  164. }