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
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ struct App {
tray: Option<Tray>,
/// How far the uploads have got, as last shown on the tray icon.
tray_progress: Option<tray::Progress>,
/// 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.
Expand Down Expand Up @@ -1585,6 +1587,7 @@ impl ApplicationHandler<UserEvent> 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)
Expand Down Expand Up @@ -1886,6 +1889,11 @@ impl ApplicationHandler<UserEvent> 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)));
Expand Down Expand Up @@ -2414,6 +2422,7 @@ fn main() {
recording_lock,
tray: None,
tray_progress: None,
tray_recording: false,
output,
sessions: Vec::new(),
style: remembered.style(),
Expand Down
11 changes: 10 additions & 1 deletion src/tray/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<super::Progress>,
Expand Down Expand Up @@ -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
Expand All @@ -79,6 +82,7 @@ impl Tray {
pub fn new(hotkey: Option<&str>, callback: Callback) -> Result<Self, String> {
let sni = Sni {
capture_label: capture_label(hotkey),
recording: false,
callback,
progress: None,
color: super::color(TrayIconStyle::default()),
Expand All @@ -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) {}
Expand Down
16 changes: 15 additions & 1 deletion src/tray/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -124,6 +129,7 @@ impl Target {
pub struct Tray {
status_item: Retained<NSStatusItem>,
capture_item: Retained<NSMenuItem>,
record_item: Retained<NSMenuItem>,
menu: Retained<NSMenu>,
_target: Retained<Target>,
}
Expand All @@ -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 {
Expand Down Expand Up @@ -190,6 +197,7 @@ impl Tray {
let tray = Self {
status_item,
capture_item,
record_item,
menu,
_target: target,
};
Expand All @@ -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) {}

Expand Down
11 changes: 11 additions & 0 deletions src/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
26 changes: 22 additions & 4 deletions src/tray/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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),
Expand All @@ -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.
Expand Down Expand Up @@ -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(),
Expand All @@ -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() {
Expand Down Expand Up @@ -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.
Expand All @@ -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(),
Expand All @@ -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() {
Expand Down
Loading