Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Delete and Backspace delete the layer while a panel widget has focus (#1565)

buivanvu94873e92dbaad5
diff --git a/crates/ui-egui/src/point_curve.rs b/crates/ui-egui/src/point_curve.rs
index 57fa30c6e..74e542b8b 100644
--- a/crates/ui-egui/src/point_curve.rs
+++ b/crates/ui-egui/src/point_curve.rs
@@ -163,6 +163,9 @@ pub fn interact(ui: &mut Ui, resp: &Response, graph: Rect, pts: &mut Vec<[f32; 2
// Arrows edit this graph rather than navigating to another widget on the next frame.
ui.memory_mut(|m| m.set_focus_lock_filter(resp.id, egui::EventFilter { horizontal_arrows: true, vertical_arrows: true, ..Default::default() }));
// Keys while the graph has focus: Delete/Backspace removes, arrows nudge (Shift: ×10).
+ if resp.has_focus() {
+ crate::shortcut_dispatch::claim_delete_keys(ui.ctx(), resp.id);
+ }
if resp.has_focus()
&& let Some(i) = state.selected.filter(|i| *i < pts.len())
{
diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rs
index 24171701c..666549d47 100644
--- a/crates/ui-egui/src/shortcut_dispatch.rs
+++ b/crates/ui-egui/src/shortcut_dispatch.rs
@@ -104,23 +104,39 @@ pub fn bindings(app: &PhotocraftApp) -> Vec<(String, KeyboardShortcut)> {
pub enum Focus {
None,
Widget,
+ /// A focused widget that edits with ⌫ / Delete itself (a Curves graph deletes its selected
+ /// point), see [`claim_delete_keys`].
+ DeletingWidget,
Text,
}
+/// Where [`claim_delete_keys`] keeps the id of the widget that owns ⌫ / Delete while focused.
+fn delete_owner_id() -> egui::Id {
+ egui::Id::new("shortcut_dispatch::delete_owner")
+}
+
+/// Called by a widget that has keyboard focus and uses ⌫ / Delete itself (the Curves graph):
+/// while it keeps focus those keys reach it instead of Edit › Clear or Delete Layer. Every other
+/// focused widget (a Layers row, a slider) leaves them to the shortcuts, as in Photoshop (#1534).
+pub fn claim_delete_keys(ctx: &egui::Context, id: egui::Id) {
+ ctx.data_mut(|d| d.insert_temp(delete_owner_id(), id));
+}
+
impl Focus {
pub fn of(ctx: &egui::Context) -> Focus {
// A layer rename keeps its keys even in the frame egui drops its focus (Esc, Tab).
if ctx.text_edit_focused() || crate::layer_row_ui::rename_active(ctx) {
Focus::Text
} else if ctx.egui_wants_keyboard_input() {
- Focus::Widget
+ let owner = ctx.data(|d| d.get_temp::<egui::Id>(delete_owner_id()));
+ if owner.is_some() && ctx.memory(|m| m.focused()) == owner { Focus::DeletingWidget } else { Focus::Widget }
} else {
Focus::None
}
}
/// May `sc` fire with this focus? A focused widget keeps its navigation keys (arrows, ↩,
- /// Space, Delete…); a text field keeps everything but ⌘ shortcuts (minus its own editing
+ /// Space, Esc…, plus ⌫ / Delete when it claimed them); a text field keeps everything but ⌘ shortcuts (minus its own editing
/// ones: select all, clipboard, undo) and the function keys.
pub fn allows(self, sc: &KeyboardShortcut) -> bool {
let k = sc.logical_key;
@@ -152,8 +168,6 @@ impl Focus {
| Key::Space
| Key::Tab
| Key::Escape
- | Key::Backspace
- | Key::Delete
| Key::Home
| Key::End
| Key::PageUp
@@ -162,8 +176,11 @@ impl Focus {
match self {
Focus::None => true,
Focus::Widget => sc.modifiers.command || !navigation,
+ Focus::DeletingWidget => sc.modifiers.command || !(navigation || matches!(k, Key::Backspace | Key::Delete)),
Focus::Text => {
- let text_edit = !sc.modifiers.alt && matches!(k, Key::A | Key::C | Key::X | Key::V | Key::Z | Key::Y) || navigation;
+ let text_edit = !sc.modifiers.alt && matches!(k, Key::A | Key::C | Key::X | Key::V | Key::Z | Key::Y)
+ || navigation
+ || matches!(k, Key::Backspace | Key::Delete);
function || (sc.modifiers.command && !text_edit)
}
}
diff --git a/crates/ui-egui/src/shortcut_dispatch/tests.rs b/crates/ui-egui/src/shortcut_dispatch/tests.rs
index bcd6c76b0..ff6c1fac4 100644
--- a/crates/ui-egui/src/shortcut_dispatch/tests.rs
+++ b/crates/ui-egui/src/shortcut_dispatch/tests.rs
@@ -439,7 +439,7 @@ fn names(h: &Harness<'_, PhotocraftApp>) -> Vec<String> {
/// pixel layer on an adjustment layer.
#[test]
fn delete_without_a_selection_deletes_the_selected_layer() {
- for place in [Place::Canvas, Place::LayersRow] {
+ for place in [Place::Canvas, Place::LayersRow, Place::FocusedWidget] {
for key in ["Delete", "Backspace"] {
let mut h = harness();
put_focus(&mut h, place);