feat: add Ctrl+P keyboard shortcut for printing
- Add KeyChar message and listener for character key presses - Handle Ctrl+P in update to trigger print task - Rebuild and install binary
This commit is contained in:
parent
2bc2abf070
commit
fe1d7c0ebb
1 changed files with 32 additions and 0 deletions
32
src/main.rs
32
src/main.rs
|
|
@ -115,6 +115,7 @@ enum Message {
|
||||||
PrintTask {
|
PrintTask {
|
||||||
result: Result<(), String>,
|
result: Result<(), String>,
|
||||||
},
|
},
|
||||||
|
KeyChar(Key),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NoctuaApp {
|
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 } => {
|
Message::PrintTask { result } => {
|
||||||
self.printing = false;
|
self.printing = false;
|
||||||
match result {
|
match result {
|
||||||
|
|
@ -333,6 +361,10 @@ impl Application for NoctuaApp {
|
||||||
Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
|
Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => {
|
||||||
Some(Message::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 {
|
Event::Keyboard(keyboard::Event::KeyPressed {
|
||||||
key: Key::Named(Named::PageUp | Named::ArrowUp | Named::ArrowLeft),
|
key: Key::Named(Named::PageUp | Named::ArrowUp | Named::ArrowLeft),
|
||||||
..
|
..
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue