BlockingOverlay: built-in spinner and message; Cancel shows the card and lets Escape close it

This commit is contained in:
aiongg
2026-11-12 14:32:46 +00:00
parent 76be23a0f7
commit e4ad9acabe
3 changed files with 63 additions and 14 deletions
+39 -5
View File
@@ -2,8 +2,11 @@
use super::frame::{self, DialogOutput, FOOTER};
use crate::contract::{
Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, Mount, Overlay, Pad, Pointer as _, Props as _, Role, SaveKey,
Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, HeaderRegion, Mount, Overlay, Pad, Pointer as _, Props as _, Role,
SaveKey, Show as _, ShowContainer as _,
};
use crate::display::{Spinner, Text};
use crate::layout_components::Row;
use crate::runtime::env::Label;
use crate::runtime::focus::{FocusPolicy, Initial};
use crate::runtime::overlay::{Backdrop, CloseReason, Rank};
@@ -23,11 +26,12 @@ pub struct BlockingOverlayStyle {
/// A modal layer that takes the window's input while it is shown, with the wait cursor. Its label is the accessible
/// name. Without a card the layer has no look at all; with one, the body sits on the dialog surface, centred.
///
/// - Progress: [`message`](BlockingOverlay::message) shows a spinner and the message over the body, on the card.
/// - Open state: bound with [`opened`](BlockingOverlay::opened), else a session slot that starts open. Show it
/// every pass the work lasts, or bind the state to the work.
/// - Cancel: Escape closes it when [`cancellable`](BlockingOverlay::cancellable) says so, and a footer button
/// named by [`cancel`](BlockingOverlay::cancel) closes it as `CloseReason::Requested`; the open state then reads
/// `false`.
/// `false`. A Cancel button also shows the card and lets Escape close it.
#[derive(crate::contract::Component, crate::contract::Props)]
#[component(container)]
pub struct BlockingOverlay<'a> {
@@ -42,6 +46,7 @@ pub struct BlockingOverlay<'a> {
#[prop]
cursor: CursorIcon,
cancel: Option<Label>,
message: Option<Label>,
closed: Option<CloseReason>,
picked: Option<usize>,
}
@@ -53,7 +58,17 @@ impl<'a> BlockingOverlay<'a> {
overlay.rank = Rank::modal(Backdrop::None);
overlay.focus = FocusPolicy::DIALOG.initial(Initial::Keep).cancel(CancelRule::Ignore);
let props = ContainerProps::new().mount(Mount::Layer(overlay));
BlockingOverlay { common: Common::new(), props, open: None, cursor: CursorIcon::Wait, cancel: None, closed: None, picked: None }.label(name)
BlockingOverlay {
common: Common::new(),
props,
open: None,
cursor: CursorIcon::Wait,
cancel: None,
message: None,
closed: None,
picked: None,
}
.label(name)
}
fn with_overlay(mut self, change: impl FnOnce(&mut Overlay)) -> Self {
@@ -83,12 +98,22 @@ impl<'a> BlockingOverlay<'a> {
self.with_overlay(|overlay| overlay.focus = overlay.focus.cancel(if on { CancelRule::Close } else { CancelRule::Ignore }))
}
/// A footer with a Cancel button named `label`; a click closes the overlay as `CloseReason::Requested`.
/// A spinner and `message` over the body, on the card; the message also names the overlay.
#[must_use]
pub fn message(mut self, message: impl Into<Label>) -> Self {
let message = message.into();
self.props = self.props.header(HeaderRegion::new());
self.message = Some(message.clone());
self.card(true).label(message)
}
/// A footer with a Cancel button named `label`, on the card. A click closes the overlay as
/// `CloseReason::Requested`, Escape as `CloseReason::Escape`.
#[must_use]
pub fn cancel(mut self, label: impl Into<Label>) -> Self {
self.props = self.props.footer(FOOTER);
self.cancel = Some(label.into());
self
self.card(true).cancellable(true)
}
/// Saves the open state under `key`.
@@ -130,6 +155,15 @@ impl Container for BlockingOverlay<'_> {
self.open.as_mut().map(Bind::reborrow)
}
/// The spinner and the message.
fn header(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) {
let Some(message) = self.message.clone() else { return };
Row::new().show(ui, |ui, _| {
Spinner::new().show(ui);
Text::new(message).show(ui);
});
}
fn footer(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) {
let buttons: Vec<FooterButton> = self.cancel.iter().map(|label| FooterButton::new(label.clone(), DialogRole::Cancel)).collect();
frame::footer(
+3 -9
View File
@@ -137,15 +137,9 @@ fn dialog_frame_with_footer(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
fn blocking_overlay(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let scrim = *data.value("scrim", true);
let card = *data.value("wait card", true);
let mut checks = *data.value("checks", [true, false]);
let shown: StoryOutput = BlockingOverlay::new("Exporting")
.opened(Bind::bound(data.value("open", true)))
.card(card)
.scrim(scrim)
.show(ui, |ui, _| body(ui, &mut checks))
.into();
*data.value("checks", [true, false]) = checks;
shown
let overlay = BlockingOverlay::new("Exporting").scrim(scrim);
let overlay = if card { overlay.message("Exporting 24 pages…").cancel("Cancel") } else { overlay };
overlay.opened(Bind::bound(data.value("open", true))).show(ui, |_, _| {}).into()
}
#[story(ModalDragStrip, covers = [Default], drive = Drive::Never)]
+21
View File
@@ -170,3 +170,24 @@ fn a_dialog_opens_on_its_first_field_else_its_default_button() {
assert_eq!(focus_on_open(false, true), Some("OK"), "without a field the default button takes it");
assert_eq!(focus_on_open(false, false), None, "without either nothing takes it");
}
/// A progress overlay shows a spinner beside its message on its card, is named by the message, and its Cancel makes
/// Escape close it.
#[trial]
fn a_progress_overlay_shows_a_spinner_and_its_message_and_escape_cancels_it() {
let open = std::rc::Rc::new(Cell::new(true));
let seen = std::rc::Rc::clone(&open);
let mut fixture = Fixture::new(move |ui| {
let mut now = seen.get();
BlockingOverlay::new("Working").opened(Bind::bound(&mut now)).message("Exporting 24 pages").cancel("Stop").show(ui, |_, _| {});
seen.set(now);
});
fixture.settle();
let spinner = std::any::type_name::<crate::display::Spinner>();
assert!(!fixture.styles_of(spinner).is_empty(), "the card shows a spinner");
assert!(fixture.texts().iter().any(|run| run.text == "Exporting 24 pages"), "the card shows the message");
assert!(fixture.by_label("Exporting 24 pages").is_some(), "the message names the overlay");
fixture.key(Key::Escape, Modifiers::NONE);
fixture.settle();
assert!(!open.get(), "Escape closed the overlay that has a Cancel button");
}