Keep model management independent of document state

This commit is contained in:
mighty-programmer committed 2026-10-10 22:37:09 +03:00
1 parent 620fef4291
commit d7ff8f79ca
4 files changed
+65 -9

No files matched your search

+1 -1
View File
@@ -205,7 +205,7 @@ pub fn drop_floating(s: &mut Session) -> Result<Value> {
/// Commands that leave a floating piece floating: its own, and ones that don't touch documents.
fn keeps_floating(id: &str) -> bool {
matches!(id, "select.float" | "select.drop") || ["view.", "window.", "help.", "prefs."].iter().any(|p| id.starts_with(p))
matches!(id, "select.float" | "select.drop") || ["view.", "window.", "help.", "prefs.", "models."].iter().any(|p| id.starts_with(p))
}
/// Before any command (`Session::dispatch`): Undo puts a floating piece back (and is used up:
+47 -3
View File
@@ -84,9 +84,7 @@ impl Sampler for ModelSampler<'_> {
for (dst, v) in p.iter_mut().take(3).zip(rgb) {
*dst = if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 };
}
if !p[3].is_finite() {
p[3] = 0.0;
}
p[3] = if p[3].is_finite() { p[3].clamp(0.0, 1.0) } else { 0.0 };
}
px
}
@@ -395,6 +393,33 @@ mod tests {
assert_eq!(loaded.integrations.object_model, crate::prefs::ObjectModel::Classical);
}
#[test]
fn managing_models_does_not_drop_a_floating_selection_or_edit_the_document() {
let (mut s, _) = session("rgb", 8);
s.execute("layer.new.layer", json!({})).unwrap();
s.edit("paint", |doc, layer| {
let bounds = doc.bounds();
doc.layer_mut(layer.unwrap()).unwrap().surface_mut().unwrap().fill_rect(bounds, &[1.0, 0.0, 0.0, 1.0]);
Ok(())
})
.unwrap();
s.execute("select.rect", json!({"x":0,"y":0,"width":2,"height":2})).unwrap();
s.execute("select.float", json!({"dx":2,"dy":1})).unwrap();
let doc = s.active().unwrap().doc.clone();
let history = s.active().unwrap().history.past_len();
let journal = s.journal.len();
for (command, params) in
[("models.list", json!({})), ("models.download", json!({"id":"birefnet-hr-matting"})), ("models.remove", json!({"id":"birefnet-hr-matting"}))]
{
s.execute(command, params).unwrap();
let state = s.active().unwrap();
assert!(Arc::ptr_eq(&state.doc, &doc));
assert_eq!(state.history.past_len(), history);
assert_eq!(state.floating.as_ref().unwrap().offset, (2, 1));
assert_eq!(s.journal.len(), journal);
}
}
#[test]
fn preferences_select_a_model_and_explicit_classical_overrides_it() {
let (mut s, fake) = session("rgb", 32);
@@ -478,6 +503,25 @@ mod tests {
assert_eq!(s.active().unwrap().doc.layers[0].mask.as_ref().unwrap().value(7, 2), 0.0);
}
#[test]
fn out_of_range_and_non_finite_alpha_only_affect_the_model_input() {
// The existing segmentation sampler composites transparent pixels over neutral gray.
for (alpha, expected) in [(2.0, 0.25), (f32::NAN, 0.5)] {
let (mut s, fake) = session("rgb", 32);
s.edit("HDR alpha", |doc, _| {
doc.icc_profile = Some(Builtin::Srgb.profile().to_bytes());
let bounds = doc.bounds();
doc.layers[0].surface_mut().unwrap().fill_rect(bounds, &[0.25, 0.25, 0.25, alpha]);
Ok(())
})
.unwrap();
let before: Vec<u32> = pixels(&s).iter().map(|v| v.to_bits()).collect();
s.execute("select.subject", json!({"model":"birefnet-hr-matting","sampleAllLayers":false})).unwrap();
assert_eq!(pixels(&s).iter().map(|v| v.to_bits()).collect::<Vec<_>>(), before);
assert!(fake.calls.lock().unwrap()[0].1.px.iter().flatten().all(|v| v.is_finite() && (*v - expected).abs() < 0.005));
}
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn cancelling_model_inference_discards_result_and_unlocks_document() {
+9 -5
View File
@@ -62,7 +62,9 @@ cargo run --release -p photocraft-engine --features local-ml --example local_mod
tests cover scoped removal, busy state and dependency panic recovery.
- Engine tests cover RGB/Gray/CMYK/Lab at 8/16/32-bit, ICC input conversion, fractional alpha,
layer-mask replacement, selection modes and rectangle mapping, one-step Undo/Redo, cancellation,
missing-model errors, recorded method choices and batch/droplet capability inheritance.
missing-model errors, recorded method choices, batch/droplet capability inheritance and model
management while a selection floats. Non-finite/out-of-range alpha is sanitized for model input
while original 32-bit pixel values are preserved.
- Full engine, CLI and UI test suites pass locally. UI preference/localization coverage includes
every supported complete-menu language. Default and `local-ml` build paths retain classical
operation without installing models.
@@ -76,10 +78,12 @@ cargo run --release -p photocraft-engine --features local-ml --example local_mod
- `xtask perf --quick --bench perf_scenarios` passes for the existing selection/retouch scenarios;
it does not establish a sub-100 ms budget for the optional models.
The dedicated `local-models.yml` workflow adds Linux, Windows and macOS CPU runtime tests and
native build checks. Windows/Linux results must be verified in CI before removing the draft
status; they have not been run on this Mac. Quantitative accuracy evaluation, packaged installer
enablement, web inference, point prompts and GPU acceleration remain separate work.
The dedicated `local-models.yml` workflow runs Linux, Windows and macOS CPU runtime tests and
native build checks. The [initial cross-platform run](https://github.com/mighty-programmer/photocraft/actions/runs/37780207526)
passes on all three platforms. Final document-state regression tests are being rerun in the
same matrix before this contribution leaves draft. Upstream PR CI needs maintainer approval for
this first contribution. Quantitative accuracy evaluation, packaged installer / sandbox download
checks, web inference, point prompts and GPU acceleration remain separate work.
Image sources and licences: [SOURCES.md](images/SOURCES.md),
[LICENSE-local-models.txt](images/LICENSE-local-models.txt).
+8
View File
@@ -49,6 +49,14 @@ The desktop cache is `Models/` under the existing per-user configuration directo
preferences. Each immutable model revision gets its own directory with `LICENSE.txt` and
`SOURCE.txt`; inference verifies the hashes again, including SAM's external weights.
Linux uses the existing XDG configuration path, so an AppImage stores models outside its
read-only mounted image and a Flatpak uses its sandbox's XDG configuration directory. The current
Flatpak manifests omit network permission and do not enable `local-ml`. A distributor opting in
must also choose how explicit downloads receive network access (or prepopulate a verified cache);
this backend does not alter sandbox permissions. Windows uses `%APPDATA%\\Photocraft\\Models`
outside portable/override mode. Installer packaging and sandbox download checks remain separate
from the native CPU build matrix.
## Commands and CLI
| Command | Parameters |