Make keymaps reusable across platforms (#10811)

This PR includes two relevant changes:
- Platform binds (super, windows, cmd) will now parse on all platforms,
regardless of which one is being used. While very counter-intuitive
(this means that `cmd-d` will actually be triggered by `win-d` on
windows) this makes it possible to reuse keymap files across platforms
easily
- There is now a KeyContext `os == linux`, `os == macos` or `os ==
windows` available in keymaps. This allows users to specify certain
blocks of keybinds only for one OS, allowing you to minimize the amount
of keymappings that you have to re-configure for each platform.

Release Notes:

- Added `os` KeyContext, set to either `linux`, `macos` or `windows`
- Fixed keymap parsing errors when `cmd` was used on linux, `super` was
used on mac, etc.
This commit is contained in:
ElKowar
2024-04-22 13:24:25 -07:00
committed by GitHub
parent 33baa377c7
commit 7f81bfb6b7
11 changed files with 24 additions and 15 deletions
+14
View File
@@ -25,6 +25,20 @@ impl<'a> TryFrom<&'a str> for KeyContext {
}
impl KeyContext {
/// Initialize a new [`KeyContext`] that contains an `os` key set to either `macos`, `linux`, `windows` or `unknown`.
pub fn new_with_defaults() -> Self {
let mut context = Self::default();
#[cfg(target_os = "macos")]
context.set("os", "macos");
#[cfg(target_os = "linux")]
context.set("os", "linux");
#[cfg(target_os = "windows")]
context.set("os", "windows");
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
context.set("os", "unknown");
context
}
/// Parse a key context from a string.
/// The key context format is very simple:
/// - either a single identifier, such as `StatusBar`
+1 -6
View File
@@ -74,12 +74,7 @@ impl Keystroke {
"alt" => alt = true,
"shift" => shift = true,
"fn" => function = true,
#[cfg(target_os = "macos")]
"cmd" => platform = true,
#[cfg(target_os = "linux")]
"super" => platform = true,
#[cfg(target_os = "windows")]
"win" => platform = true,
"cmd" | "super" | "win" => platform = true,
_ => {
if let Some(next) = components.peek() {
if next.is_empty() && source.ends_with('-') {