feat(tray): background processes and close/open menu

This commit is contained in:
DecDuck
2024-12-21 15:09:49 +11:00
parent d9a51cf187
commit 3d60fd50d8
5 changed files with 114 additions and 27 deletions

View File

@ -7,6 +7,8 @@ use crate::DB;
use core::time;
use log::{debug, error, info};
use rayon::ThreadPoolBuilder;
use serde::ser::{Error, SerializeMap};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::fs::{create_dir_all, File};
use std::io;
@ -92,6 +94,30 @@ impl GameDownloadAgent {
}
}
pub fn from_contexts(
id: String,
version: String,
base_dir: String,
manifest: DropManifest,
contexts: Vec<DropDownloadContext>,
sender: Sender<DownloadManagerSignal>,
) -> Self {
let control_flag = DownloadThreadControl::new(DownloadThreadControlFlag::Stop);
let me = Self {
id,
version,
control_flag,
manifest: Mutex::new(Some(manifest)),
base_dir,
contexts: Mutex::new(contexts),
progress: Arc::new(ProgressObject::new(0, 0, sender.clone())),
sender,
};
me.set_progress_object_params();
me
}
// Blocking
pub fn setup_download(&self) -> Result<(), GameDownloadError> {
self.ensure_manifest_exists()?;
@ -307,3 +333,25 @@ impl GameDownloadAgent {
Ok(())
}
}
#[derive(Serialize, Deserialize)]
pub struct GameDownloadAgentOfflineState {
id: String,
version: String,
base_dir: String,
manifest: DropManifest,
contexts: Vec<DropDownloadContext>,
}
impl GameDownloadAgentOfflineState {
fn to_download_agent(self, sender: Sender<DownloadManagerSignal>) -> GameDownloadAgent {
GameDownloadAgent::from_contexts(
self.id,
self.version,
self.base_dir,
self.manifest,
self.contexts,
sender,
)
}
}

View File

@ -30,21 +30,22 @@ pub enum DownloadManagerSignal {
/// to the registry and queue
Queue(String, String, usize),
/// Tells the Manager to stop the current
/// download and return
/// download, sync everything to disk, and
/// then exit
Finish,
Cancel,
/// Any error which occurs in the agent
Error(GameDownloadError),
/// Pushes UI update
Update,
/// Causes the Download Agent status to be synced to disk
Sync(usize),
}
pub enum DownloadManagerStatus {
Downloading,
Paused,
Empty,
Error(GameDownloadError),
Finished,
}
#[derive(Serialize, Clone)]

View File

@ -141,6 +141,19 @@ impl DownloadManagerBuilder {
self.app_handle.emit("update_queue", event_data).unwrap();
}
fn stop_and_wait_current_download(&self) {
self.set_status(DownloadManagerStatus::Paused);
if let Some(current_flag) = &self.active_control_flag {
current_flag.set(DownloadThreadControlFlag::Stop);
}
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
if let Some(current_download_thread) = download_thread_lock.take() {
current_download_thread.join().unwrap();
}
drop(download_thread_lock);
}
fn sync_download_agent(&self) {}
fn remove_and_cleanup_game(&mut self, game_id: &String) -> Arc<GameDownloadAgent> {
@ -182,12 +195,6 @@ impl DownloadManagerBuilder {
DownloadManagerSignal::Queue(game_id, version, target_download_dir) => {
self.manage_queue_signal(game_id, version, target_download_dir);
}
DownloadManagerSignal::Finish => {
if let Some(active_control_flag) = self.active_control_flag {
active_control_flag.set(DownloadThreadControlFlag::Stop)
}
return Ok(());
}
DownloadManagerSignal::Error(e) => {
self.manage_error_signal(e);
}
@ -197,8 +204,9 @@ impl DownloadManagerBuilder {
DownloadManagerSignal::Update => {
self.push_manager_update();
}
DownloadManagerSignal::Sync(index) => {
self.sync_download_agent();
DownloadManagerSignal::Finish => {
self.stop_and_wait_current_download();
return Ok(());
}
};
}
@ -345,16 +353,7 @@ impl DownloadManagerBuilder {
self.sender.send(DownloadManagerSignal::Update).unwrap();
}
fn manage_cancel_signal(&mut self) {
self.set_status(DownloadManagerStatus::Paused);
if let Some(current_flag) = &self.active_control_flag {
current_flag.set(DownloadThreadControlFlag::Stop);
}
let mut download_thread_lock = self.current_download_thread.lock().unwrap();
if let Some(current_download_thread) = download_thread_lock.take() {
current_download_thread.join().unwrap();
}
drop(download_thread_lock);
self.stop_and_wait_current_download();
info!("cancel waited for download to finish");