Skip to content

Commit

Permalink
Implement Copy and Paste actions for text::Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Sep 16, 2023
1 parent c6d0443 commit d051f21
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion core/src/text/editor.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Editor {
content: text_editor::Content,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
enum Message {
Edit(text_editor::Action),
}
Expand Down
11 changes: 11 additions & 0 deletions graphics/src/text/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 8 additions & 3 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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))));
}
}
}
Expand Down

0 comments on commit d051f21

Please sign in to comment.