EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge #1762: Fix #341: on macOS, a quit goes through `terminate:`
dotCooCooe04ec3da1a08
diff --cc Cargo.lockindex 6911da6a5,325893ce2..55948b2fe--- a/Cargo.lock+++ b/Cargo.lock@@@ -1049,6 -991,17 +1049,17 @@@ dependencies ="thiserror 2.0.21",]+ [[package]]+ name = "craft-quit"+ version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=82a24b006eefa036b43c09a67fb9a3fbd17ffe63#82a24b006eefa036b43c09a67fb9a3fbd17ffe63"++source = "git+https://github.com/storytold/craft-libs?rev=1e6f127844ec61b3b8afdcc2bb76e7cfe470153f#1e6f127844ec61b3b8afdcc2bb76e7cfe470153f"+ dependencies = [+ "dispatch2",+ "egui",- "objc2 0.6.4",++ "objc2 0.6.5",+ "objc2-app-kit 0.3.2",+ ]+[[package]]name = "craft-ui"version = "0.1.0"@@@ -1774,11 -1672,11 +1785,12 @@@ dependencies =[[package]]name = "effectcraft-ui-egui"-version = "0.16.0"+version = "0.17.0"dependencies = ["chrono",+ "craft-quit","craft-ui",+ "craft-update","effectcraft-color","effectcraft-engine","effectcraft-geom",diff --cc Cargo.tomlindex 0e1fd2cd5,b26720cf9..2793695b4--- a/Cargo.toml+++ b/Cargo.toml@@@ -11,9 -11,9 +11,10 @@@ rust-version = "1.95repository = "https://github.com/storytold/effectcraft"[workspace.dependencies]-craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "9f75d3dc19b4b514c4f3d4847ed1ec1b0e86b4ef", version = "=0.1.0", default-features = false, features = ["read"] }-craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "82a24b006eefa036b43c09a67fb9a3fbd17ffe63" }-craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "82a24b006eefa036b43c09a67fb9a3fbd17ffe63" }+craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f", version = "=0.1.0", default-features = false, features = ["read"] }+craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f" }+craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f", features = ["egui"] }++craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1e6f127844ec61b3b8afdcc2bb76e7cfe470153f" }# internaleffectcraft-time = { path = "crates/time" }effectcraft-geom = { path = "crates/geom" }diff --cc crates/ui-egui/src/lib.rsindex c21dfef1a,5a34927b2..55b5a0bd3--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@@ -2204,37 -1620,11 +2204,40 @@@ impl EffectcraftApp}}- fn frame(&mut self, ui: &mut egui::Ui) {- let ctx = ui.ctx().clone();- self.apply_gpu_failure(&ctx);- self.auto.begin_frame();- self.frames.set_context(&ctx);+ /// The app's only non-paint entry: one tick of background work.+ ///+ /// This runs for every eframe logic callback, including the logic-only ticks of a hidden+ /// window (which never reach [`Self::draw`]). It must therefore do no input handling, no+ /// layout and no presentation: engine/job queues, autosave, playback, audio, the control+ /// channel, screenshots and the close guard. Everything here is idempotent for a pass that+ /// `Context::request_discard` replays.+ pub fn tick(&mut self, ctx: &egui::Context, frame: &eframe::Frame) {+ self.now = self.clock.now();+ // The compositor exists before the first paint and while hidden (device-loss handling+ // must run there too).+ self.init_gpu(frame);+ self.apply_gpu_failure(ctx);+ if self.gpu_failure_applied == Some(true) {+ self.close_after_device_loss(ctx);++ // On macOS the app quits through `-[NSApplication terminate:]` instead of closing the++ // window while AppKit's run loop is running (#341).++ craft_quit::on_close_request(ctx);+ return;+ }+ // Focus comes from the fresh viewport copy; a full pass's `InputState::focused` is only a+ // fallback (a logic-only tick leaves it at the previous pass's value).+ let focused = ctx.input(|i| i.viewport().focused.or(Some(i.focused)));+ self.focused = focused.unwrap_or(self.focused);+ // The interface language's fallback fonts are installed in `draw` (fonts need a pass), but+ // commands handled here use the right catalog already.+ let language = crate::i18n::language(self);+ crate::i18n::set_current(language);+ if self.styled_language != language {+ self.want_paint = true;+ }+ // Frames and worker completions wake the app even while it is hidden.+ self.frames.set_context(ctx);+ updates_ui::tick(self, ctx);if self.session.render_job.is_some() {self.session.poll_render();ctx.request_repaint_after(std::time::Duration::from_millis(100));@@@ -2285,181 -1682,12 +2288,183 @@@}self.command_inbox = Some(rx);}- let tab_had_widget_focus = ctx.egui_wants_keyboard_input();- menus::handle_shortcuts(self, &ctx);- if !ctx.input(|i| i.pointer.primary_down()) {- self.ui.viewer.property_interacting = false;- }- let t = self.tokens;+ // A close request (the window's button, Quit, `app.quit`) must be answered while hidden.- panels::unsaved::on_close_requested(self, ctx);++ // On macOS a close that nothing held open quits through `-[NSApplication terminate:]`++ // (#341).++ panels::unsaved::close_or_quit(self, ctx);+ let title = panels::unsaved::window_title(self);+ if title != self.window_title {+ ctx.send_viewport_cmd(egui::ViewportCommand::Title(title.clone()));+ self.window_title = title;+ }+ self.tick_autosave(ctx);+ self.tick_workspace_autosave(ctx);+ let now = self.now;+ prefs_live::tick(self, ctx, now);+ // Playback advances without a paint (its frames and the audio clock are background work).+ // Idle prefetch keeps its old condition: it only runs once a viewer has drawn.+ if (self.playback.playing || self.last_viewer_scale.is_some())+ && let Some(cid) = self.session.active_comp_id()+ {+ let (scale, active) = (self.last_viewer_scale.unwrap_or(1.0), self.input_active);+ self.advance_playback(ctx, scale, now, active, cid);+ }+ self.drain_control(ctx, now);+ // Screenshot completion, deadlines and issuance are app-side work: the callback only+ // stores pixels and wakes this viewport.+ self.tick_screenshots(ctx);+ // Time-based FPS readout over UI passes (a hidden logic-only tick is not a frame).+ let dt = (now - self.last_time) as f32;+ if dt > 0.0 && dt < 2.0 {+ self.fps = self.fps * 0.9 + (1.0 / dt).min(480.0) * 0.1;+ }+ self.last_time = now;+ // A hidden window paints when work needs a UI pass: queued input and waits, a settled+ // screenshot, or playback into a child viewport.+ let focused = self.focused;+ let child_viewports = !self.ui.dock.layout.floating.is_empty() || prefs_live::video_preview_active(self, focused);+ let paint = self.want_paint+ || !self.synthetic.is_empty()+ || !self.input_waiters.is_empty()+ || self.pending_screenshots.iter().any(|s| s.slot.is_none())+ || (self.playback.playing && child_viewports);+ if paint {+ self.request_hidden_paint(ctx);+ }+ }++ /// Ask the host for a UI pass even when the window is minimized or occluded.+ ///+ /// Native eframe honours `RequestPaintWhileHidden`; the shared web backend does not carry it+ /// yet (a browser does not paint a hidden tab), so the web build only requests a normal+ /// repaint and hidden web screenshots/input remain unqualified.+ fn request_hidden_paint(&mut self, ctx: &egui::Context) {+ self.want_paint = false;+ #[cfg(not(target_arch = "wasm32"))]+ ctx.send_viewport_cmd(egui::ViewportCommand::RequestPaintWhileHidden);+ ctx.request_repaint();+ }++ /// Install the theme and fonts for the current language. Fonts need a real pass: the pass+ /// that installs them draws nothing, and the next pass draws with them (the two-pass warm-up+ /// the app has always had). Never call this from a logic-only tick.+ fn ui_setup(&mut self, ctx: &egui::Context) {+ panels::viewer_text::install_hooks(ctx);+ let language = crate::i18n::language(self);+ crate::i18n::set_current(language);+ if (!self.styled || self.styled_language != language) && self.synthetic_in_flight {+ // The install needs a pass that draws nothing, but this pass has already applied a+ // synthetic batch: defer the theme/font install by one pass instead of dropping the+ // input (feeds are only put into a pass that can draw).+ ctx.request_repaint();+ return;+ }+ if !self.styled || self.styled_language != language {+ theme::install(ctx, &self.tokens, language);+ self.styled_language = language;+ if !self.styled {+ fit_window(ctx);+ self.styled = true;+ }+ self.fonts_ready = false;+ ctx.request_repaint();+ } else if !self.fonts_ready {+ // The pass after the install draws: egui has the new font definitions by now.+ self.fonts_ready = true;+ ctx.request_repaint();+ }+ }++ /// One UI pass: setup, input-driven shortcuts, layout and painting. Called from+ /// `eframe::App::ui` and by tests with the root `Ui`.+ pub fn draw(&mut self, ui: &mut egui::Ui, frame: &eframe::Frame) {+ panels::viewer_text::begin_ui(ui.ctx());+ self.draw_inner(ui, frame);+ // All exits, including fonts/device loss, observe this pass's real custom availability.+ panels::viewer_text::finish_ime(self, ui);+ }++ /// Context creators install these hooks before the first full pass.+ pub fn install_ime_reset_hooks(ctx: &egui::Context) {+ panels::viewer_text::install_hooks(ctx);+ }++ /// Host wrappers call this after their last logic mutation, without replaying UI events.+ pub fn finish_ime_logic(&self, ctx: &egui::Context) {+ panels::viewer_text::finish_logic(self, ctx);+ }++ fn draw_inner(&mut self, ui: &mut egui::Ui, frame: &eframe::Frame) {+ let ctx = ui.ctx().clone();+ if self.gpu_failure_applied == Some(true) {+ self.close_after_device_loss(&ctx);+ return;+ }+ self.ui_setup(&ctx);+ if !self.fonts_ready {+ // A pass that installs fonts draws nothing. No synthetic batch is in flight here (the+ // hook feeds input only to a ready app), and events egui already applied are never+ // fed again: that would double the pointer/key state of one click.+ ctx.request_repaint();+ return;+ }+ self.frame(ui);+ #[cfg(not(target_arch = "wasm32"))]+ pointer_wrap::finish_native(&ctx, frame);+ // wasm32: no frame threads; render queued frames now, between UI frames.+ if cfg!(target_arch = "wasm32") && self.frames.pump(std::time::Duration::from_millis(if self.playback.playing { 24 } else { 40 })) {+ ctx.request_repaint();+ }+ // Input belongs to this known UI phase: record it for the next tick.+ let active = ctx.input(|i| !i.events.is_empty() || i.pointer.any_down() || i.pointer.is_moving());+ self.input_active = active;+ self.last_pointer_down = ctx.input(|i| i.pointer.any_down());+ // Geometry of a completed pass (a discarded pass is laid out again; only the final one+ // describes the surface the next capture will see). Window position is irrelevant.+ if !ctx.will_discard() {+ let rect = ctx.content_rect();+ let ppp = ctx.pixels_per_point();+ let changed = self.draw_geom.is_none_or(|g| g.rect.size() != rect.size() || g.ppp != ppp);+ if changed {+ self.geometry_epoch = self.geometry_epoch.wrapping_add(1);+ }+ self.draw_geom = Some(DrawGeometry { rect, ppp, epoch: self.geometry_epoch });+ self.ui_pass_seq = self.ui_pass_seq.saturating_add(1);+ }+ // A batch is acknowledged once a UI pass applied it, even if layout was later discarded.+ self.mark_synthetic_applied();+ // `AfterInput` replies wait for the final (non-discarded) pass that applied the last+ // synthetic event: a hidden request is answered only after its forced pass, and a pass+ // egui throws away cannot produce an early success reply.+ if !ctx.will_discard() && self.synthetic.is_empty() {+ for w in self.input_waiters.drain(..) {+ let _ = w.send(json!({"ok": true, "result": null}));+ }+ } else if !self.input_waiters.is_empty() {+ self.want_paint = true;+ ctx.request_repaint();+ }+ if self.frames.inflight() > 0 {+ ctx.request_repaint_after(std::time::Duration::from_millis(30));+ }+ }++ /// The panels, menus, dialogs and overlays of one pass.+ fn frame(&mut self, ui: &mut egui::Ui) {+ let ctx = ui.ctx().clone();+ // Only a viewer that draws in *this* pass records a scale: a closed viewer, the Home+ // screen or another comp must stop idle prefetch at a stale scale.+ self.last_viewer_scale = None;+ self.apply_prefs(&ctx);+ // Video Preview is a second viewport: only a real pass can update it.+ prefs_live::frame(self, &ctx);+ self.auto.begin_frame();+ let tab_had_widget_focus = ctx.egui_wants_keyboard_input();+ menus::handle_shortcuts(self, &ctx);+ if !ctx.input(|i| i.pointer.primary_down()) {+ self.ui.viewer.property_interacting = false;+ }+ let t = self.tokens;let full = ui.max_rect();if self.dialog.is_some() {ui.disable();