Files
craft-libs/crates/dialogs/src/lib.rs
T
bflatastic 03ef0d0817 Merge remote-tracking branch 'origin/next' into sync/next
# Conflicts:
#	crates/automation/Cargo.toml
#	crates/automation/src/client.rs
#	crates/automation/src/control.rs
#	crates/automation/src/screenshot.rs
#	crates/automation/src/synthetic.rs
#	crates/automation/tests/it/main.rs
#	crates/automation/tests/it/synthetic.rs
#	crates/color-ui/src/palette.rs
#	crates/color-ui/src/ui/area.rs
#	crates/color-ui/src/ui/disc/stories.rs
#	crates/color-ui/src/ui/mod.rs
#	crates/color-ui/src/ui/paint_chip.rs
#	crates/color-ui/src/ui/pair/stories.rs
#	crates/color-ui/src/ui/palette/stories.rs
#	crates/color-ui/src/ui/panel.rs
#	crates/color-ui/src/ui/picker/stories.rs
#	crates/color-ui/src/ui/picker_dialog.rs
#	crates/color-ui/src/ui/swatches/library.rs
#	crates/color-ui/src/ui/swatches/panel.rs
#	crates/color-ui/src/ui/swatches/stories.rs
#	crates/color-ui/src/ui/wheel/stories.rs
#	crates/color-ui/tests/it/main.rs
#	crates/color-ui/tests/it/panels.rs
#	crates/color-ui/tests/it/picker.rs
#	crates/dialogs/src/lib.rs
#	crates/dialogs/src/native.rs
#	crates/dialogs/src/portal.rs
#	crates/dialogs/src/service.rs
#	crates/draw/src/ui/brush_presets/panel.rs
#	crates/draw/src/ui/brush_presets/picker.rs
#	crates/draw/src/ui/brush_presets/stories.rs
#	crates/draw/src/ui/gradient/palette/mod.rs
#	crates/draw/src/ui/pattern/editor.rs
#	crates/draw/src/ui/pen/stories.rs
#	crates/draw/src/ui/transform/mod.rs
#	crates/draw/src/ui/transform/transform_box.rs
#	crates/draw/src/ui/transform/transform_box/stories.rs
#	crates/draw/src/ui/transform/transform_fields.rs
#	crates/draw/tests/it/brush_presets.rs
#	crates/draw/tests/it/main.rs
#	crates/draw/tests/it/transform_box.rs
#	crates/font-manager/src/axes.rs
#	crates/font-manager/src/picker.rs
#	crates/font-manager/src/picker/rows.rs
#	crates/font-manager/src/picker/stories.rs
#	crates/font-manager/tests/it/main.rs
#	crates/font-manager/tests/it/picker.rs
#	crates/host/src/files.rs
#	crates/i18n/src/lib.rs
#	crates/imaging/src/ui/layer_style/styles.rs
#	crates/keymap/src/keymap.rs
#	crates/keymap/src/matcher.rs
#	crates/layers/src/ui/filter/mod.rs
#	crates/layers/src/ui/filter/stories.rs
#	crates/layers/src/ui/mock.rs
#	crates/layers/src/ui/panel/mod.rs
#	crates/layers/src/ui/panel/stories.rs
#	crates/layers/src/ui/tree.rs
#	crates/layers/tests/it/panel_drag_menu.rs
#	crates/layers/tests/it/panel_filter.rs
#	crates/menu/src/bar.rs
#	crates/menu/src/driver.rs
#	crates/menu/src/model.rs
#	crates/menu/src/shortcuts/pages.rs
#	crates/menu/src/shortcuts/state.rs
#	crates/menu/src/shortcuts/stories.rs
#	crates/menu/src/shortcuts/view.rs
#	crates/menu/tests/it/main.rs
#	crates/menu/tests/it/native.rs
#	crates/menu/tests/it/shortcuts/mod.rs
#	crates/pages/src/ui/mod.rs
#	crates/pages/src/ui/view.rs
#	crates/platform/README.md
#	crates/platform/src/clipboard_tests.rs
#	crates/platform/src/open.rs
#	crates/print/src/ui/stories.rs
#	crates/text/src/ui/character.rs
#	crates/text/src/ui/character/stories.rs
#	crates/text/src/ui/find.rs
#	crates/text/src/ui/find/dialog.rs
#	crates/text/src/ui/find/stories.rs
#	crates/text/src/ui/ruler.rs
#	crates/text/src/ui/ruler/paint.rs
#	crates/text/src/ui/ruler/run.rs
#	crates/text/tests/it/main.rs
#	crates/text/tests/it/ruler.rs
#	crates/ui-canvas/src/canvas/mod.rs
#	crates/ui-canvas/src/canvas/tool.rs
#	crates/ui-canvas/src/canvas/tool_host.rs
#	crates/ui-canvas/src/canvas_controls/grid/mod.rs
#	crates/ui-canvas/src/canvas_controls/grid/stories.rs
#	crates/ui-canvas/src/canvas_controls/guides/stories.rs
#	crates/ui-canvas/src/canvas_controls/task_bar.rs
#	crates/ui-canvas/src/canvas_controls/task_bar/stories.rs
#	crates/ui-canvas/tests/it/canvas/navigate.rs
#	crates/ui-canvas/tests/it/canvas/rulers.rs
#	crates/ui-canvas/tests/it/canvas/tool.rs
#	crates/ui-canvas/tests/it/canvas_controls/grid.rs
#	crates/ui-contract/src/contract/body/presses.rs
#	crates/ui-contract/src/contract/items/drag.rs
#	crates/ui-contract/src/contract/items/hooks.rs
#	crates/ui-contract/src/contract/items/lead.rs
#	crates/ui-contract/src/contract/overlay/placement.rs
#	crates/ui-contract/src/contract/overlay/props.rs
#	crates/ui-contract/src/contract/overlay/ui.rs
#	crates/ui-contract/src/contract/steps/label.rs
#	crates/ui-contract/src/contract/text.rs
#	crates/ui-contract/src/contract/tooltip.rs
#	crates/ui-contract/src/layout/body.rs
#	crates/ui-contract/src/primitives/draw.rs
#	crates/ui-contract/src/primitives/icon.rs
#	crates/ui-contract/src/primitives/mod.rs
#	crates/ui-contract/src/primitives/textures/thumbnails.rs
#	crates/ui-contract/src/runtime/commands/board.rs
#	crates/ui-contract/src/runtime/commands/mod.rs
#	crates/ui-contract/src/runtime/drag.rs
#	crates/ui-contract/src/runtime/egui/input.rs
#	crates/ui-contract/src/runtime/egui/plugin.rs
#	crates/ui-contract/src/runtime/frame.rs
#	crates/ui-contract/src/runtime/keys.rs
#	crates/ui-contract/src/runtime/keys/input.rs
#	crates/ui-contract/src/runtime/keys/route.rs
#	crates/ui-contract/src/runtime/pointer.rs
#	crates/ui-contract/src/runtime/wheel.rs
#	crates/ui-contract/src/runtime/windows.rs
#	crates/ui-contract/src/runtime/windows/viewport.rs
#	crates/ui-contract/src/style/cx.rs
#	crates/ui-contract/src/style/span.rs
#	crates/ui-contract/src/testing/access.rs
#	crates/ui-contract/src/testing/icon_trials.rs
#	crates/ui-contract/tests/it/body/mod.rs
#	crates/ui-contract/tests/it/runtime/mod.rs
#	crates/ui-contract/tests/it/runtime/wheel.rs
#	crates/ui-contract/tests/it/runtime/windows.rs
#	crates/ui-contract/tests/it/text/editor.rs
#	crates/ui-gallery/src/mock/panels.rs
#	crates/ui-panels/src/dialog/mod.rs
#	crates/ui-panels/src/docking/group.rs
#	crates/ui-panels/src/rails/drawer/stories.rs
#	crates/ui-panels/src/rails/tool.rs
#	crates/ui-panels/src/rails/tool/stories.rs
#	crates/ui-panels/src/rails/tool/trials.rs
#	crates/ui-panels/src/sections/footer.rs
#	crates/ui-panels/tests/it/dialogs.rs
#	crates/ui-patterns/src/patterns/dock/hide.rs
#	crates/ui-patterns/src/patterns/dock/panes.rs
#	crates/ui-patterns/src/patterns/form_dialogs/dialog.rs
#	crates/ui-patterns/src/patterns/form_dialogs/dialog/stories.rs
#	crates/ui-patterns/src/patterns/form_dialogs/list_manager.rs
#	crates/ui-patterns/src/patterns/form_dialogs/list_manager/stories.rs
#	crates/ui-patterns/src/patterns/form_dialogs/mod.rs
#	crates/ui-patterns/src/patterns/form_dialogs/new_document.rs
#	crates/ui-patterns/src/patterns/form_dialogs/new_document/cards.rs
#	crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs
#	crates/ui-patterns/src/patterns/form_dialogs/param.rs
#	crates/ui-patterns/src/patterns/form_dialogs/pdf_export.rs
#	crates/ui-patterns/src/patterns/form_dialogs/pdf_export/stories.rs
#	crates/ui-patterns/src/patterns/form_dialogs/report.rs
#	crates/ui-patterns/src/patterns/form_dialogs/report/stories.rs
#	crates/ui-patterns/src/patterns/form_dialogs/rows.rs
#	crates/ui-patterns/src/patterns/form_dialogs/save_for_web.rs
#	crates/ui-patterns/src/patterns/form_dialogs/save_for_web/hosted.rs
#	crates/ui-patterns/src/patterns/form_dialogs/save_for_web/model.rs
#	crates/ui-patterns/src/patterns/form_dialogs/save_for_web/options.rs
#	crates/ui-patterns/src/patterns/form_dialogs/save_for_web/stories.rs
#	crates/ui-patterns/src/patterns/panels/palette.rs
#	crates/ui-patterns/src/patterns/panels/palette/stories.rs
#	crates/ui-patterns/src/patterns/preferences/document/model.rs
#	crates/ui-patterns/src/patterns/preferences/page.rs
#	crates/ui-patterns/src/patterns/preferences/window.rs
#	crates/ui-patterns/src/patterns/preferences/window/stories.rs
#	crates/ui-patterns/src/patterns/presets/browser.rs
#	crates/ui-patterns/src/patterns/presets/browser/stories.rs
#	crates/ui-patterns/src/patterns/presets/manager.rs
#	crates/ui-patterns/src/patterns/presets/mod.rs
#	crates/ui-patterns/src/patterns/presets/named.rs
#	crates/ui-patterns/src/patterns/presets/picker.rs
#	crates/ui-patterns/tests/it/docking/drop_session.rs
#	crates/ui-patterns/tests/it/docking/native.rs
#	crates/ui-patterns/tests/it/form_dialogs/hosted.rs
#	crates/ui-patterns/tests/it/form_dialogs/list_manager.rs
#	crates/ui-patterns/tests/it/form_dialogs/mod.rs
#	crates/ui-patterns/tests/it/form_dialogs/params.rs
#	crates/ui-patterns/tests/it/form_dialogs/save_for_web.rs
#	crates/ui-patterns/tests/it/panels/palette.rs
#	crates/ui-patterns/tests/it/presets/browser.rs
#	crates/ui-shell/src/shell/app_shell.rs
#	crates/ui-shell/src/shell/app_shell/stories.rs
#	crates/ui-shell/src/shell/mod.rs
#	crates/ui-shell/src/shell/start.rs
#	crates/ui-shell/src/shell/start/stories.rs
#	crates/ui-shell/tests/it/commands/mod.rs
#	crates/ui-shell/tests/it/shell/app_shell.rs
#	crates/ui-shell/tests/it/shell/mod.rs
#	crates/ui-widgets/src/bars/bar.rs
#	crates/ui-widgets/src/buttons/button.rs
#	crates/ui-widgets/src/buttons/face.rs
#	crates/ui-widgets/src/buttons/mod.rs
#	crates/ui-widgets/src/buttons/stories.rs
#	crates/ui-widgets/src/choice/mod.rs
#	crates/ui-widgets/src/choice/swatch.rs
#	crates/ui-widgets/src/choice/swatch/stories.rs
#	crates/ui-widgets/src/choice/swatch_table.rs
#	crates/ui-widgets/src/choice/swatch_table/stories.rs
#	crates/ui-widgets/src/display/text.rs
#	crates/ui-widgets/src/list/row.rs
#	crates/ui-widgets/src/list/row/paint.rs
#	crates/ui-widgets/src/list/stories.rs
#	crates/ui-widgets/src/list/view_stories.rs
#	crates/ui-widgets/src/menu/bar.rs
#	crates/ui-widgets/src/menu/item.rs
#	crates/ui-widgets/src/menu/search.rs
#	crates/ui-widgets/src/menu/stories.rs
#	crates/ui-widgets/src/pickers/mod.rs
#	crates/ui-widgets/src/pickers/reference_point.rs
#	crates/ui-widgets/src/pickers/reference_point/stories.rs
#	crates/ui-widgets/src/tabs/strip.rs
#	crates/ui-widgets/src/text/number.rs
#	crates/ui-widgets/src/text/number/stories.rs
#	crates/ui-widgets/src/tile_grid/grid.rs
#	crates/ui-widgets/tests/it/buttons.rs
#	crates/ui-widgets/tests/it/label/mod.rs
#	crates/ui-widgets/tests/it/label/tooltip.rs
#	crates/ui-widgets/tests/it/main.rs
#	crates/ui-widgets/tests/it/menu/bar.rs
#	crates/ui-widgets/tests/it/menu/mod.rs
#	crates/ui-widgets/tests/it/menu/rows.rs
#	crates/ui-widgets/tests/it/menu/search.rs
#	crates/ui-widgets/tests/it/pickers/select_list.rs
#	crates/ui-widgets/tests/it/text/mod.rs
2026-11-22 01:16:36 +07:00

