diff --git a/crates/story/src/modal_story.rs b/crates/story/src/modal_story.rs index 8f88a8bb..4545cd33 100644 --- a/crates/story/src/modal_story.rs +++ b/crates/story/src/modal_story.rs @@ -299,6 +299,13 @@ impl ModalStory { .gap_4() .child(input.clone()) .child(date_picker.clone()) + .child( + Button::new("send-notification") + .child("Test Notification") + .on_click(|_, cx| { + cx.push_notification("Hello this is message from Drawer.") + }), + ) .child( div() .border_1() @@ -494,7 +501,7 @@ impl Render for ModalStory { WebView::new(cx, webview) }); - webview.update(cx, |webview, cx| { + webview.update(cx, |webview, _| { webview.load_url("https://github.com/explore"); }); cx.open_drawer(move |drawer, cx| { diff --git a/crates/ui/src/drawer.rs b/crates/ui/src/drawer.rs index c6d4baf3..1535a6ed 100644 --- a/crates/ui/src/drawer.rs +++ b/crates/ui/src/drawer.rs @@ -29,7 +29,7 @@ pub fn init(cx: &mut AppContext) { pub struct Drawer { pub(crate) focus_handle: FocusHandle, pub(crate) placement: Placement, - size: DefiniteLength, + pub(crate) size: DefiniteLength, resizable: bool, on_close: Rc, title: Option, diff --git a/crates/ui/src/root.rs b/crates/ui/src/root.rs index 75648ea6..459411c0 100644 --- a/crates/ui/src/root.rs +++ b/crates/ui/src/root.rs @@ -6,9 +6,9 @@ use crate::{ Placement, }; use gpui::{ - canvas, div, px, AnyView, Bounds, FocusHandle, InteractiveElement, IntoElement, - ParentElement as _, Pixels, Render, Styled, View, ViewContext, VisualContext as _, - WindowContext, + canvas, div, prelude::FluentBuilder as _, AnyView, DefiniteLength, FocusHandle, + InteractiveElement, IntoElement, ParentElement as _, Render, Styled, View, ViewContext, + VisualContext as _, WindowContext, }; use std::{ ops::{Deref, DerefMut}, @@ -173,7 +173,7 @@ impl ContextModal for ViewContext<'_, V> { fn open_drawer_at(&mut self, placement: Placement, build: F) where - F: Fn(Drawer, &mut WindowContexbuildt) -> Drawer + 'static, + F: Fn(Drawer, &mut WindowContext) -> Drawer + 'static, { self.deref_mut().open_drawer_at(placement, build) } @@ -230,7 +230,7 @@ pub struct Root { active_drawer: Option, active_modals: Vec, pub notification: View, - drawer_bounds: Bounds, + drawer_size: Option, view: AnyView, } @@ -254,7 +254,7 @@ impl Root { active_drawer: None, active_modals: Vec::new(), notification: cx.new_view(NotificationList::new), - drawer_bounds: Bounds::default(), + drawer_size: None, view, } } @@ -288,11 +288,11 @@ impl Root { } } - fn has_active_right_drawer(&self, cx: &WindowContext) -> bool { + fn active_drawer_placement(&self, cx: &WindowContext) -> Option { if let Some(drawer) = Root::read(cx).active_drawer.as_ref() { - drawer.placement == Placement::Right + Some(drawer.placement) } else { - false + None } } @@ -304,15 +304,18 @@ impl Root { .and_then(|w| w.root_view(cx).ok()) .expect("The window root view should be of type `ui::Root`."); - let right_offset = if root.read(cx).has_active_right_drawer(cx) { - root.read(cx).drawer_bounds.size.width - } else { - px(0.) + let active_drawer_placement = root.read(cx).active_drawer_placement(cx); + + let (mt, mr) = match active_drawer_placement { + Some(Placement::Right) => (None, root.read(cx).drawer_size), + Some(Placement::Top) => (root.read(cx).drawer_size, None), + _ => (None, None), }; Some( div() - .mr(right_offset) + .when_some(mt, |this, offset| this.mt(offset)) + .when_some(mr, |this, offset| this.mr(offset)) .child(root.read(cx).notification.clone()), ) } @@ -331,10 +334,12 @@ impl Root { drawer.focus_handle = active_drawer.focus_handle.clone(); drawer.placement = active_drawer.placement; + let drawer_size = drawer.size; + return Some( div().relative().child(drawer).child( canvas( - move |bounds, cx| root.update(cx, |r, _| r.drawer_bounds = bounds), + move |_, cx| root.update(cx, |r, _| r.drawer_size = Some(drawer_size)), |_, _, _| {}, ) .absolute()