PhotoCraft: An open-source, photo editor written by scratch in pure Rust
System title bar: hide the in-app mark and title, show the file in the OS title (#1903)
rafi612f39abb70b94f
diff --git a/crates/ui-egui/src/brand.rs b/crates/ui-egui/src/brand.rsindex c36019131..a601e95c3 100644--- a/crates/ui-egui/src/brand.rs+++ b/crates/ui-egui/src/brand.rs@@ -68,6 +68,9 @@ mod tests {let ctx = Context::default();crate::PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);let mut app = crate::PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ // The system title bar hides the in-app mark (the OS draws its own icon), so paint the+ // app-drawn bar here.+ app.custom_titlebar = true;let mut frame = || {let mut out = ctx.run_ui(egui::RawInput::default(), |ui| crate::panels::title_bar(&mut app, ui));let uploads = out.textures_delta.set.values().flat_map(|d| d.iter()).filter(|d| d.image.size() == [128, 128]).count();diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex ccad02f2a..bc61fc2b2 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -384,6 +384,9 @@ pub struct PhotocraftApp {/// Windows and Linux: the window has no OS decorations and the app's top bar is the title bar/// (caption buttons, window dragging and edge resizing, `titlebar`).pub custom_titlebar: bool,+ /// Last window title sent to the OS (`ViewportCommand::Title`, see `panels::sync_window_title`):+ /// sent again only when it changes, so idle frames don't spam the backend.+ last_window_title: String,fonts_ready: bool,/// Screen rect of the main canvas last frame (for overlays and the navigator).pub last_canvas_rect: egui::Rect,@@ -537,6 +540,7 @@ impl PhotocraftApp {styled: false,integrated_titlebar: false,custom_titlebar: false,+ last_window_title: String::new(),fonts_ready: false,last_canvas_rect: egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0)),drop_canvas_rect: None,@@ -1172,6 +1176,9 @@ impl eframe::App for PhotocraftApp {ui.disable();ui.set_opacity(1.0);}+ // The OS title bar (and the taskbar / Alt-Tab entry) follows the active file; with the+ // system title bar this is where the document name lives, as the in-app title is hidden.+ panels::sync_window_title(self, &ctx);let chrome = !self.ui.view.hides_chrome();if !chrome && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) {let _ = menus::invoke(self, &ctx, "view.screenMode.standard", serde_json::json!({}));diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 9bdd9626c..e8a6eab5f 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -398,10 +398,13 @@ fn color_chips(app: &mut PhotocraftApp, ui: &mut egui::Ui) {/// The app's top bar: brand mark, menus, the document title and the workspace controls. With/// [`PhotocraftApp::custom_titlebar`] (Windows and Linux) it is also the window's title bar, as in-/// Photoshop on Windows: the caption buttons take its right end (`titlebar`).+/// Photoshop on Windows: the caption buttons take its right end (`titlebar`). With the system+/// title bar (Windows and Linux, `system_title_bar`) the OS draws its own icon and title, so the+/// in-app bar shows menus and workspace controls only: no brand mark, no centred title.pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let t = Tokens::get(ui.ctx());let custom = app.custom_titlebar;+ let system = system_title_bar(app);let left = if cfg!(target_os = "macos") && app.integrated_titlebar { 78 } else { 10 };let right = if custom { 0 } else { 10 };let bar = egui::Panel::top("title_bar")@@ -428,10 +431,12 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {// whatever room is left between them, shortened or dropped rather than drawn over them.let (mut menus_right, mut controls_left) = (full.left(), full.right());ui.horizontal_centered(|ui| {- let side = if t.pro { 18.0 } else { 20.0 };- let (mark, _) = ui.allocate_exact_size(vec2(side, side), Sense::hover());- crate::brand::paint_mark(ui, mark);- ui.add_space(6.0);+ if !system {+ let side = if t.pro { 18.0 } else { 20.0 };+ let (mark, _) = ui.allocate_exact_size(vec2(side, side), Sense::hover());+ crate::brand::paint_mark(ui, mark);+ ui.add_space(6.0);+ }// With the macOS menu bar the menus are at the top of the screen instead.menus_right = if app.services.native_menu.is_some() { ui.cursor().left() } else { crate::menus::menu_bar(app, ui) };// The menu bar takes the whole row, so the right-hand group gets its own rect:@@ -493,6 +498,9 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {};});ui.ctx().data_mut(|d| d.insert_temp(span_id, (menus_right, controls_left)));+ if system {+ return;+ }let font = theme::medium(13.0);let galley = ui.painter().layout_no_wrap(title.clone(), font.clone(), t.text_dim);if let Some(x) = title_x(full.center().x, menus_right, controls_left, galley.size().x) {@@ -511,6 +519,32 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}}+/// Whether the window shows the system's title bar instead of the app drawing its own: Windows+/// and Linux with Preferences › Interface › System Title Bar. macOS always has system decorations,+/// but keeps the in-app brand mark and centred title, so it never counts as system mode here; nor+/// does the web build (the browser tab has no document title), nor an embedder or the snapshot+/// example that never turned the preference on.+pub fn system_title_bar(app: &PhotocraftApp) -> bool {+ !app.custom_titlebar && app.session.prefs().interface.system_title_bar && !cfg!(any(target_os = "macos", target_arch = "wasm32"))+}++/// The OS window title: the active document's name suffixed with the app name (a `*` prefix+/// marks unsaved changes, like the `•` in the app-drawn title), or just the app name with no+/// document open.+pub fn window_title(app: &PhotocraftApp) -> String {+ app.session.active().map(|d| format!("{}{} \u{2014} PhotoCraft", if d.is_dirty() { "*" } else { "" }, d.doc.name)).unwrap_or_else(|| "PhotoCraft".into())+}++/// Keep the OS window title (and the taskbar / Alt-Tab entry) on the active file. Sends+/// `ViewportCommand::Title` only when the title changed since the last frame.+pub fn sync_window_title(app: &mut PhotocraftApp, ctx: &egui::Context) {+ let want = window_title(app);+ if app.last_window_title != want {+ app.last_window_title = want.clone();+ ctx.send_viewport_cmd(egui::ViewportCommand::Title(want));+ }+}+/// Minimum space kept between the window title and the menus or controls beside it.const TITLE_GAP: f32 = 16.0;/// Free room the title bar needs after its other controls to show the Discord button.@@ -535,7 +569,7 @@ fn title_x(center: f32, menus_right: f32, controls_left: f32, width: f32) -> Opt#[cfg(test)]mod title_tests {- use super::title_x;+ use super::{system_title_bar, title_x, window_title};#[test]fn title_never_overlaps_menus_or_controls() {@@ -550,6 +584,96 @@ mod title_tests {assert_eq!(title_x(f32::NAN, 420.0, 1300.0, 60.0), Some(436.0));assert_eq!(title_x(400.0, f32::INFINITY, 1300.0, 60.0), None);}++ fn app(custom_titlebar: bool) -> crate::PhotocraftApp {+ let mut app = crate::PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.custom_titlebar = custom_titlebar;+ // The preference that makes the shell launch without its own title bar.+ app.session.edit_prefs(|p| p.interface.system_title_bar = !custom_titlebar);+ app+ }++ #[test]+ fn without_the_preference_the_mark_stays() {+ // The default (e.g. the web build or the snapshot example): no custom bar, no preference.+ let app = crate::PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ assert!(!system_title_bar(&app));+ }++ fn open_named(app: &mut crate::PhotocraftApp, name: &str) {+ app.session.add_document(+ photocraft_doc::Document::new(name, photocraft_doc::Size::new(4, 4), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8),+ None,+ );+ }++ #[test]+ fn window_title_with_no_document_is_the_app_name() {+ assert_eq!(window_title(&app(true)), "PhotoCraft");+ }++ #[test]+ fn window_title_follows_the_active_file() {+ let mut app = app(true);+ open_named(&mut app, "foo.psd");+ assert_eq!(window_title(&app), "foo.psd \u{2014} PhotoCraft");+ // Unsaved changes gain a `*` prefix, like the `•` in the app-drawn title.+ if let Some(st) = app.session.active_mut() {+ st.revision += 1;+ }+ assert_eq!(window_title(&app), "*foo.psd \u{2014} PhotoCraft");+ }++ #[test]+ fn system_mode_hides_the_mark_and_the_centred_title() {+ for custom in [true, false] {+ let mut app = app(custom);+ open_named(&mut app, "foo.psd");+ let ctx = egui::Context::default();+ crate::PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);+ let out = ctx.run_ui(egui::RawInput::default(), |ui| super::title_bar(&mut app, ui));+ let mut out = out;+ out.textures_delta.clear();+ let mut texts = Vec::new();+ for shape in &out.shapes {+ collect_text(&shape.shape, &mut texts);+ }+ let system = system_title_bar(&app);+ assert_eq!(system, !custom && !cfg!(target_os = "macos"));+ assert_eq!(crate::brand::mark_rect(&ctx).is_some(), !system, "mark in system mode (custom={custom})");+ assert_eq!(texts.iter().any(|t| t.contains("foo.psd")), !system, "centred title in system mode (custom={custom}): {texts:?}");+ }+ }++ fn collect_text(shape: &egui::Shape, out: &mut Vec<String>) {+ match shape {+ egui::Shape::Text(t) => out.push(t.galley.text().to_string()),+ egui::Shape::Vec(shapes) => shapes.iter().for_each(|s| collect_text(s, out)),+ _ => {}+ }+ }++ #[test]+ fn sync_window_title_sends_only_on_change() {+ let mut app = app(true);+ let ctx = egui::Context::default();+ crate::PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);+ super::sync_window_title(&mut app, &ctx);+ assert_eq!(app.last_window_title, "PhotoCraft");+ let mut out = ctx.run_ui(egui::RawInput::default(), |_| {});+ out.textures_delta.clear();+ let cmds = out.viewport_output.remove(&egui::ViewportId::ROOT).map(|v| v.commands).unwrap_or_default();+ assert!(cmds.iter().any(|c| matches!(c, egui::ViewportCommand::Title(t) if t == "PhotoCraft")), "{cmds:?}");+ // Same document state again: the cached title stops a repeat command.+ super::sync_window_title(&mut app, &ctx);+ let mut out = ctx.run_ui(egui::RawInput::default(), |_| {});+ out.textures_delta.clear();+ let cmds = out.viewport_output.remove(&egui::ViewportId::ROOT).map(|v| v.commands).unwrap_or_default();+ assert!(!cmds.iter().any(|c| matches!(c, egui::ViewportCommand::Title(_))), "repeat Title: {cmds:?}");+ open_named(&mut app, "foo.psd");+ super::sync_window_title(&mut app, &ctx);+ assert_eq!(app.last_window_title, "foo.psd \u{2014} PhotoCraft");+ }}// ----------------------------------------------------------------------------- options bar