add macOS key bindings: Opt+Backspace/Delete word-delete, Cmd+Up/Down document navigation

This commit is contained in:
jess 2026-04-07 18:00:21 -07:00
parent 989dd98468
commit e04e5b1953
1 changed files with 40 additions and 1 deletions

View File

@ -1,9 +1,11 @@
use iced_wgpu::core::keyboard;
use iced_wgpu::core::keyboard::key;
use iced_wgpu::core::text::Wrapping;
use iced_wgpu::core::{
Background, Border, Color, Element, Font, Length, Padding, Shadow, Theme,
};
use iced_widget::container;
use iced_widget::text_editor::{self, Style};
use iced_widget::text_editor::{self, Binding, KeyPress, Motion, Status, Style};
#[derive(Debug, Clone)]
pub enum Message {
@ -43,6 +45,7 @@ impl EditorState {
.height(Length::Fill)
.padding(Padding { top: 38.0, right: 8.0, bottom: 8.0, left: 8.0 })
.wrapping(Wrapping::Word)
.key_binding(macos_key_binding)
.style(|_theme, _status| Style {
background: Background::Color(Color::from_rgb(0.08, 0.08, 0.10)),
border: Border::default(),
@ -72,3 +75,39 @@ impl EditorState {
.into()
}
}
fn macos_key_binding(key_press: KeyPress) -> Option<Binding<Message>> {
let KeyPress { key, modifiers, status, .. } = &key_press;
if !matches!(status, Status::Focused { .. }) {
return None;
}
match key.as_ref() {
keyboard::Key::Named(key::Named::Backspace) if modifiers.alt() => {
Some(Binding::Sequence(vec![
Binding::Select(Motion::WordLeft),
Binding::Backspace,
]))
}
keyboard::Key::Named(key::Named::Delete) if modifiers.alt() => {
Some(Binding::Sequence(vec![
Binding::Select(Motion::WordRight),
Binding::Delete,
]))
}
keyboard::Key::Named(key::Named::ArrowUp) if modifiers.logo() && modifiers.shift() => {
Some(Binding::Select(Motion::DocumentStart))
}
keyboard::Key::Named(key::Named::ArrowDown) if modifiers.logo() && modifiers.shift() => {
Some(Binding::Select(Motion::DocumentEnd))
}
keyboard::Key::Named(key::Named::ArrowUp) if modifiers.logo() => {
Some(Binding::Move(Motion::DocumentStart))
}
keyboard::Key::Named(key::Named::ArrowDown) if modifiers.logo() => {
Some(Binding::Move(Motion::DocumentEnd))
}
_ => Binding::from_key_press(key_press),
}
}