Skip to content

Commit

Permalink
Add more standard hotkey actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Riateche committed Apr 6, 2024
1 parent 2a82b60 commit d04b3f2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
29 changes: 25 additions & 4 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,19 @@ fn main() {
})
}
Key::Named(NamedKey::Home) => editor.action(Action::Motion {
motion: Motion::Home,
motion: if ctrl_pressed {
Motion::DocumentStart
} else {
Motion::Home
},
select: shift_pressed,
}),
Key::Named(NamedKey::End) => editor.action(Action::Motion {
motion: Motion::End,
motion: if ctrl_pressed {
Motion::DocumentEnd
} else {
Motion::End
},
select: shift_pressed,
}),
Key::Named(NamedKey::PageUp) => editor.action(Action::Motion {
Expand All @@ -249,9 +257,19 @@ fn main() {
Key::Named(NamedKey::Escape) => editor.action(Action::Escape),
Key::Named(NamedKey::Enter) => editor.action(Action::Enter),
Key::Named(NamedKey::Backspace) => {
editor.action(Action::Backspace)
editor.action(if ctrl_pressed {
Action::DeleteStartOfWord
} else {
Action::Backspace
})
}
Key::Named(NamedKey::Delete) => {
editor.action(if ctrl_pressed {
Action::DeleteEndOfWord
} else {
Action::Delete
})
}
Key::Named(NamedKey::Delete) => editor.action(Action::Delete),
Key::Named(key) => {
if let Some(text) = key.to_text() {
for c in text.chars() {
Expand Down Expand Up @@ -293,6 +311,9 @@ fn main() {
});
}
}
"a" => {
editor.action(Action::SelectAll);
}
_ => {}
}
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,16 @@ impl Buffer {
cursor.index = self.lines.get(cursor.line)?.text().len();
cursor_x_opt = None;
}
Motion::DocumentStart => {
cursor.line = 0;
cursor.index = 0;
cursor_x_opt = None;
}
Motion::DocumentEnd => {
cursor.line = self.lines.len() - 1;
cursor.index = self.lines.get(cursor.line)?.text().len();
cursor_x_opt = None;
}
Motion::PageUp => {
(cursor, cursor_x_opt) = self.cursor_motion(
font_system,
Expand Down
4 changes: 4 additions & 0 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ pub enum Motion {
ParagraphStart,
/// Move cursor to end of paragraph
ParagraphEnd,
/// Move cursor to start of document
DocumentStart,
/// Move cursor to end of document
DocumentEnd,
/// Move cursor up one page
PageUp,
/// Move cursor down one page
Expand Down
44 changes: 44 additions & 0 deletions src/edit/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,22 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
}
self.selection = Selection::None;
}
Action::SelectAll => {
self.action(
font_system,
Action::Motion {
motion: Motion::DocumentStart,
select: false,
},
);
self.action(
font_system,
Action::Motion {
motion: Motion::DocumentEnd,
select: true,
},
);
}
Action::Insert(character) => {
if character.is_control() && !['\t', '\n', '\u{92}'].contains(&character) {
// Filter out special chars (except for tab), use Action instead
Expand Down Expand Up @@ -714,6 +730,20 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
}
}
}
Action::DeleteStartOfWord => {
if self.delete_selection() {
// Deleted selection
} else {
self.action(
font_system,
Action::Motion {
motion: Motion::PreviousWord,
select: true,
},
);
self.delete_selection();
}
}
Action::Delete => {
if self.delete_selection() {
// Deleted selection
Expand Down Expand Up @@ -749,6 +779,20 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
}
}
}
Action::DeleteEndOfWord => {
if self.delete_selection() {
// Deleted selection
} else {
self.action(
font_system,
Action::Motion {
motion: Motion::NextWord,
select: true,
},
);
self.delete_selection();
}
}
Action::Indent => {
// Get start and end of selection
let (start, end) = match self.selection_bounds() {
Expand Down
6 changes: 6 additions & 0 deletions src/edit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ pub enum Action {
},
/// Escape, clears selection
Escape,
/// Select text from start to end
SelectAll,
/// Insert character at cursor
Insert(char),
/// Create new line
Enter,
/// Delete text behind cursor
Backspace,
/// Delete text behind cursor to next word boundary
DeleteStartOfWord,
/// Delete text in front of cursor
Delete,
/// Delete text in front of cursor to next word boundary
DeleteEndOfWord,
// Indent text (typically Tab)
Indent,
// Unindent text (typically Shift+Tab)
Expand Down

0 comments on commit d04b3f2

Please sign in to comment.