diff --git a/crates/gpui_elements/src/editable_text.rs b/crates/gpui_elements/src/editable_text.rs new file mode 100644 index 0000000000..768a5fd18e --- /dev/null +++ b/crates/gpui_elements/src/editable_text.rs @@ -0,0 +1,5 @@ +mod actions; +mod state_field; + +pub use actions::*; +pub use state_field::*; diff --git a/crates/gpui_elements/src/editable_text/actions.rs b/crates/gpui_elements/src/editable_text/actions.rs new file mode 100644 index 0000000000..78e41b1d55 --- /dev/null +++ b/crates/gpui_elements/src/editable_text/actions.rs @@ -0,0 +1,205 @@ +use gpui::{App, Window}; + +/// The key context used for input element keybindings. +pub const DEFAULT_INPUT_CONTEXT: &str = "Input"; + +gpui::actions!( + actions, + [ + /// Blur focus from the input. + Escape, + /// Insert a newline at the cursor position. + Enter, + /// Insert a tab character at the cursor position. + Tab, + /// Delete the character before the cursor. + Backspace, + /// Delete the character after the cursor. + Delete, + /// Delete the word before the cursor. + DeleteWordLeft, + /// Delete the word after the cursor. + DeleteWordRight, + /// Delete from the cursor to the beginning of the line. + DeleteToBeginningOfLine, + /// Delete from the cursor to the end of the line. + DeleteToEndOfLine, + /// Move the cursor one character to the left. + Left, + /// Move the cursor one character to the right. + Right, + /// Move the cursor up one visual line. + Up, + /// Move the cursor down one visual line. + Down, + /// Move cursor to the start of the current line. + Home, + /// Move cursor to the end of the current line. + End, + /// Move cursor to the beginning of the content. + MoveToBeginning, + /// Move cursor to the end of the content. + MoveToEnd, + /// Move cursor one word to the left. + WordLeft, + /// Move cursor one word to the right. + WordRight, + /// Select all text content. + SelectAll, + /// Extend selection one character to the left. + SelectLeft, + /// Extend selection one character to the right. + SelectRight, + /// Extend selection up one visual line. + SelectUp, + /// Extend selection down one visual line. + SelectDown, + /// Extend selection to the beginning of the content. + SelectToBeginning, + /// Extend selection to the end of the content. + SelectToEnd, + /// Extend selection one word to the left. + SelectWordLeft, + /// Extend selection one word to the right. + SelectWordRight, + /// Cut selected text to clipboard. + Cut, + /// Copy selected text to clipboard. + Copy, + /// Paste from clipboard at the cursor position. + Paste, + /// Undo the last edit. + Undo, + /// Redo the last undone edit. + Redo, + /// Show the platform character palette. + ShowCharacterPalette, + ] +); + +pub fn default_bindings() -> gpui::ActionBindingCollection { + let mut bindings = gpui::ActionBindingCollection::default() + .with::("backspace") + .with::("delete") + .with::("tab") + .with::("enter") + .with::("left") + .with::("right") + .with::("up") + .with::("down") + .with::("secondary-a") + .with::("shift-left") + .with::("shift-right") + .with::("shift-up") + .with::("shift-down") + .with::("secondary-c") + .with::("secondary-x") + .with::("secondary-v") + .with::("secondary-z") + .with::("secondary-shift-z") + .with::("escape") + .with::("secondary-space"); + + #[cfg(target_os = "macos")] + { + bindings = bindings + .with::("alt-backspace") + .with::("alt-delete") + .with::("cmd-backspace") + .with::("ctrl-k") + // Mac keyboards don't have Home/End keys, so cmd-left/right are standard + .with::("cmd-left") + .with::("cmd-right") + .with::("cmd-up") + .with::("cmd-down") + .with::("cmd-shift-up") + .with::("cmd-shift-down") + .with::("alt-left") + .with::("alt-right") + .with::("alt-shift-left") + .with::("alt-shift-right"); + } + + #[cfg(not(target_os = "macos"))] + { + bindings = bindings + .with::("ctrl-backspace") + .with::("ctrl-delete") + .with::("ctrl-shift-backspace") + .with::("ctrl-shift-delete") + .with::("home") + .with::("end") + .with::("ctrl-home") + .with::("ctrl-end") + .with::("ctrl-shift-home") + .with::("ctrl-shift-end") + .with::("ctrl-left") + .with::("ctrl-right") + .with::("ctrl-shift-left") + .with::("ctrl-shift-right"); + } + + bindings +} + +pub trait EditableTextActionHandler { + fn escape(&mut self, _: &Escape, _w: &mut Window, _cx: &mut App) {} + + fn insert_enter(&mut self, _: &Enter, _w: &mut Window, _cx: &mut App) {} + fn insert_tab(&mut self, _: &Tab, _w: &mut Window, _cx: &mut App) {} + + fn backspace(&mut self, _: &Backspace, _w: &mut Window, _cx: &mut App) {} + fn delete(&mut self, _: &Delete, _w: &mut Window, _cx: &mut App) {} + + fn delete_word_left(&mut self, _: &DeleteWordLeft, _w: &mut Window, _cx: &mut App) {} + fn delete_word_right(&mut self, _: &DeleteWordRight, _w: &mut Window, _cx: &mut App) {} + fn delete_to_line_start( + &mut self, + _: &DeleteToBeginningOfLine, + _w: &mut Window, + _cx: &mut App, + ) { + } + fn delete_to_line_end(&mut self, _: &DeleteToEndOfLine, _w: &mut Window, _cx: &mut App) {} + + fn nav_left(&mut self, _: &Left, _w: &mut Window, _cx: &mut App) {} + fn nav_right(&mut self, _: &Right, _w: &mut Window, _cx: &mut App) {} + fn nav_up(&mut self, _: &Up, _w: &mut Window, _cx: &mut App) {} + fn nav_down(&mut self, _: &Down, _w: &mut Window, _cx: &mut App) {} + fn nav_line_start(&mut self, _: &Home, _w: &mut Window, _cx: &mut App) {} + fn nav_line_end(&mut self, _: &End, _w: &mut Window, _cx: &mut App) {} + fn nav_start(&mut self, _: &MoveToBeginning, _w: &mut Window, _cx: &mut App) {} + fn nav_end(&mut self, _: &MoveToEnd, _w: &mut Window, _cx: &mut App) {} + fn nav_left_word(&mut self, _: &WordLeft, _w: &mut Window, _cx: &mut App) {} + fn nav_right_word(&mut self, _: &WordRight, _w: &mut Window, _cx: &mut App) {} + + fn select_all(&mut self, _: &SelectAll, _w: &mut Window, _cx: &mut App) {} + fn select_left(&mut self, _: &SelectLeft, _w: &mut Window, _cx: &mut App) {} + fn select_right(&mut self, _: &SelectRight, _w: &mut Window, _cx: &mut App) {} + fn select_up(&mut self, _: &SelectUp, _w: &mut Window, _cx: &mut App) {} + fn select_down(&mut self, _: &SelectDown, _w: &mut Window, _cx: &mut App) {} + fn select_start(&mut self, _: &SelectToBeginning, _w: &mut Window, _cx: &mut App) {} + fn select_end(&mut self, _: &SelectToEnd, _w: &mut Window, _cx: &mut App) {} + fn select_left_word(&mut self, _: &SelectWordLeft, _w: &mut Window, _cx: &mut App) {} + fn select_right_word(&mut self, _: &SelectWordRight, _w: &mut Window, _cx: &mut App) {} + + fn cut(&mut self, _: &Cut, _w: &mut Window, _cx: &mut App) {} + fn copy(&mut self, _: &Copy, _w: &mut Window, _cx: &mut App) {} + fn paste(&mut self, _: &Paste, _w: &mut Window, _cx: &mut App) {} + + fn undo(&mut self, _: &Undo, _w: &mut Window, _cx: &mut App) {} + fn redo(&mut self, _: &Redo, _w: &mut Window, _cx: &mut App) {} + + fn show_character_palette(&mut self, _: &ShowCharacterPalette, _w: &mut Window, _cx: &mut App) { + } + + fn on_mouse_down( + &mut self, + _position: gpui::Point, + _w: &mut Window, + _cx: &mut App, + ) { + } + fn on_mouse_up(&mut self, _event: &gpui::MouseUpEvent, _w: &mut Window, _cx: &mut App) {} + fn on_mouse_move(&mut self, _event: &gpui::MouseMoveEvent, _w: &mut Window, _cx: &mut App) {} +} diff --git a/crates/gpui_elements/src/editable_text/state_field.rs b/crates/gpui_elements/src/editable_text/state_field.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/crates/gpui_elements/src/lib.rs b/crates/gpui_elements/src/lib.rs index 7839bc5393..ecd8bbbeee 100644 --- a/crates/gpui_elements/src/lib.rs +++ b/crates/gpui_elements/src/lib.rs @@ -1 +1,2 @@ +pub mod editable_text; pub mod input;