mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-30 10:03:56 +01:00
Merge commit 'e0a3e6e93c1114ae06e834ea986e37628c1b9fc7' into HEAD
This commit is contained in:
commit
02752b2f27
3 files changed
+245
-34
No files matched your search
+199
-32
@@ -2,11 +2,11 @@
|
||||
//! overrides (`UiState::keymap`, id → "Ctrl+Shift+E" or "" for unbound), conflict detection and
|
||||
//! the shortcut editor window (Help ▸ Shortcuts list) and the Edit ▸ Settings ▸ Shortcuts page.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::shortcut_dispatch::Dispatcher;
|
||||
use craft_keymap::{Active, Chord, CommandDef, CommandRef, ConflictKind, KeyInput, Layout, Lookup, Mods, Overrides, Platform, Scope, Scopes};
|
||||
use craft_keymap::{Active, Chord, CommandDef, CommandRef, ConflictKind, KeyInput, Layout, Lookup, Mods, Overrides, Platform, Scope, Scopes, Trigger};
|
||||
use egui::{Event, Id, Key, KeyboardShortcut, Modifiers, RichText, vec2};
|
||||
use egui_phosphor::regular as ic;
|
||||
|
||||
@@ -189,22 +189,58 @@ pub fn format_shortcut(sc: &KeyboardShortcut) -> String {
|
||||
s
|
||||
}
|
||||
|
||||
/// Every bound shortcut as egui's shortcut type.
|
||||
fn active(keymap: &Keymap) -> Vec<KeyboardShortcut> {
|
||||
bindings().iter().filter_map(|b| Some(parse(shortcut(keymap, b)?)?.to_egui_shortcut())).collect()
|
||||
const REDO_COMMAND_ID: &str = "edit.redo";
|
||||
const ALTERNATE_REDO_SHORTCUT: Chord = Chord::new(Mods { shift: true, ..Mods::CMD }, craft_keymap::Key::Z);
|
||||
|
||||
/// Keep the alternate in Redo's defaults unless a registered binding claims it.
|
||||
fn alternate_redo_shortcut(overrides: &Overrides, platform: Platform) -> Option<Chord> {
|
||||
let alternate = Trigger::single(ALTERNATE_REDO_SHORTCUT);
|
||||
let app_scope = Scope::app();
|
||||
let mut seen_variant_ids = BTreeSet::new();
|
||||
for (key, texts) in overrides.iter() {
|
||||
let command;
|
||||
let id = if key.contains(' ') {
|
||||
let Some(parsed) = CommandRef::parse_key(key) else { continue };
|
||||
command = parsed;
|
||||
|
||||
// The shared keymap prefers exact keys, then the first variant in key order
|
||||
if command.key_text() != command.id || overrides.get(&command.id).is_some() || !seen_variant_ids.insert(command.id.clone()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
command.id.as_str()
|
||||
} else {
|
||||
key
|
||||
};
|
||||
|
||||
let claims_alternate = texts
|
||||
.iter()
|
||||
.filter_map(|text| craft_keymap::Binding::parse(text).ok())
|
||||
.any(|binding| binding.scope_or(&app_scope).is_app() && binding.trigger.effective(platform) == alternate);
|
||||
if claims_alternate && bindings().iter().any(|binding| binding.id == id) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
Some(ALTERNATE_REDO_SHORTCUT)
|
||||
}
|
||||
|
||||
/// The catalog as craft-keymap sees it, in `bindings()` order: the first one listed wins a shared
|
||||
/// shortcut.
|
||||
fn catalog() -> Vec<CommandDef> {
|
||||
/// The catalog for craft-keymap in `bindings()` order. The first listed command wins a shared shortcut.
|
||||
fn catalog(alternate_redo: Option<Chord>) -> Vec<CommandDef> {
|
||||
bindings()
|
||||
.iter()
|
||||
.map(|b| {
|
||||
let item = CommandDef::new(b.id, b.label, Scope::app());
|
||||
match b.default.and_then(parse) {
|
||||
Some(chord) => item.with_default(craft_keymap::Binding::new(chord)),
|
||||
None => item,
|
||||
.map(|binding| {
|
||||
let mut command = CommandDef::new(binding.id, binding.label, Scope::app());
|
||||
if let Some(chord) = binding.default.and_then(parse) {
|
||||
command = command.with_default(craft_keymap::Binding::new(chord));
|
||||
}
|
||||
if binding.id == REDO_COMMAND_ID
|
||||
&& let Some(chord) = alternate_redo
|
||||
{
|
||||
command = command.with_default(craft_keymap::Binding::new(chord));
|
||||
}
|
||||
|
||||
command
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -223,7 +259,8 @@ fn overrides(keymap: &Keymap) -> Overrides {
|
||||
let mut out = Overrides::new();
|
||||
for (id, text) in keymap {
|
||||
let texts = if text.is_empty() { Vec::new() } else { vec![override_text(text)] };
|
||||
// Only an overlong id or text is refused, and neither can be a shortcut.
|
||||
|
||||
// The shared keymap enforces its storage limits here
|
||||
let _ = out.insert(id.clone(), texts);
|
||||
}
|
||||
out
|
||||
@@ -231,7 +268,10 @@ fn overrides(keymap: &Keymap) -> Overrides {
|
||||
|
||||
/// The keymap with `keymap`'s shortcuts for `platform`.
|
||||
fn resolved(keymap: &Keymap, platform: Platform) -> craft_keymap::Keymap {
|
||||
craft_keymap::Keymap::new(catalog(), Scopes::new(), platform).with_overrides(overrides(keymap)).with_delete_keys(craft_keymap::DeleteKeys::Apart)
|
||||
let overrides = overrides(keymap);
|
||||
let commands = catalog(alternate_redo_shortcut(&overrides, platform));
|
||||
|
||||
craft_keymap::Keymap::new(commands, Scopes::new(), platform).with_overrides(overrides).with_delete_keys(craft_keymap::DeleteKeys::Apart)
|
||||
}
|
||||
|
||||
/// The resolved keymap and the dispatcher that runs it against each frame's keys.
|
||||
@@ -261,6 +301,12 @@ impl Bindings {
|
||||
}
|
||||
}
|
||||
|
||||
/// Refresh browser capture from the same bindings used for dispatch.
|
||||
#[cfg(any(test, target_arch = "wasm32"))]
|
||||
pub(crate) fn sync_captured(&self) {
|
||||
refresh_captured(&self.stored, self.map.platform(), || alternate_is_bound(&self.map));
|
||||
}
|
||||
|
||||
/// The ids of the commands this frame's key presses run, in press order. The presses are
|
||||
/// removed from the input.
|
||||
pub(crate) fn dispatch(&mut self, input: &mut egui::InputState) -> Vec<String> {
|
||||
@@ -324,33 +370,61 @@ pub fn rebind(keymap: &mut Keymap, b: &Binding, value: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The bound shortcuts the browser must not act on, for the web build's `keydown` listener:
|
||||
/// the keymap they were taken from and the shortcuts.
|
||||
static CAPTURED: std::sync::Mutex<Option<(Keymap, Vec<KeyboardShortcut>)>> = std::sync::Mutex::new(None);
|
||||
/// The keymap, platform and shortcuts for the web build's `keydown` listener.
|
||||
static CAPTURED: std::sync::Mutex<Option<(Keymap, Platform, Vec<KeyboardShortcut>)>> = std::sync::Mutex::new(None);
|
||||
|
||||
/// Refresh [`captured`] when the keymap changed (once per frame; cheap when it didn't).
|
||||
/// Refresh [`captured`] from stored shortcuts for the current platform.
|
||||
pub fn sync_captured(keymap: &Keymap) {
|
||||
let mut c = CAPTURED.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if c.as_ref().is_some_and(|(k, _)| k == keymap) {
|
||||
let platform = Platform::current();
|
||||
refresh_captured(keymap, platform, || alternate_is_bound(&resolved(keymap, platform)));
|
||||
}
|
||||
|
||||
fn alternate_is_bound(map: &craft_keymap::Keymap) -> bool {
|
||||
!matches!(map.lookup(&ALTERNATE_REDO_SHORTCUT, false, &Active::NONE), Lookup::None)
|
||||
}
|
||||
|
||||
fn refresh_captured(keymap: &Keymap, platform: Platform, alternate_bound: impl FnOnce() -> bool) {
|
||||
let mut captured = CAPTURED.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if captured.as_ref().is_some_and(|(stored, stored_platform, _)| stored == keymap && *stored_platform == platform) {
|
||||
return;
|
||||
}
|
||||
// Copy, cut and paste stay with the browser: egui reads them as clipboard events.
|
||||
|
||||
// Copy, cut and paste stay with the browser because egui reads clipboard events
|
||||
let clipboard = [Key::C, Key::V, Key::X];
|
||||
let keys: Vec<KeyboardShortcut> = active(keymap)
|
||||
.into_iter()
|
||||
.filter(|sc| {
|
||||
let fkey = sc.logical_key.name().strip_prefix('F').is_some_and(|n| n.parse::<u8>().is_ok());
|
||||
(sc.modifiers.command || sc.modifiers.alt || fkey) && !(sc.modifiers.command && !sc.modifiers.shift && clipboard.contains(&sc.logical_key))
|
||||
let mut keys: Vec<KeyboardShortcut> = bindings()
|
||||
.iter()
|
||||
.filter_map(|binding| parse(shortcut(keymap, binding)?))
|
||||
.filter(|chord| chord.effective(platform) != ALTERNATE_REDO_SHORTCUT)
|
||||
.map(|chord| chord.to_egui_shortcut())
|
||||
.filter(|shortcut| {
|
||||
let function_key = shortcut.logical_key.name().strip_prefix('F').is_some_and(|number| number.parse::<u8>().is_ok());
|
||||
let clipboard_key = shortcut.modifiers.command && !shortcut.modifiers.shift && clipboard.contains(&shortcut.logical_key);
|
||||
(shortcut.modifiers.command || shortcut.modifiers.alt || function_key) && !clipboard_key
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Only admitted bindings can claim the alternate for the app
|
||||
if alternate_bound() {
|
||||
keys.push(ALTERNATE_REDO_SHORTCUT.to_egui_shortcut());
|
||||
}
|
||||
|
||||
log::debug!("browser shortcuts taken over: {}", keys.len());
|
||||
*c = Some((keymap.clone(), keys));
|
||||
*captured = Some((keymap.clone(), platform, keys));
|
||||
}
|
||||
|
||||
/// Whether a key press is one of the app's shortcuts the browser must leave alone.
|
||||
pub fn captured(mods: Modifiers, key: Key) -> bool {
|
||||
let c = CAPTURED.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
c.as_ref().is_some_and(|(_, keys)| keys.iter().any(|sc| sc.logical_key == key && mods.matches_logically(sc.modifiers)))
|
||||
let captured = CAPTURED.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let Some((_, platform, keys)) = captured.as_ref() else { return false };
|
||||
|
||||
keys.iter().filter(|shortcut| shortcut.logical_key == key).any(|shortcut| {
|
||||
// Undo and Redo distinguish Shift on the same letter
|
||||
if key == Key::Z {
|
||||
Mods::from_egui(&mods, *platform) == Mods::from_egui(&shortcut.modifiers, *platform)
|
||||
} else {
|
||||
mods.matches_logically(shortcut.modifiers)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const CAPTURE: &str = "keymap_capture";
|
||||
@@ -722,12 +796,35 @@ mod tests {
|
||||
fn the_browser_leaves_app_shortcuts_alone_but_keeps_the_clipboard() {
|
||||
let mut keymap = Keymap::new();
|
||||
keymap.insert("file.close".into(), "Alt+W".into());
|
||||
sync_captured(&keymap);
|
||||
|
||||
keymap.insert("edit.undo".into(), String::new());
|
||||
let mut resolved = bindings_for(&keymap);
|
||||
resolved.sync_captured();
|
||||
|
||||
let ctrl = Modifiers { ctrl: true, command: true, ..Modifiers::NONE };
|
||||
assert!(captured(ctrl, Key::S) && captured(ctrl, Key::O) && captured(ctrl, Key::D));
|
||||
assert!(captured(Modifiers { shift: true, ..ctrl }, Key::Z), "the second Redo key is taken over too");
|
||||
assert!(captured(Modifiers::NONE, Key::F9) && captured(Modifiers::ALT, Key::W));
|
||||
assert!(!captured(ctrl, Key::C) && !captured(ctrl, Key::V) && !captured(ctrl, Key::X), "clipboard keys reach the browser");
|
||||
assert!(!captured(Modifiers::NONE, Key::M) && !captured(Modifiers::NONE, Key::F), "plain keys need no capture");
|
||||
|
||||
keymap.remove("edit.undo");
|
||||
keymap.insert("edit.redo".into(), String::new());
|
||||
resolved.sync(&keymap, Platform::Windows);
|
||||
resolved.sync_captured();
|
||||
assert!(!captured(Modifiers { shift: true, ..ctrl }, Key::Z), "clearing Redo gives its second key back");
|
||||
|
||||
keymap.insert("file.save".into(), "Control+Shift+Z".into());
|
||||
resolved.sync(&keymap, Platform::Windows);
|
||||
resolved.sync_captured();
|
||||
assert!(captured(Modifiers { shift: true, ..ctrl }, Key::Z), "a custom binding also takes the key from the browser");
|
||||
|
||||
resolved.sync(&Keymap::new(), Platform::Mac);
|
||||
resolved.sync_captured();
|
||||
|
||||
let command = Modifiers::MAC_CMD | Modifiers::COMMAND;
|
||||
assert!(captured(command | Modifiers::SHIFT, Key::Z));
|
||||
assert!(!captured(ctrl | Modifiers::SHIFT, Key::Z), "physical Control on macOS does not run Redo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -887,6 +984,72 @@ mod tests {
|
||||
assert_eq!(run(Modifiers::SHIFT, Key::M), None, "Shift+M is not bound");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_default_key_yields_to_the_stored_shortcuts() {
|
||||
let second_press = press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT);
|
||||
let run = |keymap: &Keymap| bindings_for(keymap).command_of(&second_press);
|
||||
let mut keymap = Keymap::new();
|
||||
|
||||
assert_eq!(run(&keymap).as_deref(), Some("edit.redo"));
|
||||
assert_eq!(bindings_for(&keymap).command_of(&press(Key::Y, Modifiers::COMMAND)).as_deref(), Some("edit.redo"));
|
||||
assert_eq!(shortcut_of(&keymap, "edit.redo"), Some("Ctrl+Y"), "menus keep showing the first key");
|
||||
|
||||
// Another item the user put on that key keeps it, without a shared-shortcut warning
|
||||
keymap.insert("file.save".into(), "Ctrl+Shift+Z".into());
|
||||
assert_eq!(run(&keymap).as_deref(), Some("file.save"));
|
||||
assert!(bindings_for(&keymap).conflict(binding("file.save")).is_none());
|
||||
|
||||
// Changing or clearing Redo replaces both of its keys
|
||||
for stored in ["F7", ""] {
|
||||
keymap = Keymap::from([("edit.redo".into(), stored.into())]);
|
||||
assert_eq!(run(&keymap), None, "Redo stored as {stored:?}");
|
||||
}
|
||||
|
||||
rebind(&mut keymap, binding("edit.redo"), "Ctrl+Y");
|
||||
|
||||
assert_eq!(run(&keymap).as_deref(), Some("edit.redo"), "reset restores the second key");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unreadable_redo_setting_falls_back_to_both_defaults() {
|
||||
let keymap = Keymap::from([("edit.redo".into(), "UnknownFutureKey".into())]);
|
||||
let resolved = bindings_for(&keymap);
|
||||
|
||||
assert_eq!(resolved.command_of(&press(Key::Y, Modifiers::COMMAND)).as_deref(), Some("edit.redo"));
|
||||
assert_eq!(resolved.command_of(&press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT)).as_deref(), Some("edit.redo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_command_does_not_take_redos_second_key() {
|
||||
let keymap = Keymap::from([("removed.command".into(), "Ctrl+Shift+Z".into())]);
|
||||
let resolved = bindings_for(&keymap);
|
||||
|
||||
assert_eq!(resolved.command_of(&press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT)).as_deref(), Some("edit.redo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_platform_equivalent_stored_shortcut_takes_redos_second_key() {
|
||||
let keymap = Keymap::from([("file.save".into(), "Control+Shift+Z".into())]);
|
||||
let resolved = bindings_for(&keymap);
|
||||
|
||||
assert_eq!(resolved.command_of(&press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT)).as_deref(), Some("file.save"));
|
||||
assert!(resolved.conflict(binding("file.save")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shadowed_override_does_not_take_redos_second_key() {
|
||||
let mut keymap = Keymap::from([("file.save {}".into(), "Ctrl+Shift+Z".into()), ("file.save".into(), "F7".into())]);
|
||||
let mut resolved = bindings_for(&keymap);
|
||||
let second_press = press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT);
|
||||
|
||||
assert_eq!(resolved.command_of(&second_press).as_deref(), Some("edit.redo"));
|
||||
|
||||
keymap.remove("file.save");
|
||||
resolved.sync(&keymap, Platform::Windows);
|
||||
|
||||
assert_eq!(resolved.command_of(&second_press).as_deref(), Some("file.save"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stored_shortcut_replaces_the_default_and_an_empty_one_unbinds() {
|
||||
let mut keymap = Keymap::new();
|
||||
@@ -903,10 +1066,14 @@ mod tests {
|
||||
#[test]
|
||||
fn ctrl_in_the_notation_is_the_command_key_on_macos() {
|
||||
let mut resolved = Bindings::default();
|
||||
resolved.sync(&Keymap::new(), Platform::Mac);
|
||||
let keymap = Keymap::from([("file.close".into(), "Control+Shift+Z".into())]);
|
||||
resolved.sync(&keymap, Platform::Mac);
|
||||
let cmd = Modifiers { mac_cmd: true, command: true, ..Modifiers::NONE };
|
||||
|
||||
assert_eq!(resolved.command_of(&press(Key::S, cmd)).as_deref(), Some("file.save"));
|
||||
assert_eq!(resolved.command_of(&press(Key::S, Modifiers::CTRL)), None, "the Control key is another key");
|
||||
assert_eq!(resolved.command_of(&press(Key::Z, cmd | Modifiers::SHIFT)).as_deref(), Some("edit.redo"));
|
||||
assert_eq!(resolved.command_of(&press(Key::Z, Modifiers::CTRL | Modifiers::SHIFT)).as_deref(), Some("file.close"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1405,16 +1405,16 @@ impl eframe::App for TextureCraftApp {
|
||||
autosave_notice::poll(self);
|
||||
}
|
||||
i18n::set_language(&self.ui.prefs.language);
|
||||
self.bindings.sync(&self.ui.keymap, craft_keymap::egui::platform(ctx));
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
keymap::sync_captured(&self.ui.keymap);
|
||||
self.bindings.sync_captured();
|
||||
web_files::sync_unsaved(self);
|
||||
}
|
||||
self.fonts.update(ctx, i18n::current().code, self.ui.windows.settings);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
updates_ui::tick(self, ctx);
|
||||
|
||||
self.bindings.sync(&self.ui.keymap, craft_keymap::egui::platform(ctx));
|
||||
menus::shortcuts(self, ctx);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
|
||||
@@ -56,6 +56,50 @@ fn a_held_key_runs_its_command_again() {
|
||||
assert!(!hidden(&h), "the repeat toggled it back");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn browser_capture_follows_redo_changes_in_the_stored_keymap() {
|
||||
use texturecraft_ui_egui::keymap::{Keymap, captured, sync_captured};
|
||||
|
||||
let mut keymap = Keymap::from([("edit.undo".into(), String::new())]);
|
||||
let redo_modifiers = Modifiers::COMMAND | Modifiers::SHIFT;
|
||||
|
||||
sync_captured(&keymap);
|
||||
assert!(captured(redo_modifiers, Key::Z));
|
||||
|
||||
keymap.insert("edit.redo".into(), String::new());
|
||||
sync_captured(&keymap);
|
||||
assert!(!captured(redo_modifiers, Key::Z));
|
||||
|
||||
// Fill the shared resolver before the saved file command is admitted
|
||||
for index in 0..craft_keymap::MAX_COMMANDS {
|
||||
keymap.insert(format!("expired.{index}"), String::new());
|
||||
}
|
||||
keymap.insert("file.save".into(), "Ctrl+Shift+Z".into());
|
||||
sync_captured(&keymap);
|
||||
|
||||
assert!(!captured(redo_modifiers, Key::Z), "an ignored binding leaves the browser shortcut free");
|
||||
|
||||
sync_captured(&Keymap::new());
|
||||
assert!(captured(redo_modifiers, Key::Z), "reset restores capture");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_shift_z_redoes_the_undone_step() {
|
||||
let mut harness = harness();
|
||||
texturecraft_engine::execute(&mut harness.state_mut().session, "layer.add.fill", &serde_json::Value::Null).unwrap();
|
||||
let labels_after_add = harness.state().session.history_labels();
|
||||
|
||||
harness.state_mut().session.undo().unwrap();
|
||||
harness.run_steps(2);
|
||||
|
||||
assert_ne!(harness.state().session.history_labels(), labels_after_add, "the step is undone");
|
||||
|
||||
harness.event(press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT, false));
|
||||
harness.run_steps(2);
|
||||
|
||||
assert_eq!(harness.state().session.history_labels(), labels_after_add);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shortcut_stored_in_the_settings_replaces_the_default() {
|
||||
let mut h = harness();
|
||||
|
||||
Reference in new issue
Block a user