From 0ae70b62cbc3e827d4edc3c9b58e4d647e5052e7 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 3 Sep 2021 17:19:57 +0200 Subject: [PATCH] Use `on_after_layout` to cap the rendered sidebar item's width --- gpui/src/elements/hooks.rs | 16 ++++++++-------- zed/src/workspace/sidebar.rs | 9 +++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/gpui/src/elements/hooks.rs b/gpui/src/elements/hooks.rs index 6d1e86a2173ab201e921b131828d29e4b73b7a44..9337578297ca038321189e39aae1b0d1c5cf72d2 100644 --- a/gpui/src/elements/hooks.rs +++ b/gpui/src/elements/hooks.rs @@ -7,22 +7,22 @@ use crate::{ pub struct Hooks { child: ElementBox, - before_layout: Option>, + after_layout: Option>, } impl Hooks { pub fn new(child: ElementBox) -> Self { Self { child, - before_layout: None, + after_layout: None, } } - pub fn on_before_layout( + pub fn on_after_layout( mut self, - f: impl 'static + FnMut(SizeConstraint, &mut LayoutContext), + f: impl 'static + FnMut(Vector2F, &mut LayoutContext), ) -> Self { - self.before_layout = Some(Box::new(f)); + self.after_layout = Some(Box::new(f)); self } } @@ -36,10 +36,10 @@ impl Element for Hooks { constraint: SizeConstraint, cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { - if let Some(handler) = self.before_layout.as_mut() { - handler(constraint, cx); - } let size = self.child.layout(constraint, cx); + if let Some(handler) = self.after_layout.as_mut() { + handler(size, cx); + } (size, ()) } diff --git a/zed/src/workspace/sidebar.rs b/zed/src/workspace/sidebar.rs index dca3687240ad2b6e78ab3eaa5d4ee0e3cfcfb1ea..afe4b328f2cbd6d6b71b641bf0a81da34d6f7801 100644 --- a/zed/src/workspace/sidebar.rs +++ b/zed/src/workspace/sidebar.rs @@ -113,7 +113,6 @@ impl Sidebar { container.add_child(self.render_resize_handle(settings, cx)); } - let width = self.width.clone(); container.add_child( Flexible::new( 1., @@ -122,9 +121,11 @@ impl Sidebar { .with_max_width(*self.width.borrow()) .boxed(), ) - .on_before_layout(move |constraint, _| { - let mut width = width.borrow_mut(); - *width = width.min(constraint.max.x()); + .on_after_layout({ + let width = self.width.clone(); + move |size, _| { + *width.borrow_mut() = size.x(); + } }) .boxed(), )