DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge mac-titlebar update: macOS: dragging a title-bar control no longer moves the window
aionggbbf7ea975f0f
diff --cc apps/designcraft/Cargo.tomlindex 58d6b1618,404534581..03dbfc445--- a/apps/designcraft/Cargo.toml+++ b/apps/designcraft/Cargo.toml@@@ -31,19 -29,8 +31,20 @@@ arboard = { workspace = true# The same winit eframe uses: to pick the X11 backend (file drag-and-drop) in main.rs.winit = { version = "0.30", default-features = false, features = ["x11"] }+[target.'cfg(windows)'.dependencies]+# EnumDisplayDevices (src/gpu.rs): which GPU drives the display, so the window draws on the GPU+# that shows it; drawing on a GPU without a monitor reset the display driver. Safe wrappers over+# Win32 (MIT); the `user` feature is User32.dll only.+winsafe = { version = "0.0.29", default-features = false, features = ["user"] }+[target.'cfg(target_os = "macos")'.dependencies]muda = { version = "0.21", default-features = false }- # The input context, to drop the IME's marked text when the Type tool ends a composition.-# The versions muda uses: the window's `movable` flag (see `mac_window`), through safe methods.-objc2 = { version = "0.6", default-features = false, features = ["std"] }-objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSResponder", "NSWindow"] }++# The input context, to drop the IME's marked text when the Type tool ends a composition; the++# window's `movable` flag (see `mac_window`). The versions muda uses, through safe methods.+objc2 = "0.6"- objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSTextInputContext"] }++objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSResponder", "NSTextInputContext", "NSWindow"] }+# Finder double-clicks, Open With, Dock drops and `open -a DesignCraft brochure.idml` arrive as a+# kAEOpenDocuments Apple event, which winit 0.30 doesn't handle. This crate (also used by PhotoCraft,+# PdfCraft and WordCraft) registers the handler behind a safe API (Objective-C interop needs unsafe,+# which the workspace forbids). Pinned exactly: review the diff before bumping.+fmv-macos-events = "=0.1.0"diff --cc apps/designcraft/src/main.rsindex 0a56b79c7,b1233a59d..1c2163554--- a/apps/designcraft/src/main.rs+++ b/apps/designcraft/src/main.rs@@@ -19,8 -15,9 +19,10 @@@ mod control_servermod gpu;mod logging;#[cfg(target_os = "macos")]+ mod mac_window;+ #[cfg(target_os = "macos")]mod native_menu;+mod profile;use designcraft_engine::Session;use designcraft_ui_egui::{DesignApp, Services};@@@ -29,11 -26,11 +31,14 @@@ struct Appapp: DesignApp,#[cfg(target_os = "macos")]menu: Option<native_menu::NativeMenu>,+ /// The window's start: the adapter and backend chosen for it; see `gpu`.+ startup: std::sync::Arc<gpu::Startup>,+ /// Documents and quit requests from Finder, the Dock and Open With.+ #[cfg(target_os = "macos")]+ apple_events: fmv_macos_events::Inbox,+ /// The window's `movable` flag as last set; see `mac_window`.+ #[cfg(target_os = "macos")]+ movable: Option<bool>,- /// The graphics start in progress (`gpu.json`) until a frame has been presented; see `gpu`.- startup: Option<gpu::Startup>,/// Frames begun so far.frames: u32,}@@@ -68,13 -61,9 +73,15 @@@ impl eframe::App for Appself.app.raw_input_hook(raw);}fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {+ self.startup.enter();self.app.ui(ui);#[cfg(target_os = "macos")]+ if self.app.take_ime_discard() {+ discard_marked_text();+ }++ #[cfg(target_os = "macos")]+ mac_window::sync_movable(ui.ctx(), &mut self.movable);+ self.startup.leave();}fn on_exit(&mut self) {// A normal exit before the third frame is not a crash.@@@ -388,97 -296,56 +395,99 @@@ fn main() -> std::process::ExitCodeb.with_x11();}));}- eframe::run_native(- "DesignCraft",- options,- Box::new(move |cc| {- let mut gpu = gpu;- if let Some(rs) = &cc.wgpu_render_state {- // What a bug report needs to know about the graphics driver.- let info = rs.adapter.get_info();- log::info!("graphics: {} on {:?} ({:?}; driver {} {})", info.name, info.backend, info.device_type, info.driver, info.driver_info);- if let (Some(s), Some(backend)) = (gpu.as_mut(), gpu::Backend::of(info.backend)) {- s.started_with(backend);+ // Registered before the event loop starts, so it catches the Finder event that launched the app+ // as well as later ones. Lives until the event loop returns; the app creator only borrows it.+ #[cfg(target_os = "macos")]+ let apple_events = apple_events::AppleEvents::install();+ #[cfg(target_os = "macos")]+ let events = &apple_events;+ let created = startup.clone();+ // A panic that ends the window (egui-wgpu's own) is caught here: never a crash.+ let run = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {+ eframe::run_native(+ "DesignCraft",+ options,+ Box::new(move |cc| {+ let mut restarted = None;+ if let Some(rs) = &cc.wgpu_render_state {+ // What a bug report needs to know about the graphics driver.+ let info = rs.adapter.get_info();+ log::info!(+ "graphics: {} on {:?} ({:?}; driver {} {}; power preference {power:?})",+ info.name,+ info.backend,+ info.device_type,+ info.driver,+ info.driver_info+ );+ created.started_with(info.backend);+ created.created(&cc.egui_ctx);+ if !skip.is_empty() {+ // A processor chosen by `WGPU_POWER_PREF` can be the one that can't show the window.+ let hint = if chosen_gpu { " (without WGPU_POWER_PREF it uses the one that drives your display)" } else { "" };+ restarted = Some(format!(+ "The graphics processor tried first couldn't show the window, so DesignCraft started again on {} ({:?}, {:?}){hint}.",+ info.name.trim(),+ info.backend,+ info.device_type+ ));+ }}- }- let mut session = Session::new();- // Crash recovery: reopen what a previous run left unsaved, then keep it current.- session.recovery_dir = designcraft_engine::recovery::default_dir();- let recovered = session.execute("file.recovery.open", &serde_json::json!({})).ok();- let mut app = DesignApp::new(session, services());- if let Some(line) = gpu.as_ref().and_then(gpu::Startup::status_line) {- app.status(line);- }- // Recovered documents matter more than the graphics note; both are in the log.- if let Some(n) = recovered.as_ref().and_then(|r| r["opened"].as_array()).map(Vec::len).filter(|n| *n > 0) {- app.status(format!("Recovered {n} unsaved document{} from the last session.", if n == 1 { "" } else { "s" }));- }- load_prefs(&mut app);- app.integrated_titlebar = cfg!(target_os = "macos");- if let Some(port) = control_port {- let rx = control_server::start(port, cc.egui_ctx.clone());- app = app.with_control(rx);- }- if sample {- let _ = app.run("file.newSample", serde_json::json!({}));- }- for f in files {- if let Err(e) = app.open_file("file.open", serde_json::json!({"path": f})) {- eprintln!("designcraft: {f}: {e}");+ let mut session = Session::new();+ // Crash recovery: reopen what a previous run left unsaved, then keep it current.+ session.recovery_dir = profile::recovery_dir();+ let recovered = session.execute("file.recovery.open", &serde_json::json!({})).ok();+ let mut app = DesignApp::new(session, services());+ if let Some(line) = restarted.or_else(|| created.status_line()) {+ app.status(line);}- }- Ok(Box::new(App {- app,- #[cfg(target_os = "macos")]- menu: None,- #[cfg(target_os = "macos")]- movable: None,- startup: gpu,- frames: 0,- }))- }),- )+ // Recovered documents matter more than the graphics note; both are in the log.+ if let Some(n) = recovered.as_ref().and_then(|r| r["opened"].as_array()).map(Vec::len).filter(|n| *n > 0) {+ app.status(format!("Recovered {n} unsaved document{} from the last session.", if n == 1 { "" } else { "s" }));+ }+ load_prefs(&mut app);+ app.integrated_titlebar = cfg!(target_os = "macos");+ if let Some(port) = control_port {+ let rx = control_server::start(port, cc.egui_ctx.clone());+ app = app.with_control(rx);+ }+ if sample {+ let _ = app.run("file.newSample", serde_json::json!({}));+ }+ for f in files {+ if let Err(e) = app.open_file("file.open", serde_json::json!({"path": f})) {+ log::warn!("{f}: {e}");+ }+ }+ // The first frame is due from now (`gpu::watch_first_frame`).+ created.ready();+ Ok(Box::new(App {+ app,+ #[cfg(target_os = "macos")]+ menu: None,+ startup: created,+ #[cfg(target_os = "macos")]+ apple_events: events.connect(&cc.egui_ctx),++ #[cfg(target_os = "macos")]++ movable: None,+ frames: 0,+ }))+ }),+ )+ }))+ .map_err(|payload| designcraft_engine::guard::panic_message(payload.as_ref()));+ // The window's end, or a graphics failure that starts the app again. A failure is logged:+ // standard error and the log file, since a Windows start has no console to show it.+ // Documents that arrived after the last frame, for a start again to open too.+ #[cfg(target_os = "macos")]+ startup.opening(apple_events.queued());+ match gpu::finish(run, &startup) {+ Ok(()) => std::process::ExitCode::SUCCESS,+ Err(e) => {+ log::error!("DesignCraft stopped: {e}");+ std::process::ExitCode::FAILURE+ }+ }}#[cfg(test)]