From ad0f6f61175d1875c51deabbe9ead55d6ecd42ab Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 10 May 2020 13:04:45 +0200 Subject: [PATCH] Resize component: better handling of resize=false --- emigui/src/containers/resize.rs | 55 +++++++++++++++++---------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/emigui/src/containers/resize.rs b/emigui/src/containers/resize.rs index 0a5773401..9ab44a830 100644 --- a/emigui/src/containers/resize.rs +++ b/emigui/src/containers/resize.rs @@ -133,10 +133,6 @@ impl Resize { // TODO: a common trait for Things that follow this pattern impl Resize { pub fn show(mut self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) { - if !self.resizable { - return add_contents(ui); - } - let id = ui.make_child_id("scroll"); self.min_size = self.min_size.min(ui.available_space()); self.max_size = self.max_size.min(ui.available_space()); @@ -155,26 +151,31 @@ impl Resize { let position = ui.cursor(); - // Resize-corner: - let corner_size = Vec2::splat(16.0); // TODO: style - let corner_rect = Rect::from_min_size( - position + state.size + self.handle_offset - corner_size, - corner_size, - ); - let corner_interact = ui.interact_rect(corner_rect, id.with("corner")); - - if corner_interact.active { - if let Some(mouse_pos) = ui.input().mouse_pos { - // This is the desired size. We may not be able to achieve it. - - state.size = - mouse_pos - position + 0.5 * corner_interact.rect.size() - self.handle_offset; - // We don't clamp to max size, because we want to be able to push against outer bounds. - // For instance, if we are inside a bigger Resize region, we want to expand that. - // state.size = state.size.clamp(self.min_size..=self.max_size); - state.size = state.size.max(self.min_size); + let corner_interact = if self.resizable { + // Resize-corner: + let corner_size = Vec2::splat(16.0); // TODO: style + let corner_rect = Rect::from_min_size( + position + state.size + self.handle_offset - corner_size, + corner_size, + ); + let corner_interact = ui.interact_rect(corner_rect, id.with("corner")); + + if corner_interact.active { + if let Some(mouse_pos) = ui.input().mouse_pos { + // This is the desired size. We may not be able to achieve it. + + state.size = mouse_pos - position + 0.5 * corner_interact.rect.size() + - self.handle_offset; + // We don't clamp to max size, because we want to be able to push against outer bounds. + // For instance, if we are inside a bigger Resize region, we want to expand that. + // state.size = state.size.clamp(self.min_size..=self.max_size); + state.size = state.size.max(self.min_size); + } } - } + Some(corner_interact) + } else { + None + }; // ------------------------------ @@ -224,10 +225,12 @@ impl Resize { // ------------------------------ - paint_resize_corner(ui, &corner_interact); + if let Some(corner_interact) = corner_interact { + paint_resize_corner(ui, &corner_interact); - if corner_interact.hovered || corner_interact.active { - ui.ctx().output().cursor_icon = CursorIcon::ResizeNwSe; + if corner_interact.hovered || corner_interact.active { + ui.ctx().output().cursor_icon = CursorIcon::ResizeNwSe; + } } ui.memory().resize.insert(id, state);