Implement Copy and Paste actions for text::Editor
This commit is contained in:
parent
c6d0443627
commit
d051f21597
4 changed files with 24 additions and 5 deletions
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue