mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-23 00:44:15 +01:00
On NixOS the key bindings belong in the configuration, with the rest of
komorebi. services.komorebi.keybindings takes the format (skhd, whkd or sxhkd),
the bindings as text or as a file (exactly one of the two, asserted), the mode
(auto, engine or static; engine by default, as PlanMode::default() is),
chordTimeout (3000 ms), layout (us, the only one), strict and appAliases. skhd's
.load is refused by an assertion: the file it reads would not be in the store.
At build time the module runs komorebic hotkeys compile on the file in the
package komorebi-keybindings, whose share/kglobalaccel goes into the system
profile, where KWin finds the static shortcuts when the session starts. Its
diagnostics are in the build log and in diagnostics.txt, and strict passes
--strict, which fails the build on a warning or an error. execStart gains
--keybindings with the same store file, its format and mode, --chord-timeout and
an --app-alias per alias, so the daemon plans the file as the build did and runs
the bindings the build left to the engine. The unit already releases the keys
after the daemon stops.
The check keybindings-fixtures runs every file of the fixture corpus through
komorebic hotkeys compile --mode auto --dry-run, the command the module runs,
and diffs what it prints against nix/tests/keybindings.expected; it also checks
that docs/{skhdrc,whkdrc,sxhkdrc}.sample are the corpus's sample case.
nixos-module-eval gains the key binding scenarios: text, file, both, no format,
.load and a layout that is not us, and one with the typed config together with
key bindings, whose execStart has the checked file and then the keys. The corpus
gains the case vm, in the three formats: a binding a shortcut file can hold, and
one on the comma key, which X-KDE-Shortcuts cannot, so that the engine runs it
in auto mode. The VM suites vm-keys-skhd, vm-keys-whkd and vm-keys-sxhkd
configure the module with that case in their format and press both keys; they
share one script through the generator's script attribute.
For strict, nixos-module-eval has two scenarios with the same skhd text, which
has a line komorebic cannot read: strict = true and the default. Evaluation
never reads the key bindings, so the checks read the script of the build: with
strict it runs hotkeys compile --strict and exits 1 when that fails, without it
there is no --strict and the diagnostics are only printed to the build log. A
third check says the daemon gets the same command line either way. ARCHITECTURE
names them in the row for strict.
komorebic quickstart writes the sample skhdrc next to komorebi.json and
applications.json, from docs/skhdrc.sample, where the daemon and hotkeys compile
read it; the flake's source filter keeps that file for the build.
Docs: the user guide docs/usage/keybindings.md, the three sample files,
docs/example-configurations.md for KDE and NixOS, the options in
docs/installation.md, and the module's section of ARCHITECTURE. Whether the
engine or static shortcuts should be the default stays open in DESIGN: a static
shortcut took 233 ms to run its command at the median on KWin 6.8.1, the engine
3 ms, but only the static shortcut survives a dead daemon.
142 lines
4.7 KiB
Rust
142 lines
4.7 KiB
Rust
//! The whkd reader against whkd's own parser, `whkd-parser`, on the whkdrc files of the corpus and
|
|
//! on the example of whkd's README: both must read the same bindings.
|
|
|
|
use komorebi_hotkeys::App;
|
|
use komorebi_hotkeys::Body;
|
|
use komorebi_hotkeys::Format;
|
|
use komorebi_hotkeys::ParseOptions;
|
|
use komorebi_hotkeys::Run;
|
|
use komorebi_hotkeys::Scope;
|
|
use komorebi_hotkeys::Then;
|
|
use komorebi_hotkeys::qt::Combo;
|
|
use komorebi_hotkeys::qt::Mods;
|
|
use komorebi_hotkeys::qt::key_to_qt;
|
|
use komorebi_hotkeys::whkd;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
/// The bindings of a file, as (keys, what they do), and the pause binding with its hook.
|
|
type Read = (Vec<(Combo, Body)>, Option<(Combo, Option<String>)>);
|
|
|
|
/// The combo a list of whkd key names stands for: the last is the key, the others modifiers.
|
|
fn combo(keys: &[String]) -> Combo {
|
|
let (key, modifiers) = keys.split_last().unwrap();
|
|
let mut mods = Mods::NONE;
|
|
for modifier in modifiers {
|
|
mods |= match modifier.to_ascii_lowercase().as_str() {
|
|
"alt" => Mods::ALT,
|
|
"ctrl" => Mods::CTRL,
|
|
"shift" => Mods::SHIFT,
|
|
"win" => Mods::META,
|
|
other => panic!("the corpus has no modifier {other}"),
|
|
};
|
|
}
|
|
Combo::new(mods, key_to_qt(Format::Whkd, key).unwrap())
|
|
}
|
|
|
|
/// What whkd-parser reads.
|
|
fn theirs(path: &Path) -> Read {
|
|
let whkdrc = whkd_parser::load(&path.to_path_buf()).unwrap();
|
|
let mut bindings = Vec::new();
|
|
for (keys, apps) in &whkdrc.app_bindings {
|
|
let scopes = apps
|
|
.iter()
|
|
.map(|binding| Scope {
|
|
app: match binding.process_name.as_deref() {
|
|
Some("Default") => App::Other,
|
|
Some(name) => App::Named(name.to_string()),
|
|
None => panic!("an application binding with no application"),
|
|
},
|
|
then: match binding.command.as_str() {
|
|
"Ignore" => Then::Ignore,
|
|
command => Then::Run(Run::Command(command.to_string())),
|
|
},
|
|
})
|
|
.collect();
|
|
bindings.push((combo(keys), Body::Scoped(scopes)));
|
|
}
|
|
for binding in &whkdrc.bindings {
|
|
bindings.push((
|
|
combo(&binding.keys),
|
|
Body::Run(Run::Command(binding.command.clone())),
|
|
));
|
|
}
|
|
let pause = whkdrc
|
|
.pause_binding
|
|
.as_ref()
|
|
.map(|keys| (combo(keys), whkdrc.pause_hook.clone()));
|
|
(bindings, pause)
|
|
}
|
|
|
|
/// What the whkd reader reads.
|
|
fn ours(path: &Path) -> Read {
|
|
let text = std::fs::read_to_string(path).unwrap();
|
|
let parsed = whkd::parse(&text, &ParseOptions::default());
|
|
assert_eq!(parsed.keymap.modes.len(), 1);
|
|
let bindings = parsed.keymap.modes[0]
|
|
.bindings
|
|
.iter()
|
|
.map(|binding| (binding.chord[0], binding.body.clone()))
|
|
.collect();
|
|
let pause = parsed
|
|
.keymap
|
|
.pause
|
|
.map(|pause| (pause.combo, pause.hook.clone()));
|
|
(bindings, pause)
|
|
}
|
|
|
|
#[test]
|
|
fn every_whkdrc_of_the_corpus_reads_as_whkd_reads_it() {
|
|
let fixtures = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures");
|
|
let mut compared = 0;
|
|
for entry in std::fs::read_dir(fixtures).unwrap() {
|
|
let path = entry.unwrap().path();
|
|
if path.extension().is_some_and(|e| e == "whkdrc") {
|
|
assert_eq!(ours(&path), theirs(&path), "{}", path.display());
|
|
compared += 1;
|
|
}
|
|
}
|
|
assert_eq!(compared, 10);
|
|
}
|
|
|
|
#[test]
|
|
fn the_example_of_whkds_readme_reads_as_whkd_reads_it() {
|
|
let example = r#".shell pwsh # can be one of cmd | pwsh | powershell
|
|
.pause alt + shift + p # can be any hotkey combo to toggle all other hotkeys on and off
|
|
.pause_hook echo "you can call whatever powershell command you want here"
|
|
|
|
alt + n [
|
|
# ProcessName as shown by `Get-Process`
|
|
Firefox : echo "hello firefox"
|
|
|
|
# Spaces are fine, no quotes required
|
|
Google Chrome : echo "hello chrome"
|
|
]
|
|
|
|
alt + q [
|
|
# Default is a keyword which will apply to all apps
|
|
# If you only have Default, this is the same as doing "alt + q : komorebic close"
|
|
Default : komorebic close
|
|
|
|
# Ignore is a keyword which will skip running the hotkey for the given process
|
|
Google Chrome : Ignore
|
|
]
|
|
|
|
# focus windows with komorebi
|
|
alt + h : komorebic focus left
|
|
alt + j : komorebic focus down
|
|
alt + k : komorebic focus up
|
|
alt + l : komorebic focus right
|
|
"#;
|
|
let path = std::env::temp_dir().join(format!(
|
|
"komorebi-hotkeys-whkd-readme-{}.whkdrc",
|
|
std::process::id()
|
|
));
|
|
std::fs::write(&path, example).unwrap();
|
|
let (ours, theirs) = (ours(&path), theirs(&path));
|
|
let _ = std::fs::remove_file(&path);
|
|
|
|
assert_eq!(ours.0.len(), 6);
|
|
assert_eq!(ours, theirs);
|
|
}
|