macOS: Add key equivalents for non-Latin layouts (#20401)
Closes #16343
Closes #10972
Release Notes:
- (breaking change) On macOS when using a keyboard that supports an
extended Latin character set (e.g. French, German, ...) keyboard
shortcuts are automatically updated so that they can be typed without
`option`. This fixes several long-standing problems where some keyboards
could not type some shortcuts.
- This mapping works the same way as
[macOS](https://developer.apple.com/documentation/swiftui/view/keyboardshortcut(_:modifiers:localization:)).
For example on a German keyboard shortcuts like `cmd->` become `cmd-:`,
`cmd-[` and `cmd-]` become `cmd-ö` and `cmd-ä`. This mapping happens at
the time keyboard layout files are read so the keybindings are visible
in the command palette. To opt out of this behavior for your custom
keyboard shortcuts, set `"use_layout_keys": true` in your binding
section. For the mappings used for each layout [see
here](a890df1863/crates/settings/src/key_equivalents.rs (L7)
).
---------
Co-authored-by: Will <will@zed.dev>
This commit is contained in:
parent
07821083df
commit
ff4f67993b
16 changed files with 435 additions and 10 deletions
|
@ -339,6 +339,7 @@ chrono = { version = "0.4", features = ["serde"] }
|
||||||
clap = { version = "4.4", features = ["derive"] }
|
clap = { version = "4.4", features = ["derive"] }
|
||||||
clickhouse = "0.11.6"
|
clickhouse = "0.11.6"
|
||||||
cocoa = "0.26"
|
cocoa = "0.26"
|
||||||
|
cocoa-foundation = "0.2.0"
|
||||||
convert_case = "0.6.0"
|
convert_case = "0.6.0"
|
||||||
core-foundation = "0.9.3"
|
core-foundation = "0.9.3"
|
||||||
core-foundation-sys = "0.8.6"
|
core-foundation-sys = "0.8.6"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"context": "VimControl && !menu",
|
"context": "VimControl && !menu",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"i": ["vim::PushOperator", { "Object": { "around": false } }],
|
"i": ["vim::PushOperator", { "Object": { "around": false } }],
|
||||||
"a": ["vim::PushOperator", { "Object": { "around": true } }],
|
"a": ["vim::PushOperator", { "Object": { "around": true } }],
|
||||||
|
@ -171,6 +172,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == normal",
|
"context": "vim_mode == normal",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"escape": "editor::Cancel",
|
"escape": "editor::Cancel",
|
||||||
"ctrl-[": "editor::Cancel",
|
"ctrl-[": "editor::Cancel",
|
||||||
|
@ -224,6 +226,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "VimControl && VimCount",
|
"context": "VimControl && VimCount",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"0": ["vim::Number", 0],
|
"0": ["vim::Number", 0],
|
||||||
":": "vim::CountCommand"
|
":": "vim::CountCommand"
|
||||||
|
@ -231,6 +234,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == visual",
|
"context": "vim_mode == visual",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
":": "vim::VisualCommand",
|
":": "vim::VisualCommand",
|
||||||
"u": "vim::ConvertToLowerCase",
|
"u": "vim::ConvertToLowerCase",
|
||||||
|
@ -279,6 +283,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == insert",
|
"context": "vim_mode == insert",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"escape": "vim::NormalBefore",
|
"escape": "vim::NormalBefore",
|
||||||
"ctrl-c": "vim::NormalBefore",
|
"ctrl-c": "vim::NormalBefore",
|
||||||
|
@ -304,6 +309,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == insert && !(showing_code_actions || showing_completions)",
|
"context": "vim_mode == insert && !(showing_code_actions || showing_completions)",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"ctrl-p": "editor::ShowCompletions",
|
"ctrl-p": "editor::ShowCompletions",
|
||||||
"ctrl-n": "editor::ShowCompletions"
|
"ctrl-n": "editor::ShowCompletions"
|
||||||
|
@ -311,6 +317,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == replace",
|
"context": "vim_mode == replace",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"escape": "vim::NormalBefore",
|
"escape": "vim::NormalBefore",
|
||||||
"ctrl-c": "vim::NormalBefore",
|
"ctrl-c": "vim::NormalBefore",
|
||||||
|
@ -328,6 +335,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == waiting",
|
"context": "vim_mode == waiting",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"tab": "vim::Tab",
|
"tab": "vim::Tab",
|
||||||
"enter": "vim::Enter",
|
"enter": "vim::Enter",
|
||||||
|
@ -341,6 +349,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == operator",
|
"context": "vim_mode == operator",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"escape": "vim::ClearOperators",
|
"escape": "vim::ClearOperators",
|
||||||
"ctrl-c": "vim::ClearOperators",
|
"ctrl-c": "vim::ClearOperators",
|
||||||
|
@ -349,6 +358,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == a || vim_operator == i || vim_operator == cs",
|
"context": "vim_operator == a || vim_operator == i || vim_operator == cs",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"w": "vim::Word",
|
"w": "vim::Word",
|
||||||
"shift-w": ["vim::Word", { "ignorePunctuation": true }],
|
"shift-w": ["vim::Word", { "ignorePunctuation": true }],
|
||||||
|
@ -376,6 +386,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == c",
|
"context": "vim_operator == c",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"c": "vim::CurrentLine",
|
"c": "vim::CurrentLine",
|
||||||
"d": "editor::Rename", // zed specific
|
"d": "editor::Rename", // zed specific
|
||||||
|
@ -384,6 +395,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == d",
|
"context": "vim_operator == d",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"d": "vim::CurrentLine",
|
"d": "vim::CurrentLine",
|
||||||
"s": ["vim::PushOperator", "DeleteSurrounds"],
|
"s": ["vim::PushOperator", "DeleteSurrounds"],
|
||||||
|
@ -393,6 +405,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == gu",
|
"context": "vim_operator == gu",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"g u": "vim::CurrentLine",
|
"g u": "vim::CurrentLine",
|
||||||
"u": "vim::CurrentLine"
|
"u": "vim::CurrentLine"
|
||||||
|
@ -400,6 +413,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == gU",
|
"context": "vim_operator == gU",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"g shift-u": "vim::CurrentLine",
|
"g shift-u": "vim::CurrentLine",
|
||||||
"shift-u": "vim::CurrentLine"
|
"shift-u": "vim::CurrentLine"
|
||||||
|
@ -407,6 +421,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == g~",
|
"context": "vim_operator == g~",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"g ~": "vim::CurrentLine",
|
"g ~": "vim::CurrentLine",
|
||||||
"~": "vim::CurrentLine"
|
"~": "vim::CurrentLine"
|
||||||
|
@ -414,6 +429,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == gq",
|
"context": "vim_operator == gq",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"g q": "vim::CurrentLine",
|
"g q": "vim::CurrentLine",
|
||||||
"q": "vim::CurrentLine",
|
"q": "vim::CurrentLine",
|
||||||
|
@ -423,6 +439,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == y",
|
"context": "vim_operator == y",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"y": "vim::CurrentLine",
|
"y": "vim::CurrentLine",
|
||||||
"s": ["vim::PushOperator", { "AddSurrounds": {} }]
|
"s": ["vim::PushOperator", { "AddSurrounds": {} }]
|
||||||
|
@ -430,30 +447,35 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == ys",
|
"context": "vim_operator == ys",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"s": "vim::CurrentLine"
|
"s": "vim::CurrentLine"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == >",
|
"context": "vim_operator == >",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
">": "vim::CurrentLine"
|
">": "vim::CurrentLine"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == <",
|
"context": "vim_operator == <",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"<": "vim::CurrentLine"
|
"<": "vim::CurrentLine"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_operator == gc",
|
"context": "vim_operator == gc",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"c": "vim::CurrentLine"
|
"c": "vim::CurrentLine"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "vim_mode == literal",
|
"context": "vim_mode == literal",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"ctrl-@": ["vim::Literal", ["ctrl-@", "\u0000"]],
|
"ctrl-@": ["vim::Literal", ["ctrl-@", "\u0000"]],
|
||||||
"ctrl-a": ["vim::Literal", ["ctrl-a", "\u0001"]],
|
"ctrl-a": ["vim::Literal", ["ctrl-a", "\u0001"]],
|
||||||
|
@ -497,6 +519,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "BufferSearchBar && !in_replace",
|
"context": "BufferSearchBar && !in_replace",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"enter": "vim::SearchSubmit",
|
"enter": "vim::SearchSubmit",
|
||||||
"escape": "buffer_search::Dismiss"
|
"escape": "buffer_search::Dismiss"
|
||||||
|
@ -504,6 +527,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "ProjectPanel || CollabPanel || OutlinePanel || ChatPanel || VimControl || EmptyPane || SharedScreen || MarkdownPreview || KeyContextView",
|
"context": "ProjectPanel || CollabPanel || OutlinePanel || ChatPanel || VimControl || EmptyPane || SharedScreen || MarkdownPreview || KeyContextView",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
// window related commands (ctrl-w X)
|
// window related commands (ctrl-w X)
|
||||||
"ctrl-w": null,
|
"ctrl-w": null,
|
||||||
|
@ -554,6 +578,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "EmptyPane || SharedScreen || MarkdownPreview || KeyContextView",
|
"context": "EmptyPane || SharedScreen || MarkdownPreview || KeyContextView",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
":": "command_palette::Toggle",
|
":": "command_palette::Toggle",
|
||||||
"g /": "pane::DeploySearch"
|
"g /": "pane::DeploySearch"
|
||||||
|
@ -562,6 +587,7 @@
|
||||||
{
|
{
|
||||||
// netrw compatibility
|
// netrw compatibility
|
||||||
"context": "ProjectPanel && not_editing",
|
"context": "ProjectPanel && not_editing",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
":": "command_palette::Toggle",
|
":": "command_palette::Toggle",
|
||||||
"%": "project_panel::NewFile",
|
"%": "project_panel::NewFile",
|
||||||
|
@ -589,6 +615,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"context": "OutlinePanel && not_editing",
|
"context": "OutlinePanel && not_editing",
|
||||||
|
"use_layout_keys": true,
|
||||||
"bindings": {
|
"bindings": {
|
||||||
"j": "menu::SelectNext",
|
"j": "menu::SelectNext",
|
||||||
"k": "menu::SelectPrev",
|
"k": "menu::SelectPrev",
|
||||||
|
|
|
@ -546,7 +546,6 @@ impl InputExample {
|
||||||
|
|
||||||
impl Render for InputExample {
|
impl Render for InputExample {
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
let num_keystrokes = self.recent_keystrokes.len();
|
|
||||||
div()
|
div()
|
||||||
.bg(rgb(0xaaaaaa))
|
.bg(rgb(0xaaaaaa))
|
||||||
.track_focus(&self.focus_handle(cx))
|
.track_focus(&self.focus_handle(cx))
|
||||||
|
@ -561,7 +560,7 @@ impl Render for InputExample {
|
||||||
.flex()
|
.flex()
|
||||||
.flex_row()
|
.flex_row()
|
||||||
.justify_between()
|
.justify_between()
|
||||||
.child(format!("Keystrokes: {}", num_keystrokes))
|
.child(format!("Keyboard {}", cx.keyboard_layout()))
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
.border_1()
|
.border_1()
|
||||||
|
@ -607,6 +606,7 @@ fn main() {
|
||||||
KeyBinding::new("end", End, None),
|
KeyBinding::new("end", End, None),
|
||||||
KeyBinding::new("ctrl-cmd-space", ShowCharacterPalette, None),
|
KeyBinding::new("ctrl-cmd-space", ShowCharacterPalette, None),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let window = cx
|
let window = cx
|
||||||
.open_window(
|
.open_window(
|
||||||
WindowOptions {
|
WindowOptions {
|
||||||
|
@ -642,6 +642,13 @@ fn main() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
cx.on_keyboard_layout_change({
|
||||||
|
move |cx| {
|
||||||
|
window.update(cx, |_, cx| cx.notify()).ok();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
window
|
window
|
||||||
.update(cx, |view, cx| {
|
.update(cx, |view, cx| {
|
||||||
cx.focus_view(&view.text_input);
|
cx.focus_view(&view.text_input);
|
||||||
|
|
|
@ -243,6 +243,7 @@ pub struct AppContext {
|
||||||
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
|
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
|
||||||
pub(crate) window_handles: FxHashMap<WindowId, AnyWindowHandle>,
|
pub(crate) window_handles: FxHashMap<WindowId, AnyWindowHandle>,
|
||||||
pub(crate) keymap: Rc<RefCell<Keymap>>,
|
pub(crate) keymap: Rc<RefCell<Keymap>>,
|
||||||
|
pub(crate) keyboard_layout: SharedString,
|
||||||
pub(crate) global_action_listeners:
|
pub(crate) global_action_listeners:
|
||||||
FxHashMap<TypeId, Vec<Rc<dyn Fn(&dyn Any, DispatchPhase, &mut Self)>>>,
|
FxHashMap<TypeId, Vec<Rc<dyn Fn(&dyn Any, DispatchPhase, &mut Self)>>>,
|
||||||
pending_effects: VecDeque<Effect>,
|
pending_effects: VecDeque<Effect>,
|
||||||
|
@ -252,6 +253,7 @@ pub struct AppContext {
|
||||||
// TypeId is the type of the event that the listener callback expects
|
// TypeId is the type of the event that the listener callback expects
|
||||||
pub(crate) event_listeners: SubscriberSet<EntityId, (TypeId, Listener)>,
|
pub(crate) event_listeners: SubscriberSet<EntityId, (TypeId, Listener)>,
|
||||||
pub(crate) keystroke_observers: SubscriberSet<(), KeystrokeObserver>,
|
pub(crate) keystroke_observers: SubscriberSet<(), KeystrokeObserver>,
|
||||||
|
pub(crate) keyboard_layout_observers: SubscriberSet<(), Handler>,
|
||||||
pub(crate) release_listeners: SubscriberSet<EntityId, ReleaseListener>,
|
pub(crate) release_listeners: SubscriberSet<EntityId, ReleaseListener>,
|
||||||
pub(crate) global_observers: SubscriberSet<TypeId, Handler>,
|
pub(crate) global_observers: SubscriberSet<TypeId, Handler>,
|
||||||
pub(crate) quit_observers: SubscriberSet<(), QuitHandler>,
|
pub(crate) quit_observers: SubscriberSet<(), QuitHandler>,
|
||||||
|
@ -279,6 +281,7 @@ impl AppContext {
|
||||||
|
|
||||||
let text_system = Arc::new(TextSystem::new(platform.text_system()));
|
let text_system = Arc::new(TextSystem::new(platform.text_system()));
|
||||||
let entities = EntityMap::new();
|
let entities = EntityMap::new();
|
||||||
|
let keyboard_layout = SharedString::from(platform.keyboard_layout());
|
||||||
|
|
||||||
let app = Rc::new_cyclic(|this| AppCell {
|
let app = Rc::new_cyclic(|this| AppCell {
|
||||||
app: RefCell::new(AppContext {
|
app: RefCell::new(AppContext {
|
||||||
|
@ -302,6 +305,7 @@ impl AppContext {
|
||||||
window_handles: FxHashMap::default(),
|
window_handles: FxHashMap::default(),
|
||||||
windows: SlotMap::with_key(),
|
windows: SlotMap::with_key(),
|
||||||
keymap: Rc::new(RefCell::new(Keymap::default())),
|
keymap: Rc::new(RefCell::new(Keymap::default())),
|
||||||
|
keyboard_layout,
|
||||||
global_action_listeners: FxHashMap::default(),
|
global_action_listeners: FxHashMap::default(),
|
||||||
pending_effects: VecDeque::new(),
|
pending_effects: VecDeque::new(),
|
||||||
pending_notifications: FxHashSet::default(),
|
pending_notifications: FxHashSet::default(),
|
||||||
|
@ -310,6 +314,7 @@ impl AppContext {
|
||||||
event_listeners: SubscriberSet::new(),
|
event_listeners: SubscriberSet::new(),
|
||||||
release_listeners: SubscriberSet::new(),
|
release_listeners: SubscriberSet::new(),
|
||||||
keystroke_observers: SubscriberSet::new(),
|
keystroke_observers: SubscriberSet::new(),
|
||||||
|
keyboard_layout_observers: SubscriberSet::new(),
|
||||||
global_observers: SubscriberSet::new(),
|
global_observers: SubscriberSet::new(),
|
||||||
quit_observers: SubscriberSet::new(),
|
quit_observers: SubscriberSet::new(),
|
||||||
layout_id_buffer: Default::default(),
|
layout_id_buffer: Default::default(),
|
||||||
|
@ -323,6 +328,19 @@ impl AppContext {
|
||||||
|
|
||||||
init_app_menus(platform.as_ref(), &mut app.borrow_mut());
|
init_app_menus(platform.as_ref(), &mut app.borrow_mut());
|
||||||
|
|
||||||
|
platform.on_keyboard_layout_change(Box::new({
|
||||||
|
let app = Rc::downgrade(&app);
|
||||||
|
move || {
|
||||||
|
if let Some(app) = app.upgrade() {
|
||||||
|
let cx = &mut app.borrow_mut();
|
||||||
|
cx.keyboard_layout = SharedString::from(cx.platform.keyboard_layout());
|
||||||
|
cx.keyboard_layout_observers
|
||||||
|
.clone()
|
||||||
|
.retain(&(), move |callback| (callback)(cx));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
platform.on_quit(Box::new({
|
platform.on_quit(Box::new({
|
||||||
let cx = app.clone();
|
let cx = app.clone();
|
||||||
move || {
|
move || {
|
||||||
|
@ -356,6 +374,27 @@ impl AppContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the id of the current keyboard layout
|
||||||
|
pub fn keyboard_layout(&self) -> &SharedString {
|
||||||
|
&self.keyboard_layout
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invokes a handler when the current keyboard layout changes
|
||||||
|
pub fn on_keyboard_layout_change<F>(&self, mut callback: F) -> Subscription
|
||||||
|
where
|
||||||
|
F: 'static + FnMut(&mut AppContext),
|
||||||
|
{
|
||||||
|
let (subscription, activate) = self.keyboard_layout_observers.insert(
|
||||||
|
(),
|
||||||
|
Box::new(move |cx| {
|
||||||
|
callback(cx);
|
||||||
|
true
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
activate();
|
||||||
|
subscription
|
||||||
|
}
|
||||||
|
|
||||||
/// Gracefully quit the application via the platform's standard routine.
|
/// Gracefully quit the application via the platform's standard routine.
|
||||||
pub fn quit(&self) {
|
pub fn quit(&self) {
|
||||||
self.platform.quit();
|
self.platform.quit();
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use collections::HashMap;
|
||||||
|
|
||||||
use crate::{Action, KeyBindingContextPredicate, Keystroke};
|
use crate::{Action, KeyBindingContextPredicate, Keystroke};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
@ -22,22 +24,37 @@ impl Clone for KeyBinding {
|
||||||
impl KeyBinding {
|
impl KeyBinding {
|
||||||
/// Construct a new keybinding from the given data.
|
/// Construct a new keybinding from the given data.
|
||||||
pub fn new<A: Action>(keystrokes: &str, action: A, context_predicate: Option<&str>) -> Self {
|
pub fn new<A: Action>(keystrokes: &str, action: A, context_predicate: Option<&str>) -> Self {
|
||||||
Self::load(keystrokes, Box::new(action), context_predicate).unwrap()
|
Self::load(keystrokes, Box::new(action), context_predicate, None).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load a keybinding from the given raw data.
|
/// Load a keybinding from the given raw data.
|
||||||
pub fn load(keystrokes: &str, action: Box<dyn Action>, context: Option<&str>) -> Result<Self> {
|
pub fn load(
|
||||||
|
keystrokes: &str,
|
||||||
|
action: Box<dyn Action>,
|
||||||
|
context: Option<&str>,
|
||||||
|
key_equivalents: Option<&HashMap<char, char>>,
|
||||||
|
) -> Result<Self> {
|
||||||
let context = if let Some(context) = context {
|
let context = if let Some(context) = context {
|
||||||
Some(KeyBindingContextPredicate::parse(context)?)
|
Some(KeyBindingContextPredicate::parse(context)?)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let keystrokes = keystrokes
|
let mut keystrokes: SmallVec<[Keystroke; 2]> = keystrokes
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.map(Keystroke::parse)
|
.map(Keystroke::parse)
|
||||||
.collect::<Result<_>>()?;
|
.collect::<Result<_>>()?;
|
||||||
|
|
||||||
|
if let Some(equivalents) = key_equivalents {
|
||||||
|
for keystroke in keystrokes.iter_mut() {
|
||||||
|
if keystroke.key.chars().count() == 1 {
|
||||||
|
if let Some(key) = equivalents.get(&keystroke.key.chars().next().unwrap()) {
|
||||||
|
keystroke.key = key.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
keystrokes,
|
keystrokes,
|
||||||
action,
|
action,
|
||||||
|
|
|
@ -169,6 +169,7 @@ pub(crate) trait Platform: 'static {
|
||||||
|
|
||||||
fn on_quit(&self, callback: Box<dyn FnMut()>);
|
fn on_quit(&self, callback: Box<dyn FnMut()>);
|
||||||
fn on_reopen(&self, callback: Box<dyn FnMut()>);
|
fn on_reopen(&self, callback: Box<dyn FnMut()>);
|
||||||
|
fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>);
|
||||||
|
|
||||||
fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
|
fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
|
||||||
fn get_menus(&self) -> Option<Vec<OwnedMenu>> {
|
fn get_menus(&self) -> Option<Vec<OwnedMenu>> {
|
||||||
|
@ -180,6 +181,7 @@ pub(crate) trait Platform: 'static {
|
||||||
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
|
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
|
||||||
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
|
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
|
||||||
fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
|
fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
|
||||||
|
fn keyboard_layout(&self) -> String;
|
||||||
|
|
||||||
fn compositor_name(&self) -> &'static str {
|
fn compositor_name(&self) -> &'static str {
|
||||||
""
|
""
|
||||||
|
|
|
@ -138,6 +138,14 @@ impl<P: LinuxClient + 'static> Platform for P {
|
||||||
self.with_common(|common| common.text_system.clone())
|
self.with_common(|common| common.text_system.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn keyboard_layout(&self) -> String {
|
||||||
|
"unknown".into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_keyboard_layout_change(&self, _callback: Box<dyn FnMut()>) {
|
||||||
|
// todo(linux)
|
||||||
|
}
|
||||||
|
|
||||||
fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
|
fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
|
||||||
on_finish_launching();
|
on_finish_launching();
|
||||||
|
|
||||||
|
|
|
@ -136,6 +136,11 @@ unsafe fn build_classes() {
|
||||||
open_urls as extern "C" fn(&mut Object, Sel, id, id),
|
open_urls as extern "C" fn(&mut Object, Sel, id, id),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
decl.add_method(
|
||||||
|
sel!(onKeyboardLayoutChange:),
|
||||||
|
on_keyboard_layout_change as extern "C" fn(&mut Object, Sel, id),
|
||||||
|
);
|
||||||
|
|
||||||
decl.register()
|
decl.register()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,6 +157,7 @@ pub(crate) struct MacPlatformState {
|
||||||
text_hash_pasteboard_type: id,
|
text_hash_pasteboard_type: id,
|
||||||
metadata_pasteboard_type: id,
|
metadata_pasteboard_type: id,
|
||||||
reopen: Option<Box<dyn FnMut()>>,
|
reopen: Option<Box<dyn FnMut()>>,
|
||||||
|
on_keyboard_layout_change: Option<Box<dyn FnMut()>>,
|
||||||
quit: Option<Box<dyn FnMut()>>,
|
quit: Option<Box<dyn FnMut()>>,
|
||||||
menu_command: Option<Box<dyn FnMut(&dyn Action)>>,
|
menu_command: Option<Box<dyn FnMut(&dyn Action)>>,
|
||||||
validate_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
|
validate_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
|
||||||
|
@ -196,6 +202,7 @@ impl MacPlatform {
|
||||||
open_urls: None,
|
open_urls: None,
|
||||||
finish_launching: None,
|
finish_launching: None,
|
||||||
dock_menu: None,
|
dock_menu: None,
|
||||||
|
on_keyboard_layout_change: None,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -785,6 +792,10 @@ impl Platform for MacPlatform {
|
||||||
self.0.lock().reopen = Some(callback);
|
self.0.lock().reopen = Some(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>) {
|
||||||
|
self.0.lock().on_keyboard_layout_change = Some(callback);
|
||||||
|
}
|
||||||
|
|
||||||
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
|
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
|
||||||
self.0.lock().menu_command = Some(callback);
|
self.0.lock().menu_command = Some(callback);
|
||||||
}
|
}
|
||||||
|
@ -797,6 +808,22 @@ impl Platform for MacPlatform {
|
||||||
self.0.lock().validate_menu_command = Some(callback);
|
self.0.lock().validate_menu_command = Some(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn keyboard_layout(&self) -> String {
|
||||||
|
unsafe {
|
||||||
|
let current_keyboard = TISCopyCurrentKeyboardLayoutInputSource();
|
||||||
|
|
||||||
|
let input_source_id: *mut Object = TISGetInputSourceProperty(
|
||||||
|
current_keyboard,
|
||||||
|
kTISPropertyInputSourceID as *const c_void,
|
||||||
|
);
|
||||||
|
let input_source_id: *const std::os::raw::c_char =
|
||||||
|
msg_send![input_source_id, UTF8String];
|
||||||
|
let input_source_id = CStr::from_ptr(input_source_id).to_str().unwrap();
|
||||||
|
|
||||||
|
input_source_id.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn app_path(&self) -> Result<PathBuf> {
|
fn app_path(&self) -> Result<PathBuf> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let bundle: id = NSBundle::mainBundle();
|
let bundle: id = NSBundle::mainBundle();
|
||||||
|
@ -1259,6 +1286,16 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let app: id = msg_send![APP_CLASS, sharedApplication];
|
let app: id = msg_send![APP_CLASS, sharedApplication];
|
||||||
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
|
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
|
||||||
|
|
||||||
|
let notification_center: *mut Object =
|
||||||
|
msg_send![class!(NSNotificationCenter), defaultCenter];
|
||||||
|
let name = ns_string("NSTextInputContextKeyboardSelectionDidChangeNotification");
|
||||||
|
let _: () = msg_send![notification_center, addObserver: this as id
|
||||||
|
selector: sel!(onKeyboardLayoutChange:)
|
||||||
|
name: name
|
||||||
|
object: nil
|
||||||
|
];
|
||||||
|
|
||||||
let platform = get_mac_platform(this);
|
let platform = get_mac_platform(this);
|
||||||
let callback = platform.0.lock().finish_launching.take();
|
let callback = platform.0.lock().finish_launching.take();
|
||||||
if let Some(callback) = callback {
|
if let Some(callback) = callback {
|
||||||
|
@ -1289,6 +1326,20 @@ extern "C" fn will_terminate(this: &mut Object, _: Sel, _: id) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" fn on_keyboard_layout_change(this: &mut Object, _: Sel, _: id) {
|
||||||
|
let platform = unsafe { get_mac_platform(this) };
|
||||||
|
let mut lock = platform.0.lock();
|
||||||
|
if let Some(mut callback) = lock.on_keyboard_layout_change.take() {
|
||||||
|
drop(lock);
|
||||||
|
callback();
|
||||||
|
platform
|
||||||
|
.0
|
||||||
|
.lock()
|
||||||
|
.on_keyboard_layout_change
|
||||||
|
.get_or_insert(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) {
|
extern "C" fn open_urls(this: &mut Object, _: Sel, _: id, urls: id) {
|
||||||
let urls = unsafe {
|
let urls = unsafe {
|
||||||
(0..urls.count())
|
(0..urls.count())
|
||||||
|
@ -1395,6 +1446,17 @@ unsafe fn ns_url_to_path(url: id) -> Result<PathBuf> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[link(name = "Carbon", kind = "framework")]
|
||||||
|
extern "C" {
|
||||||
|
fn TISCopyCurrentKeyboardLayoutInputSource() -> *mut Object;
|
||||||
|
fn TISGetInputSourceProperty(
|
||||||
|
inputSource: *mut Object,
|
||||||
|
propertyKey: *const c_void,
|
||||||
|
) -> *mut Object;
|
||||||
|
|
||||||
|
pub static kTISPropertyInputSourceID: CFStringRef;
|
||||||
|
}
|
||||||
|
|
||||||
mod security {
|
mod security {
|
||||||
#![allow(non_upper_case_globals)]
|
#![allow(non_upper_case_globals)]
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -162,6 +162,12 @@ impl Platform for TestPlatform {
|
||||||
self.text_system.clone()
|
self.text_system.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn keyboard_layout(&self) -> String {
|
||||||
|
"zed.keyboard.example".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_keyboard_layout_change(&self, _: Box<dyn FnMut()>) {}
|
||||||
|
|
||||||
fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
|
fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,6 +197,14 @@ impl Platform for WindowsPlatform {
|
||||||
self.text_system.clone()
|
self.text_system.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn keyboard_layout(&self) -> String {
|
||||||
|
"unknown".into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_keyboard_layout_change(&self, _callback: Box<dyn FnMut()>) {
|
||||||
|
// todo(windows)
|
||||||
|
}
|
||||||
|
|
||||||
fn run(&self, on_finish_launching: Box<dyn 'static + FnOnce()>) {
|
fn run(&self, on_finish_launching: Box<dyn 'static + FnOnce()>) {
|
||||||
on_finish_launching();
|
on_finish_launching();
|
||||||
let vsync_event = unsafe { Owned::new(CreateEventW(None, false, false, None).unwrap()) };
|
let vsync_event = unsafe { Owned::new(CreateEventW(None, false, false, None).unwrap()) };
|
||||||
|
|
185
crates/settings/src/key_equivalents.rs
Normal file
185
crates/settings/src/key_equivalents.rs
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
use collections::HashMap;
|
||||||
|
|
||||||
|
// On some keyboards (e.g. German QWERTZ) it is not possible to type the full ASCII range
|
||||||
|
// without using option. This means that some of our built in keyboard shortcuts do not work
|
||||||
|
// for those users.
|
||||||
|
//
|
||||||
|
// The way macOS solves this problem is to move shortcuts around so that they are all reachable,
|
||||||
|
// even if the mnemoic changes. https://developer.apple.com/documentation/swiftui/keyboardshortcut/localization-swift.struct
|
||||||
|
//
|
||||||
|
// For example, cmd-> is the "switch window" shortcut because the > key is right above tab.
|
||||||
|
// To ensure this doesn't cause problems for shortcuts defined for a QWERTY layout, apple moves
|
||||||
|
// any shortcuts defined as cmd-> to cmd-:. Coincidentally this s also the same keyboard position
|
||||||
|
// as cmd-> on a QWERTY layout.
|
||||||
|
//
|
||||||
|
// Another example is cmd-[ and cmd-], as they cannot be typed without option, those keys are remapped to cmd-ö
|
||||||
|
// and cmd-ä. These shortcuts are not in the same position as a QWERTY keyboard, because on a QWERTZ keyboard
|
||||||
|
// the + key is in the way; and shortcuts bound to cmd-+ are still typed as cmd-+ on either keyboard (though the
|
||||||
|
// specific key moves)
|
||||||
|
//
|
||||||
|
// As far as I can tell, there's no way to query the mappings Apple uses except by rendering a menu with every
|
||||||
|
// possible key combination, and inspecting the UI to see what it rendered. So that's what we did...
|
||||||
|
//
|
||||||
|
// These mappings were generated by running https://github.com/ConradIrwin/keyboard-inspector, tidying up the
|
||||||
|
// output to remove languages with no mappings and other oddities, and converting it to a less verbose representation with:
|
||||||
|
// jq -s 'map(to_entries | map({key: .key, value: [(.value | to_entries | map(.key) | join("")), (.value | to_entries | map(.value) | join(""))]}) | from_entries) | add'
|
||||||
|
// From there I used multi-cursor to produce this match statement.
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
pub fn get_key_equivalents(layout: &str) -> Option<HashMap<char, char>> {
|
||||||
|
let (from, to) = match layout {
|
||||||
|
"com.apple.keylayout.Welsh" => ("#", "£"),
|
||||||
|
"com.apple.keylayout.Turkmen" => ("qc]Q`|[XV\\^v~Cx}{", "äçöÄžŞňÜÝş№ýŽÇüÖŇ"),
|
||||||
|
"com.apple.keylayout.Turkish-QWERTY-PC" => (
|
||||||
|
"$\\|`'[}^=.#{*+:/~;)(@<,&]>\"",
|
||||||
|
"+,;<ığÜ&.ç^Ğ(:Ş*>ş=)'Öö/üÇI",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Sami-PC" => (
|
||||||
|
"}*x\"w[~^/@`]{|<)>W(\\X=Qq&':;",
|
||||||
|
"Æ(čŊšøŽ&´\"žæØĐ;=:Š)đČ`Áá/ŋÅå",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.LatinAmerican" => {
|
||||||
|
("[^~>`(<\\@{;*&/):]|='}\"", "{&>:<);¿\"[ñ(/'=Ñ}¡*´]¨")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.IrishExtended" => ("#", "£"),
|
||||||
|
"com.apple.keylayout.Icelandic" => ("[}=:/'){(*&;^|`\"\\>]<~@", "æ´*Ð'ö=Æ)(/ð&Þ<Öþ:´;>\""),
|
||||||
|
"com.apple.keylayout.German-DIN-2137" => {
|
||||||
|
("}~/<^>{`:\\)&=[]@|;#'\"(*", "Ä>ß;&:Ö<Ü#=/*öä\"'ü§´`)(")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.FinnishSami-PC" => {
|
||||||
|
(")=*\"\\[@{:>';/<|~(]}^`&", "=`(ˆ@ö\"ÖÅ:¨å´;*>)äÄ&</")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.FinnishExtended" => {
|
||||||
|
("];{`:'*<~=/}\\|&[\"($^)>@", "äåÖ<Ũ(;>`´Ä'*/öˆ)€&=:\"")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.Faroese" => ("}\";/$>^@~`:&[*){|]=(\\<'", "ÐØæ´€:&\"><Æ/å(=Å*ð`)';ø"),
|
||||||
|
"com.apple.keylayout.Croatian-PC" => {
|
||||||
|
("{@~;<=>(&*['|]\":/}^`)\\", "Š\">č;*:)/(šćŽđĆČ'Đ&<=ž")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.Croatian" => ("{@;<~=>(&*['|]\":}^)\\`", "Š\"č;>*:)'(šćŽđĆČĐ&=ž<"),
|
||||||
|
"com.apple.keylayout.Azeri" => (":{W?./\"[}<]|,>';w", "IÖÜ,ş.ƏöĞÇğ/çŞəıü"),
|
||||||
|
"com.apple.keylayout.Albanian" => ("\\'~;:|<>`\"@", "ë@>çÇË;:<'\""),
|
||||||
|
"com.apple.keylayout.SwissFrench" => (
|
||||||
|
":@&'~^)$;\"][\\/#={!|*+`<(>}",
|
||||||
|
"ü\"/^>&=çè`àé$'*¨ö+£(!<;):ä",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Swedish" => ("(]\\\"~$`^{|/>*:;<)&=[}'@", ")ä'^>€<&Ö*´:(Åå;=/`öĨ\""),
|
||||||
|
"com.apple.keylayout.Swedish-Pro" => {
|
||||||
|
("/^*`'{|)$>&<[\\;(~\"}@]:=", "´&(<¨Ö*=€:/;ö'å)>^Ä\"äÅ`")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.Spanish" => ("|!\\<{[:;@`/~].'>}\"^", "\"¡'¿Ññº´!<.>;ç`Ç:¨/"),
|
||||||
|
"com.apple.keylayout.Spanish-ISO" => (
|
||||||
|
"|~`]/:)(<&^>*;#}\"{.\\['@",
|
||||||
|
"\"><;.º=)¿/&Ç(´·not found¨Ñç'ñ`\"",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Portuguese" => (")`/'^\"<];>[:{@}(&*=~", "=<'´&`;~º:çªÇ\"^)/(*>"),
|
||||||
|
"com.apple.keylayout.Italian" => (
|
||||||
|
"*7};8:!5%(1&4]^\\6)32>.</0|$,'{[`\"~9#@",
|
||||||
|
"8)*ò£!1ç59&7($6§è0'\"/:.,é°4;ù^ì<%>à32",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Italian-Pro" => {
|
||||||
|
("/:@[]'\\=){;|#<\"(*^&`}>~", "'é\"òàìù*=çè§£;^)(&/<°:>")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.Irish" => ("#", "£"),
|
||||||
|
"com.apple.keylayout.German" => ("=`#'}:)/\"^&]*{;|[<(>~@\\", "*<§´ÄÜ=ß`&/ä(Öü'ö;):>\"#"),
|
||||||
|
"com.apple.keylayout.French" => (
|
||||||
|
"*}7;8:!5%(1&4]\\^6)32>.</0|${'[`\"~9#@",
|
||||||
|
"8*è)!°1(59&7'$`6§0\"é/;.:à£4¨ù^<%>ç32",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.French-numerical" => (
|
||||||
|
"|!52;][>&@\"%'{)<~7.1/^(}*8#0$9`6\\3:4",
|
||||||
|
"£1(é)$^/72%5ù¨0.>è;&:69*8!3à4ç<§`\"°'",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.French-PC" => (
|
||||||
|
"!&\"_$}/72>8]#:31)*<%4;6\\-{['@(0|5.`9~^",
|
||||||
|
"17%°4£:èé/_$3§\"&08.5'!-*)¨^ù29àμ(;<ç>6",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Finnish" => ("/^*`)'{|$>&<[\\~;(\"}@]:=", "´&(<=¨Ö*€:/;ö'>å)^Ä\"äÅ`"),
|
||||||
|
"com.apple.keylayout.Danish" => ("=[;'`{}|>]*^(&@~)<\\/$\":", "`æå¨<ÆØ*:ø(&)/\">=;'´€^Å"),
|
||||||
|
"com.apple.keylayout.Canadian-CSA" => ("\\?']/><[{}|~`\"", "àÉèçé\"'^¨ÇÀÙùÈ"),
|
||||||
|
"com.apple.keylayout.British" => ("#", "£"),
|
||||||
|
"com.apple.keylayout.Brazilian-ABNT2" => ("\"|~?`'/^\\", "`^\"Ç'´ç¨~"),
|
||||||
|
"com.apple.keylayout.Belgian" => (
|
||||||
|
"`3/*<\\8>7#&96@);024(|'1\":$[~5.%^}]{!",
|
||||||
|
"<\":8.`!/è37ç§20)àé'9£ù&%°4^>(;56*$¨1",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Austrian" => ("/^*`'{|)>&<[\\;(~\"}@]:=#", "ß&(<´Ö'=:/;ö#ü)>`Ä\"äÜ*§"),
|
||||||
|
"com.apple.keylayout.Slovak-QWERTY" => (
|
||||||
|
"):9;63'\"]^/+@~>`?<!#5&${2}%*18(704[",
|
||||||
|
"0\"íôžš§!ä6'%2Ň:ňˇ?13ť74ÚľÄ58+á9ýéčú",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Slovak" => (
|
||||||
|
"!$`10&:#4^*~{%5')}6/\"[8]97?;<@23>(+",
|
||||||
|
"14ň+é7\"3č68ŇÚ5ť§0Äž'!úáäíýˇô?2ľš:9%",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Polish" => (
|
||||||
|
"&)|?,%:;^}]_{!+#(*`/[~<\"$.>'@=\\",
|
||||||
|
":\"$Ż.+Łł=)(ćź§]!/_<żó>śę?,ńą%[;",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Lithuanian" => ("+#&=!%1*@73^584$26", "ŽĘŲžĄĮąŪČųęŠįūėĖčš"),
|
||||||
|
"com.apple.keylayout.Hungarian" => (
|
||||||
|
"}(*@\"{=/|;>'[`<~\\!$&0#:]^)+",
|
||||||
|
"Ú)(\"ÁŐóüŰé:áőíÜÍű'!=ö+Éú/ÖÓ",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Hungarian-QWERTY" => (
|
||||||
|
"=]#>@/&<`0')~(\\!:*;$\"+^{|}[",
|
||||||
|
"óú+:\"ü=ÜíöáÖÍ)ű'É(é!ÁÓ/ŐŰÚő",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Czech-QWERTY" => (
|
||||||
|
"9>0[2()\"}@]46%5;#8{*7^~+!3?&'<$/1`:",
|
||||||
|
"í:éúě90!(2)čž5řů3áÚ8ý6`%1šˇ7§?4'+¨\"",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Maltese" => ("[`}{#]~", "ġżĦĠ£ħŻ"),
|
||||||
|
"com.apple.keylayout.Turkish" => (
|
||||||
|
"|}(#>&^-/`$%@]~*,[\"<_.{:'\\)",
|
||||||
|
"ÜI%\"Ç)/ş.<'(*ı>_öğ-ÖŞçĞ$,ü:",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Turkish-Standard" => {
|
||||||
|
("|}(#>=&^`@]~*,;[\"<.{:'\\)", "ÜI)^;*'&ö\"ıÖ(.çğŞ:,ĞÇşü=")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.NorwegianSami-PC" => {
|
||||||
|
("\"}~<`&>':{@*^|\\)=([]/;", "ˆÆ>;</:¨ÅØ\"(&*@=`)øæ´å")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.Serbian-Latin" => {
|
||||||
|
(";\\@>&'<]\"|(=}^)`[~:*{", "čž\":'ć;đĆŽ)*Đ&=<š>Č(Š")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.Slovenian" => ("]`^@)&\":'*=<{;}(~>\\|[", "đ<&\"='ĆČć(*;ŠčĐ)>:žŽš"),
|
||||||
|
"com.apple.keylayout.SwedishSami-PC" => {
|
||||||
|
("@=<^|`>){'&\"}]~[/:*\\(;", "\"`;&*<:=Ö¨/ˆÄä>ö´Å(@)å")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.SwissGerman" => (
|
||||||
|
"={#:\\}!(+]/<\";$'`*[>&^~@)|",
|
||||||
|
"¨é*è$à+)!ä';`üç^<(ö:/&>\"=£",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Hawaiian" => ("'", "ʻ"),
|
||||||
|
"com.apple.keylayout.NorthernSami" => (
|
||||||
|
":/[<{X\"wQx\\(;~>W}`*@])'^|=q&",
|
||||||
|
"Å´ø;ØČŊšÁčđ)åŽ:ŠÆž(\"æ=ŋ&Đ`á/",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.USInternational-PC" => ("^~", "ˆ˜"),
|
||||||
|
"com.apple.keylayout.NorwegianExtended" => ("^~", "ˆ˜"),
|
||||||
|
"com.apple.keylayout.Norwegian" => ("`'~\"\\*|=/@)[:}&><]{(^;", "<¨>^@(*`´\"=øÅÆ/:;æØ)&å"),
|
||||||
|
"com.apple.keylayout.ABC-QWERTZ" => {
|
||||||
|
("\"}~<`>'&#:{@*^|\\)=(]/;[", "`Ä>;<:´/§ÜÖ\"(&'#=*)äßüö")
|
||||||
|
}
|
||||||
|
"com.apple.keylayout.ABC-AZERTY" => (
|
||||||
|
">[$61%@7|)&8\":}593(.4^<!{`2]\\#;~*/'0",
|
||||||
|
"/^4§&52è£07!%°*(ç\"9;'6.1¨<é$`3)>8:ùà",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Czech" => (
|
||||||
|
"(7*#193620?/{)@~!$8+;:%4\">`^]&5}[<'",
|
||||||
|
"9ý83+íšžěéˇ'Ú02`14á%ů\"5č!:¨6)7ř(ú?§",
|
||||||
|
),
|
||||||
|
"com.apple.keylayout.Brazilian-Pro" => ("^~", "ˆ˜"),
|
||||||
|
_ => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
debug_assert!(from.chars().count() == to.chars().count());
|
||||||
|
|
||||||
|
Some(HashMap::from_iter(from.chars().zip(to.chars())))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
pub fn get_key_equivalents(_layout: &str) -> Option<HashMap<char, char>> {
|
||||||
|
None
|
||||||
|
}
|
|
@ -19,6 +19,8 @@ pub struct KeymapFile(Vec<KeymapBlock>);
|
||||||
pub struct KeymapBlock {
|
pub struct KeymapBlock {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
context: Option<String>,
|
context: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
use_layout_keys: Option<bool>,
|
||||||
bindings: BTreeMap<String, KeymapAction>,
|
bindings: BTreeMap<String, KeymapAction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +76,14 @@ impl KeymapFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_to_cx(self, cx: &mut AppContext) -> Result<()> {
|
pub fn add_to_cx(self, cx: &mut AppContext) -> Result<()> {
|
||||||
for KeymapBlock { context, bindings } in self.0 {
|
let key_equivalents = crate::key_equivalents::get_key_equivalents(&cx.keyboard_layout());
|
||||||
|
|
||||||
|
for KeymapBlock {
|
||||||
|
context,
|
||||||
|
use_layout_keys,
|
||||||
|
bindings,
|
||||||
|
} in self.0
|
||||||
|
{
|
||||||
let bindings = bindings
|
let bindings = bindings
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(keystroke, action)| {
|
.filter_map(|(keystroke, action)| {
|
||||||
|
@ -110,7 +119,18 @@ impl KeymapFile {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.log_err()
|
.log_err()
|
||||||
.map(|action| KeyBinding::load(&keystroke, action, context.as_deref()))
|
.map(|action| {
|
||||||
|
KeyBinding::load(
|
||||||
|
&keystroke,
|
||||||
|
action,
|
||||||
|
context.as_deref(),
|
||||||
|
if use_layout_keys.unwrap_or_default() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
key_equivalents.as_ref()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>>>()?;
|
.collect::<Result<Vec<_>>>()?;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
mod editable_setting_control;
|
mod editable_setting_control;
|
||||||
mod json_schema;
|
mod json_schema;
|
||||||
|
mod key_equivalents;
|
||||||
mod keymap_file;
|
mod keymap_file;
|
||||||
mod settings_file;
|
mod settings_file;
|
||||||
mod settings_store;
|
mod settings_store;
|
||||||
|
|
|
@ -4730,6 +4730,7 @@ impl Render for Workspace {
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
let mut context = KeyContext::new_with_defaults();
|
let mut context = KeyContext::new_with_defaults();
|
||||||
context.add("Workspace");
|
context.add("Workspace");
|
||||||
|
context.set("keyboard_layout", cx.keyboard_layout().clone());
|
||||||
let centered_layout = self.centered_layout
|
let centered_layout = self.centered_layout
|
||||||
&& self.center.panes().len() == 1
|
&& self.center.panes().len() == 1
|
||||||
&& self.active_item(cx).is_some();
|
&& self.active_item(cx).is_some();
|
||||||
|
|
|
@ -808,6 +808,7 @@ pub fn handle_keymap_file_changes(
|
||||||
VimModeSetting::register(cx);
|
VimModeSetting::register(cx);
|
||||||
|
|
||||||
let (base_keymap_tx, mut base_keymap_rx) = mpsc::unbounded();
|
let (base_keymap_tx, mut base_keymap_rx) = mpsc::unbounded();
|
||||||
|
let (keyboard_layout_tx, mut keyboard_layout_rx) = mpsc::unbounded();
|
||||||
let mut old_base_keymap = *BaseKeymap::get_global(cx);
|
let mut old_base_keymap = *BaseKeymap::get_global(cx);
|
||||||
let mut old_vim_enabled = VimModeSetting::get_global(cx).0;
|
let mut old_vim_enabled = VimModeSetting::get_global(cx).0;
|
||||||
cx.observe_global::<SettingsStore>(move |cx| {
|
cx.observe_global::<SettingsStore>(move |cx| {
|
||||||
|
@ -822,6 +823,11 @@ pub fn handle_keymap_file_changes(
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
|
cx.on_keyboard_layout_change(move |_| {
|
||||||
|
keyboard_layout_tx.unbounded_send(()).ok();
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
load_default_keymap(cx);
|
load_default_keymap(cx);
|
||||||
|
|
||||||
cx.spawn(move |cx| async move {
|
cx.spawn(move |cx| async move {
|
||||||
|
@ -829,6 +835,7 @@ pub fn handle_keymap_file_changes(
|
||||||
loop {
|
loop {
|
||||||
select_biased! {
|
select_biased! {
|
||||||
_ = base_keymap_rx.next() => {}
|
_ = base_keymap_rx.next() => {}
|
||||||
|
_ = keyboard_layout_rx.next() => {}
|
||||||
user_keymap_content = user_keymap_file_rx.next() => {
|
user_keymap_content = user_keymap_file_rx.next() => {
|
||||||
if let Some(user_keymap_content) = user_keymap_content {
|
if let Some(user_keymap_content) = user_keymap_content {
|
||||||
match KeymapFile::parse(&user_keymap_content) {
|
match KeymapFile::parse(&user_keymap_content) {
|
||||||
|
@ -854,7 +861,7 @@ fn reload_keymaps(cx: &mut AppContext, keymap_content: &KeymapFile) {
|
||||||
load_default_keymap(cx);
|
load_default_keymap(cx);
|
||||||
keymap_content.clone().add_to_cx(cx).log_err();
|
keymap_content.clone().add_to_cx(cx).log_err();
|
||||||
cx.set_menus(app_menus());
|
cx.set_menus(app_menus());
|
||||||
cx.set_dock_menu(vec![MenuItem::action("New Window", workspace::NewWindow)])
|
cx.set_dock_menu(vec![MenuItem::action("New Window", workspace::NewWindow)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_default_keymap(cx: &mut AppContext) {
|
pub fn load_default_keymap(cx: &mut AppContext) {
|
||||||
|
|
|
@ -23,6 +23,8 @@ The file contains a JSON array of objects with `"bindings"`. If no `"context"` i
|
||||||
|
|
||||||
Within each binding section a [key sequence](#keybinding-syntax) is mapped to an [action](#actions). If conflicts are detected they are resolved as [described below](#precedence).
|
Within each binding section a [key sequence](#keybinding-syntax) is mapped to an [action](#actions). If conflicts are detected they are resolved as [described below](#precedence).
|
||||||
|
|
||||||
|
If you are using a non-QWERTY, Latin-character keyboard, you may want to set `use_layout_keys` to `true`. See [Non-QWERTY keyboards](#non-qwerty-keyboards) for more information.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
|
@ -58,7 +60,7 @@ Each key press is a sequence of modifiers followed by a key. The modifiers are:
|
||||||
- `shift-` The shift key
|
- `shift-` The shift key
|
||||||
- `fn-` The function key
|
- `fn-` The function key
|
||||||
|
|
||||||
The keys can be any single unicode codepoint that your keyboard generates (for example `a`, `0`, `£` or `ç`), or any named key (`tab`, `f1`, `shift`, or `cmd`).
|
The keys can be any single unicode codepoint that your keyboard generates (for example `a`, `0`, `£` or `ç`), or any named key (`tab`, `f1`, `shift`, or `cmd`). If you are using a non-Latin layout (e.g. Cyrillic), you can bind either to the cyrillic character, or the latin character that that key generates with `cmd` pressed.
|
||||||
|
|
||||||
A few examples:
|
A few examples:
|
||||||
|
|
||||||
|
@ -89,7 +91,7 @@ For example:
|
||||||
|
|
||||||
```
|
```
|
||||||
# in an editor, it might look like this:
|
# in an editor, it might look like this:
|
||||||
Workspace os=macos
|
Workspace os=macos keyboard_layout=com.apple.keylayout.QWERTY
|
||||||
Pane
|
Pane
|
||||||
Editor mode=full extension=md inline_completion vim_mode=insert
|
Editor mode=full extension=md inline_completion vim_mode=insert
|
||||||
|
|
||||||
|
@ -130,6 +132,38 @@ The other kind of conflict that arises is when you have two bindings, one of whi
|
||||||
|
|
||||||
When this happens, and both bindings are active in the current context, Zed will wait for 1 second after you tupe `ctrl-w` to se if you're about to type `left`. If you don't type anything, or if you type a different key, then `DeleteToNextWordEnd` will be triggered. If you do, then `DeleteToEndOfLine` will be triggered.
|
When this happens, and both bindings are active in the current context, Zed will wait for 1 second after you tupe `ctrl-w` to se if you're about to type `left`. If you don't type anything, or if you type a different key, then `DeleteToNextWordEnd` will be triggered. If you do, then `DeleteToEndOfLine` will be triggered.
|
||||||
|
|
||||||
|
### Non-QWERTY keyboards
|
||||||
|
|
||||||
|
As of Zed 0.162.0, Zed has some support for non-QWERTY keyboards on macOS. Better support for non-QWERTY keyboards on Linux is planned.
|
||||||
|
|
||||||
|
There are roughly three categories of keyboard to consider:
|
||||||
|
|
||||||
|
Keyboards that support full ASCII (QWERTY, DVORAK, COLEMAK, etc.). On these keyboards bindings are resolved based on the character that would be generated by the key. So to type `cmd-[`, find the key labelled `[` and press it with command.
|
||||||
|
|
||||||
|
Keyboards that are mostly non-ASCII, but support full ASCII when the command key is pressed. For example Cyrillic keyboards, Armenian, Hebrew, etc. On these keyboards bindings are resolved based on the character that would be generated by typing the key with command pressed. So to type `ctrl-a`, find the key that generates `cmd-a`. For these keyboards, keyboard shortcuts are displayed in the app using their ASCII equivalents. If the ASCII-equivalents are not printed on your keyboard, you can use the macOS keyboard viewer and holding down the `cmd` key to find things (though often the ASCII equivalents are in a QWERTY layout).
|
||||||
|
|
||||||
|
Finally keyboards that support extended Latin alphabets (usually ISO keyboards) require the most support. For example French AZERTY, German QWERTZ, etc. On these keyboards it is often not possible to type the entire ASCII range without option. To ensure that shortcuts _can_ be typed without option, keyboard shortcuts are mapped to "key equivalents" in the same way as [macOS](). This mapping is defined per layout, and is a compromise between leaving keyboard shortcuts triggered by the same character they are defined with, keeping shortcuts in the same place as a QWERTY layout, and moving shortcuts out of the way of system shortcuts.
|
||||||
|
|
||||||
|
For example on a German QWERTZ keyboard, the `cmd->` shortcut is moved to `cmd-:` because `cmd->` is the system window switcher and this is where that shortcut is typed on a QWERTY keyboard. `cmd-+` stays the same because + is still typable without option, and as a result, `cmd-[` and `cmd-]` become `cmd-ö` and `cmd-ä`, moving out of the way of the `+` key.
|
||||||
|
|
||||||
|
If you are defining shortcuts in your personal keymap, you can opt-out of the key equivalent mapping by setting `use_layout_keys` to `true` in your keymap:
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"bindings": {
|
||||||
|
"ctrl->": "editor::Indent" // parsed as ctrl-: when a German QWERTZ keyboard is active
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"use_layout_keys": true,
|
||||||
|
"bindings": {
|
||||||
|
"ctrl->": "editor::Indent" // remains ctrl-> when a German QWERTZ keyboard is active
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
## Tips and tricks
|
## Tips and tricks
|
||||||
|
|
||||||
### Disabling a binding
|
### Disabling a binding
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue