From f14d39338a9e1efe54d74dc2097bfe541bd48c7a Mon Sep 17 00:00:00 2001 From: Michael Burr Date: Thu, 5 Nov 2026 21:05:51 +0000 Subject: [PATCH] feat(hotkey): hotkeys for the last region, the active window and fullscreen Three opt-in captures under Capture and record, beside the active display: the last region (an error until a region was taken), the focused window and every display. The capture menu shows their keys. --- src/capture.rs | 5 +++++ src/main.rs | 15 +++++++++++++++ src/main_window.rs | 28 ++++++++++++++++------------ src/settings.rs | 36 +++++++++++++++++++++++++++++++++++- src/tray/macos.rs | 5 ++++- 5 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/capture.rs b/src/capture.rs index 9f3f751..1f8ffda 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -430,6 +430,11 @@ fn focused_window() -> Option { .find(|w| w.is_focused().unwrap_or(false)) } +/// The focused window's id, for `Target::Window`. +pub fn active_window() -> Option { + focused_window()?.id().ok() +} + /// A window's process name and title, for naming. fn names(window: &xcap::Window) -> (Option, Option) { let by_pid = window.pid().ok().and_then(process_name); diff --git a/src/main.rs b/src/main.rs index 9049f05..4279cbb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -336,6 +336,21 @@ impl App { ToolAction::CaptureDisplay => { return self.grab(event_loop, capture::Target::ActiveDisplay); } + ToolAction::CaptureLastRegion => { + return match self.last_region { + Some(r) => self.grab(event_loop, capture::Target::Region(r)), + None => self.report(event_loop, "take a region screenshot first".into(), true), + }; + } + ToolAction::CaptureWindow => { + return match capture::active_window() { + Some(id) => self.grab(event_loop, capture::Target::Window(id)), + None => self.report(event_loop, "no window is focused".into(), true), + }; + } + ToolAction::CaptureEverything => { + return self.grab(event_loop, capture::Target::Everything); + } ToolAction::UploadClipboard => return self.upload_clipboard(event_loop), }; self.open_session(event_loop, purpose); diff --git a/src/main_window.rs b/src/main_window.rs index 330db81..0b73f73 100644 --- a/src/main_window.rs +++ b/src/main_window.rs @@ -581,6 +581,12 @@ impl MainWindow { self.capture_menu = (capture::windows(), capture::displays()); } let (windows, displays) = &self.capture_menu; + // A capture's hotkey, if it has one; where the desktop picks the + // keys, snapr doesn't know them. + let tool_hotkeys = &self.form.saved.tool_hotkeys; + let hotkey = |action: ToolAction| { + tool_hotkeys.iter().find(|h| h.action == action).map_or("", |h| h.hotkey.as_str()) + }; egui::Popup::menu(&chevron) .align(egui::RectAlign::RIGHT_START) .gap(4.0) @@ -592,7 +598,11 @@ impl MainWindow { ui.close(); } let last = ui - .add_enabled(self.last_region.is_some(), egui::Button::new("Last region")) + .add_enabled( + self.last_region.is_some(), + egui::Button::new("Last region") + .shortcut_text(hotkey(ToolAction::CaptureLastRegion)), + ) .on_disabled_hover_text("Take a region screenshot first"); if last.clicked() && let Some(r) = self.last_region @@ -613,16 +623,8 @@ impl MainWindow { if displays.is_empty() { ui.weak("No displays"); } else { - // Its hotkey, if it has one; where the desktop picks - // the keys, snapr doesn't know them. - let hotkey = self - .form - .saved - .tool_hotkeys - .iter() - .find(|h| h.action == ToolAction::CaptureDisplay) - .map_or("", |h| h.hotkey.as_str()); - let active = egui::Button::new("Active display").shortcut_text(hotkey); + let active = egui::Button::new("Active display") + .shortcut_text(hotkey(ToolAction::CaptureDisplay)); if ui .add(active) .on_hover_text("The display the focused window is on") @@ -653,7 +655,9 @@ impl MainWindow { } else { "Fullscreen" }; - if ui.button(everything).clicked() { + let everything = + egui::Button::new(everything).shortcut_text(hotkey(ToolAction::CaptureEverything)); + if ui.add(everything).clicked() { actions.push(Action::CaptureTarget(capture::Target::Everything)); ui.close(); } diff --git a/src/settings.rs b/src/settings.rs index 595e12f..bf904d9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -299,6 +299,12 @@ pub enum ToolAction { PinRegion, /// Screenshot the display the focused window is on. CaptureDisplay, + /// Screenshot the same region as the last screenshot. + CaptureLastRegion, + /// Screenshot the focused window. + CaptureWindow, + /// Screenshot every display. + CaptureEverything, /// Upload what's on the clipboard: copied files, an image or text. UploadClipboard, } @@ -313,7 +319,12 @@ impl ToolAction { ]; /// The captures that can have a hotkey besides region and record. - pub const CAPTURES: [ToolAction; 1] = [ToolAction::CaptureDisplay]; + pub const CAPTURES: [ToolAction; 4] = [ + ToolAction::CaptureDisplay, + ToolAction::CaptureLastRegion, + ToolAction::CaptureWindow, + ToolAction::CaptureEverything, + ]; pub fn is_capture(self) -> bool { Self::CAPTURES.contains(&self) @@ -326,6 +337,9 @@ impl ToolAction { ToolAction::ScanQr => "scan-qr", ToolAction::PinRegion => "pin-region", ToolAction::CaptureDisplay => "capture-display", + ToolAction::CaptureLastRegion => "capture-last-region", + ToolAction::CaptureWindow => "capture-window", + ToolAction::CaptureEverything => "capture-everything", ToolAction::UploadClipboard => "upload-clipboard", } } @@ -336,6 +350,9 @@ impl ToolAction { ToolAction::ScanQr => "Read a QR code", ToolAction::PinRegion => "Pin a region to the screen", ToolAction::CaptureDisplay => "Capture active display", + ToolAction::CaptureLastRegion => "Capture last region", + ToolAction::CaptureWindow => "Capture active window", + ToolAction::CaptureEverything => "Capture fullscreen", ToolAction::UploadClipboard => "Upload the clipboard", } } @@ -381,6 +398,9 @@ impl TrayClick { TrayClick::Tool(ToolAction::ScanQr) => "Reads a QR code", TrayClick::Tool(ToolAction::PinRegion) => "Pins a region to the screen", TrayClick::Tool(ToolAction::CaptureDisplay) => "Captures the active display", + TrayClick::Tool(ToolAction::CaptureLastRegion) => "Captures the last region", + TrayClick::Tool(ToolAction::CaptureWindow) => "Captures the active window", + TrayClick::Tool(ToolAction::CaptureEverything) => "Captures everything", TrayClick::Tool(ToolAction::UploadClipboard) => "Uploads the clipboard", } } @@ -923,6 +943,20 @@ mod tests { assert!(old.tool_hotkeys.is_empty()); } + #[test] + fn capture_hotkeys_round_trip() { + let settings = Settings { + tool_hotkeys: ToolAction::CAPTURES + .map(|action| ToolHotkey { action, hotkey: String::new() }) + .to_vec(), + ..Settings::default() + }; + let text = toml::to_string_pretty(&settings).unwrap(); + assert_eq!(toml::from_str::(&text).unwrap(), settings, "{text}"); + let ids: std::collections::HashSet<_> = ToolAction::CAPTURES.map(ToolAction::id).into(); + assert_eq!(ids.len(), ToolAction::CAPTURES.len()); + } + #[test] fn tray_click_round_trip() { for click in TrayClick::all() { diff --git a/src/tray/macos.rs b/src/tray/macos.rs index 483881e..7cf0e34 100644 --- a/src/tray/macos.rs +++ b/src/tray/macos.rs @@ -156,7 +156,10 @@ impl Tray { ToolAction::PinRegion => sel!(pinRegion:), ToolAction::UploadClipboard => sel!(uploadClipboard:), // Not a tool: not in `ALL`. - ToolAction::CaptureDisplay => continue, + ToolAction::CaptureDisplay + | ToolAction::CaptureLastRegion + | ToolAction::CaptureWindow + | ToolAction::CaptureEverything => continue, }; add_to(&tools, tool.label(), Some(action)); }