448 lines
15 KiB
Rust

#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
//! Native only (rfd does not build for the web): file and message dialogs that never block the event loop.
//!
//! A blocking `rfd::FileDialog` or `rfd::MessageDialog` runs AppKit's modal loop
//! (`-[NSSavePanel runModal]`) inside winit's event handler on macOS. An event that arrives while
//! that loop spins (a file dragged over the window, the panel resized) re-enters the handler, winit
//! panics with "tried to handle event while another event is currently being handled", and since
//! the panic can't unwind out of the AppKit callback, the app aborts.
//!
//! [`Dialogs`] never blocks: [`Dialogs::ask`] and [`Dialogs::ask_message`] queue a request,
//! [`Dialogs::show_pending`] shows the first one at the end of the frame on the main thread, and
//! [`Dialogs::take_answers`] hands back `(then, Answer)` pairs on a later frame. `then` is the
//! app's own continuation (a command to re-run, a field to fill), so each app keeps its command
//! model. One dialog shows at a time; later requests wait in order.
//!
//! On macOS, Open and Folder show as a free-standing panel that doesn't block the window, as
//! native apps do; Save and message boxes show as a sheet on the parent window. Elsewhere every
//! dialog is a modal owned by the parent window.
#![warn(missing_docs)]
mod filter;
mod native;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))]
pub(crate) mod portal;
pub(crate) mod probe;
pub mod save;
#[cfg(test)]
pub(crate) mod save_tests;
pub mod service;
#[cfg(feature = "ui")]
pub mod ui;
/// The registrations of this crate's UI types, for its integration binary.
#[cfg(feature = "screen-testing")]
pub mod testing {
#[cfg(feature = "suite")]
craft_ui::registry!();
}
#[cfg(target_os = "macos")]
mod macos;
#[cfg(test)]
pub(crate) mod tests;
use std::collections::VecDeque;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::PathBuf;
use std::sync::mpsc::{self, Receiver, Sender, TryRecvError};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
pub use filter::Filter;
/// Wakes the app's event loop so it runs a frame (`ctx.request_repaint()` in an egui app). Called
/// from any thread when a dialog has answered or stopped.
pub use craft_host::Wake;
/// Runs around a native dialog's blocking show: it gets the show as a closure and calls it once, so the app can mark
/// the wait as waiting for the user.
pub type ShowHook = Box<dyn FnMut(&mut dyn FnMut())>;
/// Answers every request instead of a native dialog (tests and automation).
pub type Override = Box<dyn FnMut(&Request) -> Answer>;
/// Which file dialog to show.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Kind {
/// Choose existing files: one, or several when `multiple`.
Open {
/// Whether the user may choose more than one file.
multiple: bool,
},
/// Choose one folder.
Folder,
/// Choose where to write a file.
Save,
}
/// A file dialog request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileDialog {
/// Which dialog to show.
pub kind: Kind,
/// The dialog's title, or its message line on macOS, where panels have no title bar.
pub title: Option<String>,
/// The file types to offer. Extensions match in any letter case on every platform.
pub filters: Vec<Filter>,
/// The folder the dialog starts in.
pub directory: Option<PathBuf>,
/// The file name a Save dialog suggests.
pub file_name: Option<String>,
}
impl FileDialog {
/// A dialog of `kind` with nothing else set.
pub fn new(kind: Kind) -> Self {
Self { kind, title: None, filters: Vec::new(), directory: None, file_name: None }
}
/// Choose one existing file.
pub fn open() -> Self {
Self::new(Kind::Open { multiple: false })
}
/// Choose one or more existing files.
pub fn open_many() -> Self {
Self::new(Kind::Open { multiple: true })
}
/// Choose a folder.
pub fn folder() -> Self {
Self::new(Kind::Folder)
}
/// Choose where to save.
pub fn save() -> Self {
Self::new(Kind::Save)
}
/// Sets the dialog's title (its message line on macOS).
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Adds a file type: `name` as the dialog lists it, `extensions` without the dot.
pub fn filter(mut self, name: impl Into<String>, extensions: &[&str]) -> Self {
self.filters.push(Filter::new(name, extensions));
self
}
/// Sets the folder the dialog opens in.
pub fn directory(mut self, directory: impl Into<PathBuf>) -> Self {
self.directory = Some(directory.into());
self
}
/// Sets the file name a Save dialog suggests.
pub fn file_name(mut self, file_name: impl Into<String>) -> Self {
self.file_name = Some(file_name.into());
self
}
}
/// The icon a message box shows.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MessageLevel {
/// An informational icon.
#[default]
Info,
/// A warning icon.
Warning,
/// An error icon.
Error,
}
/// The buttons a message box offers. The custom variants name the buttons, in the order of the
/// plain variant they replace.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum MessageButtons {
/// One OK button.
#[default]
Ok,
/// OK and Cancel.
OkCancel,
/// Yes and No.
YesNo,
/// Yes, No and Cancel.
YesNoCancel,
/// One button with this label.
OkCustom(String),
/// Two buttons with these labels, in the order of OK and Cancel.
OkCancelCustom(String, String),
/// Three buttons with these labels, in the order of Yes, No and Cancel.
YesNoCancelCustom(String, String, String),
}
/// The button a message box was closed with. A custom button answers with its label.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MessageResult {
/// OK.
Ok,
/// Cancel.
Cancel,
/// Yes.
Yes,
/// No.
No,
/// A custom button, by its label.
Custom(String),
}
/// A message box request.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Message {
/// The icon.
pub level: MessageLevel,
/// The box's title.
pub title: String,
/// The body text.
pub description: String,
/// The buttons offered.
pub buttons: MessageButtons,
}
/// What [`Dialogs`] was asked to show, as an [`Override`] sees it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Request {
/// A file dialog.
File(FileDialog),
/// A message box.
Message(Message),
}
/// How a dialog ended.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Answer {
/// The user closed a file dialog without choosing.
Cancelled,
/// The chosen files or folder.
Paths(Vec<PathBuf>),
/// A Save panel's answer: the name as typed and the file type picked, when the panel reports it (the portal's
/// current filter; rfd reports `None`).
Saved {
/// The name as typed.
path: PathBuf,
/// The index of the picked filter, when the panel reports it.
filter: Option<usize>,
},
/// The button a message box was closed with.
Message(MessageResult),
/// No dialog could be shown, or it stopped without an answer; the text says why. Report it as
/// an error rather than taking it for Cancel.
Unavailable(String),
}
/// The answer of a dialog whose worker ended without answering (it panicked, or couldn't start).
pub(crate) const STOPPED: &str = "the dialog stopped without an answer";
/// Hands one dialog's answer back to [`Dialogs`] from any thread, and wakes the app. Dropped
/// without an answer (a worker that panicked or never started), it still wakes the app, which then
/// answers [`Answer::Unavailable`], so a dialog never stays open for good.
pub(crate) struct Reply {
tx: Option<Sender<Answer>>,
wake: Wake,
}
impl Reply {
pub(crate) fn send(mut self, answer: Answer) {
if let Some(tx) = self.tx.take() {
// The app gone meanwhile has no use for the answer.
let _ = tx.send(answer);
}
}
}
impl Drop for Reply {
fn drop(&mut self) {
// The sender goes first, so the woken app sees either the answer or the disconnect.
self.tx = None;
self.wake.wake();
}
}
/// Who shows a request.
enum Show {
Native,
Override(Override),
/// Tests: a stand-in for the native dialog that answers (or doesn't) through the reply.
#[cfg(test)]
Custom(Box<dyn FnMut(Request, Reply) -> native::Shown>),
}
/// The dialog on screen.
struct Open<T> {
then: T,
answer: Receiver<Answer>,
shown: native::Shown,
}
/// The app's dialogs: a queue of requests, the one on screen, and the answers not yet taken.
///
/// `T` is the app's continuation for an answer. It stays on the main thread: no `Send` bound.
pub struct Dialogs<T> {
wake: Wake,
queue: VecDeque<(Request, T)>,
open: Option<Open<T>>,
answers: Vec<(T, Answer)>,
show: Show,
hook: Option<ShowHook>,
}
impl<T> Dialogs<T> {
/// A dialog queue that calls `wake` when an answer arrives.
pub fn new(wake: Wake) -> Self {
Self { wake, queue: VecDeque::new(), open: None, answers: Vec::new(), show: Show::Native, hook: None }
}
/// Runs `hook` around every native dialog's blocking show.
pub fn set_show_hook(&mut self, hook: ShowHook) {
self.hook = Some(hook);
}
/// Queues a file dialog. It shows at the next [`Self::show_pending`] once the dialogs asked
/// before it have answered; its answer comes back with `then`.
pub fn ask(&mut self, dialog: FileDialog, then: T) {
self.push(Request::File(dialog), then);
}
/// Queues a message box, like [`Self::ask`].
pub fn ask_message(&mut self, message: Message, then: T) {
self.push(Request::Message(message), then);
}
fn push(&mut self, request: Request, then: T) {
if self.open.is_some() {
self.raise();
}
self.queue.push_back((request, then));
self.wake.wake();
}
/// Shows the next queued request unless a dialog is open. Call it on the main thread at the end
/// of every frame, with the window as `parent`. On macOS, Save and message boxes need the
/// parent (they show as its sheet) and answer [`Answer::Unavailable`] without one.
pub fn show_pending<W: HasWindowHandle + HasDisplayHandle>(&mut self, parent: Option<&W>) {
self.poll();
while self.open.is_none() {
let Some((request, then)) = self.queue.pop_front() else { return };
let (tx, answer) = mpsc::channel();
let reply = Reply { tx: Some(tx), wake: self.wake.clone() };
// A panic while showing (rfd's own checks) must not unwind into winit's handler.
let shown = match &mut self.show {
Show::Override(answer_with) => {
catch_unwind(AssertUnwindSafe(|| reply.send(answer_with(&request)))).map(|()| native::Shown::default())
}
Show::Native => catch_unwind(AssertUnwindSafe(|| {
let (mut asked, mut shown) = (Some((request, reply)), None);
let mut run = || {
if let Some((request, reply)) = asked.take() {
shown = Some(native::show(request, parent, reply));
}
};
match &mut self.hook {
Some(hook) => hook(&mut run),
None => run(),
}
shown.unwrap_or_default()
})),
#[cfg(test)]
Show::Custom(show) => catch_unwind(AssertUnwindSafe(|| show(request, reply))),
};
let shown = match shown {
Ok(shown) => shown,
Err(panic) => {
let why = panic_text(panic.as_ref()).unwrap_or(STOPPED);
self.answers.push((then, Answer::Unavailable(format!("the dialog couldn't be shown: {why}"))));
continue;
}
};
self.open = Some(Open { then, answer, shown });
self.poll();
}
}
/// The answers that came in since the last call, in the order the dialogs were asked.
pub fn take_answers(&mut self) -> Vec<(T, Answer)> {
self.poll();
std::mem::take(&mut self.answers)
}
/// A dialog is on screen, or its answer hasn't been collected yet (by [`Self::show_pending`]
/// or [`Self::take_answers`]).
pub fn is_open(&self) -> bool {
self.open.is_some()
}
/// Brings the open free-standing panel (Open or Folder on macOS) to the front, for a command
/// that would show it again. Does nothing elsewhere, where dialogs are modal to the window.
pub fn raise(&self) {
#[cfg(target_os = "macos")]
if let Some(panel) = self.open.as_ref().and_then(|open| open.shown.panel.as_ref()) {
macos::raise(panel);
}
}
/// Requests waiting behind the open dialog.
pub fn queued(&self) -> usize {
self.queue.len()
}
/// Answers every request with `answer_with` instead of a native dialog (tests and automation);
/// `None` shows native dialogs again. The answers still come back from
/// [`Self::take_answers`], after [`Self::show_pending`], like real ones.
pub fn set_override(&mut self, answer_with: Option<Override>) {
self.show = answer_with.map_or(Show::Native, Show::Override);
}
/// Stops the open dialog when `gone` says nobody waits for its answer any more (its pick was
/// forgotten): a portal Save closes. Other dialogs cannot be closed from outside; their answer
/// comes as usual and the caller drops it.
pub(crate) fn cancel_open_if(&mut self, gone: impl FnOnce(&T) -> bool) {
if let Some(open) = &mut self.open
&& gone(&open.then)
{
open.shown.cancel_save();
}
}
/// Tests: show requests through `show` instead of a native dialog.
#[cfg(test)]
fn set_custom(&mut self, show: impl FnMut(Request, Reply) -> native::Shown + 'static) {
self.show = Show::Custom(Box::new(show));
}
/// Moves the open dialog's answer, once it is in, to the answers.
fn poll(&mut self) {
let Some(open) = &self.open else { return };
let answer = match open.answer.try_recv() {
Ok(answer) => answer,
Err(TryRecvError::Empty) => return,
Err(TryRecvError::Disconnected) => Answer::Unavailable(STOPPED.into()),
};
if let Some(open) = self.open.take() {
self.answers.push((open.then, answer));
}
}
}
fn panic_text(panic: &(dyn std::any::Any + Send)) -> Option<&str> {
panic.downcast_ref::<&str>().copied().or_else(|| panic.downcast_ref::<String>().map(String::as_str))
}
#[cfg(all(test, feature = "screen-testing"))]
extern crate self as craft_dialogs;
#[cfg(all(test, feature = "screen-testing"))]
#[path = "../tests/it/main.rs"]
pub(crate) mod integration;
#[cfg(test)]
fn main() {
craft_ui::registry!();
craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::Registry::of(&[register]));
}