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::{Pixels, Point, Rectangle, Size};
use std::sync::Arc;
pub trait Editor: Sized + 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 {
Move(Motion),
Select(Motion),
SelectWord,
SelectLine,
Insert(char),
Paste(Arc<String>),
Enter,
Backspace,
Delete,

View file

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

View file

@ -398,6 +398,17 @@ impl editor::Editor for Editor {
editor
.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 => {
editor.action(font_system.raw(), cosmic_text::Action::Enter);
}

View file

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