GSplitter.cpp 4.4 KB

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