diff --git a/viewport/src/editor.rs b/viewport/src/editor.rs index 2b0a765..9d875a1 100644 --- a/viewport/src/editor.rs +++ b/viewport/src/editor.rs @@ -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> { + 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), + } +}