From eb5e2251bdb71c75e1da86b0f575cd0e13cafa6a Mon Sep 17 00:00:00 2001
From: Héctor Ramón Jiménez <hector0193@gmail.com>
Date: Sat, 14 Mar 2020 08:16:07 +0100
Subject: Trigger `PaneGrid` resize on click

---
 native/src/widget/pane_grid.rs | 82 ++++++++++++++++++++++++------------------
 1 file changed, 47 insertions(+), 35 deletions(-)

(limited to 'native/src')

diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs
index 5229962d..62148764 100644
--- a/native/src/widget/pane_grid.rs
+++ b/native/src/widget/pane_grid.rs
@@ -111,6 +111,47 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> {
         self.on_resize = Some(Box::new(f));
         self
     }
+
+    fn trigger_resize(
+        &mut self,
+        layout: Layout<'_>,
+        cursor_position: Point,
+        messages: &mut Vec<Message>,
+    ) {
+        if let Some(on_resize) = &self.on_resize {
+            if let Some((split, _)) = self.state.picked_split() {
+                let bounds = layout.bounds();
+
+                let splits = self.state.splits(
+                    f32::from(self.spacing),
+                    Size::new(bounds.width, bounds.height),
+                );
+
+                if let Some((axis, rectangle, _)) = splits.get(&split) {
+                    let ratio = match axis {
+                        Axis::Horizontal => {
+                            let position =
+                                cursor_position.x - bounds.x + rectangle.x;
+
+                            (position / (rectangle.x + rectangle.width))
+                                .max(0.1)
+                                .min(0.9)
+                        }
+                        Axis::Vertical => {
+                            let position =
+                                cursor_position.y - bounds.y + rectangle.y;
+
+                            (position / (rectangle.y + rectangle.height))
+                                .max(0.1)
+                                .min(0.9)
+                        }
+                    };
+
+                    messages.push(on_resize(ResizeEvent { split, ratio }));
+                }
+            }
+        }
+    }
 }
 
 #[derive(Debug, Clone, Copy)]
@@ -280,6 +321,11 @@ where
                             sorted_splits.first()
                         {
                             self.state.pick_split(split, *axis);
+                            self.trigger_resize(
+                                layout,
+                                cursor_position,
+                                messages,
+                            );
                         }
                     }
                     ButtonState::Released => {
@@ -288,41 +334,7 @@ where
                 }
             }
             Event::Mouse(mouse::Event::CursorMoved { .. }) => {
-                if let Some(on_resize) = &self.on_resize {
-                    if let Some((split, _)) = self.state.picked_split() {
-                        let bounds = layout.bounds();
-
-                        let splits = self.state.splits(
-                            f32::from(self.spacing),
-                            Size::new(bounds.width, bounds.height),
-                        );
-
-                        if let Some((axis, rectangle, _)) = splits.get(&split) {
-                            let ratio = match axis {
-                                Axis::Horizontal => {
-                                    let position = cursor_position.x - bounds.x
-                                        + rectangle.x;
-
-                                    (position / (rectangle.x + rectangle.width))
-                                        .max(0.1)
-                                        .min(0.9)
-                                }
-                                Axis::Vertical => {
-                                    let position = cursor_position.y - bounds.y
-                                        + rectangle.y;
-
-                                    (position
-                                        / (rectangle.y + rectangle.height))
-                                        .max(0.1)
-                                        .min(0.9)
-                                }
-                            };
-
-                            messages
-                                .push(on_resize(ResizeEvent { split, ratio }));
-                        }
-                    }
-                }
+                self.trigger_resize(layout, cursor_position, messages);
             }
             Event::Keyboard(keyboard::Event::Input { modifiers, .. }) => {
                 *self.modifiers = modifiers;
-- 
cgit