mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Toasts: one queue per window; each AppShell, embedded ones included, shows its own window's toasts at the end-bottom corner of its body (Anchor::Corner replaces Anchor::Viewport), so nested shells no longer each draw every card at the viewport corner; NoticeCenter posts to its window, and the NoticeCenter and ToastStack stories show three notices in their own area, as the old ones did
This commit is contained in:
@@ -146,7 +146,7 @@ pub(crate) fn opener_floor(overlay: &Overlay) -> f32 {
|
||||
overlay.min_width.points(width)
|
||||
}
|
||||
|
||||
/// The anchor a layer opened by `opener` hangs from: the opener's own, else the layer's `Pointer` or `Viewport`
|
||||
/// The anchor a layer opened by `opener` hangs from: the opener's own, else the layer's `Pointer` or `Corner`
|
||||
/// place, else the pointer for a context request a secondary release made, else the rect a key's context request
|
||||
/// named, else the opener's rect.
|
||||
pub(super) fn opener_anchor(opener: &Opener, opening: Opening, place: &Place) -> Anchor {
|
||||
@@ -154,7 +154,7 @@ pub(super) fn opener_anchor(opener: &Opener, opening: Opening, place: &Place) ->
|
||||
return anchor.clone();
|
||||
}
|
||||
match (place, opening) {
|
||||
(Place::Anchored { anchor: anchor @ (Anchor::Pointer | Anchor::Viewport(_)), .. }, _) => anchor.clone(),
|
||||
(Place::Anchored { anchor: anchor @ (Anchor::Pointer | Anchor::Corner(..)), .. }, _) => anchor.clone(),
|
||||
(_, Opening::Pointer) => Anchor::Pointer,
|
||||
(_, Opening::At(rect)) => Anchor::Rect(rect),
|
||||
(_, Opening::Plain) => Anchor::Response,
|
||||
|
||||
@@ -15,8 +15,9 @@ pub enum Anchor {
|
||||
Point(Pos2),
|
||||
/// Where the pointer was when the overlay opened.
|
||||
Pointer,
|
||||
/// A corner or edge of the viewport's safe rect, inset by the panel padding.
|
||||
Viewport(Align2),
|
||||
/// A corner or edge of a rect in the opener's layer space, inset by the panel padding and kept inside the safe
|
||||
/// rect: the body a toast stack sits in.
|
||||
Corner(Rect, Align2),
|
||||
}
|
||||
|
||||
/// Where a floating layer sits.
|
||||
|
||||
@@ -18,20 +18,20 @@ pub(crate) fn resolve_anchor(ctx: &Context, id: Id, overlay: &Overlay, own: Rect
|
||||
let (opener, opening) = first.active(ctx, stored_opening(ctx, id));
|
||||
let to_screen = areas::layer_to_screen(ctx, opener.layer);
|
||||
return match opener_anchor(opener, opening, &overlay.place) {
|
||||
Anchor::Rect(rect) => to_screen * rect,
|
||||
Anchor::Rect(rect) | Anchor::Corner(rect, _) => to_screen * rect,
|
||||
Anchor::Point(at) => to_screen * Rect::from_min_max(at, at),
|
||||
_ => to_screen * opener.rect,
|
||||
};
|
||||
}
|
||||
match &overlay.place {
|
||||
Place::Anchored { anchor: Anchor::Rect(rect), .. } => own_to_screen * *rect,
|
||||
Place::Anchored { anchor: Anchor::Rect(rect) | Anchor::Corner(rect, _), .. } => own_to_screen * *rect,
|
||||
Place::Anchored { anchor: Anchor::Point(at), .. } => own_to_screen * Rect::from_min_max(*at, *at),
|
||||
_ => own_to_screen * own,
|
||||
}
|
||||
}
|
||||
|
||||
/// The overlay with the place's anchor that of the opener that opened it, for the anchors the host reads from the
|
||||
/// place (`Pointer`, `Viewport`).
|
||||
/// place (`Pointer`, `Corner`).
|
||||
pub(super) fn opened_overlay(ctx: &Context, overlay: &Overlay, stored: Option<Opening>) -> Option<(Overlay, Opening)> {
|
||||
let first = overlay.opener.as_ref()?;
|
||||
let (opener, opening) = first.active(ctx, stored);
|
||||
@@ -79,12 +79,12 @@ pub(super) fn anchor_rect(ctx: &Context, layer: &Layer<'_>, bounds: Rect, memory
|
||||
}
|
||||
|
||||
/// The anchor rect of `overlay` in screen space, the one rule the host and the body's measuring run share: a
|
||||
/// `Viewport` anchor from the safe rect `bounds` less `inset`, a `Pointer` anchor from `pointer_at` (the pointer where
|
||||
/// `Corner` anchor from its rect as `resolved` gives it in screen space, within the safe rect `bounds`, less `inset`, a `Pointer` anchor from `pointer_at` (the pointer where
|
||||
/// the layer opened, taken from the pointer now when none is recorded), any other from `resolved`.
|
||||
fn resolved_anchor(ctx: &Context, overlay: &Overlay, resolved: Rect, bounds: Rect, inset: f32, pointer_at: &mut Option<Pos2>) -> Rect {
|
||||
let point = |at: Pos2| Rect::from_min_max(at, at);
|
||||
match &overlay.place {
|
||||
Place::Anchored { anchor: Anchor::Viewport(at), .. } => point(at.pos_in_rect(&bounds.shrink(inset))),
|
||||
Place::Anchored { anchor: Anchor::Corner(_, at), .. } => point(at.pos_in_rect(&resolved.intersect(bounds).shrink(inset))),
|
||||
Place::Anchored { anchor: Anchor::Pointer, .. } => {
|
||||
point(*pointer_at.get_or_insert_with(|| areas::pointer_pos(ctx).unwrap_or(bounds.center())))
|
||||
}
|
||||
|
||||
@@ -250,10 +250,10 @@ impl Overlay {
|
||||
overlay
|
||||
}
|
||||
|
||||
/// A toast stack: the viewport's bottom end corner. Tab reaches its cards, and focus returns from them to where it came
|
||||
/// from when the last one ends.
|
||||
pub fn toast() -> Overlay {
|
||||
let place = Place::anchored(Anchor::Viewport(Align2::RIGHT_BOTTOM), Side::Above, Align::End);
|
||||
/// 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 mut overlay = Overlay::base(Rank::Toast, place, OverlayFit::ContentCapped(NOTE_SHARE), OwnerClass::Widget, FocusPolicy::TAB_LAYER);
|
||||
overlay.dismiss = Dismiss::new().outside(Outside::Keep);
|
||||
overlay
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
//! The toast host (15.5): one layer per window root (its id derives from the scope of the root's body) that lays out
|
||||
//! the queued toasts and tracks their holds and ends. A component of
|
||||
//! a higher layer draws each card (`notices::ToastStack`).
|
||||
//! the window's queued toasts at the end-bottom corner of its body and tracks their holds and ends. A component of a
|
||||
//! higher layer draws each card (`notices::ToastStack`).
|
||||
|
||||
use super::overlay::{self, Anchor, Layer, Overlay, Place};
|
||||
use super::overlay::{self, Layer, Overlay};
|
||||
use crate::runtime::egui::areas;
|
||||
use crate::runtime::egui::memory as store;
|
||||
use crate::runtime::env::Env;
|
||||
use crate::runtime::env::{Env, WindowKey};
|
||||
use crate::runtime::memory::sub;
|
||||
use crate::runtime::motion;
|
||||
use crate::runtime::toast::{self, Toast, ToastEnd};
|
||||
use craft_behavior::overlay::{Align, Side};
|
||||
use egui::{Align2, IdSalt, Rect, Ui};
|
||||
|
||||
/// The toasts of the cards shown last, so the layer's exit fade still shows them after the queue emptied.
|
||||
@@ -32,25 +32,26 @@ impl CardOutcome {
|
||||
}
|
||||
}
|
||||
|
||||
/// The layer that shows the toast queue of a viewport. The window root calls `show` once per pass, from its own
|
||||
/// body, so two roots in one viewport keep two layers.
|
||||
/// The layer that shows the toast queue of a window. The window root calls `show` once per pass with its window and
|
||||
/// its body's rect, so each window, an embedded one included, shows its own toasts in its own body.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct ToastHost;
|
||||
|
||||
impl ToastHost {
|
||||
/// Lays out every queued toast of the running viewport, oldest at the top, in the viewport's end corner, and
|
||||
/// Lays out every queued toast of `window`, oldest at the top, at the end-bottom corner of `body` (a rect of `ui`'s
|
||||
/// layer), and
|
||||
/// draws each with `card(ui, toast, salt)`, where `salt` is stable for the card. A card the pointer rests on keeps
|
||||
/// its timer paused; the first show of a toast announces it. When the queue empties the layer fades out with the
|
||||
/// last cards.
|
||||
///
|
||||
/// 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, card: &mut dyn FnMut(&mut Ui, &Toast, IdSalt) -> CardOutcome) {
|
||||
pub fn show(ui: &Ui, window: &WindowKey, body: Rect, card: &mut dyn FnMut(&mut Ui, &Toast, IdSalt) -> CardOutcome) {
|
||||
let ctx = ui.ctx();
|
||||
if ui.is_sizing_pass() {
|
||||
return;
|
||||
}
|
||||
let cards = toast::cards(ctx);
|
||||
let cards = toast::cards(ctx, window);
|
||||
let env = Env::of(ui);
|
||||
let id = sub(ui.scope_id(), "toast_host");
|
||||
let open = !cards.is_empty();
|
||||
@@ -68,9 +69,9 @@ impl ToastHost {
|
||||
if open {
|
||||
store::put(ctx, sub(id, "shown"), Shown(toasts.clone()));
|
||||
}
|
||||
let overlay = Overlay::toast().place(Place::anchored(Anchor::Viewport(corner(&env)), Side::Above, Align::End));
|
||||
let layer =
|
||||
Layer { id, owner: id, overlay: &overlay, anchor: Rect::NOTHING, env: &env, exiting: !open, fade: presence.t, size: None, region: None };
|
||||
let overlay = Overlay::toast(body, 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| {
|
||||
for (at, toast) in toasts.iter().enumerate() {
|
||||
let live = cards.get(at);
|
||||
@@ -87,7 +88,7 @@ impl ToastHost {
|
||||
}
|
||||
}
|
||||
|
||||
/// The viewport's end-bottom corner: right in left-to-right, left in right-to-left.
|
||||
/// The body's end-bottom corner: right in left-to-right, left in right-to-left.
|
||||
fn corner(env: &Env) -> Align2 {
|
||||
if env.direction.is_rtl() { Align2::LEFT_BOTTOM } else { Align2::RIGHT_BOTTOM }
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
//! The toast queue: one per viewport, filled from any code with a `Context`.
|
||||
//! The toast queues: one per window (`WindowKey`), filled from any code with a `Context`.
|
||||
//!
|
||||
//! The shell's `ToastHost` shows the queue and tells it which cards the pointer or focus holds. A card's timer runs
|
||||
//! only while nothing holds it. Each new toast is announced once, when it is first shown. At most `MAX_CARDS` cards
|
||||
//! are queued; a toast's end (an action taken, a dismissal, the timer) is read once with `end`.
|
||||
//! The window's shell shows its queue through `ToastHost`, at the end-bottom corner of its body, and tells it which
|
||||
//! cards the pointer or focus holds; an embedded shell shows its own queue inside its own body. A card's timer runs only
|
||||
//! while nothing holds it. Each new toast is announced once, when it is first shown. At most `MAX_CARDS` cards are
|
||||
//! queued per window; a toast's end (an action taken, a dismissal, the timer) is read once with `end`.
|
||||
|
||||
use crate::runtime::access::{self, Politeness};
|
||||
use crate::runtime::egui::{pass, repaint};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::env::{Label, WindowKey};
|
||||
use crate::runtime::frame::{self, PerViewport, Stored};
|
||||
use craft_theme::variant::Tone;
|
||||
use egui::{Context, ViewportId};
|
||||
@@ -138,10 +139,12 @@ struct Live {
|
||||
held: bool,
|
||||
}
|
||||
|
||||
/// The queues of one viewport's windows, with the ids, the timers' clock and the ends they share.
|
||||
#[derive(Default)]
|
||||
struct Queue {
|
||||
next: u64,
|
||||
live: Vec<Live>,
|
||||
/// Each window's cards, oldest first.
|
||||
windows: Vec<(WindowKey, Vec<Live>)>,
|
||||
/// The pass time of the last end hook, to measure the timers' elapsed time.
|
||||
last_tick: Option<f64>,
|
||||
/// How toasts ended, until `end` reads them.
|
||||
@@ -150,6 +153,23 @@ struct Queue {
|
||||
|
||||
impl Stored for Queue {}
|
||||
|
||||
impl Queue {
|
||||
fn window(&mut self, window: &WindowKey) -> &mut Vec<Live> {
|
||||
let at = match self.windows.iter().position(|(key, _)| key == window) {
|
||||
Some(at) => at,
|
||||
None => {
|
||||
self.windows.push((window.clone(), Vec::new()));
|
||||
self.windows.len() - 1
|
||||
}
|
||||
};
|
||||
&mut self.windows[at].1
|
||||
}
|
||||
|
||||
fn live(&mut self) -> impl Iterator<Item = &mut Live> {
|
||||
self.windows.iter_mut().flat_map(|(_, live)| live.iter_mut())
|
||||
}
|
||||
}
|
||||
|
||||
fn record(ended: &mut Vec<(ToastId, ToastEnd)>, id: ToastId, end: ToastEnd) {
|
||||
ended.push((id, end));
|
||||
if ended.len() > ENDS_KEPT {
|
||||
@@ -157,13 +177,14 @@ fn record(ended: &mut Vec<(ToastId, ToastEnd)>, id: ToastId, end: ToastEnd) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Posts `toast` to the running viewport.
|
||||
pub fn post(ctx: &Context, toast: Toast) -> ToastId {
|
||||
post_to(ctx, pass::current_viewport(ctx), toast)
|
||||
/// Posts `toast` to the window `window` of the running viewport. A body posts to its shell's window
|
||||
/// (`BodyCx::shell`); code outside any body posts to the app's window, `WindowKey::main()` for the first.
|
||||
pub fn post(ctx: &Context, window: &WindowKey, toast: Toast) -> ToastId {
|
||||
post_to(ctx, pass::current_viewport(ctx), window, toast)
|
||||
}
|
||||
|
||||
/// Posts `toast` to `viewport`.
|
||||
pub fn post_to(ctx: &Context, viewport: ViewportId, toast: Toast) -> ToastId {
|
||||
/// Posts `toast` to the window `window` of `viewport`.
|
||||
pub fn post_to(ctx: &Context, viewport: ViewportId, window: &WindowKey, toast: Toast) -> ToastId {
|
||||
if !crate::runtime::measure::effect(ctx, "toast::post") {
|
||||
// No toast is numbered 0, so a later dismiss of this id removes nothing.
|
||||
return ToastId(0);
|
||||
@@ -172,10 +193,11 @@ pub fn post_to(ctx: &Context, viewport: ViewportId, toast: Toast) -> ToastId {
|
||||
queue.next += 1;
|
||||
let id = ToastId(queue.next);
|
||||
let remaining = toast.timeout;
|
||||
queue.live.push(Live { card: Card { id, toast }, shown: false, remaining, held: false });
|
||||
while queue.live.len() > MAX_CARDS {
|
||||
let at = queue.live.iter().position(|live| live.card.toast.actions.is_empty()).unwrap_or(0);
|
||||
queue.live.remove(at);
|
||||
let live = queue.window(window);
|
||||
live.push(Live { card: Card { id, toast }, shown: false, remaining, held: false });
|
||||
while live.len() > MAX_CARDS {
|
||||
let at = live.iter().position(|live| live.card.toast.actions.is_empty()).unwrap_or(0);
|
||||
live.remove(at);
|
||||
}
|
||||
id
|
||||
});
|
||||
@@ -197,9 +219,12 @@ pub(crate) fn close(ctx: &Context, id: ToastId, end: ToastEnd) {
|
||||
fn remove(ctx: &Context, id: ToastId, end: Option<ToastEnd>) {
|
||||
for viewport in pass::live_viewports(ctx) {
|
||||
let removed = PerViewport::<Queue>::with(ctx, viewport, |queue| {
|
||||
let before = queue.live.len();
|
||||
queue.live.retain(|live| live.card.id != id);
|
||||
let removed = queue.live.len() != before;
|
||||
let mut removed = false;
|
||||
for (_, live) in &mut queue.windows {
|
||||
let before = live.len();
|
||||
live.retain(|live| live.card.id != id);
|
||||
removed |= live.len() != before;
|
||||
}
|
||||
if let Some(end) = end.filter(|_| removed) {
|
||||
record(&mut queue.ended, id, end);
|
||||
}
|
||||
@@ -226,22 +251,25 @@ pub fn end(ctx: &Context, id: ToastId) -> Option<ToastEnd> {
|
||||
pub fn keep(ctx: &Context, id: ToastId) {
|
||||
for viewport in pass::live_viewports(ctx) {
|
||||
PerViewport::<Queue>::with(ctx, viewport, |queue| {
|
||||
if let Some(live) = queue.live.iter_mut().find(|live| live.card.id == id) {
|
||||
if let Some(live) = queue.live().find(|live| live.card.id == id) {
|
||||
live.remaining = None;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// The cards of the running viewport, oldest first.
|
||||
pub fn cards(ctx: &Context) -> Vec<Card> {
|
||||
PerViewport::<Queue>::with(ctx, pass::current_viewport(ctx), |queue| queue.live.iter().map(|live| live.card.clone()).collect())
|
||||
/// The cards of the window `window` in the running viewport, oldest first.
|
||||
pub fn cards(ctx: &Context, window: &WindowKey) -> Vec<Card> {
|
||||
PerViewport::<Queue>::with(ctx, pass::current_viewport(ctx), |queue| {
|
||||
let cards = queue.windows.iter().find(|(key, _)| key == window);
|
||||
cards.map_or_else(Vec::new, |(_, live)| live.iter().map(|live| live.card.clone()).collect())
|
||||
})
|
||||
}
|
||||
|
||||
/// The host shows `id` in this pass; `held` says the pointer or focus is on it. The first show announces it.
|
||||
pub fn shown(ctx: &Context, id: ToastId, text: &str, held: bool) {
|
||||
let announce = PerViewport::<Queue>::with(ctx, pass::current_viewport(ctx), |queue| {
|
||||
let live = queue.live.iter_mut().find(|live| live.card.id == id)?;
|
||||
let live = queue.live().find(|live| live.card.id == id)?;
|
||||
live.held = held;
|
||||
let first = !live.shown;
|
||||
live.shown = true;
|
||||
@@ -260,7 +288,7 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
let wake = PerViewport::<Queue>::with(ctx, viewport, |queue| {
|
||||
let elapsed = queue.last_tick.map_or(0.0, |last| (now - last).max(0.0)) as f32;
|
||||
queue.last_tick = Some(now);
|
||||
for live in &mut queue.live {
|
||||
for live in queue.live() {
|
||||
if live.shown
|
||||
&& !live.held
|
||||
&& let Some(remaining) = &mut live.remaining
|
||||
@@ -268,14 +296,17 @@ pub(crate) fn end_pass(ctx: &Context) {
|
||||
*remaining -= elapsed;
|
||||
}
|
||||
}
|
||||
for live in std::mem::take(&mut queue.live) {
|
||||
if live.remaining.is_none_or(|remaining| remaining > 0.0) {
|
||||
queue.live.push(live);
|
||||
} else {
|
||||
record(&mut queue.ended, live.card.id, ToastEnd::Expired);
|
||||
for (_, live) in &mut queue.windows {
|
||||
for card in std::mem::take(live) {
|
||||
if card.remaining.is_none_or(|remaining| remaining > 0.0) {
|
||||
live.push(card);
|
||||
} else {
|
||||
record(&mut queue.ended, card.card.id, ToastEnd::Expired);
|
||||
}
|
||||
}
|
||||
}
|
||||
queue.live.iter().filter(|live| live.shown && !live.held).filter_map(|live| live.remaining).reduce(f32::min)
|
||||
queue.windows.retain(|(_, live)| !live.is_empty());
|
||||
queue.live().filter(|live| live.shown && !live.held).filter_map(|live| live.remaining).reduce(f32::min)
|
||||
});
|
||||
if let Some(seconds) = wake {
|
||||
repaint::request_after(ctx, viewport, f64::from(seconds));
|
||||
|
||||
@@ -52,7 +52,7 @@ impl Gallery {
|
||||
let notes = self.persisted.notes().into_iter().chain(self.drop_note.take());
|
||||
for note in notes {
|
||||
if self.noted.insert(note.clone()) {
|
||||
toast::post(ctx, Toast::new(note));
|
||||
toast::post(ctx, &WindowKey::main(), Toast::new(note));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,7 +558,9 @@ impl<'a, S> AppShell<'a, S> {
|
||||
outer.note_dialog();
|
||||
}
|
||||
|
||||
ToastStack::show(ui);
|
||||
// The window's toasts sit at the end-bottom corner of 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);
|
||||
|
||||
let mut edges: Vec<(RevealEdge, Rect)> = regions
|
||||
.slots
|
||||
|
||||
@@ -4,6 +4,7 @@ mod record;
|
||||
|
||||
use craft_ui::contract::{Bind, Politeness, Show as _};
|
||||
use craft_ui::runtime::commands::{FireSource, fire};
|
||||
use craft_ui::runtime::env::WindowKey;
|
||||
use craft_ui::runtime::toast::{self, Toast};
|
||||
use craft_ui::testing::{Frames, is_measuring, measuring_run, take_effects, trial};
|
||||
use craft_ui::text::TextField;
|
||||
@@ -17,11 +18,11 @@ fn a_measuring_run_fires_and_announces_nothing() {
|
||||
measuring_run(&ctx, || {
|
||||
assert!(!fire(&ctx, "save", FireSource::Palette));
|
||||
craft_ui::contract::announce(&ctx, "Saved", Politeness::Polite);
|
||||
let id = toast::post(&ctx, Toast::new("Saved"));
|
||||
let id = toast::post(&ctx, &WindowKey::main(), Toast::new("Saved"));
|
||||
toast::dismiss(&ctx, id);
|
||||
});
|
||||
assert_eq!(take_effects(&ctx), ["commands::fire", "announce", "toast::post"]);
|
||||
assert!(toast::cards(&ctx).is_empty(), "no toast was posted");
|
||||
assert!(toast::cards(&ctx, &WindowKey::main()).is_empty(), "no toast was posted");
|
||||
});
|
||||
assert!(craft_ui::runtime::frame::take_output(&frames.ctx).announcements.is_empty(), "nothing was announced");
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The notices family: the settings notices and the toast stack.
|
||||
//! The notices family: the settings notices and each window's toasts.
|
||||
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::notices::{SaveResult, SettingsNoticeState, SettingsStatus, ToastStack};
|
||||
use craft_ui::notices::{SaveResult, SettingsNoticeState, SettingsStatus};
|
||||
use craft_ui::runtime::env::WindowKey;
|
||||
use craft_ui::runtime::toast::{self, Toast};
|
||||
use craft_ui::shell::AppShell;
|
||||
@@ -29,21 +29,44 @@ fn repeated_save_failures_escalate_and_a_success_clears_them() {
|
||||
assert_eq!(status.tone(), Tone::Good);
|
||||
}
|
||||
|
||||
/// A toast stack in a shell body shows once per pass although the body also runs for sizing; debug builds panic
|
||||
/// when its presence stage opens twice in a pass.
|
||||
/// A shell shows its window's toasts once per pass although its body also runs for sizing; debug builds panic when
|
||||
/// the toast layer's presence stage opens twice in a pass.
|
||||
#[trial]
|
||||
fn a_toast_stack_in_a_shell_body_opens_its_layer_once_per_pass() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
fn a_shell_opens_its_toast_layer_once_per_pass() {
|
||||
let window = WindowKey::new("toast-stack-shell");
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if toast::cards(&ctx).is_empty() {
|
||||
toast::post(&ctx, Toast::new("poster.png was saved to Pictures."));
|
||||
if toast::cards(&ctx, &window).is_empty() {
|
||||
toast::post(&ctx, &window, Toast::new("poster.png was saved to Pictures."));
|
||||
}
|
||||
AppShell::embedded(WindowKey::new("toast-stack-shell")).body(|ui, _, _| ToastStack::show(ui)).show(ui, &mut ());
|
||||
AppShell::embedded(window.clone()).body(|_, _, _| {}).show(ui, &mut ());
|
||||
});
|
||||
fixture.settle();
|
||||
assert!(!fixture.texts().is_empty(), "the toast card shows");
|
||||
}
|
||||
|
||||
/// A toast posted to an embedded shell's window shows once, inside that shell's body, and not in the shell beside it.
|
||||
#[trial]
|
||||
fn a_toast_shows_in_the_body_of_its_own_window() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let ctx = ui.ctx().clone();
|
||||
let right = WindowKey::new("right-shell");
|
||||
if toast::cards(&ctx, &right).is_empty() {
|
||||
toast::post(&ctx, &right, Toast::new("poster.png was saved.").timeout(None));
|
||||
}
|
||||
ui.columns(2, |columns| {
|
||||
AppShell::embedded(WindowKey::new("left-shell")).body(|_, _, _| {}).show(&mut columns[0], &mut ());
|
||||
AppShell::embedded(right).body(|_, _, _| {}).show(&mut columns[1], &mut ());
|
||||
});
|
||||
});
|
||||
fixture.room(egui::Vec2::new(800.0, 400.0));
|
||||
fixture.settle();
|
||||
let cards: Vec<egui::Rect> =
|
||||
fixture.texts().iter().filter(|painted| painted.text == "poster.png was saved.").map(|painted| painted.rect).collect();
|
||||
assert_eq!(cards.len(), 1, "one card, in one window");
|
||||
assert!(cards[0].left() > fixture.shown().center().x, "the card sits in the right-hand shell");
|
||||
}
|
||||
|
||||
/// Dismissing the focused toast hands the focus to the next card, and the last one back to where it came from.
|
||||
#[trial]
|
||||
fn a_dismissed_toast_hands_the_focus_on_and_the_last_returns_it() {
|
||||
@@ -54,15 +77,15 @@ fn a_dismissed_toast_hands_the_focus_on_and_the_last_returns_it() {
|
||||
let mut posted = false;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let ctx = ui.ctx().clone();
|
||||
let window = WindowKey::new("toast-focus-shell");
|
||||
if !posted {
|
||||
toast::post(&ctx, Toast::new("poster.png was saved."));
|
||||
toast::post(&ctx, Toast::new("flyer.png was saved."));
|
||||
toast::post(&ctx, &window, Toast::new("poster.png was saved."));
|
||||
toast::post(&ctx, &window, Toast::new("flyer.png was saved."));
|
||||
posted = true;
|
||||
}
|
||||
AppShell::embedded(WindowKey::new("toast-focus-shell"))
|
||||
AppShell::embedded(window)
|
||||
.body(|ui, _, _| {
|
||||
Button::new("Before").show(ui);
|
||||
ToastStack::show(ui);
|
||||
})
|
||||
.show(ui, &mut ());
|
||||
});
|
||||
@@ -81,9 +104,10 @@ fn a_dismissed_toast_hands_the_focus_on_and_the_last_returns_it() {
|
||||
fixture.key(Key::Tab, Modifiers::NONE).idle(2);
|
||||
assert_eq!(focused_name(&fixture).as_deref(), Some("Dismiss"), "Tab reaches the first card's Dismiss");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(4);
|
||||
assert_eq!(toast::cards(fixture.ctx()).len(), 1, "one card ended");
|
||||
let window = WindowKey::new("toast-focus-shell");
|
||||
assert_eq!(toast::cards(fixture.ctx(), &window).len(), 1, "one card ended");
|
||||
assert_eq!(focused_name(&fixture).as_deref(), Some("Dismiss"), "the focus moved to the other card's Dismiss");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(4);
|
||||
assert!(toast::cards(fixture.ctx()).is_empty(), "both cards ended");
|
||||
assert!(toast::cards(fixture.ctx(), &window).is_empty(), "both cards ended");
|
||||
assert_eq!(focused_name(&fixture).as_deref(), Some("Before"), "the focus is back where it came from");
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! `runtime::toast`, which `ToastStack` shows, and reports how each one ended.
|
||||
|
||||
use crate::contract::{NoValue, Outcome, Politeness};
|
||||
use crate::runtime::env::{Label, WordSet};
|
||||
use crate::runtime::env::{Label, WindowKey, WordSet};
|
||||
use crate::runtime::memory::{unique, with_session};
|
||||
use crate::runtime::toast::{self, Toast, ToastAction, ToastEnd, ToastId};
|
||||
use crate::runtime::words::CommonWords;
|
||||
@@ -87,11 +87,15 @@ fn posted_slot() -> egui::Id {
|
||||
unique("craft_ui.notice_center")
|
||||
}
|
||||
|
||||
/// The app's way to post notices. It holds only its timeout: the queue, the timers, the cap of three cards, the
|
||||
/// hover pause and the announcement are the toast service's. A notice with a dismiss key shows a "Don't Show Again"
|
||||
/// The app's way to post notices to a window. It holds only the window and its timeout: the queue, the timers, the cap
|
||||
/// of three cards, the hover pause and the announcement are the toast service's. A body posts to its shell's window
|
||||
/// (`BodyCx::shell`). A notice with a dismiss key shows a "Don't Show Again"
|
||||
/// action; `show` reports how posted notices ended.
|
||||
#[derive(Clone, Debug, PartialEq, crate::contract::Pattern, crate::contract::Props)]
|
||||
pub struct NoticeCenter {
|
||||
/// The window whose toasts show the notices.
|
||||
#[prop]
|
||||
window: WindowKey,
|
||||
/// Seconds before a notice hides, from the app's notification duration.
|
||||
#[prop(quantity = "seconds")]
|
||||
timeout: Option<f32>,
|
||||
@@ -104,12 +108,12 @@ impl Default for NoticeCenter {
|
||||
}
|
||||
|
||||
impl NoticeCenter {
|
||||
/// A center whose notices hide after the toast service's default time.
|
||||
/// A center for the app's first window whose notices hide after the toast service's default time.
|
||||
pub fn new() -> Self {
|
||||
NoticeCenter { timeout: Some(toast::TIMEOUT) }
|
||||
NoticeCenter { window: WindowKey::main(), timeout: Some(toast::TIMEOUT) }
|
||||
}
|
||||
|
||||
/// Posts `notice` to the running viewport; `None` when its dismiss key is in `silenced`.
|
||||
/// Posts `notice` to the center's window in the running viewport; `None` when its dismiss key is in `silenced`.
|
||||
pub fn post(&self, ctx: &Context, notice: &PostedNotice, silenced: &[String]) -> Option<ToastId> {
|
||||
if notice.dismiss_key.as_ref().is_some_and(|key| silenced.contains(key)) {
|
||||
return None;
|
||||
@@ -121,7 +125,7 @@ impl NoticeCenter {
|
||||
if notice.dismiss_key.is_some() {
|
||||
card = card.action(ToastAction::new(dont_show_again()));
|
||||
}
|
||||
let id = toast::post(ctx, card);
|
||||
let id = toast::post(ctx, &self.window, card);
|
||||
with_session(ctx, posted_slot(), |posted: &mut Posted| {
|
||||
posted.0.push((id, notice.dismiss_key.clone()));
|
||||
if posted.0.len() > TRACKED {
|
||||
|
||||
@@ -7,8 +7,9 @@ use super::{
|
||||
use crate::contract::{Politeness, Show as _};
|
||||
use crate::display::{Shade, Text};
|
||||
use crate::primitives::IconSpec;
|
||||
use crate::runtime::env::{Label, WindowKey};
|
||||
use crate::runtime::toast;
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, stage, story};
|
||||
use craft_behavior::IconId;
|
||||
use craft_theme::variant::Tone;
|
||||
use egui::Ui;
|
||||
@@ -91,23 +92,38 @@ fn settings_notices(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
Text::new("Preferences").show(ui).into()
|
||||
}
|
||||
|
||||
/// Three notices in the bottom corner of the story's stage: a neutral one, a warning with Don't Show Again and an
|
||||
/// error that stays. Notices that ended stay gone.
|
||||
#[story(NoticeCenter, covers = [], drive = Drive::Never)]
|
||||
fn notice_center(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
fn notice_center(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let ctx = ui.ctx().clone();
|
||||
if toast::cards(&ctx).is_empty() {
|
||||
let notice = PostedNotice::new("Exported").lines(vec!["poster.png".to_owned()]).tone(Tone::Good);
|
||||
NoticeCenter::new().post(&ctx, ¬ice, &[]);
|
||||
let window = WindowKey::new("notice-center-story");
|
||||
if !std::mem::replace(data.value("posted", false), true) {
|
||||
let center = NoticeCenter::new().window(window.clone());
|
||||
let _ = center.post(&ctx, &PostedNotice::new("Pasted from the clipboard").sticky(), &[]);
|
||||
let warning = PostedNotice::new("Profile mismatch").lines(vec!["The document uses its own profile.".to_owned()]).tone(Tone::Warn);
|
||||
let _ = center.post(&ctx, &warning.dismiss_key("profile-mismatch".to_owned()).sticky(), &[]);
|
||||
let error = PostedNotice::new("Couldn't save the document").lines(vec!["The disk is full.".to_owned()]).tone(Tone::Bad);
|
||||
let _ = center.post(&ctx, &error.sticky(), &[]);
|
||||
}
|
||||
NoticeCenter::new().show(ui);
|
||||
Text::new("Notices").show(ui).into()
|
||||
let area = stage(ui);
|
||||
ToastStack::show(ui, &window, area);
|
||||
NoticeCenter::new().window(window).show(ui).into()
|
||||
}
|
||||
|
||||
/// A neutral toast, a warning with ten lines and an error in the bottom corner of the story's stage.
|
||||
#[story(ToastStack, covers = [], drive = Drive::Never)]
|
||||
fn toast_stack(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
fn toast_stack(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let ctx = ui.ctx().clone();
|
||||
if toast::cards(&ctx).is_empty() {
|
||||
toast::post(&ctx, toast::Toast::new("poster.png was saved to Pictures.").tone(Tone::Good).title("Exported"));
|
||||
let window = WindowKey::new("toast-stack-story");
|
||||
if !std::mem::replace(data.value("posted", false), true) {
|
||||
let lines = (1..=10).map(|n| format!("Warning {n}"));
|
||||
toast::post(&ctx, &window, toast::Toast::new(Label::Empty).title("Pasted from the clipboard").timeout(None));
|
||||
toast::post(&ctx, &window, toast::Toast::new(Label::Empty).title("Opened with 10 warnings").lines(lines).tone(Tone::Warn).timeout(None));
|
||||
let error = toast::Toast::new(Label::Empty).title("Couldn't save the document").lines(["The disk is full."]).tone(Tone::Bad);
|
||||
toast::post(&ctx, &window, error.timeout(None));
|
||||
}
|
||||
ToastStack::show(ui);
|
||||
let area = stage(ui);
|
||||
ToastStack::show(ui, &window, area);
|
||||
Text::new("Toasts").show(ui).into()
|
||||
}
|
||||
|
||||
@@ -3,17 +3,19 @@
|
||||
use crate::contract::toast::{CardOutcome, ToastHost};
|
||||
use crate::contract::{Props as _, Show as _};
|
||||
use crate::notices::{Notice, NoticeAction, NoticeLayout};
|
||||
use crate::runtime::env::WindowKey;
|
||||
use crate::runtime::toast::ToastEnd;
|
||||
use egui::Ui;
|
||||
use egui::{Rect, Ui};
|
||||
|
||||
/// The cards of the toast queue. Whoever owns the window root calls `show` once per pass.
|
||||
/// The cards of a window's toast queue. Whoever owns the window root calls `show` once per pass.
|
||||
#[derive(Clone, Copy, Debug, Default, crate::contract::Pattern)]
|
||||
pub struct ToastStack;
|
||||
|
||||
impl ToastStack {
|
||||
/// Shows every queued toast of the running viewport through the toast host.
|
||||
pub fn show(ui: &Ui) {
|
||||
ToastHost::show(ui, &mut |ui, toast, salt| {
|
||||
/// 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| {
|
||||
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 {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::contract::{Bind, CollectionEvent, Props as _, Show as _};
|
||||
use craft_ui::notices::{Notice, NoticeAction, ToastStack};
|
||||
use craft_ui::runtime::env::WindowKey;
|
||||
use craft_ui::runtime::toast;
|
||||
use craft_ui::table::{MetadataGroup, MetadataRow, MetadataTable};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
@@ -92,10 +93,12 @@ fn notice_title_and_message_select_and_copy_apart() {
|
||||
fn toast_text_selects_and_copies() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if toast::cards(&ctx).is_empty() {
|
||||
toast::post(&ctx, toast::Toast::new("poster.png was saved to Pictures.").tone(Tone::Good).title("Exported").timeout(None));
|
||||
let window = WindowKey::new("toast-text");
|
||||
if toast::cards(&ctx, &window).is_empty() {
|
||||
let card = toast::Toast::new("poster.png was saved to Pictures.").tone(Tone::Good).title("Exported").timeout(None);
|
||||
toast::post(&ctx, &window, card);
|
||||
}
|
||||
ToastStack::show(ui);
|
||||
ToastStack::show(ui, &window, ui.max_rect());
|
||||
});
|
||||
fixture.settle();
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
|
||||
+1
-1
@@ -234,7 +234,7 @@ One row per name in the author table (Overview) and per `pub` item a component a
|
||||
| `RowState` | An item's row facts from the collection; the row's id scope is its key | `craft_ui::contract` | 17.2, 17.5 |
|
||||
| Row shape | The theme's row drawing shape, `RowStyle` | craft-theme | 4.6 |
|
||||
| `ListRowStyle` | `ListRow`'s style struct; it reads the theme's `RowStyle` | `craft_ui::list` | 17.7 |
|
||||
| `runtime::toast::post` | Posts a toast from any code with a `Context` | `craft_ui::runtime::toast` | 15.5 |
|
||||
| `runtime::toast::post` | Posts a toast to a window's queue from any code with a `Context`; the window's shell shows it at its body's end-bottom corner | `craft_ui::runtime::toast` | 15.5 |
|
||||
| `SaveKey` | A crate-prefixed key for the saved lifetime, never derived from an id | `craft_ui::runtime::memory` | 7.2 |
|
||||
| `ScopeKind`, `CancelRule`, `Initial`, `EnterRule` | A focus scope's kind, Escape rule, initial focus and Enter rule | craft-behavior (`ScopeKind`, `CancelRule`), `craft_ui::runtime::focus` (`Initial`, `EnterRule`) | 9.4 |
|
||||
| `Scroll`, `ScrollAnchor`, `Notch` | `ContainerProps.scroll`, its anchor and its wheel notch distance | `craft_ui::contract` | 16.2 |
|
||||
|
||||
Reference in New Issue
Block a user