Implement Copy and Paste actions for text::Editor

This commit is contained in:
Héctor Ramón Jiménez 2023-09-16 15:40:16 +02:00
parent c6d0443627
commit d051f21597
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 24 additions and 5 deletions

View file

@ -1,6 +1,8 @@
use crate::text::LineHeight; use crate::text::LineHeight;
use crate::{Pixels, Point, Rectangle, Size}; use crate::{Pixels, Point, Rectangle, Size};
use std::sync::Arc;
pub trait Editor: Sized + Default { pub trait Editor: Sized + Default {
type Font: Copy + PartialEq + Default; type Font: Copy + PartialEq + Default;
@ -30,13 +32,14 @@ pub trait Editor: Sized + Default {
); );
} }
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Action { pub enum Action {
Move(Motion), Move(Motion),
Select(Motion), Select(Motion),
SelectWord, SelectWord,
SelectLine, SelectLine,
Insert(char), Insert(char),
Paste(Arc<String>),
Enter, Enter,
Backspace, Backspace,
Delete, Delete,

View file

@ -9,7 +9,7 @@ struct Editor {
content: text_editor::Content, content: text_editor::Content,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone)]
enum Message { enum Message {
Edit(text_editor::Action), Edit(text_editor::Action),
} }

View file

@ -398,6 +398,17 @@ impl editor::Editor for Editor {
editor editor
.action(font_system.raw(), cosmic_text::Action::Insert(c)); .action(font_system.raw(), cosmic_text::Action::Insert(c));
} }
Action::Paste(text) => {
editor.insert_string(&text, None);
// TODO: Fix cosmic-text
// Cursor should be marked as moved after `insert_string`.
let cursor = editor.cursor();
editor
.buffer_mut()
.shape_until_cursor(font_system.raw(), cursor);
}
Action::Enter => { Action::Enter => {
editor.action(font_system.raw(), cosmic_text::Action::Enter); editor.action(font_system.raw(), cosmic_text::Action::Enter);
} }

View file

@ -12,6 +12,7 @@ use crate::core::{
}; };
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::Arc;
pub use crate::style::text_editor::{Appearance, StyleSheet}; pub use crate::style::text_editor::{Appearance, StyleSheet};
pub use text::editor::{Action, Motion}; pub use text::editor::{Action, Motion};
@ -253,10 +254,14 @@ where
Update::Edit(action) => { Update::Edit(action) => {
shell.publish(on_edit(action)); shell.publish(on_edit(action));
} }
Update::Copy => todo!(), Update::Copy => {
if let Some(selection) = self.content.selection() {
clipboard.write(selection);
}
}
Update::Paste => { Update::Paste => {
if let Some(_contents) = clipboard.read() { if let Some(contents) = clipboard.read() {
todo!() shell.publish(on_edit(Action::Paste(Arc::new(contents))));
} }
} }
} }