Implement copy/cut/paste, only use iced_winit

This commit is contained in:
Jeremy Soller 2023-10-31 13:18:43 -06:00
parent 03977fda6a
commit a6976daa25
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
6 changed files with 74 additions and 310 deletions

View file

@ -1,7 +1,7 @@
use cosmic::iced::keyboard::{KeyCode, Modifiers};
use std::{collections::HashMap, fmt};
use crate::{fl, Message};
use crate::Message;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct KeyBind {
@ -26,6 +26,9 @@ impl KeyBind {
}};
}
bind!(CTRL, X, Cut);
bind!(CTRL, C, Copy);
bind!(CTRL, V, Paste);
bind!(CTRL, N, New);
bind!(CTRL, O, OpenFileDialog);
bind!(CTRL, S, Save);

View file

@ -4,7 +4,7 @@ use cosmic::{
app::{message, Command, Core, Settings},
executor,
iced::{
event, keyboard, subscription,
clipboard, event, keyboard, subscription,
widget::{row, text},
Alignment, Length, Limits,
},
@ -48,7 +48,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
localize::localize();
let settings = Settings::default().size_limits(Limits::NONE.min_width(400.0).min_height(200.0));
let mut settings = Settings::default();
//TODO: fix wayland resize on iced_winit
settings = settings.client_decorations(false);
//TODO: allow size limits on iced_winit
//settings = settings.size_limits(Limits::NONE.min_width(400.0).min_height(200.0));
let flags = ();
cosmic::app::run::<App>(settings, flags)?;
@ -65,12 +72,16 @@ pub struct App {
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Message {
Cut,
Copy,
KeyBind(KeyBind),
New,
OpenFileDialog,
OpenFile(PathBuf),
OpenProjectDialog,
OpenProject(PathBuf),
Paste,
PasteValue(String),
Save,
TabActivate(segmented_button::Entity),
TabClose(segmented_button::Entity),
@ -383,6 +394,27 @@ impl cosmic::Application for App {
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::Cut => match self.active_tab() {
Some(tab) => {
let mut editor = tab.editor.lock().unwrap();
let selection_opt = editor.copy_selection();
editor.delete_selection();
if let Some(selection) = selection_opt {
return clipboard::write(selection);
}
}
None => {}
},
Message::Copy => match self.active_tab() {
Some(tab) => {
let mut editor = tab.editor.lock().unwrap();
let selection_opt = editor.copy_selection();
if let Some(selection) = selection_opt {
return clipboard::write(selection);
}
}
None => {}
},
Message::KeyBind(key_bind) => {
for (config_key_bind, config_message) in self.config.keybinds.iter() {
if config_key_bind == &key_bind {
@ -425,6 +457,22 @@ impl cosmic::Application for App {
Message::OpenProject(path) => {
self.open_project(path);
}
Message::Paste => {
return clipboard::read(|value_opt| match value_opt {
Some(value) => message::app(Message::PasteValue(value)),
None => message::none(),
});
}
Message::PasteValue(value) => {
println!("Paste {:?}", value);
match self.active_tab() {
Some(tab) => {
let mut editor = tab.editor.lock().unwrap();
editor.insert_string(&value, None);
}
None => {}
}
}
Message::Save => {
let mut title_opt = None;

View file

@ -96,9 +96,9 @@ pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
menu_key(fl!("undo"), "Ctrl + Z", Message::Todo),
menu_key(fl!("redo"), "Ctrl + Shift + Z", Message::Todo),
MenuTree::new(horizontal_rule(1)),
menu_key(fl!("cut"), "Ctrl + X", Message::Todo),
menu_key(fl!("copy"), "Ctrl + C", Message::Todo),
menu_key(fl!("paste"), "Ctrl + V", Message::Todo),
menu_item(fl!("cut"), Message::Cut),
menu_item(fl!("copy"), Message::Copy),
menu_item(fl!("paste"), Message::Paste),
MenuTree::new(horizontal_rule(1)),
menu_key(fl!("find"), "Ctrl + F", Message::Todo),
menu_key(fl!("replace"), "Ctrl + H", Message::Todo),

View file

@ -32,7 +32,7 @@ impl Tab {
.unwrap();
let mut editor = ViEditor::new(editor);
editor.set_passthrough(false);
editor.set_passthrough(!cfg!(feature = "vi"));
Self {
path_opt: None,