Dialog
A modal window over a dimmed page, with a title, a body and footer buttons. Escape or a click outside closes it, Tab stays inside, and focus goes back to the button that opened it. AlertDialog asks for a decision.
Default
if ui.add(Button::new("Open Dialog")).clicked() {
state.profile = true;
}
let other = &mut state.other;
Dialog::new("profile")
.title("Edit profile")
.description("Make changes to your profile here. Click save when you are done.")
.footer(|ui| {
if ui.add(Button::primary("Save changes")).clicked() {
ui.close();
}
if ui.add(Button::new("Cancel")).clicked() {
ui.close();
}
})
.show(&ctx, &mut state.profile, |ui| {
ui.label("Name");
ui.add(fastframe_ui::Input::new(&mut state.name));
if ui.add(Button::new("Open Other Dialog")).clicked() {
*other = true;
}
});
Dialog::new("profile-other")
.title("Other Dialog")
.description("A dialog opened over another sits a little lower.")
.footer(|ui| {
if ui.add(Button::primary("Confirm")).clicked() {
ui.close();
}
})
.show(&ctx, &mut state.other, |ui| {
ui.label("Close it and focus goes back to the first dialog.");
});
Without title
if ui.add(Button::new("Dialog without Title")).clicked() {
state.untitled = true;
}
Dialog::new("untitled").show(&ctx, &mut state.untitled, |ui| {
ui.label("A dialog needs no title: this one only has a body.");
});
Custom actions
if ui.add(Button::new("Custom Buttons")).clicked() {
state.restart = true;
}
let restarted = &mut state.restarted;
Dialog::new("restart")
.title("Update installed")
.description("Restart to finish installing the update.")
.footer(|ui| {
if ui.add(Button::primary("Restart Now")).clicked() {
*restarted += 1;
ui.close();
}
if ui.add(Button::new("Later")).clicked() {
ui.close();
}
})
.show(&ctx, &mut state.restart, |_| {});
Scrollable
if ui.add(Button::new("Scrollable Dialog")).clicked() {
state.scrollable = true;
}
Dialog::new("scrollable")
.title("Dialog with scrollbar")
.footer(|ui| {
if ui.add(Button::primary("Confirm")).clicked() {
ui.close();
}
if ui.add(Button::new("Cancel")).clicked() {
ui.close();
}
})
.show(&ctx, &mut state.scrollable, |ui| {
for line in 1..=60 {
ui.label(format!(
"Line {line}: the body scrolls when it outgrows the window."
));
}
});
Padding and width
if ui.add(Button::new("Custom Paddings")).clicked() {
state.padding = true;
}
Dialog::new("padding")
.title("Custom Dialog Title")
.padding(12.0)
.show(&ctx, &mut state.padding, |ui| {
ui.label("Twelve points of padding instead of sixteen.");
});
if ui
.add(Button::new("Custom Width (400px), centred"))
.clicked()
{
state.narrow = true;
}
Dialog::new("narrow")
.title("Narrow")
.width(400.0)
.centered()
.show(&ctx, &mut state.narrow, |ui| {
ui.label("400 points wide, in the middle of the window.");
});
Entrance
for (label, entrance) in [
("Slide down", DialogEntrance::SlideDown),
("Fade", DialogEntrance::Fade),
("Fade and slide", DialogEntrance::FadeSlide),
("None", DialogEntrance::None),
] {
if ui.add(Button::new(label)).clicked() {
state.entrance = Some(entrance);
}
}
// Shown only while the app holds the entrance: the dialog still
// leaves the way it came once the app lets it go.
if let Some(entrance) = state.entrance {
let mut open = true;
Dialog::new("entrance")
.title("Entrance")
.entrance(entrance)
.show(&ctx, &mut open, |ui| {
ui.label("Each entrance leaves the way it came.");
});
if !open {
state.entrance = None;
}
}
Overlay and keyboard
if ui.add(Button::new("Without overlay")).clicked() {
state.no_overlay = true;
}
Dialog::new("no-overlay")
.title("No overlay")
.overlay(false)
.show(&ctx, &mut state.no_overlay, |ui| {
ui.label("The page stays bright, but still takes no clicks.");
});
if ui.add(Button::new("Only the buttons close it")).clicked() {
state.sticky = true;
}
Dialog::new("sticky")
.title("Stays open")
.description("Neither Escape nor a click outside closes this one.")
.overlay_closable(false)
.keyboard(false)
.footer(|ui| {
if ui.add(Button::primary("Done")).clicked() {
ui.close();
}
})
.show(&ctx, &mut state.sticky, |_| {});
Alert dialog
if ui.add(Button::new("Discard Draft")).clicked() {
state.discard = true;
}
let answer = AlertDialog::new("discard")
.title("Discard the draft?")
.description("Your changes have not been saved.")
.confirm()
.ok_text("Discard")
.show(&ctx, &mut state.discard);
if answer.action == Some(AlertDialogAction::Ok) {
state.discarded += 1;
}
Destructive
if ui
.add(Button::destructive("Delete Account").style(&fastframe_ui::button_styles::Outline))
.clicked()
{
state.delete = true;
}
let answer = AlertDialog::new("delete-account")
.title("Are you absolutely sure?")
.description(
"This cannot be undone. It deletes your account and removes your data from our servers.",
)
.confirm()
.ok_text("Delete")
.destructive()
.show(&ctx, &mut state.delete);
if answer.action == Some(AlertDialogAction::Ok) {
state.deleted += 1;
}
Keyboard
if ui.add(Button::new("Review Notice")).clicked() {
state.notice = true;
}
AlertDialog::new("notice")
.title("Important Notice")
.description("Escape does not close this one: read it and press OK.")
.keyboard(false)
.show(&ctx, &mut state.notice);
Prevent close
if ui.add(Button::new("Close During Sync")).clicked() {
state.sync = true;
}
let synced = state.synced;
let answer = AlertDialog::new("sync")
.title("Sync in progress")
.description("Sync stays open after each run. Cancel closes it.")
.confirm()
.ok_text("Sync again")
.close_on_ok(false)
.show_with_body(&ctx, &mut state.sync, |ui| {
ui.label(format!("Synced {synced} times."));
});
if answer.action == Some(AlertDialogAction::Ok) {
state.synced += 1;
}
The story above is gallery/src/stories/dialog.rs, running in your browser. Every snippet on this page is cut from it.