Layers: Ctrl+C, Ctrl+X and Ctrl+V copy, cut and paste a layer

The desktop and web shells deliver these keys as clipboard events, not
key presses, so the layer shortcuts never fired. Outside a text field
the clipboard events now run the shortcuts bound to Ctrl+C, Ctrl+X and
Ctrl+V. Copying a layer by shortcut puts its name on the system
clipboard, since Ctrl+V only reaches the app while that holds text or
an image. Layer rows carry their name for screen readers and tests.

Fixes #90, fixes #94.
This commit is contained in:
bflatastic committed 2026-11-02 07:36:27 +07:00
1 parent 8a7f0ac708
commit fe2e1545a6
3 files changed
+71 -1

No files matched your search

+1
View File
@@ -277,6 +277,7 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
let selected = app.session.selected_layer() == Some(layer.id);
let h = 38.0;
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), h), Sense::click_and_drag());
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::ListItem, true, selected, &layer.name));
let p = ui.painter().clone();
let bg = if selected && app.session.target == StackTarget::Content {
tk.row_selected
+25 -1
View File
@@ -255,12 +255,13 @@ fn menu_item(app: &mut TextureCraftApp, ctx: &egui::Context, ui: &mut egui::Ui,
/// plus the fixed brush keys.
/// Keys pressed in a floating panel's own OS window run the shortcuts the main window's keys do.
#[cfg(not(target_arch = "wasm32"))]
pub fn forwarded_shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context, events: Vec<egui::Event>, _modifiers: Modifiers) {
pub fn forwarded_shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context, events: Vec<egui::Event>, held: Modifiers) {
if crate::keymap::capturing(ctx) {
return;
}
let active = crate::keymap::active(&app.ui.keymap);
for event in events {
let event = clipboard_key(&event, held).unwrap_or(event);
let egui::Event::Key { key, pressed: true, repeat: false, modifiers, .. } = event else { continue };
let Some((id, _)) = active.iter().find(|(_, sc)| sc.logical_key == key && modifiers.matches_logically(sc.modifiers)) else { continue };
invoke(app, ctx, id);
@@ -271,6 +272,10 @@ pub fn shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context) {
if crate::keymap::capturing(ctx) || (ctx.memory(|m| m.focused().is_some()) && ctx.egui_wants_keyboard_input()) {
return;
}
ctx.input_mut(|i| {
let keys: Vec<_> = i.events.iter().filter_map(|e| clipboard_key(e, i.modifiers)).collect();
i.events.extend(keys);
});
if !ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. }))) {
return;
}
@@ -295,6 +300,25 @@ pub fn shortcuts(app: &mut TextureCraftApp, ctx: &egui::Context) {
}
});
for id in fire {
if matches!(id, "layer.copy" | "layer.cut")
&& let Some(layer) = app.session.selected_layer().and_then(|l| app.session.doc.project.layers.get(&l))
{
// Ctrl+V reaches the app only while the system clipboard holds text or an image.
ctx.copy_text(layer.name.clone());
}
invoke(app, ctx, id);
}
}
/// The desktop and web shells turn Ctrl+C, Ctrl+X and Ctrl+V (and Ctrl+Insert, Shift+Delete,
/// Shift+Insert) into clipboard events instead of key presses. Outside a text field they are the
/// shortcuts bound to Ctrl+C, Ctrl+X and Ctrl+V (copy, cut and paste a layer by default).
fn clipboard_key(event: &egui::Event, held: Modifiers) -> Option<egui::Event> {
let key = match event {
egui::Event::Copy => Key::C,
egui::Event::Cut => Key::X,
egui::Event::Paste(_) | egui::Event::PasteImage(_) => Key::V,
_ => return None,
};
Some(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: held | Modifiers::COMMAND })
}
+45
View File
@@ -0,0 +1,45 @@
//! Ctrl+C, Ctrl+X and Ctrl+V on a layer clicked in the stack. The desktop and web shells deliver
//! these keys as clipboard events, not key presses, so the tests send those events.
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use texturecraft_engine::core::LayerId;
use texturecraft_ui_egui::TextureCraftApp;
/// The sample session with its selected layer clicked in the stack, and that layer.
fn harness() -> (Harness<'static, TextureCraftApp>, LayerId) {
let session = texturecraft_engine::sample_session(256, 64).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
h.run_steps(4);
let layer = h.state().session.selected_layer().unwrap();
let name = h.state().session.doc.project.layers[&layer].name.clone();
h.get_by_label(&name).click();
h.run_steps(3);
(h, layer)
}
fn count(h: &Harness<'_, TextureCraftApp>) -> usize {
h.state().session.doc.project.layers.len()
}
#[test]
fn ctrl_x_cuts_the_clicked_layer() {
let (mut h, layer) = harness();
h.event(egui::Event::Cut);
h.run_steps(3);
assert!(!h.state().session.doc.project.layers.contains_key(&layer), "Ctrl+X removes the layer");
assert!(h.state().session.clipboard.is_some(), "and keeps it on the layer clipboard");
}
#[test]
fn ctrl_c_then_ctrl_v_pastes_a_copy() {
let (mut h, _) = harness();
let before = count(&h);
h.event(egui::Event::Copy);
h.run_steps(3);
assert!(h.state().session.clipboard.is_some(), "Ctrl+C copies the layer");
// The shell sends the system clipboard's text with the paste; Ctrl+C put the layer name there.
h.event(egui::Event::Paste("Layer".into()));
h.run_steps(3);
assert!(count(&h) > before, "Ctrl+V pastes the layer");
}