Skip to content

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

perf(ui): the Info panel composites its Color Sampler readouts once per revision (#4528) (#4753)

FinnNk51674512dbd2
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 38118955c..be3da05d4 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -512,6 +512,8 @@ pub struct PhotocraftApp {
navigator_gpu: HashMap<DocId, crate::gpu_canvas::ViewParams>,
/// Documents whose GPU canvas texture holds the plain document (`canvas::ensure_gpu`).
navigator_plain: std::collections::HashSet<DocId>,
+ /// The Info panel's Color Sampler readouts for (document, revision) (`panels::sampler_readouts`).
+ info_samplers: Option<panels::SamplerReadouts>,
/// Display profile readings (#569).
monitors: monitor_status::State,
checker: Option<egui::TextureHandle>,
@@ -774,6 +776,7 @@ impl PhotocraftApp {
display_memo: HashMap::new(),
navigator_gpu: HashMap::new(),
navigator_plain: std::collections::HashSet::new(),
+ info_samplers: None,
monitors: Default::default(),
checker: None,
drag: None,
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 6d0b9b003..1b3fc8e04 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1826,14 +1826,13 @@ fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
);
// Color Sampler points (#1046): each under its number, with its position in the current ruler
// unit and the same RGB/CMYK readout as the pointer (one composite pixel each).
- let samplers = app.session.execute("view.colorSamplers.list", json!({})).ok();
- let samplers = samplers.as_ref().and_then(|v| v["samplers"].as_array()).cloned().unwrap_or_default();
+ let samplers = sampler_readouts(app);
if !samplers.is_empty() {
widgets::hairline(ui);
let cell = |ui: &mut egui::Ui, s: String| {
ui.label(RichText::new(s).font(mono.clone()).color(t.text));
};
- for s in &samplers {
+ for s in samplers.iter() {
let n = s["number"].as_i64().unwrap_or(0);
let at = s["position"]
.as_array()
@@ -1856,6 +1855,25 @@ fn info_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
}
+/// The Info panel's Color Sampler readouts and the (document, revision) they were composited for.
+pub(crate) type SamplerReadouts = ((photocraft_doc::DocId, u64), std::sync::Arc<Vec<Value>>);
+
+/// The Color Sampler readouts of the active document, composited once per revision (#4528):
+/// adding, moving or deleting a sampler and every pixel edit bump the revision, so between them
+/// the Info panel reuses them instead of compositing each point through the stack every frame.
+pub(crate) fn sampler_readouts(app: &mut PhotocraftApp) -> std::sync::Arc<Vec<Value>> {
+ let Some(key) = app.session.active().map(|s| (s.doc.id, s.revision)) else { return std::sync::Arc::default() };
+ if let Some((k, v)) = &app.info_samplers
+ && *k == key
+ {
+ return v.clone();
+ }
+ let list = app.session.execute("view.colorSamplers.list", json!({})).ok();
+ let v = std::sync::Arc::new(list.as_ref().and_then(|v| v["samplers"].as_array()).cloned().unwrap_or_default());
+ app.info_samplers = Some((key, v.clone()));
+ v
+}
+
fn navigator(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
let Some(idx) = app.session.active_index() else {
@@ -6035,3 +6053,27 @@ mod properties_target_regressions {
#[cfg(test)]
#[path = "history_panel_select_tests.rs"]
mod history_panel_select_tests;
+
+#[cfg(test)]
+mod sampler_readout_tests {
+ use super::*;
+
+ /// Composited once per revision: repeated frames share the readouts; adding a sampler or an
+ /// edit recomputes them (#4528).
+ #[test]
+ fn sampler_readouts_are_reused_until_the_document_changes() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 32, "height": 32, "background": "white"})).unwrap();
+ app.run("view.colorSamplers.add", json!({"x": 4, "y": 4})).unwrap();
+ let a = sampler_readouts(&mut app);
+ assert_eq!(a.len(), 1);
+ assert_eq!(a[0]["rgb"], json!([255, 255, 255]));
+ assert!(std::sync::Arc::ptr_eq(&a, &sampler_readouts(&mut app)), "the next frame reuses them");
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ let b = sampler_readouts(&mut app);
+ assert!(!std::sync::Arc::ptr_eq(&a, &b));
+ assert_eq!(b[0]["rgb"], json!([255, 0, 0]), "an edit shows");
+ app.run("view.colorSamplers.add", json!({"x": 8, "y": 8})).unwrap();
+ assert_eq!(sampler_readouts(&mut app).len(), 2, "so does a new sampler");
+ }
+}