summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-16 15:40:16 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-16 15:40:16 +0200
commitd051f21597bb333ac10183aaa3214a292e9aa365 (patch)
tree0735e31bae1fefcf004ebba199bd5ec759040605
parentc6d0443627c22dcf1576303e5a426aa3622f1b7d (diff)
downloadiced-d051f21597bb333ac10183aaa3214a292e9aa365.tar.gz
iced-d051f21597bb333ac10183aaa3214a292e9aa365.tar.bz2
iced-d051f21597bb333ac10183aaa3214a292e9aa365.zip
Implement `Copy` and `Paste` actions for `text::Editor`
-rw-r--r--core/src/text/editor.rs5
-rw-r--r--examples/editor/src/main.rs2
-rw-r--r--graphics/src/text/editor.rs11
-rw-r--r--widget/src/text_editor.rs11
4 files changed, 24 insertions, 5 deletions
diff --git a/core/src/text/editor.rs b/core/src/text/editor.rs
index 5532fac5..003557c1 100644
--- a/core/src/text/editor.rs
+++ b/core/src/text/editor.rs
@@ -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,
diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs
index 2a70b34c..11819c69 100644
--- a/examples/editor/src/main.rs
+++ b/examples/editor/src/main.rs
@@ -9,7 +9,7 @@ struct Editor {
content: text_editor::Content,
}
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone)]
enum Message {
Edit(text_editor::Action),
}
diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs
index 1e375a25..1890cb82 100644
--- a/graphics/src/text/editor.rs
+++ b/graphics/src/text/editor.rs
@@ -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);
}
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index ec7a6d1d..0bb6b7d3 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -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))));
}
}
}