Skip to content
29 changes: 24 additions & 5 deletions crates/engine/src/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Unit {
pub struct TaskInfo {
pub id: u64,
/// A stable id: `import`, `export`, `scan`, `previews`, `smartPreviews`, `lightroom`, `merge`, `download`,
/// `faces`, `denoise`, `findMissing`, `applePhotos`, `cache`, `focus`.
/// `faces`, `denoise`, `findMissing`, `applePhotos`, `cache`, `focus`, `segment`.
pub kind: &'static str,
/// English; the frontend translates it.
pub label: String,
Expand All @@ -92,7 +92,7 @@ pub struct TaskInfo {
struct Entry {
id: u64,
kind: &'static str,
label: String,
label: Mutex<String>,
started: web_time::Instant,
done: AtomicU64,
total: AtomicU64,
Expand Down Expand Up @@ -155,7 +155,7 @@ impl Entry {
TaskInfo {
id: self.id,
kind: self.kind,
label: self.label.clone(),
label: lock(&self.label).clone(),
done: self.done.load(Ordering::Relaxed),
total: self.total.load(Ordering::Relaxed),
unit: Unit::from_u8(self.unit.load(Ordering::Relaxed)),
Expand All @@ -180,7 +180,7 @@ impl Activity {
let entry = Arc::new(Entry {
id,
kind,
label: capped(label),
label: Mutex::new(capped(label)),
started: web_time::Instant::now(),
done: AtomicU64::new(0),
total: AtomicU64::new(0),
Expand All @@ -204,7 +204,7 @@ impl Activity {
pub fn cancel(&self, id: u64) -> Result<(), String> {
let tasks = lock(&self.inner.tasks);
let entry = tasks.iter().find(|e| e.id == id).ok_or_else(|| format!("no task {id}"))?;
if entry.try_cancel() { Ok(()) } else { Err(format!("{} can't be cancelled", entry.label)) }
if entry.try_cancel() { Ok(()) } else { Err(format!("{} can't be cancelled", lock(&entry.label))) }
}

/// Ask every cancellable task to stop; returns how many were asked.
Expand All @@ -230,6 +230,11 @@ impl TaskGuard {
self.entry.id
}

/// Change the English label without replacing the row or its cancellation flag.
pub fn label(&self, text: &str) {
*lock(&self.entry.label) = capped(text);
}

/// `done` of `total` (`total` 0 = not known yet). `done` is kept at most `total`.
pub fn progress(&self, done: u64, total: u64) {
self.entry.progress(done, total);
Expand Down Expand Up @@ -318,6 +323,20 @@ mod tests {
assert!(a.list().is_empty());
}

#[test]
fn a_label_change_keeps_the_row_and_accepted_cancellation() {
let a = Activity::default();
let g = a.start("segment", "Queued AI mask paste", Cancel::Yes);
let id = g.id();
a.cancel(id).unwrap();
g.label("Recomputing AI masks");
assert_eq!(a.list()[0].id, id);
assert_eq!(a.list()[0].label, "Recomputing AI masks");
assert!(g.is_cancelled());
g.label(&"ą".repeat(MAX_TEXT + 10));
assert_eq!(a.list()[0].label.chars().count(), MAX_TEXT);
}

#[test]
fn guard_drop_on_panic_unwind_removes_the_row() {
let a = Activity::default();
Expand Down
24 changes: 6 additions & 18 deletions crates/engine/src/cmd/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Paste Settings from Previous",
["Edit"],
Some("Cmd+Alt+V"),
"{ids?, groups?: [settingsGroup] (default: the copy groups)} — from the photo that was active before this one",
"{ids?, groups?: [settingsGroup] (default: the copy groups)} — from the photo that was active before this one; copied AI masks are recomputed on each destination (SAM 3 required)",
has_active,
|s, p| {
let c = "develop.pastePrevious";
Expand All @@ -1155,22 +1155,17 @@ pub fn specs() -> Vec<CommandSpec> {
let groups = groups_param(p, c)?.unwrap_or_else(|| s.copy_groups.clone());
let partial = lightcraft_develop::extract_groups(&s.develop_of(prev).unwrap_or_default(), &groups);
let ids: Vec<PhotoId> = s.targets(p)?.into_iter().filter(|id| *id != prev).collect();
let ops = ids
.iter()
.filter_map(|id| s.develop_of(*id).map(|d| (*id, d)))
.filter_map(|(id, d)| s.develop_op(id, lightcraft_develop::apply_partial(&d, &partial, 1.0), "Paste from Previous"))
.collect::<Vec<_>>();
let n = ops.len();
s.commit("Paste from Previous", Op::Batch { ops })?;
Ok(json!({"changed": n, "from": prev.0}))
let mut out = s.paste_settings(&ids, &partial, "Paste from Previous")?;
out["from"] = json!(prev.0);
Ok(out)
}
),
cmd!(
"develop.paste",
"Paste Edit Settings",
["Edit"],
Some("Cmd+V"),
"{ids?, groups?: [settingsGroup] (paste only these of the copied groups)}",
"{ids?, groups?: [settingsGroup] (paste only these of the copied groups)} — copied Object/Describe masks are recomputed on each destination (SAM 3 required); successive app pastes queue captured targets/settings and return {pending, changed: 0, targets, queued, task}",
has_clipboard,
|s, p| {
let c = "develop.paste";
Expand All @@ -1182,14 +1177,7 @@ pub fn specs() -> Vec<CommandSpec> {
clip = lightcraft_develop::extract_groups(&full, &only);
}
let ids = s.targets(p)?;
let ops = ids
.iter()
.filter_map(|id| s.develop_of(*id).map(|d| (*id, d)))
.filter_map(|(id, d)| s.develop_op(id, lightcraft_develop::apply_partial(&d, &clip, 1.0), "Paste Settings"))
.collect::<Vec<_>>();
let n = ops.len();
s.commit("Paste Settings", Op::Batch { ops })?;
Ok(json!({"changed": n}))
s.paste_settings(&ids, &clip, "Paste Settings")
}
),
cmd!(
Expand Down
20 changes: 18 additions & 2 deletions crates/engine/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#[cfg(feature = "sam")]
mod download;
mod paste;
#[cfg(feature = "sam")]
mod worker;

Expand Down Expand Up @@ -132,6 +133,10 @@ pub struct Segmenter {
pending: Option<PendingClicks>,
messages: Vec<String>,
#[cfg(feature = "sam")]
paste: Option<paste::Pending>,
#[cfg(feature = "sam")]
paste_queue: std::collections::VecDeque<paste::Pending>,
#[cfg(feature = "sam")]
worker: worker::Worker,
#[cfg(feature = "sam")]
download: download::Downloader,
Expand All @@ -150,6 +155,10 @@ impl Default for Segmenter {
pending: None,
messages: Vec::new(),
#[cfg(feature = "sam")]
paste: None,
#[cfg(feature = "sam")]
paste_queue: std::collections::VecDeque::new(),
#[cfg(feature = "sam")]
worker: worker::Worker::default(),
#[cfg(feature = "sam")]
download: download::Downloader::default(),
Expand Down Expand Up @@ -182,7 +191,7 @@ impl Segmenter {
/// Whether requests are queued or running (loading, analyzing the photo, a click…).
pub fn busy(&self) -> bool {
#[cfg(feature = "sam")]
return self.worker.pending() > 0;
return self.worker.pending() > 0 || self.paste.is_some() || !self.paste_queue.is_empty();
Comment thread
ChokunPlayZ marked this conversation as resolved.
Resolved
#[cfg(not(feature = "sam"))]
false
}
Expand Down Expand Up @@ -385,8 +394,14 @@ impl Session {
/// selection stays on its content when they change, and changing them doesn't ask again.
#[cfg_attr(not(feature = "sam"), allow(dead_code))]
pub(crate) fn segment_key(&self, id: PhotoId) -> Option<(u64, lightcraft_develop::DevelopSettings)> {
self.segment_key_for(id, &self.catalog.photo(id)?.develop)
}

/// Also used for a paste's proposed look, before any settings are committed.
#[cfg_attr(not(feature = "sam"), allow(dead_code))]
fn segment_key_for(&self, id: PhotoId, settings: &lightcraft_develop::DevelopSettings) -> Option<(u64, lightcraft_develop::DevelopSettings)> {
let p = self.catalog.photo(id)?;
let mut d = (*p.develop).clone();
let mut d = settings.clone();
d.masks.clear();
// (the crop doesn't show in the uncropped input: it doesn't ask again either)
d.crop = Default::default();
Expand Down Expand Up @@ -667,6 +682,7 @@ impl Session {
if idle {
self.segmenter.pending = None;
}
self.poll_mask_paste(&mut polled);
}
polled
}
Expand Down
Loading