diff options
author | 2020-03-14 08:16:07 +0100 | |
---|---|---|
committer | 2020-03-14 08:16:07 +0100 | |
commit | eb5e2251bdb71c75e1da86b0f575cd0e13cafa6a (patch) | |
tree | 9d8143729c6a1beb5d122f4f59ee1fd8bd6c5e21 /native/src/widget/pane_grid.rs | |
parent | f08cb4ad565799689d07bacc190fbe0436a63648 (diff) | |
download | iced-eb5e2251bdb71c75e1da86b0f575cd0e13cafa6a.tar.gz iced-eb5e2251bdb71c75e1da86b0f575cd0e13cafa6a.tar.bz2 iced-eb5e2251bdb71c75e1da86b0f575cd0e13cafa6a.zip |
Trigger `PaneGrid` resize on click
Diffstat (limited to 'native/src/widget/pane_grid.rs')
-rw-r--r-- | native/src/widget/pane_grid.rs | 82 |
1 files changed, 47 insertions, 35 deletions
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; |