mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Delete and Backspace delete the layer while a panel widget has focus (#1565)
A focused non-text widget (a Layers row on Windows) reserved Delete/Backspace as navigation keys, so the shortcut never fired (#1534). They now reach Delete Layer / Edit > Clear unless the focused widget uses them itself: the Curves graph claims them while focused (claim_delete_keys), so Delete still removes its selected point. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
64496178d0
commit
873e92dbaa
3 files changed
+26
-6
No files matched your search
@@ -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())
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user