Преглед изворни кода

LibGUI: Factor out Splitter hit testing into a separate function

Andreas Kling пре 5 година
родитељ
комит
7aa62665a3
2 измењених фајлова са 24 додато и 14 уклоњено
  1. 22 14
      Libraries/LibGUI/Splitter.cpp
  2. 2 0
      Libraries/LibGUI/Splitter.h

+ 22 - 14
Libraries/LibGUI/Splitter.cpp

@@ -60,29 +60,37 @@ void Splitter::leave_event(Core::Event&)
     update();
 }
 
-void Splitter::mousedown_event(MouseEvent& event)
+void Splitter::get_resize_candidates_at(const Gfx::Point& position, Widget*& first, Widget*& second)
 {
-    if (event.button() != MouseButton::Left)
-        return;
-    m_resizing = true;
-    int x_or_y = event.position().primary_offset_for_orientation(m_orientation);
-    Widget* first_resizee { nullptr };
-    Widget* second_resizee { nullptr };
+    int x_or_y = position.primary_offset_for_orientation(m_orientation);
     int fudge = layout()->spacing();
     for_each_child_widget([&](auto& child) {
         int child_start = child.relative_rect().first_edge_for_orientation(m_orientation);
         int child_end = child.relative_rect().last_edge_for_orientation(m_orientation);
         if (x_or_y > child_end && (x_or_y - fudge) <= child_end)
-            first_resizee = &child;
+            first = &child;
         if (x_or_y < child_start && (x_or_y + fudge) >= child_start)
-            second_resizee = &child;
+            second = &child;
         return IterationDecision::Continue;
     });
-    ASSERT(first_resizee && second_resizee);
-    m_first_resizee = first_resizee->make_weak_ptr();
-    m_second_resizee = second_resizee->make_weak_ptr();
-    m_first_resizee_start_size = first_resizee->size();
-    m_second_resizee_start_size = second_resizee->size();
+    ASSERT(first);
+    ASSERT(second);
+}
+
+void Splitter::mousedown_event(MouseEvent& event)
+{
+    if (event.button() != MouseButton::Left)
+        return;
+    m_resizing = true;
+
+    Widget* first { nullptr };
+    Widget* second { nullptr };
+    get_resize_candidates_at(event.position(), first, second);
+
+    m_first_resizee = first->make_weak_ptr();
+    m_second_resizee = second->make_weak_ptr();
+    m_first_resizee_start_size = first->size();
+    m_second_resizee_start_size = second->size();
     m_resize_origin = event.position();
 }
 

+ 2 - 0
Libraries/LibGUI/Splitter.h

@@ -45,6 +45,8 @@ protected:
     virtual void leave_event(Core::Event&) override;
 
 private:
+    void get_resize_candidates_at(const Gfx::Point&, Widget*&, Widget*&);
+
     Orientation m_orientation;
     bool m_resizing { false };
     Gfx::Point m_resize_origin;