diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index d4925756c..549154ba7 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -188,11 +188,7 @@ impl eframe::App for EffectcraftApp { /// refreshed. A close request must be answered while hidden too; an answered file dialog can change the project, /// so the tick's dialog answers run first. fn logic(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { - // Never `ctx.input(|i| i.time)` here: `Context::run_logic` leaves events, pointer state and - // time at the previous full pass's values (only the viewport information is refreshed). - self.tick(ctx, frame); - self.finish_ime_logic(ctx); - self.close_path(ctx); + self.logic_pass(ctx, frame); } /// Draw one UI pass through `draw`. diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index d93b5673b..065109b9f 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -167,6 +167,15 @@ impl EffectcraftApp { } } + /// The shared app's `App::logic`: `tick`, the IME pass's close and, last, the root window's close path. + pub(crate) fn logic_pass(&mut self, ctx: &egui::Context, frame: &eframe::Frame) { + // Never `ctx.input(|i| i.time)` here: `Context::run_logic` leaves events, pointer state and + // time at the previous full pass's values (only the viewport information is refreshed). + self.tick(ctx, frame); + self.finish_ime_logic(ctx); + self.close_path(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 diff --git a/crates/ui-egui/src/bench.rs b/crates/ui-egui/src/bench.rs index 567588de4..23054a863 100644 --- a/crates/ui-egui/src/bench.rs +++ b/crates/ui-egui/src/bench.rs @@ -20,7 +20,9 @@ pub struct Driver { impl Driver { pub fn new(app: EffectcraftApp, size: egui::Vec2) -> Driver { - Driver { ctx: egui::Context::default(), app, frame: eframe::Frame::_new_kittest(), time: 0.0, size } + let ctx = egui::Context::default(); + crate::framework::install_headless(&ctx); + Driver { ctx, app, frame: eframe::Frame::_new_kittest(), time: 0.0, size } } /// Run one frame (layout + paint + tessellation); its wall time in ms. @@ -30,10 +32,11 @@ impl Driver { pub fn step_with(&mut self, events: Vec) -> f64 { self.time += 1.0 / 60.0; - let input = + let mut input = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, self.size)), time: Some(self.time), events, ..Default::default() }; let t0 = web_time::Instant::now(); let (app, frame) = (&mut self.app, &mut self.frame); + eframe::App::raw_input_hook(app, &self.ctx, &mut input); let mut out = self.ctx.run_ui(input, |ui| { eframe::App::logic(app, ui.ctx(), frame); eframe::App::ui(app, ui, frame); diff --git a/crates/ui-egui/src/lifecycle_tests.rs b/crates/ui-egui/src/lifecycle_tests.rs index ab02aff37..8ad7e902c 100644 --- a/crates/ui-egui/src/lifecycle_tests.rs +++ b/crates/ui-egui/src/lifecycle_tests.rs @@ -23,7 +23,14 @@ fn raw(size: egui::Vec2) -> egui::RawInput { } fn tick_with(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame, raw: &egui::RawInput) -> egui::LogicOutput { - ctx.run_logic(raw, |ctx| app.tick(ctx, frame)) + ctx.run_logic(raw, |ctx| app.logic_pass(ctx, frame)) +} + +/// A context with the runtime installed, as the app's window has it. +fn headless() -> egui::Context { + let ctx = egui::Context::default(); + crate::framework::install_headless(&ctx); + ctx } fn hidden_tick(app: &mut EffectcraftApp, ctx: &egui::Context, frame: &eframe::Frame) { @@ -58,7 +65,7 @@ fn send(app: &mut EffectcraftApp, method: &str, params: serde_json::Value) -> st #[test] fn a_logic_only_tick_installs_no_fonts_and_records_no_geometry() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); hidden_tick(&mut app, &ctx, &frame); @@ -69,7 +76,7 @@ fn a_logic_only_tick_installs_no_fonts_and_records_no_geometry() { #[test] fn a_control_command_that_needs_no_ui_completes_without_a_pass() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); let reply = send(&mut app, "engine.execute", json!({"command": "comp.new", "params": {"name": "Queued", "width": 32, "height": 18, "duration": 1}})); @@ -81,8 +88,7 @@ fn a_control_command_that_needs_no_ui_completes_without_a_pass() { #[test] fn a_pass_that_cannot_draw_receives_no_synthetic_input() { - let ctx = egui::Context::default(); - crate::framework::install_headless(&ctx); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); assert!(!app.is_ready(), "a fresh app installs its fonts in its first pass"); @@ -98,7 +104,9 @@ fn a_pass_that_cannot_draw_receives_no_synthetic_input() { assert!(app.pending_synthetic_events().is_empty()); let mut setup_pass = ctx.run_ui(setup.take(), |ui| app.draw(ui, &frame)); setup_pass.textures_delta.clear(); - assert!(!app.is_ready(), "the pass that installs the fonts draws nothing"); + // The font install asks egui for a discard pass, which draws with the fonts and gets no input. + assert!(app.is_ready(), "the frame that installs the fonts draws in its discard pass"); + assert_eq!(app.synthetic.len(), 5, "the discard pass got no synthetic input: the whole gesture still waits"); // Once fonts are ready the click reaches the UI exactly once, one event per pass. let mut passes = 0; while reply.try_recv().is_err() { @@ -114,7 +122,7 @@ fn a_pass_that_cannot_draw_receives_no_synthetic_input() { #[test] fn one_pending_synthetic_batch_is_never_duplicated_while_hidden() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -148,7 +156,7 @@ fn one_pending_synthetic_batch_is_never_duplicated_while_hidden() { #[test] fn geometry_epoch_tracks_size_and_scale_but_not_window_position() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); let first = ready(&mut app, &ctx, &frame); @@ -166,7 +174,7 @@ fn geometry_epoch_tracks_size_and_scale_but_not_window_position() { #[test] fn only_a_viewer_that_draws_records_a_prefetch_scale() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -185,7 +193,7 @@ fn only_a_viewer_that_draws_records_a_prefetch_scale() { #[test] fn playback_advances_on_a_logic_only_tick() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); app.playback.playing = true; @@ -218,7 +226,7 @@ fn workspace_layout_autosave_writes_on_a_hidden_tick() { app.use_manual_clock(); app.ui.focused = PanelKind::Timeline; app.clock_mut().advance(2.0); - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); hidden_tick(&mut app, &ctx, &frame); assert!(store.read(WORKSPACES_FILE).is_some(), "the layout is saved while the window is hidden"); @@ -236,7 +244,7 @@ fn asks_to_close(ctx: &egui::Context, mut f: impl FnMut(&egui::Context)) -> bool #[test] #[cfg(not(target_arch = "wasm32"))] fn restart_to_update_with_unsaved_changes_opens_the_unsaved_changes_prompt() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); assert!(app.session.is_dirty(), "creating a comp and a layer leaves unsaved changes"); @@ -253,7 +261,7 @@ fn restart_to_update_with_unsaved_changes_opens_the_unsaved_changes_prompt() { #[test] #[cfg(not(target_arch = "wasm32"))] fn restart_to_update_with_nothing_unsaved_leaves_the_close_to_the_updater() { - let ctx = egui::Context::default(); + let ctx = headless(); let mut app = app_with_comp(); app.updates_rt.manual_window_open = true; assert!(!asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, true))); @@ -263,7 +271,7 @@ fn restart_to_update_with_nothing_unsaved_leaves_the_close_to_the_updater() { #[test] #[cfg(not(target_arch = "wasm32"))] fn restart_to_update_with_the_startup_prompt_open_leaves_the_window_open() { - let ctx = egui::Context::default(); + let ctx = headless(); let mut app = app_with_comp(); app.updates_rt.updater.offer(craft_update::Release { tag: "v99.0.0".into(), @@ -272,6 +280,14 @@ fn restart_to_update_with_the_startup_prompt_open_leaves_the_window_open() { html_url: "https://github.com/storytold/effectcraft/releases/tag/v99.0.0".into(), ..Default::default() }); + // The offer reads on a thread whether this copy installs itself; a tick opens the prompt with its answer. + for _ in 0..400 { + if app.updates_rt.updater.prompt_open() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + app.updates_rt.updater.tick(&ctx); + } assert!(app.updates_rt.updater.prompt_open()); app.updates_rt.manual_window_open = true; assert!(!asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, false)), "the prompt carries the restart"); @@ -281,7 +297,7 @@ fn restart_to_update_with_the_startup_prompt_open_leaves_the_window_open() { #[test] fn the_close_guard_answers_a_close_request_without_a_pass() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); assert!(app.session.is_dirty(), "creating a comp and a layer leaves unsaved changes"); @@ -363,7 +379,7 @@ fn image(width: usize, height: usize) -> Arc { #[test] fn a_script_file_dialog_asks_for_a_pass_while_the_window_is_hidden() { use effectcraft_engine::scriptui::{FileDialogKind, ScriptFileDialog}; - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -392,7 +408,7 @@ fn a_script_file_dialog_asks_for_a_pass_while_the_window_is_hidden() { #[test] fn a_screenshot_is_issued_only_after_settled_passes_and_writes_from_the_tick() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -418,7 +434,7 @@ fn a_screenshot_is_issued_only_after_settled_passes_and_writes_from_the_tick() { #[test] fn a_resize_between_issue_and_completion_rejects_the_pixels_and_retries() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -445,7 +461,7 @@ fn a_resize_between_issue_and_completion_rejects_the_pixels_and_retries() { #[test] fn screenshot_admission_bounds_requests_and_a_timeout_releases_its_reservation() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -471,7 +487,7 @@ fn screenshot_admission_bounds_requests_and_a_timeout_releases_its_reservation() #[test] fn a_late_callback_after_cancellation_writes_nothing_and_replies_once() { - let ctx = egui::Context::default(); + let ctx = headless(); let frame = eframe::Frame::_new_kittest(); let mut app = app_with_comp(); ready(&mut app, &ctx, &frame); @@ -500,7 +516,7 @@ fn wake_requests(ctx: &egui::Context) -> Arc(reset_id(egui::ViewportId::ROOT))).expect("the waiting guard"); - assert!(guard.suspended && guard.request.is_none(), "the all-exit finalizer retired it without allocating"); - let second = draw_app_pass(&mut app, &ctx, &frame, vec![]) - .platform_output - .ime - .expect("returning to a drawing pass publishes the custom descriptor") - .reset_request - .expect("the deferred request"); + assert!(app.is_ready(), "the discard pass after the font install draws"); + let second = fonts.platform_output.ime.expect("the drawing discard pass publishes the custom descriptor").reset_request.expect("the deferred request"); assert_ne!(first, second); // The device-loss early return is the other all-exit path. Preset the close bookkeeping diff --git a/crates/ui-egui/tests/it/ui_workspace_focus.rs b/crates/ui-egui/tests/it/ui_workspace_focus.rs index a2f061fda..f273708b0 100644 --- a/crates/ui-egui/tests/it/ui_workspace_focus.rs +++ b/crates/ui-egui/tests/it/ui_workspace_focus.rs @@ -2,10 +2,10 @@ use std::sync::Arc; +use craft_behavior::dock::Action; use effectcraft_engine::Session; use effectcraft_engine::config::{ConfigStore, DirConfig, MemoryConfig}; use effectcraft_ui_egui::EffectcraftApp; -use craft_behavior::dock::Action; use effectcraft_ui_egui::dock::{self, Node, PanelKind, SplitSize}; use effectcraft_ui_egui::state::Tool; use egui::{Event, Id, Key, Modifiers, vec2};