Medlar/crates/app/src/shell.rs

117 lines
4 KiB
Rust

//! What the window shares with the desktop: tray, notifications, launcher progress and autostart.
//! One `Shell` per run; it outlives the window, so that closing to the tray works where windows cannot hide (Wayland).
//! Each part is left out when the desktop has no host for it.
use crate::launcher::Launcher;
use crate::notify::{Notifier, Watcher};
use crate::tray::{Tray, TrayState};
use eframe::egui::Context;
use medlar_core::model::TaskId;
use medlar_core::settings::{Mode, Settings};
use medlar_core::snapshot::Snapshot;
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, Sender, channel};
/// What the tray or a notification asks the window to do.
#[derive(Debug, Clone, PartialEq)]
pub enum Do {
Show,
Add,
PauseAll,
ResumeAll,
Mode(Mode),
Quit,
Open(PathBuf),
Folder(PathBuf),
SignIn(String, TaskId),
Retry(TaskId),
}
pub struct Shell {
pub tray: Option<Tray>,
notes: Option<Notifier>,
launcher: Option<Launcher>,
watcher: Watcher,
rx: Receiver<Do>,
shown: Option<TrayState>,
rev: u64,
/// What autostart was last made to match.
autostart: (bool, bool),
/// Set by `Do::Quit`, so that closing really closes.
pub quitting: bool,
}
impl Shell {
pub fn start(ctx: &Context, settings: &Settings) -> Shell {
let (tx, rx): (Sender<Do>, Receiver<Do>) = channel();
let tray = settings.tray.then(|| Tray::start(tx.clone(), ctx.clone())).flatten();
let notes = zbus::blocking::Connection::session().ok().map(|c| Notifier::start(c, tx, ctx.clone()));
Shell {
tray,
notes,
launcher: Launcher::start(),
watcher: Watcher::default(),
rx,
shown: None,
rev: u64::MAX,
autostart: (settings.autostart, settings.start_minimised),
quitting: false,
}
}
/// Whether closing the window leaves Medlar running in the tray instead of quitting.
pub fn hides_on_close(&self, settings: &Settings) -> bool {
hides(self.quitting, settings.close_to_tray, self.tray.is_some())
}
/// Once per frame: what was asked comes back, what changed goes out.
pub fn tick(&mut self, settings: &Settings, shot: Option<&Snapshot>) -> Vec<Do> {
let want = (settings.autostart, settings.start_minimised);
if want != self.autostart {
self.autostart = want;
if let Err(e) = crate::autostart::sync(dirs::config_dir(), want.0, want.1) {
log::warn!("autostart: {e}");
}
}
if let Some(shot) = shot {
let fresh = shot.rev != self.rev;
self.rev = shot.rev;
if fresh {
let events = self.watcher.feed(shot);
if let Some(notes) = self.notes.as_ref().filter(|_| settings.notify) {
notes.send(&events, settings);
}
}
let state = TrayState::of(shot, settings.mode);
if self.shown.as_ref() != Some(&state) {
if let Some(tray) = &self.tray {
tray.set(state.clone());
}
if let Some(launcher) = &self.launcher {
launcher.set(state.progress, state.running);
}
self.shown = Some(state);
}
}
self.rx.try_iter().collect()
}
}
/// Closing hides to the tray only when the user wants it, a tray host shows the icon, and no quit was asked for.
pub fn hides(quitting: bool, close_to_tray: bool, host: bool) -> bool {
!quitting && close_to_tray && host
}
#[cfg(test)]
mod tests {
use super::hides;
#[test]
fn close_hides_only_with_tray_on_a_host_and_no_quit() {
assert!(hides(false, true, true));
assert!(!hides(false, false, true), "close to tray off quits");
assert!(!hides(false, true, false), "no tray host quits");
assert!(!hides(true, true, true), "Quit and Ctrl+Q quit");
assert!(!hides(true, false, false));
}
}