use tray::{Tray, TrayEvent, WatchItem};
use super::*;
impl App {
pub(crate) fn sync_tray(&mut self, ctx: &egui::Context) {
if !self.settings.tray {
self.tray = None;
} else if self.tray.is_none() && !self.tray_failed {
self.tray = Tray::new(self.tray_events.0.clone(), ctx.clone());
self.tray_failed = self.tray.is_none();
}
if self.hidden && self.tray.is_none() {
self.show_window(ctx);
}
let events: Vec<_> = self.tray_events.1.try_iter().collect();
for event in events {
match event {
TrayEvent::Open => self.show_window(ctx),
TrayEvent::TogglePause => self.watch_paused = !self.watch_paused,
TrayEvent::Screenshot => self.screenshot_from_tray(ctx),
TrayEvent::RegionScreenshot => self.region_from_tray(ctx),
TrayEvent::Quit => self.quit = true,
}
}
if self.hidden && self.asking.is_some() {
self.show_window(ctx);
}
if let Some(tray) = &self.tray {
tray.update(&self.tray_tip(), self.watch_item());
}
}
fn tray_tip(&self) -> String {
if self.running() {
let current = (self.batch.done + 1).min(self.batch.total);
return format!("hdrshot: converting {current} of {}", self.batch.total);
}
match (&self.settings.watch_dir, self.settings.watch) {
(Some(_), true) if self.watch_paused => "hdrshot: watching paused".to_owned(),
(Some(dir), true) => format!("hdrshot: watching {}", folder_name(dir)),
_ => "hdrshot".to_owned(),
}
}
fn watch_item(&self) -> WatchItem {
if !self.settings.watch || self.settings.watch_dir.is_none() {
WatchItem::None
} else if self.watch_paused {
WatchItem::Resume
} else {
WatchItem::Pause
}
}
pub(crate) fn show_window(&mut self, ctx: &egui::Context) {
self.hidden = false;
ctx.send_viewport_cmd(egui::ViewportCommand::Visible(true));
ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(false));
ctx.send_viewport_cmd(egui::ViewportCommand::Focus);
}
pub(crate) fn hidden_in_tray(&mut self) {
self.hidden = true;
self.viewer = None;
if self.settings.tray_note_shown {
return;
}
let Some(tray) = &self.tray else { return };
let what = match (&self.settings.watch_dir, self.settings.watch) {
(Some(dir), true) => format!(
"It keeps converting new images saved in {}. ",
folder_name(dir)
),
_ => String::new(),
};
tray.notify(
"hdrshot is still running",
&format!(
"{what}Click its icon in the notification area to open it again, or \
right-click the icon to quit."
),
);
self.settings.tray_note_shown = true;
}
pub(crate) fn notify_batch(&self, ctx: &egui::Context) {
let Some(tray) = &self.tray else { return };
let background = self.hidden || !ctx.input(|i| i.focused);
if !background || !self.settings.notify || self.batch.outcome() == Outcome::Nothing {
return;
}
let name = self
.last_converted
.as_ref()
.and_then(|p| p.file_name())
.filter(|_| self.batch.total == 1 && self.batch.written() == 1);
let title = match name {
Some(name) => format!("Converted {}", name.to_string_lossy()),
None => self.batch.title(),
};
tray.notify(&title, &self.batch.details().replace(" ยท ", "\n"));
}
pub(crate) fn apply_start_with_windows(&mut self, ctx: &egui::Context) -> bool {
let on = self.settings.start_with_windows;
match startup::set(on) {
Ok(()) => true,
Err(e) => {
self.settings.start_with_windows = !on;
self.show_toast(e, ctx);
false
}
}
}
pub(crate) fn background_rows(&mut self, ui: &mut egui::Ui) {
let ctx = ui.ctx().clone();
let s = &mut self.settings;
theme::row_label(ui, "Tray");
if ui
.checkbox(
&mut s.tray,
"Keep running in the tray when the window is closed",
)
.on_hover_text(
"Closing the window leaves hdrshot in the notification area (the \
system tray), where it keeps watching the watch folder. Click its \
icon to open the window again; right-click it to pause watching or \
quit.",
)
.changed()
&& !s.tray
&& s.start_with_windows
{
s.start_with_windows = false;
if let Err(e) = startup::set(false) {
self.show_toast(e, &ctx);
}
}
ui.end_row();
let s = &mut self.settings;
let tray = s.tray;
if startup::SUPPORTED {
theme::row_label(ui, "Startup");
let response = ui
.add_enabled(
tray,
egui::Checkbox::new(
&mut s.start_with_windows,
"Start with Windows, in the tray",
),
)
.on_hover_text(
"Start hdrshot in the tray when you sign in to Windows, so new \
screenshots are converted without opening it first",
)
.on_disabled_hover_text("Turn on Keep running in the tray first");
if response.changed() {
self.apply_start_with_windows(&ctx);
}
ui.end_row();
}
let s = &mut self.settings;
theme::row_label(ui, "Notifications");
ui.add_enabled(
tray,
egui::Checkbox::new(
&mut s.notify,
"Notify when images are converted in the background",
),
)
.on_hover_text(
"When images are converted while the window is closed or behind \
other windows, say so with a notification. Click it to open hdrshot.",
)
.on_disabled_hover_text("Turn on Keep running in the tray first");
ui.end_row();
}
}