macOS: File menu dialogs open as sheets, so a drag over the window can't abort the app (#511)
The file dialogs were blocking rfd::FileDialog calls made inside eframe's frame, that is inside winit's event handler. On macOS the dialog runs a nested modal event loop there; a file dragged over the window while it was open reached winit's handler again, which refuses re-entry with a panic across the Objective-C boundary, aborting the app (Import with Cmd+I, then a drag). Hooks gains file_dialog_async: a host that sets it shows a command's file dialog (Import, Open, Save As, Save Frame As, presets, guides, templates, Collect Files, Relink...) without blocking, and the command runs with the answer on a later frame (menus::dialog_answered, drained at the start of each frame); a cancelled dialog runs nothing. The macOS app implements it with rfd::AsyncFileDialog, a sheet on the window answered when it closes, outside the frame. Other platforms keep the blocking dialogs (winit queues their re-entrant events), as do the few panel buttons that pick a file in place (Settings paths, shortcut import/export, effect file pickers). Test: with a non-blocking hook, Save As and Save Frame As return at once without the blocking dialog, the save happens when the answer arrives from another thread, and a cancelled dialog writes nothing. The macOS sheet itself needs a manual check (Cmd+I, drag a file over the window, pick or cancel). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QiAJeFttZp83Fh7YuzAR5f
This commit is contained in:
@@ -98,6 +98,39 @@ pub enum Dialog {
|
||||
/// winit no theme). `None` = the host does not know.
|
||||
pub type SystemThemeFn = Box<dyn Fn(&egui::Context) -> Option<egui::Theme>>;
|
||||
|
||||
/// A file dialog a host shows without blocking the frame ([`Hooks::file_dialog_async`]).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum FileDialog {
|
||||
/// Files to open or import with these extensions; several when `many`.
|
||||
Open { exts: Vec<String>, many: bool },
|
||||
/// An EffectCraft project to open.
|
||||
OpenProject,
|
||||
/// A folder.
|
||||
Folder,
|
||||
/// Where to save `default` (a file name or path) with extension `ext` (empty: no filter);
|
||||
/// `project`: the project save dialog, given the file name.
|
||||
Save { default: String, ext: String, project: bool },
|
||||
}
|
||||
|
||||
impl FileDialog {
|
||||
/// The save dialog for `default`, as [`Hooks::save_dialog`] picks it.
|
||||
pub fn save(default: &str) -> Self {
|
||||
let ext = std::path::Path::new(default).extension().map(|e| e.to_string_lossy().to_string()).unwrap_or_default();
|
||||
Self::Save { default: default.to_string(), project: ext.eq_ignore_ascii_case("ecproj"), ext }
|
||||
}
|
||||
}
|
||||
|
||||
/// Called with the paths a [`FileDialog`] picked (empty: cancelled), from any thread.
|
||||
pub type DialogDone = Box<dyn FnOnce(Vec<String>) + Send>;
|
||||
|
||||
/// A non-blocking file dialog's answer for the command that asked (`menus::dialog_answered`).
|
||||
pub(crate) struct DialogAnswer {
|
||||
pub command: String,
|
||||
pub key: &'static str,
|
||||
pub params: serde_json::Value,
|
||||
pub paths: Vec<String>,
|
||||
}
|
||||
|
||||
/// Host hooks provided by the native app (file pickers etc.).
|
||||
#[derive(Default)]
|
||||
pub struct Hooks {
|
||||
@@ -121,6 +154,11 @@ pub struct Hooks {
|
||||
/// Application actions the OS performs (`app.hide`, `app.hideOthers`, `app.showAll` on
|
||||
/// macOS). Returns false when the host doesn't handle the id.
|
||||
pub app_action: Option<Box<dyn Fn(&str) -> bool>>,
|
||||
/// Shows a command's file dialog without blocking the frame and calls `done` with the
|
||||
/// answer (the host then asks for a repaint). Used in place of the blocking pickers above for
|
||||
/// the commands' dialogs (File ▸ Import, Open, Save As…) where a modal dialog's nested event
|
||||
/// loop inside a frame is unsafe: on macOS a drag over the window then aborted the app (#511).
|
||||
pub file_dialog_async: Option<Box<dyn Fn(FileDialog, DialogDone)>>,
|
||||
}
|
||||
|
||||
impl Hooks {
|
||||
@@ -264,6 +302,8 @@ pub struct EffectcraftApp {
|
||||
/// The graphics device was lost and the window is closing (see `close_after_device_loss`);
|
||||
/// `true` keeps crash recovery for the next launch.
|
||||
device_lost_close: Option<bool>,
|
||||
/// Answers of file dialogs shown without blocking ([`Hooks::file_dialog_async`]).
|
||||
pub(crate) dialog_answers: (Sender<DialogAnswer>, Receiver<DialogAnswer>),
|
||||
/// Commands from the native menu bar.
|
||||
pub command_inbox: Option<Receiver<String>>,
|
||||
/// Pointer position in comp pixels (Info panel).
|
||||
@@ -351,6 +391,7 @@ impl EffectcraftApp {
|
||||
gpu_failures: None,
|
||||
gpu_failure_applied: None,
|
||||
device_lost_close: None,
|
||||
dialog_answers: std::sync::mpsc::channel(),
|
||||
command_inbox: None,
|
||||
pointer_comp: None,
|
||||
dialog_state: Default::default(),
|
||||
@@ -667,6 +708,13 @@ impl EffectcraftApp {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
|
||||
/// Run the commands whose non-blocking file dialogs have been answered.
|
||||
pub(crate) fn run_dialog_answers(&mut self) {
|
||||
while let Ok(answer) = self.dialog_answers.1.try_recv() {
|
||||
menus::dialog_answered(self, answer);
|
||||
}
|
||||
}
|
||||
|
||||
/// Set up the GPU compositor on egui-wgpu's device (once). Without an adapter that runs
|
||||
/// compute shaders (WebGL2, software GL) everything stays on the CPU.
|
||||
pub(crate) fn init_gpu(&mut self, frame: &eframe::Frame) {
|
||||
@@ -1507,6 +1555,7 @@ impl EffectcraftApp {
|
||||
fn frame(&mut self, ui: &mut egui::Ui) {
|
||||
let ctx = ui.ctx().clone();
|
||||
self.apply_gpu_failure(&ctx);
|
||||
self.run_dialog_answers();
|
||||
self.auto.begin_frame();
|
||||
self.frames.set_context(&ctx);
|
||||
if self.session.render_job.is_some() {
|
||||
|
||||
Reference in New Issue
Block a user