Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ fn focused_window() -> Option<xcap::Window> {
.find(|w| w.is_focused().unwrap_or(false))
}

/// The focused window's id, for `Target::Window`.
pub fn active_window() -> Option<u32> {
focused_window()?.id().ok()
}

/// A window's process name and title, for naming.
fn names(window: &xcap::Window) -> (Option<String>, Option<String>) {
let by_pid = window.pid().ok().and_then(process_name);
Expand Down
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 16 additions & 12 deletions src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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();
}
Expand Down
36 changes: 35 additions & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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)
Expand All @@ -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",
}
}
Expand All @@ -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",
}
}
Expand Down Expand Up @@ -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",
}
}
Expand Down Expand Up @@ -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::<Settings>(&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() {
Expand Down
5 changes: 4 additions & 1 deletion src/tray/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Loading