diff --git a/src/main.rs b/src/main.rs index ddbbf6b..de838c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,6 +115,7 @@ enum Message { PrintTask { result: Result<(), String>, }, + KeyChar(Key), } struct NoctuaApp { @@ -307,6 +308,33 @@ impl Application for NoctuaApp { }, ); } + Message::KeyChar(key) => { + // Handle Ctrl+P shortcut for printing + if self.modifiers.control() && key == Key::Character("p".into()) { + if self.printing { + return Task::none(); + } + + let document = match self.current_document() { + Some(doc) => doc, + None => return Task::none(), + }; + let path = document.path.clone(); + let kind = document.kind; + + self.printing = true; + + return Task::perform( + tokio::task::spawn_blocking(move || print_document_sync(&path, kind)), + |result| { + let inner = result + .map_err(|e| format!("Spawn error: {e}")) + .and_then(|r| r); + cosmic::Action::App(Message::PrintTask { result: inner }) + }, + ); + } + } Message::PrintTask { result } => { self.printing = false; match result { @@ -333,6 +361,10 @@ impl Application for NoctuaApp { Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => { Some(Message::ModifiersChanged(modifiers)) } + Event::Keyboard(keyboard::Event::KeyPressed { + key: Key::Character(c), + .. + }) if c == "p" => Some(Message::KeyChar(Key::Character(c))), Event::Keyboard(keyboard::Event::KeyPressed { key: Key::Named(Named::PageUp | Named::ArrowUp | Named::ArrowLeft), ..