//! What the window shares with the desktop: tray, notifications, launcher progress and autostart. //! One `Shell` per window; 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, notes: Option, launcher: Option, watcher: Watcher, rx: Receiver, shown: Option, 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, Receiver) = 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 hides it instead. pub fn hides_on_close(&self, settings: &Settings) -> bool { !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 { 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() } }