diff --git a/crates/ui-contract/src/contract/overlay/props.rs b/crates/ui-contract/src/contract/overlay/props.rs index 2dfc1b999..a0c7ad6be 100644 --- a/crates/ui-contract/src/contract/overlay/props.rs +++ b/crates/ui-contract/src/contract/overlay/props.rs @@ -292,7 +292,8 @@ impl Overlay { /// A toast stack at `corner` of `body`, a rect in the opener's layer space: the end-bottom corner of a window's body. /// Tab reaches its cards, and focus returns from them to where it came from when the last one ends. pub fn toast(body: Rect, corner: Align2) -> Overlay { - let place = Place::anchored(Anchor::Corner(body, corner), Side::Above, Align::End); + let align = if corner == Align2::CENTER_BOTTOM { Align::Center } else { Align::End }; + let place = Place::anchored(Anchor::Corner(body, corner), Side::Above, align); let mut overlay = Overlay::base(Rank::Toast, place, OverlayFit::ContentCapped(NOTE_SHARE), OwnerClass::Widget, FocusPolicy::TAB_LAYER); overlay.dismiss = Dismiss::new().outside(Outside::Keep); overlay diff --git a/crates/ui-contract/src/contract/toast.rs b/crates/ui-contract/src/contract/toast.rs index e163c5a34..d3dee70c0 100644 --- a/crates/ui-contract/src/contract/toast.rs +++ b/crates/ui-contract/src/contract/toast.rs @@ -47,6 +47,11 @@ impl ToastHost { /// The layer takes no room in the body that calls it, so a sizing pass of that body skips it and the real run /// opens the layer's stages once per pass. pub fn show(ui: &Ui, window: &WindowKey, body: Rect, card: &mut dyn FnMut(&mut Ui, &Toast, IdSalt) -> CardOutcome) { + Self::show_at(ui, window, body, None, card); + } + + /// Shows the queue at a bottom alignment of its body, or at the locale's trailing bottom corner when `None`. + pub fn show_at(ui: &Ui, window: &WindowKey, body: Rect, alignment: Option, card: &mut dyn FnMut(&mut Ui, &Toast, IdSalt) -> CardOutcome) { let ctx = ui.ctx(); if ui.is_sizing_pass() { return; @@ -69,7 +74,7 @@ impl ToastHost { if open { store::put(ctx, sub(id, "shown"), Shown(toasts.clone())); } - let overlay = Overlay::toast(body, corner(&env)); + let overlay = Overlay::toast(body, alignment.unwrap_or_else(|| corner(&env))); let anchor = areas::layer_to_screen(ctx, ui.layer_id()) * body; let layer = Layer { id, owner: id, overlay: &overlay, anchor, env: &env, exiting: !open, fade: presence.t, size: None, region: None }; overlay::host(ctx, &layer, &mut |ui| { diff --git a/crates/ui-shell/src/shell/app_shell.rs b/crates/ui-shell/src/shell/app_shell.rs index ef3c9bd83..a8683ed6f 100644 --- a/crates/ui-shell/src/shell/app_shell.rs +++ b/crates/ui-shell/src/shell/app_shell.rs @@ -288,6 +288,9 @@ pub struct AppShell<'a, S> { /// The screen mode. Without a binding the shell keeps it, starting at `Standard`. #[prop] screen_mode: Bind<'a, ScreenMode>, + /// The bottom alignment of notices in the body; without one they use its trailing bottom corner. + #[prop] + toast_alignment: Option, /// Whether the Tab reveal and the edge reveal work in body-only mode. Off for a presentation. #[prop] allow_reveal: bool, @@ -320,6 +323,7 @@ impl<'a, S> AppShell<'a, S> { embedded: false, content_room: None, screen_mode: Bind::kept(), + toast_alignment: None, allow_reveal: true, edge_reveal: true, document_tabs: true, @@ -430,6 +434,7 @@ impl<'a, S> AppShell<'a, S> { embedded, content_room, mut screen_mode, + toast_alignment, allow_reveal, edge_reveal, document_tabs, @@ -582,9 +587,9 @@ impl<'a, S> AppShell<'a, S> { outer.note_dialog(); } - // The window's toasts sit at the end-bottom corner of its body; an embedded shell's in the part of it in view. + // The window's toasts sit in its body; an embedded shell's in the part of it in view. let body = if embedded { regions.body.intersect(region) } else { regions.body }; - ToastStack::show(ui, &window, body); + ToastStack::show_at(ui, &window, body, toast_alignment); let mut edges: Vec<(RevealEdge, Rect)> = regions .slots diff --git a/crates/ui-shell/tests/it/notices.rs b/crates/ui-shell/tests/it/notices.rs index 0d4a5c348..4a217dd3f 100644 --- a/crates/ui-shell/tests/it/notices.rs +++ b/crates/ui-shell/tests/it/notices.rs @@ -247,3 +247,20 @@ fn a_card_left_after_a_long_still_hover_gets_its_whole_delay() { assert!(!queued(&fixture), "a full timeout after the leave the card is gone"); assert_eq!(toast::end(fixture.ctx(), card), Some(ToastEnd::Expired), "it expired"); } + +/// A shell can keep transient notices clear of controls at its trailing edge. +#[trial] +fn a_shell_can_center_its_toasts() { + let window = WindowKey::new("centered-toast-shell"); + let mut fixture = Fixture::new(move |ui| { + if toast::cards(ui.ctx(), &window).is_empty() { + toast::post(ui.ctx(), &window, Toast::new("Saved document").timeout(None)); + } + AppShell::new(window.clone()).toast_alignment(egui::Align2::CENTER_BOTTOM).body(|_, _, _| {}).show(ui, &mut ()); + }); + fixture.room(egui::vec2(900.0, 500.0)).settle(); + let text = fixture.texts().iter().find(|painted| painted.text == "Saved document").expect("the toast is drawn").rect; + let layers = craft_ui::testing::snapshot::layer_rects(fixture.ctx()); + let card = layers.iter().find(|rect| rect.contains(text.center())).expect("the toast layer contains its text"); + assert!((card.center().x - fixture.shown().center().x).abs() < 1.0, "the notice is centred: {card:?}"); +} diff --git a/crates/ui-widgets/src/notices/toasts.rs b/crates/ui-widgets/src/notices/toasts.rs index 3384269e5..f5b7ff253 100644 --- a/crates/ui-widgets/src/notices/toasts.rs +++ b/crates/ui-widgets/src/notices/toasts.rs @@ -7,7 +7,7 @@ use crate::notices::{Notice, NoticeAction, NoticeLayout}; use crate::runtime::env::{Env, WindowKey}; use crate::runtime::toast::ToastEnd; use crate::style::TypeRole; -use egui::{Rect, Ui}; +use egui::{Align2, Rect, Ui}; /// The cards of a window's toast queue. Whoever owns the window root calls `show` once per pass. A card with progress /// shows a progress bar named by its title (else its message) and the percentage under its text, before its actions. @@ -18,7 +18,12 @@ impl ToastStack { /// Shows every queued toast of `window` at the end-bottom corner of `body`, a rect of `ui`'s layer, through the /// toast host. pub fn show(ui: &Ui, window: &WindowKey, body: Rect) { - ToastHost::show(ui, window, body, &mut |ui, toast, salt| { + Self::show_at(ui, window, body, None); + } + + /// Shows the queue at a bottom alignment of the body, or its locale's trailing bottom corner when `None`. + pub fn show_at(ui: &Ui, window: &WindowKey, body: Rect, alignment: Option) { + ToastHost::show_at(ui, window, body, alignment, &mut |ui, toast, salt| { let mut notice = Notice::new(toast.tone, toast.message.clone()).layout(NoticeLayout::Card).lines(toast.lines.clone()).dismiss().id_salt(salt); if let Some(title) = &toast.title {