Move cursor motions to new Motion enum, move handling to Buffer

This commit is contained in:
Jeremy Soller 2023-12-15 09:13:14 -07:00
parent 6528e9f804
commit 018a2e9d2a
10 changed files with 567 additions and 505 deletions

View file

@ -5,7 +5,7 @@ use cosmic::{
iced_runtime::keyboard::KeyCode,
theme::{Theme, ThemeType},
};
use cosmic_text::{Action, Edit, SwashCache};
use cosmic_text::{Action, Edit, Motion, SwashCache};
use std::{cmp, sync::Mutex, time::Instant};
use crate::FONT_SYSTEM;
@ -281,35 +281,35 @@ where
match event {
Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => match key_code {
KeyCode::Left => {
editor.action(Action::Left);
editor.action(Action::Motion(Motion::Left));
status = Status::Captured;
}
KeyCode::Right => {
editor.action(Action::Right);
editor.action(Action::Motion(Motion::Right));
status = Status::Captured;
}
KeyCode::Up => {
editor.action(Action::Up);
editor.action(Action::Motion(Motion::Up));
status = Status::Captured;
}
KeyCode::Down => {
editor.action(Action::Down);
editor.action(Action::Motion(Motion::Down));
status = Status::Captured;
}
KeyCode::Home => {
editor.action(Action::Home);
editor.action(Action::Motion(Motion::Home));
status = Status::Captured;
}
KeyCode::End => {
editor.action(Action::End);
editor.action(Action::Motion(Motion::End));
status = Status::Captured;
}
KeyCode::PageUp => {
editor.action(Action::PageUp);
editor.action(Action::Motion(Motion::PageUp));
status = Status::Captured;
}
KeyCode::PageDown => {
editor.action(Action::PageDown);
editor.action(Action::Motion(Motion::PageDown));
status = Status::Captured;
}
KeyCode::Escape => {

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, SwashCache, SyntaxEditor,
Action, Attrs, Buffer, Edit, Family, FontSystem, Metrics, Motion, SwashCache, SyntaxEditor,
SyntaxSystem,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
@ -147,14 +147,26 @@ fn main() {
match event.to_option() {
EventOption::Key(event) => match event.scancode {
orbclient::K_CTRL => ctrl_pressed = event.pressed,
orbclient::K_LEFT if event.pressed => editor.action(Action::Left),
orbclient::K_RIGHT if event.pressed => editor.action(Action::Right),
orbclient::K_UP if event.pressed => editor.action(Action::Up),
orbclient::K_DOWN if event.pressed => editor.action(Action::Down),
orbclient::K_HOME if event.pressed => editor.action(Action::Home),
orbclient::K_END if event.pressed => editor.action(Action::End),
orbclient::K_PGUP if event.pressed => editor.action(Action::PageUp),
orbclient::K_PGDN if event.pressed => editor.action(Action::PageDown),
orbclient::K_LEFT if event.pressed => {
editor.action(Action::Motion(Motion::Left))
}
orbclient::K_RIGHT if event.pressed => {
editor.action(Action::Motion(Motion::Right))
}
orbclient::K_UP if event.pressed => editor.action(Action::Motion(Motion::Up)),
orbclient::K_DOWN if event.pressed => {
editor.action(Action::Motion(Motion::Down))
}
orbclient::K_HOME if event.pressed => {
editor.action(Action::Motion(Motion::Home))
}
orbclient::K_END if event.pressed => editor.action(Action::Motion(Motion::End)),
orbclient::K_PGUP if event.pressed => {
editor.action(Action::Motion(Motion::PageUp))
}
orbclient::K_PGDN if event.pressed => {
editor.action(Action::Motion(Motion::PageDown))
}
orbclient::K_ESC if event.pressed => editor.action(Action::Escape),
orbclient::K_ENTER if event.pressed => editor.action(Action::Enter),
orbclient::K_BKSP if event.pressed => editor.action(Action::Backspace),

View file

@ -2,7 +2,7 @@
use cosmic_text::{
Action, BidiParagraphs, BorrowedWithFontSystem, Buffer, Color, Edit, Editor, FontSystem,
Metrics, SwashCache,
Metrics, Motion, SwashCache,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, time::Instant};
@ -106,7 +106,7 @@ fn main() {
// Test delete of EGC
{
let cursor = editor.cursor();
editor.action(Action::Previous);
editor.action(Action::Motion(Motion::Previous));
editor.action(Action::Delete);
for c in grapheme.chars() {
editor.action(Action::Insert(c));
@ -130,7 +130,7 @@ fn main() {
{
let cursor = editor.cursor();
editor.action(Action::Enter);
editor.action(Action::Previous);
editor.action(Action::Motion(Motion::Previous));
editor.action(Action::Delete);
assert_eq!(cursor, editor.cursor());
}

View file

@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, Metrics, Shaping, Style,
SwashCache, Weight,
Action, Attrs, Buffer, Color, Edit, Editor, Family, FontSystem, Metrics, Motion, Shaping,
Style, SwashCache, Weight,
};
use orbclient::{EventOption, Renderer, Window, WindowFlag};
use std::{
@ -152,14 +152,26 @@ fn main() {
for event in window.events() {
match event.to_option() {
EventOption::Key(event) => match event.scancode {
orbclient::K_LEFT if event.pressed => editor.action(Action::Left),
orbclient::K_RIGHT if event.pressed => editor.action(Action::Right),
orbclient::K_UP if event.pressed => editor.action(Action::Up),
orbclient::K_DOWN if event.pressed => editor.action(Action::Down),
orbclient::K_HOME if event.pressed => editor.action(Action::Home),
orbclient::K_END if event.pressed => editor.action(Action::End),
orbclient::K_PGUP if event.pressed => editor.action(Action::PageUp),
orbclient::K_PGDN if event.pressed => editor.action(Action::PageDown),
orbclient::K_LEFT if event.pressed => {
editor.action(Action::Motion(Motion::Left))
}
orbclient::K_RIGHT if event.pressed => {
editor.action(Action::Motion(Motion::Right))
}
orbclient::K_UP if event.pressed => editor.action(Action::Motion(Motion::Up)),
orbclient::K_DOWN if event.pressed => {
editor.action(Action::Motion(Motion::Down))
}
orbclient::K_HOME if event.pressed => {
editor.action(Action::Motion(Motion::Home))
}
orbclient::K_END if event.pressed => editor.action(Action::Motion(Motion::End)),
orbclient::K_PGUP if event.pressed => {
editor.action(Action::Motion(Motion::PageUp))
}
orbclient::K_PGDN if event.pressed => {
editor.action(Action::Motion(Motion::PageDown))
}
orbclient::K_ENTER if event.pressed => editor.action(Action::Enter),
orbclient::K_BKSP if event.pressed => editor.action(Action::Backspace),
orbclient::K_DEL if event.pressed => editor.action(Action::Delete),