Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

feat(ui): Beep When Done plays the platform beep for finished jobs (#204) (#2658)

Reynold Hu93f2ff835a3b
diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs
index eba756471..a0ce1fa56 100644
--- a/apps/photocraft/src/services.rs
+++ b/apps/photocraft/src/services.rs
@@ -361,6 +361,7 @@ pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) ->
})),
inbox: None,
open_url: Some(Box::new(|url: &str| open::that(url).map_err(|e| e.to_string()))),
+ beep: Some(platform_beep),
clipboard_set_image: Some({
let clip = clip.clone();
Box::new(move |w: u32, h: u32, px: &[u8]| {
@@ -407,6 +408,21 @@ pub fn native(automation: Option<photocraft_automation::AuthorizedWorkspace>) ->
}
}
+/// Preferences ▸ General ▸ Beep When Done: the platform's default information beep.
+fn platform_beep() {
+ #[cfg(target_os = "windows")]
+ {
+ let _ = winsafe::MessageBeep(winsafe::co::MBP::OK);
+ }
+ #[cfg(not(target_os = "windows"))]
+ {
+ // No beep API among this app's other platforms' dependencies: write the terminal bell
+ // (audible when launched from a terminal, which is the same cue macOS terminals use).
+ use std::io::Write as _;
+ let _ = std::io::stdout().write_all(b"\x07").and_then(|()| std::io::stdout().flush());
+ }
+}
+
/// Flat-image import via photocraft-codecs (kept for reference/tests; the app uses photocraft-io).
#[allow(dead_code)]
pub fn import_flat(name: &str, bytes: &[u8]) -> Result<Document, String> {
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 9ffc0fc28..a10260b5f 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -981,7 +981,6 @@ pub const SECTIONS: [(&str, &str); 18] = [
/// preference is read anywhere outside `prefs.rs`, and while an unlisted one is read nowhere.
pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[
"general.colorPicker",
- "general.beepWhenDone",
"general.alwaysCreateSmartObjectsWhenPlacing",
"general.animatedZoom",
"general.zoomResizesWindows",
diff --git a/crates/ui-egui/src/jobs_ui.rs b/crates/ui-egui/src/jobs_ui.rs
index 13f2174cf..80944af8d 100644
--- a/crates/ui-egui/src/jobs_ui.rs
+++ b/crates/ui-egui/src/jobs_ui.rs
@@ -342,6 +342,14 @@ pub fn cancel(app: &mut PhotocraftApp, id: JobId) {
}
fn on_event(app: &mut PhotocraftApp, ctx: &egui::Context, e: JobEvent) {
+ // Preferences ▸ General ▸ Beep When Done (off by default): a finished operation beeps, like
+ // Photoshop's cue. Cancelling is the user's own action and stays silent.
+ if matches!(e.outcome, JobOutcome::Done(_))
+ && app.session.prefs().general.beep_when_done
+ && let Some(beep) = app.services.beep
+ {
+ beep();
+ }
// Control requests waiting on this job.
let reply = match &e.outcome {
JobOutcome::Done(v) => json!({"ok": true, "result": v}),
diff --git a/crates/ui-egui/src/jobs_ui_tests.rs b/crates/ui-egui/src/jobs_ui_tests.rs
index 0679c0d3d..b1f7412fd 100644
--- a/crates/ui-egui/src/jobs_ui_tests.rs
+++ b/crates/ui-egui/src/jobs_ui_tests.rs
@@ -3,7 +3,7 @@
//! dialog, and Esc / Cancel leave the document unchanged.
use std::sync::Arc;
-use std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use egui::vec2;
@@ -15,13 +15,18 @@ use serde_json::{Value, json};
use crate::PhotocraftApp;
fn app_harness() -> Harness<'static, PhotocraftApp> {
+ app_harness_with(crate::Services::default())
+}
+
+/// [`app_harness`] over the given services (the beep test records the hook's calls).
+fn app_harness_with(services: crate::Services) -> Harness<'static, PhotocraftApp> {
let mut h = Harness::builder().with_size(vec2(1280.0, 800.0)).with_max_steps(8).build_eframe(|cc| {
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
let mut s = photocraft_engine::Session::new();
s.execute("file.new", json!({"width": 320, "height": 240})).unwrap();
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("edit.fill", json!({"color": "#3366cc"})).unwrap();
- let mut app = PhotocraftApp::new(s, crate::Services::default());
+ let mut app = PhotocraftApp::new(s, services);
app.background_jobs = true;
app
});
@@ -549,3 +554,26 @@ fn a_cancelled_save_still_counts_until_its_worker_returns() {
step_until(&mut h, |a| !a.saving());
assert!(!h.state().jobs.close_after_saves, "the held close is repeated once the worker returns");
}
+
+/// Preferences ▸ General ▸ Beep When Done (off by default): a finished background job beeps
+/// once through the services hook; with the preference off it stays silent.
+#[test]
+fn a_finished_job_beeps_when_the_preference_is_on() {
+ static BEEPS: AtomicUsize = AtomicUsize::new(0);
+ fn count_beep() {
+ BEEPS.fetch_add(1, Ordering::Relaxed);
+ }
+ BEEPS.store(0, Ordering::Relaxed);
+ let mut h = app_harness_with(crate::Services { beep: Some(count_beep), ..Default::default() });
+ // Off by default: the first job finishes without a beep.
+ let gate = Arc::new(AtomicBool::new(true));
+ let _ = slow_job(h.state_mut(), &gate);
+ frames_for(&mut h, 800);
+ assert_eq!(BEEPS.load(Ordering::Relaxed), 0, "silent by default");
+ // On: the next finished job beeps once.
+ h.state_mut().run("prefs.set", json!({"path": "general.beepWhenDone", "value": true})).unwrap();
+ let gate = Arc::new(AtomicBool::new(true));
+ let _ = slow_job(h.state_mut(), &gate);
+ frames_for(&mut h, 800);
+ assert_eq!(BEEPS.load(Ordering::Relaxed), 1, "one beep for the finished job");
+}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 06c810e17..5b0456009 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -350,6 +350,9 @@ pub struct Services {
pub encode_png: Option<EncodePngFn>,
/// Open a URL in the system browser (native). Falls back to `ctx.open_url` (web) when unset.
pub open_url: Option<OpenUrlFn>,
+ /// Preferences ▸ General ▸ Beep When Done: the platform's default beep, played when a
+ /// background job finishes successfully. `None` in the web build (and in tests).
+ pub beep: Option<fn()>,
/// Files delivered asynchronously (web drag-and-drop): drained every frame.
pub inbox: Option<Inbox>,
/// OS clipboard images: copies go out, screenshots and images from other apps come in.
diff --git a/docs/scorecard.md b/docs/scorecard.md
index cc7c4c89c..4a2d450a9 100644
--- a/docs/scorecard.md
+++ b/docs/scorecard.md
@@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js
| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 16 not run, 6 not measurable yet (baseline 2026-10-05) |
| [File compatibility](#file-compatibility) | 5 | 11 | 11 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 |
| [Tools](#tools) | 21 | 17 | 4 | |
-| [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 39 of 152 (target 0) |
+| [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 38 of 152 (target 0) |
| [Type](#type) | 0 | 3 | 6 | |
| [Automation](#automation) | 3 | 2 | 6 | 630 of 630 menu items live (100.0%) |
| [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 28 of 28 crates; panic_hunt present |
@@ -175,9 +175,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t
## Workspace and UI
-**Settings that do nothing: 39 of 152** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.
+**Settings that do nothing: 38 of 152** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.
-Unread: `general.color_picker`, `general.beep_when_done`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.
+Unread: `general.color_picker`, `general.always_create_smart_objects_when_placing`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_collapse_icon_panels`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `workspace.enable_narrow_options_bar`, `tools.enable_flick_panning`, `tools.show_transformation_values`, `file_handling.image_previews`, `file_handling.save_in_background`, `file_handling.ignore_exif_profile_tag`, `file_handling.maximize_psd_compatibility`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `units_and_rulers.print_resolution`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.missing_glyph_protection`, `type_.show_font_names_in_english`, `type_.text_engine`, `type_.font_preview`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.
| Id | Target | Status | Issue | Note |
|---|---|---|---|---|