GSplitter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <LibGUI/GBoxLayout.h>
  2. #include <LibGUI/GSplitter.h>
  3. #include <LibGUI/GWindow.h>
  4. GSplitter::GSplitter(Orientation orientation, GWidget* parent)
  5. : GFrame(parent)
  6. , m_orientation(orientation)
  7. {
  8. set_layout(make<GBoxLayout>(orientation));
  9. set_fill_with_background_color(true);
  10. set_background_color(Color::WarmGray);
  11. layout()->set_spacing(3);
  12. }
  13. GSplitter::~GSplitter()
  14. {
  15. }
  16. void GSplitter::enter_event(CEvent&)
  17. {
  18. set_background_color(StylePainter::hover_highlight_color());
  19. window()->set_override_cursor(m_orientation == Orientation::Horizontal ? GStandardCursor::ResizeHorizontal : GStandardCursor::ResizeVertical);
  20. update();
  21. }
  22. void GSplitter::leave_event(CEvent&)
  23. {
  24. set_background_color(Color::WarmGray);
  25. if (!m_resizing)
  26. window()->set_override_cursor(GStandardCursor::None);
  27. update();
  28. }
  29. void GSplitter::mousedown_event(GMouseEvent& event)
  30. {
  31. if (event.button() != GMouseButton::Left)
  32. return;
  33. m_resizing = true;
  34. int x_or_y = event.position().primary_offset_for_orientation(m_orientation);
  35. GWidget* first_resizee { nullptr };
  36. GWidget* second_resizee { nullptr };
  37. int fudge = layout()->spacing();
  38. for_each_child_widget([&](auto& child) {
  39. int child_start = child.relative_rect().first_edge_for_orientation(m_orientation);
  40. int child_end = child.relative_rect().last_edge_for_orientation(m_orientation);
  41. if (x_or_y > child_end && (x_or_y - fudge) <= child_end)
  42. first_resizee = &child;
  43. if (x_or_y < child_start && (x_or_y + fudge) >= child_start)
  44. second_resizee = &child;
  45. return IterationDecision::Continue;
  46. });
  47. ASSERT(first_resizee && second_resizee);
  48. m_first_resizee = first_resizee->make_weak_ptr();
  49. m_second_resizee = second_resizee->make_weak_ptr();
  50. m_first_resizee_start_size = first_resizee->size();
  51. m_second_resizee_start_size = second_resizee->size();
  52. m_resize_origin = event.position();
  53. }
  54. void GSplitter::mousemove_event(GMouseEvent& event)
  55. {
  56. if (!m_resizing)
  57. return;
  58. auto delta = event.position() - m_resize_origin;
  59. if (!m_first_resizee || !m_second_resizee) {
  60. // One or both of the resizees were deleted during an ongoing resize, screw this.
  61. m_resizing = false;
  62. return;
  63. }
  64. int minimum_size = 0;
  65. auto new_first_resizee_size = m_first_resizee_start_size;
  66. auto new_second_resizee_size = m_second_resizee_start_size;
  67. 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));
  68. 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));
  69. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  70. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  71. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  72. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  73. }
  74. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  75. int correction = minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  76. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  77. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  78. }
  79. m_first_resizee->set_preferred_size(new_first_resizee_size);
  80. m_second_resizee->set_preferred_size(new_second_resizee_size);
  81. m_first_resizee->set_size_policy(m_orientation, SizePolicy::Fixed);
  82. m_second_resizee->set_size_policy(m_orientation, SizePolicy::Fill);
  83. invalidate_layout();
  84. }
  85. void GSplitter::mouseup_event(GMouseEvent& event)
  86. {
  87. if (event.button() != GMouseButton::Left)
  88. return;
  89. m_resizing = false;
  90. if (!rect().contains(event.position()))
  91. window()->set_override_cursor(GStandardCursor::None);
  92. }