Add layer locking and local visibility controls

This commit is contained in:
Dandaedre 2026-11-13 12:07:02 +01:00
parent 38a0179d2f
commit f0da880d35
5 changed files with 191 additions and 7 deletions

View File

@ -354,8 +354,14 @@ impl Editor {
LINE
},
);
ink.text(13.0, y + 10.0, label(i), 15.0, color);
if click(ui, &ink, [0.0, y, w, 38.0], ("layer", i)) {
ink.text(
13.0,
y + 10.0,
label(i),
15.0,
if self.layout.visible(i) { color } else { MUTED },
);
if click(ui, &ink, [0.0, y, w - 136.0, 38.0], ("layer", i)) {
if click_shift_modifier(ui) {
self.layout.toggle(i);
} else {
@ -363,6 +369,47 @@ impl Editor {
}
self.sync();
}
for (kind, x, active, text) in [
(
"lock",
w - 130.0,
self.layout.locked(i),
if self.layout.locked(i) {
"Locked"
} else {
"Lock"
},
),
(
"visibility",
w - 64.0,
!self.layout.visible(i),
if self.layout.visible(i) {
"Hide"
} else {
"Show"
},
),
] {
ink.card(
x,
y + 5.0,
60.0,
28.0,
if active { PURPLE } else { PANEL },
LINE,
);
ink.text(x + 8.0, y + 12.0, text, 12.0, TEXT);
if click(ui, &ink, [x, y + 5.0, 60.0, 28.0], (kind, i)) {
self.marquee = None;
if kind == "lock" {
self.layout.set_locked(i, !active);
} else {
self.layout.set_visible(i, active);
}
self.sync();
}
}
}
ink.line([0.0, 130.0], [w, 130.0], LINE);
if self.layout.selected_indices().len() > 1 {
@ -742,6 +789,9 @@ impl Editor {
};
screen_artwork(&screen);
for (i, g) in self.layout.layers().iter().enumerate() {
if !self.layout.visible(i) {
continue;
}
let color = if i == 0 { AMBER } else { CYAN };
let rect = Rect::from_min_size(
origin + Vec2::new(g.x as f32, g.y as f32) * scale,
@ -1272,6 +1322,37 @@ mod tests {
.collect()
}
#[test]
fn layer_buttons_protect_selection_and_toggle_drawing() {
let mut app = Editor::default();
let ctx = egui::Context::default();
frame(&mut app, &ctx, vec![]);
let before = *app.layout.layers();
for (kind, expected_count) in [
("lock", 2),
("visibility", 1),
("visibility", 2),
("lock", 2),
] {
let rect = ctx
.read_response(egui::Id::new((kind, 1_usize)))
.unwrap()
.rect;
click_shift(&mut app, &ctx, rect.center());
let output = frame(&mut app, &ctx, vec![]);
assert_eq!(layer_rects(&output).len(), expected_count);
assert_eq!(app.layout.selected(), None);
assert_eq!(*app.layout.layers(), before);
}
assert!(!app.layout.locked(1));
assert!(app.layout.visible(1));
let row = ctx
.read_response(egui::Id::new(("layer", 1_usize)))
.unwrap()
.rect;
click_shift(&mut app, &ctx, row.center());
assert_eq!(app.layout.selected(), Some(1));
}
#[test]
fn shift_click_toggles_canvas_and_list_selection() {
let mut app = Editor::default();
let ctx = egui::Context::default();

View File

@ -103,6 +103,8 @@ pub enum AlignTarget {
pub struct Layout {
layers: [Geometry; 2],
selected: [bool; 2],
locked: [bool; 2],
visible: [bool; 2],
undo: Vec<[Geometry; 2]>,
redo: Vec<[Geometry; 2]>,
drag: Option<([bool; 2], [Geometry; 2], [f64; 2])>,
@ -125,6 +127,8 @@ impl Default for Layout {
},
],
selected: [false, true],
locked: [false; 2],
visible: [true; 2],
undo: Vec::new(),
redo: Vec::new(),
drag: None,
@ -135,6 +139,34 @@ impl Layout {
pub fn layers(&self) -> &[Geometry; 2] {
&self.layers
}
pub fn locked(&self, index: usize) -> bool {
self.locked.get(index).copied().unwrap_or(false)
}
pub fn visible(&self, index: usize) -> bool {
self.visible.get(index).copied().unwrap_or(false)
}
fn selectable(&self, index: usize) -> bool {
self.visible(index) && !self.locked(index)
}
/// Editor protection flags do not change geometry or its undo history.
pub fn set_locked(&mut self, index: usize, locked: bool) {
self.cancel_drag();
if let Some(value) = self.locked.get_mut(index) {
*value = locked;
if locked {
self.selected[index] = false;
}
}
}
pub fn set_visible(&mut self, index: usize, visible: bool) {
self.cancel_drag();
if let Some(value) = self.visible.get_mut(index) {
*value = visible;
if !visible {
self.selected[index] = false;
}
}
}
/// A single selected layer, or None for zero/multiple selections.
pub fn selected(&self) -> Option<usize> {
let indices = self.selected_indices();
@ -152,13 +184,16 @@ impl Layout {
}
pub fn set_selection(&mut self, selected: [bool; 2]) {
self.finish_drag();
self.selected = selected;
self.selected = std::array::from_fn(|i| selected[i] && self.selectable(i));
}
pub fn select(&mut self, index: Option<usize>) {
self.set_selection(std::array::from_fn(|i| index == Some(i)));
}
pub fn toggle(&mut self, index: usize) {
self.finish_drag();
if !self.selectable(index) {
return;
}
if let Some(selected) = self.selected.get_mut(index) {
*selected = !*selected;
}
@ -245,7 +280,9 @@ impl Layout {
Ok(())
}
pub fn hit(&self, point: [f64; 2]) -> Option<usize> {
(0..2).rev().find(|i| self.layers[*i].contains(point))
(0..2)
.rev()
.find(|i| self.selectable(*i) && self.layers[*i].contains(point))
}
fn remember(&mut self, before: [Geometry; 2]) {
if before != self.layers {
@ -261,6 +298,9 @@ impl Layout {
if index >= 2 {
return Err("Select an existing layer.".into());
}
if !self.selectable(index) {
return Err("Unlock and show the layer before editing it.".into());
}
self.finish_drag();
let before = self.layers;
self.layers[index] = geometry;

View File

@ -507,3 +507,58 @@ fn alignment_moves_only_selected_layers_and_is_order_independent() {
l.align(A::Left, T::Selection).unwrap();
assert_eq!(*l.layers(), first);
}
#[test]
fn protected_layers_are_excluded_from_selection_and_edits() {
for hidden in [false, true] {
let mut layout = Layout::default();
let before = *layout.layers();
layout.set_selection([true; 2]);
if hidden {
layout.set_visible(1, false);
} else {
layout.set_locked(1, true);
}
assert_eq!(layout.selected(), Some(0));
assert_eq!(layout.hit(before[1].center()), None);
layout.select(Some(1));
layout.toggle(1);
assert_eq!(layout.selected(), None);
layout.select_region([0.0, 0.0], [1920.0, 1080.0], [true; 2]);
assert_eq!(layout.selected(), Some(0));
layout.nudge(10.0, 20.0).unwrap();
layout
.align(
midra_layout::Alignment::Left,
midra_layout::AlignTarget::Screen,
)
.unwrap();
assert_eq!(layout.layers()[1], before[1]);
assert!(layout.apply(1, before[0]).is_err());
layout.start_drag(before[1].center());
assert!(!layout.dragging());
layout.set_visible(1, true);
layout.set_locked(1, false);
layout.select(Some(1));
assert_eq!(layout.selected(), Some(1));
assert_eq!(layout.layers()[1], before[1]);
}
}
#[test]
fn locking_during_a_group_drag_restores_geometry_and_preserves_visibility() {
let mut layout = Layout::default();
let before = *layout.layers();
layout.set_selection([true; 2]);
layout.start_drag(before[0].center());
layout.drag_to([500.0, 500.0]);
layout.set_locked(0, true);
assert_eq!(*layout.layers(), before);
assert!(!layout.dragging());
assert!(layout.visible(0));
layout.set_visible(0, false);
layout.set_visible(0, true);
assert!(layout.locked(0));
layout.select(Some(0));
assert_eq!(layout.selected(), None);
}

View File

@ -280,14 +280,15 @@ explicit authorization at that time. No API calls are part of this planning task
### 11. Later stretch goal: layer management and export scope
Status: requested for later. This is local document organization; it can be
Status: per-layer visibility and locking are implemented for the two current layers;
the remaining management features are requested for later. This is local document organization; it can be
implemented independently of step 10 and does not authorize device connectivity.
Folders are an optional follow-up, not a prerequisite for adding/hiding layers.
- [ ] Replace the fixed two-layer document with a collection using stable local
IDs. Add an **Add layer** button and editable names. New local layers start
visible and unmapped; adding a planning layer does not create a hardware layer.
- [ ] Add per-layer **Show / Hide** controls in the layer list. Hidden layers
- [x] Add per-layer **Show / Hide** controls in the layer list. Hidden layers
keep their geometry and mapping, remain accessible in the list, and are excluded
from canvas drawing and hit testing. Hiding is local only: never send a device
hide, clear, opacity or routing command as a side effect.

View File

@ -126,7 +126,7 @@ formats, custom output resolution, image import, cropping and persistent
session files are future work. Sessions currently live only in memory.
[Layer management and export scope](midra-layout-plan.md#11-later-stretch-goal-layer-management-and-export-scope)
are also later stretch goals: **Add layer**, local **Show / Hide**, Midra layer
are also later stretch goals: **Add layer**, Midra layer
mapping in **Options**, and export of a **specific layer** or **visible layers
only**. Optional folders would organize local layers. Visibility and organization
will not send commands or change the live device.
@ -153,3 +153,10 @@ across list entries cannot accidentally scroll while selecting.
See the [project plan](midra-layout-plan.md) and
[resolution/cropping specification](midra-layout-resolutions.md) for future work.
Each layer row has **Lock / Locked** and **Hide / Show** buttons. Locked layers
remain visible but cannot be selected, dragged, nudged, aligned or edited. Hidden
layers keep their geometry and stay in the list, but are excluded from drawing
and selection. Locking or hiding clears that layer's selection; showing it does
not unlock it. These are local session controls, separate from geometry undo/redo
(which can still restore earlier geometry), and never send device commands.