Notify Highlighter of topmost line change

This commit is contained in:
Héctor Ramón Jiménez 2023-09-17 19:27:51 +02:00
parent 23d00445ff
commit 2897986f2d
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 61 additions and 31 deletions

View file

@ -47,13 +47,18 @@ pub enum Action {
Select(Motion), Select(Motion),
SelectWord, SelectWord,
SelectLine, SelectLine,
Edit(Edit),
Click(Point),
Drag(Point),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Edit {
Insert(char), Insert(char),
Paste(Arc<String>), Paste(Arc<String>),
Enter, Enter,
Backspace, Backspace,
Delete, Delete,
Click(Point),
Drag(Point),
} }
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]

View file

@ -1,10 +1,12 @@
use crate::core::text::editor::{self, Action, Cursor, Direction, Motion}; use crate::core::text::editor::{
self, Action, Cursor, Direction, Edit, Motion,
};
use crate::core::text::highlighter::{self, Highlighter}; use crate::core::text::highlighter::{self, Highlighter};
use crate::core::text::LineHeight; use crate::core::text::LineHeight;
use crate::core::{Font, Pixels, Point, Rectangle, Size}; use crate::core::{Font, Pixels, Point, Rectangle, Size};
use crate::text; use crate::text;
use cosmic_text::Edit; use cosmic_text::Edit as _;
use std::fmt; use std::fmt;
use std::sync::{self, Arc}; use std::sync::{self, Arc};
@ -370,22 +372,42 @@ impl editor::Editor for Editor {
} }
// Editing events // Editing events
Action::Insert(c) => { Action::Edit(edit) => {
editor match edit {
.action(font_system.raw(), cosmic_text::Action::Insert(c)); Edit::Insert(c) => {
editor.action(
font_system.raw(),
cosmic_text::Action::Insert(c),
);
} }
Action::Paste(text) => { Edit::Paste(text) => {
editor.insert_string(&text, None); editor.insert_string(&text, None);
} }
Action::Enter => { Edit::Enter => {
editor.action(font_system.raw(), cosmic_text::Action::Enter); editor.action(
font_system.raw(),
cosmic_text::Action::Enter,
);
} }
Action::Backspace => { Edit::Backspace => {
editor editor.action(
.action(font_system.raw(), cosmic_text::Action::Backspace); font_system.raw(),
cosmic_text::Action::Backspace,
);
} }
Action::Delete => { Edit::Delete => {
editor.action(font_system.raw(), cosmic_text::Action::Delete); editor.action(
font_system.raw(),
cosmic_text::Action::Delete,
);
}
}
let cursor = editor.cursor();
let selection = editor.select_opt().unwrap_or(cursor);
internal.topmost_line_changed =
Some(cursor.min(selection).line);
} }
// Mouse events // Mouse events

View file

@ -17,7 +17,7 @@ use std::ops::DerefMut;
use std::sync::Arc; use std::sync::Arc;
pub use crate::style::text_editor::{Appearance, Highlight, StyleSheet}; pub use crate::style::text_editor::{Appearance, Highlight, StyleSheet};
pub use text::editor::{Action, Motion}; pub use text::editor::{Action, Edit, Motion};
pub struct TextEditor<'a, Highlighter, Message, Renderer = crate::Renderer> pub struct TextEditor<'a, Highlighter, Message, Renderer = crate::Renderer>
where where
@ -301,7 +301,7 @@ where
Update::Release => { Update::Release => {
state.drag_click = None; state.drag_click = None;
} }
Update::Edit(action) => { Update::Action(action) => {
shell.publish(on_edit(action)); shell.publish(on_edit(action));
} }
Update::Copy => { Update::Copy => {
@ -311,7 +311,9 @@ where
} }
Update::Paste => { Update::Paste => {
if let Some(contents) = clipboard.read() { if let Some(contents) = clipboard.read() {
shell.publish(on_edit(Action::Paste(Arc::new(contents)))); shell.publish(on_edit(Action::Edit(Edit::Paste(
Arc::new(contents),
))));
} }
} }
} }
@ -457,7 +459,7 @@ enum Update {
Click(mouse::Click), Click(mouse::Click),
Unfocus, Unfocus,
Release, Release,
Edit(Action), Action(Action),
Copy, Copy,
Paste, Paste,
} }
@ -470,7 +472,8 @@ impl Update {
padding: Padding, padding: Padding,
cursor: mouse::Cursor, cursor: mouse::Cursor,
) -> Option<Self> { ) -> Option<Self> {
let edit = |action| Some(Update::Edit(action)); let action = |action| Some(Update::Action(action));
let edit = |edit| action(Action::Edit(edit));
match event { match event {
Event::Mouse(event) => match event { Event::Mouse(event) => match event {
@ -499,7 +502,7 @@ impl Update {
let cursor_position = cursor.position_in(bounds)? let cursor_position = cursor.position_in(bounds)?
- Vector::new(padding.top, padding.left); - Vector::new(padding.top, padding.left);
edit(Action::Drag(cursor_position)) action(Action::Drag(cursor_position))
} }
_ => None, _ => None,
}, },
@ -518,7 +521,7 @@ impl Update {
motion motion
}; };
return edit(if modifiers.shift() { return action(if modifiers.shift() {
Action::Select(motion) Action::Select(motion)
} else { } else {
Action::Move(motion) Action::Move(motion)
@ -526,9 +529,9 @@ impl Update {
} }
match key_code { match key_code {
keyboard::KeyCode::Enter => edit(Action::Enter), keyboard::KeyCode::Enter => edit(Edit::Enter),
keyboard::KeyCode::Backspace => edit(Action::Backspace), keyboard::KeyCode::Backspace => edit(Edit::Backspace),
keyboard::KeyCode::Delete => edit(Action::Delete), keyboard::KeyCode::Delete => edit(Edit::Delete),
keyboard::KeyCode::Escape => Some(Self::Unfocus), keyboard::KeyCode::Escape => Some(Self::Unfocus),
keyboard::KeyCode::C if modifiers.command() => { keyboard::KeyCode::C if modifiers.command() => {
Some(Self::Copy) Some(Self::Copy)
@ -542,7 +545,7 @@ impl Update {
} }
} }
keyboard::Event::CharacterReceived(c) if state.is_focused => { keyboard::Event::CharacterReceived(c) if state.is_focused => {
edit(Action::Insert(c)) edit(Edit::Insert(c))
} }
_ => None, _ => None,
}, },