Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Switching layers keeps the canvas when no pixel changes

Indicode BV5428f44ac528
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5319063..dc34ff7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ The first builds, and klad's licences.
- Dragging in a large drawing: shapes and strokes move under the pointer from the first move, wherever they are (in groups with effects, clipped, masked, under any blend), drawn in the background tile by tile; the layers above and the rest of the dragged layer stay in place (dragging a shape on the topmost layer no longer hid the shapes above it), and letting go redraws only the tiles the move touched.
- Edits on vector layers redraw only the tiles their shapes covered before and after, not the whole layer.
- Noise and haze no longer make every edit redraw the tiles around it.
+- Switching layers no longer redraws the whole canvas when nothing on it changes.
- Fixed tiles that could stay blank or out of date when a panel or popup was laid out for the first time while the canvas was being redrawn.
## 0.1.0 (2026-11-04)
diff --git a/crates/app/src/editor.rs b/crates/app/src/editor.rs
index bfa2ccd..3eab930 100644
--- a/crates/app/src/editor.rs
+++ b/crates/app/src/editor.rs
@@ -54,7 +54,7 @@ use klad_brush::{
use klad_core::brushes::{BrushDef, Curve, TipDef, pick_id};
use klad_core::bundle::now_ms;
use klad_core::color::parse_hex;
-use klad_core::doc::{Item, Kind, Linear, Loc, Node, Paint, Radial, Shape, Stop};
+use klad_core::doc::{Item, Kind, Linear, Loc, MASK, Node, Paint, Radial, Shape, Stop};
use klad_core::fill::{self, Fill, FillRef, FillSource, Seed};
use klad_core::history::Stack;
use klad_core::markedit::{self, LassoAction, MarkTransform, address, apply_affine, invert};
@@ -3897,6 +3897,9 @@ impl Editor {
return Ok(());
}
let id = self.scene.bundle.doc.locate_path(&path).map(|l| l.id.clone());
+ let tinted = self.scene.tint_mask && (self.scene.active_is_mask() || path.last() == Some(&MASK));
+ self.timeline.kept = (self.canvas_current() && !self.scene.onion_on && !tinted)
+ .then_some((self.scene.frame, self.scene.bundle.doc.rev));
self.scene.set_active_later(path);
if self.scene.onion_on {
self.frames_changed();
diff --git a/crates/app/src/editor/timeline.rs b/crates/app/src/editor/timeline.rs
index c4bde1f..f1e8e3e 100644
--- a/crates/app/src/editor/timeline.rs
+++ b/crates/app/src/editor/timeline.rs
@@ -98,6 +98,7 @@ pub(super) struct Timeline {
moved_at: Instant,
step_start: Option<Instant>,
pub canvas_frame: Option<u32>,
+ pub kept: Option<(u32, u64)>,
pub on_screen: Option<u32>,
shown_key: Option<u64>,
flip: Option<Flip>,
@@ -130,6 +131,7 @@ impl Default for Timeline {
moved_at: Instant::now(),
step_start: None,
canvas_frame: None,
+ kept: None,
on_screen: None,
shown_key: None,
flip: None,
@@ -407,9 +409,13 @@ impl Editor {
self.frames.heavy |= done.bytes > crate::frames::HEAVY;
self.scene.lean = self.frames.heavy;
let thumbs = std::mem::take(&mut done.thumbs);
+ let unchanged = self.timeline.kept.take() == Some((done.frame, self.scene.bundle.doc.rev))
+ && self.timeline.canvas_frame == Some(done.frame);
if self.scene.adopt(done, wanted) {
self.keep_thumbs(thumbs);
- self.dirty.extend(self.scene.all_tiles());
+ if !unchanged {
+ self.dirty.extend(self.scene.all_tiles());
+ }
self.timeline.canvas_frame = Some(self.scene.frame);
self.measure(
"canvas",
diff --git a/crates/app/src/editor/ui_tests.rs b/crates/app/src/editor/ui_tests.rs
index bef34ee..5eb1bdc 100644
--- a/crates/app/src/editor/ui_tests.rs
+++ b/crates/app/src/editor/ui_tests.rs
@@ -1110,6 +1110,43 @@ fn a_shape_previews_only_its_tiles_while_dragged_and_two_drags_in_a_row_both_lan
let _ = std::fs::remove_dir_all(&path);
}
+#[test]
+fn switching_layers_keeps_the_canvas_unless_the_picture_changes() {
+ let path = sample("switch");
+ let mut h = harness(editor(&path), vec2(1440.0, 900.0));
+ settle(&mut h, |ed| ed.canvas_current() && ed.dirty.is_empty());
+ let all = h.state().scene.all_tiles().len();
+ let switch = |h: &mut Harness<'static, Editor>, name: &str| {
+ let path = {
+ let ed = h.state();
+ let id = ed.scene.bundle.doc.find_layer(name).unwrap().id.clone();
+ match ed.scene.bundle.doc.locate(&id) {
+ Some(klad_core::doc::Loc::Layer(path)) => path,
+ _ => panic!("no layer {name}"),
+ }
+ };
+ h.state_mut().invoke(Cmd::ResetNumbers).unwrap();
+ h.state_mut().activate(path).unwrap();
+ let start = std::time::Instant::now();
+ while !h.state().canvas_current() && start.elapsed() < std::time::Duration::from_secs(20) {
+ h.run_steps(1);
+ std::thread::sleep(std::time::Duration::from_millis(2));
+ }
+ h.run_steps(2);
+ assert!(h.state().dirty.is_empty());
+ crate::stats::lock(&h.state().stats).tiles.max() as usize
+ };
+ assert_eq!(switch(&mut h, "shade"), 0, "a plain switch keeps every tile");
+ h.state_mut().invoke(Cmd::Onion).unwrap();
+ settle(&mut h, |ed| ed.canvas_current() && ed.dirty.is_empty());
+ assert_eq!(
+ switch(&mut h, "rim"),
+ all,
+ "with onion skins the picture follows the layer"
+ );
+ let _ = std::fs::remove_dir_all(&path);
+}
+
fn top_down(h: &Harness<'static, Editor>) -> Vec<String> {
h.state()
.layer_rows(super::layers::Where::Panel)
diff --git a/docs/DESIGN.md b/docs/DESIGN.md
index ce57a90..442a8c7 100644
--- a/docs/DESIGN.md
+++ b/docs/DESIGN.md
@@ -445,6 +445,7 @@ Scripts are any language that prints edit ops as JSON lines into `klad apply`. G
- Layers (M2 batch 4): layer settings sets clip to the layer below and lock alpha, adds a mask (empty, or hiding all), turns it on and off, removes it, and "paint on the mask" makes the mask the active layer (it also has its own row under its layer). Painting on a mask hides the layer under the strokes; the eraser shows it again. While a mask is active, what it hides is tinted red ("show the mask in red", on by default), and the brush panel says whether the brush or the eraser is on. The brush and fill tools record lock alpha from the active layer.
- Live previews and clipping: the editor composes the active layer between cached images of everything below and above it only when that is exact (the active layer and the groups around it are not clipped, masked or a clip base); otherwise each dirty tile composes the whole stack. A drag (a shape with V, strokes with S or V, any grab: move, scale, turn, stretch, an alt-drag copy) previews the edit it will commit: at its first move the editor takes a snapshot of the document and the pixels (tiles are shared, so this is cheap), and while the pointer moves a background job applies the drag's ops in memory, redraws only the tiles they change (vector nodes through the tile patching below, strokes through the cel's replay) and composes those tiles through the whole stack, so clips, masks, blends, group effects and everything above the dragged thing stay exactly right. One job runs at a time and the newest pointer position wins; only tiles on screen are composed while dragging. The composed tiles go straight to the canvas plane, which is shown while a preview is up. On release the edit is written at once (it costs only its tiles), a last job composes the final position, and its tiles are taken as they are when the edit only moved vector nodes; tiles the preview touched that the edit did not are composed again. In a lean scene, while a mask is tinted, or before the canvas has caught up, a drag shows only its outline and lands on release.
- Vector edits redraw only their tiles: a changed vector cel whose node order is kept is patched where its changed nodes were and are (their bounds grown for strokes, miter joins, blur and generator instances; added, removed and hidden nodes count), drawn on one surface that holds every node touching those tiles so no node is cut at a tile edge and the tiles are bit-identical to a full rasterization; a new order, or a node without bounds, redraws the cel. A swapped vector raster (an edit, undo or redo from the remembered rasters) dirties only the tiles whose pixels differ.
+- Switching the active layer rebuilds only the caches below and above it: when the canvas was complete for that frame and revision, onion skins are off and no mask is tinted, the canvas plane is kept instead of composed again.
- Composed tiles and frame pictures reach the GPU through an outbox the paint callback empties, holding the newest version of each tile: a UI pass that egui discards (a grid or popup laid out for the first time) drops its paint callback, and the tiles it carried would otherwise never be uploaded.
- Direct select (A, M2): click a path, rect or ellipse (inside groups too) to show its points; clicking the selected shape again steps down to the next shape under the pointer, and layer settings list them all; click or shift click points, drag on empty canvas for a box; drag points or handles; alt click the outline adds a point, alt click a point toggles corner and smooth, alt drag a handle breaks it; delete removes points; layer settings have corner, smooth, delete points, simplify and "make it a path". While dragging, the edited outline is drawn live; on release the edit is one `path` op whose layer is rendered in the background, then swapped in.
- Select and move (V, M4 test): a press picks what you see under the pen and drags it at once: the mark S would pick and the topmost vector shape there are compared by their layers' places in the stack and the higher one wins (clicking a shape again steps to the one under it); its layer becomes the active layer. With "this layer" only the active layer's marks or shapes are picked. A stroke picked this way is the same selection as S makes (its bar, layer settings, nudges, delete, copy), so V is the one tool for "move this". A press on empty canvas lassoes strokes on the active layer. Alt drag drags a copy: the copy is made (duplicate in place, one step) when the drag first moves, and the drag moves the copy (two history steps). A, the point editor, says so when a click lands on a brush stroke: strokes are samples, not paths.