From 5c08ab963fb5e0f7651863736e39097533161270 Mon Sep 17 00:00:00 2001 From: Michael Burr Date: Thu, 5 Nov 2026 21:09:21 +0000 Subject: [PATCH] feat(tray): record a region, and stop the recording, from the tray menu --- src/main.rs | 9 +++++++++ src/tray/linux.rs | 11 ++++++++++- src/tray/macos.rs | 16 +++++++++++++++- src/tray/mod.rs | 11 +++++++++++ src/tray/windows.rs | 26 ++++++++++++++++++++++---- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4279cbb..fc4a7b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -254,6 +254,8 @@ struct App { tray: Option, /// How far the uploads have got, as last shown on the tray icon. tray_progress: Option, + /// Whether the tray's Record item last said Stop. + tray_recording: bool, output: Output, /// Open captures. Pressing the hotkey during a capture stacks a new one /// on top (so the overlay itself can be captured); the last is active. @@ -1585,6 +1587,7 @@ impl ApplicationHandler for App { UserEvent::Hotkey | UserEvent::Tray(TrayAction::Capture) => { self.start_capture(event_loop) } + UserEvent::Tray(TrayAction::Record) => self.toggle_recording(event_loop), UserEvent::RecordHotkey => { if !self.main_window.as_ref().is_some_and(|w| w.form.recording) { self.toggle_recording(event_loop) @@ -1886,6 +1889,11 @@ impl ApplicationHandler for App { self.tray_progress = progress; tray.set_progress(progress); } + let recording = self.recording.is_some(); + if recording != self.tray_recording { + self.tray_recording = recording; + tray.set_recording(recording); + } if progress.is_some() { let t = now + Duration::from_millis(250); wake = Some(wake.map_or(t, |w| w.min(t))); @@ -2414,6 +2422,7 @@ fn main() { recording_lock, tray: None, tray_progress: None, + tray_recording: false, output, sessions: Vec::new(), style: remembered.style(), diff --git a/src/tray/linux.rs b/src/tray/linux.rs index b38aaa0..2ede25c 100644 --- a/src/tray/linux.rs +++ b/src/tray/linux.rs @@ -4,11 +4,13 @@ use ksni::blocking::{Handle, TrayMethods}; use ksni::menu::{MenuItem, StandardItem, SubMenu}; -use super::{Callback, TrayAction, capture_label}; +use super::{Callback, TrayAction, capture_label, record_label}; use crate::settings::{ToolAction, TrayClick, TrayIconStyle}; struct Sni { capture_label: String, + /// A recording is in progress, so the Record item stops it. + recording: bool, callback: Callback, /// How far the uploads have got, while there are any. progress: Option, @@ -53,6 +55,7 @@ impl ksni::Tray for Sni { }; vec![ item(self.capture_label.clone(), TrayAction::Capture), + item(record_label(self.recording).into(), TrayAction::Record), SubMenu { label: "Tools".into(), submenu: ToolAction::ALL @@ -79,6 +82,7 @@ impl Tray { pub fn new(hotkey: Option<&str>, callback: Callback) -> Result { let sni = Sni { capture_label: capture_label(hotkey), + recording: false, callback, progress: None, color: super::color(TrayIconStyle::default()), @@ -92,6 +96,11 @@ impl Tray { self.handle.update(|t| t.capture_label = label); } + /// Whether a recording is in progress, for the Record item's label. + pub fn set_recording(&self, recording: bool) { + self.handle.update(|t| t.recording = recording); + } + /// A left-click always sends `TrayAction::Click` here: the menu is only /// shown by a right-click. pub fn set_click(&self, _click: TrayClick) {} diff --git a/src/tray/macos.rs b/src/tray/macos.rs index 7cf0e34..cd50e56 100644 --- a/src/tray/macos.rs +++ b/src/tray/macos.rs @@ -13,7 +13,7 @@ use objc2_app_kit::{ }; use objc2_foundation::{NSData, NSSize, NSString}; -use super::{Callback, TrayAction, capture_label}; +use super::{Callback, TrayAction, capture_label, record_label}; use crate::settings::{ToolAction, TrayClick, TrayIconStyle}; struct Ivars { @@ -54,6 +54,11 @@ define_class!( (self.ivars().callback)(TrayAction::Capture); } + #[unsafe(method(record:))] + fn record(&self, _sender: Option<&AnyObject>) { + (self.ivars().callback)(TrayAction::Record); + } + #[unsafe(method(pickColor:))] fn pick_color(&self, _sender: Option<&AnyObject>) { (self.ivars().callback)(TrayAction::Tool(ToolAction::PickColor)); @@ -124,6 +129,7 @@ impl Target { pub struct Tray { status_item: Retained, capture_item: Retained, + record_item: Retained, menu: Retained, _target: Retained, } @@ -148,6 +154,7 @@ impl Tray { }; let add = |title: &str, action: Sel| add_to(&menu, title, Some(action)); let capture_item = add(&capture_label(hotkey), sel!(capture:)); + let record_item = add(record_label(false), sel!(record:)); let tools = NSMenu::new(mtm); for tool in ToolAction::ALL { let action = match tool { @@ -190,6 +197,7 @@ impl Tray { let tray = Self { status_item, capture_item, + record_item, menu, _target: target, }; @@ -210,6 +218,12 @@ impl Tray { .setTitle(&NSString::from_str(&capture_label(hotkey))); } + /// Whether a recording is in progress, for the Record item's label. + pub fn set_recording(&self, recording: bool) { + self.record_item + .setTitle(&NSString::from_str(record_label(recording))); + } + /// macOS tints the icon to suit the menu bar, whatever the style. pub fn set_style(&self, _style: TrayIconStyle) {} diff --git a/src/tray/mod.rs b/src/tray/mod.rs index ab61d8b..295d8c0 100644 --- a/src/tray/mod.rs +++ b/src/tray/mod.rs @@ -23,6 +23,8 @@ pub enum TrayAction { /// The icon was left-clicked: whatever `Settings::tray_click` says. Click, Capture, + /// Start recording a region, or stop the recording in progress. + Record, /// Start one of the tools, from the Tools submenu. Tool(ToolAction), /// Open the main window on the Recent page. @@ -41,6 +43,15 @@ fn capture_label(hotkey: Option<&str>) -> String { } } +/// The Record item, which stops the recording while there is one. +fn record_label(recording: bool) -> &'static str { + if recording { + "Stop recording" + } else { + "Record region" + } +} + /// What the tray icon shows while uploading. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Progress { diff --git a/src/tray/windows.rs b/src/tray/windows.rs index 475ae01..45b9d4f 100644 --- a/src/tray/windows.rs +++ b/src/tray/windows.rs @@ -24,7 +24,7 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{ WNDCLASSW, }; -use super::{Callback, TrayAction, capture_label}; +use super::{Callback, TrayAction, capture_label, record_label}; use crate::settings::{ToolAction, TrayClick, TrayIconStyle}; const CLASS_NAME: &str = "snapr.tray"; @@ -39,6 +39,7 @@ const UPLOAD_FILES: usize = 0x736e_7570; const MENU: &[(usize, TrayAction)] = &[ (1, TrayAction::Capture), + (6, TrayAction::Record), (5, TrayAction::Recent), (2, TrayAction::Settings), (3, TrayAction::OpenFolder), @@ -58,6 +59,8 @@ struct State { /// What `style` came to when the icon was last drawn. color: [u8; 4], capture_label: String, + /// A recording is in progress, so the Record item stops it. + recording: bool, callback: Callback, taskbar_created: u32, /// What a left-click does. @@ -123,6 +126,7 @@ impl Tray { style, color, capture_label: capture_label(hotkey), + recording: false, callback, taskbar_created: unsafe { RegisterWindowMessageW(wide("TaskbarCreated").as_ptr()) }, click: TrayClick::default(), @@ -146,6 +150,15 @@ impl Tray { }); } + /// Whether a recording is in progress, for the Record item's label. + pub fn set_recording(&self, recording: bool) { + STATE.with(|s| { + if let Some(state) = s.borrow_mut().as_mut() { + state.recording = recording; + } + }); + } + pub fn on_files(&self, callback: FilesCallback) { STATE.with(|s| { if let Some(state) = s.borrow_mut().as_mut() { @@ -344,14 +357,18 @@ fn click() -> TrayClick { } fn show_menu(hwnd: HWND) { - let Some(capture) = STATE.with(|s| s.borrow().as_ref().map(|st| st.capture_label.clone())) - else { + let Some((capture, recording)) = STATE.with(|s| { + s.borrow() + .as_ref() + .map(|st| (st.capture_label.clone(), st.recording)) + }) else { return; }; // In bold: what a left-click does, if it's on the menu. let default = match click() { TrayClick::Open => Some(5), TrayClick::Capture => Some(1), + TrayClick::Record => Some(6), _ => None, }; // SAFETY: standard popup-menu sequence on our own window. @@ -360,6 +377,7 @@ fn show_menu(hwnd: HWND) { for &(id, action) in MENU { let label = match action { TrayAction::Capture => capture.clone(), + TrayAction::Record => record_label(recording).into(), TrayAction::Click | TrayAction::Tool(_) => unreachable!(), TrayAction::Recent => "Recent screenshots\u{2026}".into(), TrayAction::Settings => "Settings\u{2026}".into(), @@ -370,7 +388,7 @@ fn show_menu(hwnd: HWND) { } }; AppendMenuW(menu, MF_STRING, id, wide(&label).as_ptr()); - if action == TrayAction::Capture { + if action == TrayAction::Record { // Destroyed along with `menu`. let tools = CreatePopupMenu(); for (i, tool) in ToolAction::ALL.into_iter().enumerate() {