feat: implement native system tray, background notifications, and download resumability via tauri-plugin-store

This commit is contained in:
NimBold
2026-06-16 11:38:02 +03:30
parent b6678493d3
commit 275eb6e5c4
25 changed files with 336 additions and 322 deletions
+17 -82
View File
@@ -1185,18 +1185,6 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "fallible-iterator"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "fastrand"
version = "2.4.1"
@@ -1242,7 +1230,6 @@ dependencies = [
"objc",
"regex 1.12.4",
"reqwest 0.12.28",
"rusqlite",
"semver",
"serde",
"serde_json",
@@ -1258,6 +1245,7 @@ dependencies = [
"tauri-plugin-opener",
"tauri-plugin-shell",
"tauri-plugin-single-instance",
"tauri-plugin-store",
"tempfile",
"thiserror 2.0.18",
"tokio",
@@ -1779,32 +1767,11 @@ dependencies = [
"foldhash 0.1.5",
]
[[package]]
name = "hashbrown"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
dependencies = [
"foldhash 0.2.0",
]
[[package]]
name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
dependencies = [
"foldhash 0.2.0",
]
[[package]]
name = "hashlink"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5081f264ed7adee96ea4b4778b6bb9da0a7228b084587aa3bd3ff05da7c5a3b"
dependencies = [
"hashbrown 0.17.1",
]
[[package]]
name = "heck"
@@ -2425,17 +2392,6 @@ dependencies = [
"libc",
]
[[package]]
name = "libsqlite3-sys"
version = "0.38.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6c19a05435c21ac299d71b6a9c13db3e3f47c520517d58990a462a1397a61db"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
@@ -3638,31 +3594,6 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rsqlite-vfs"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c"
dependencies = [
"hashbrown 0.16.1",
"thiserror 2.0.18",
]
[[package]]
name = "rusqlite"
version = "0.40.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11438310b19e3109b6446c33d1ed5e889428cf2e278407bc7896bc4aaea43323"
dependencies = [
"bitflags 2.13.0",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
"sqlite-wasm-rs",
]
[[package]]
name = "rust-ini"
version = "0.21.3"
@@ -4206,18 +4137,6 @@ dependencies = [
"system-deps",
]
[[package]]
name = "sqlite-wasm-rs"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75"
dependencies = [
"cc",
"js-sys",
"rsqlite-vfs",
"wasm-bindgen",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
@@ -4711,6 +4630,22 @@ dependencies = [
"zbus",
]
[[package]]
name = "tauri-plugin-store"
version = "2.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c72dda16786eb4a3f903e43a17b64d8d78dc0f00fe2aa4b757c28f617a8630b"
dependencies = [
"dunce",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"thiserror 2.0.18",
"tokio",
"tracing",
]
[[package]]
name = "tauri-runtime"
version = "2.11.2"
+1 -1
View File
@@ -51,9 +51,9 @@ keepawake = "0.6.0"
system_shutdown = "4.1.0"
tokio-tungstenite = "0.29.0"
futures-util = { version = "0.3.32", features = ["sink"] }
rusqlite = { version = "0.40.1", features = ["bundled"] }
chrono = "0.4.38"
url = "2"
tauri-plugin-store = "2.4.3"
[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.25"
objc = "0.2.7"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-112
View File
@@ -1,112 +0,0 @@
use rusqlite::{Connection, Result, params};
use tokio::sync::Mutex;
use tauri::Manager;
pub struct DbState {
pub conn: Mutex<Connection>,
}
pub fn init_db(app_handle: &tauri::AppHandle) -> Result<Connection> {
let app_dir = app_handle.path().app_data_dir().expect("Cannot get app data dir");
if !app_dir.exists() {
let _ = std::fs::create_dir_all(&app_dir);
}
let db_path = app_dir.join("firelink.sqlite");
let conn = Connection::open(db_path)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS downloads (
id TEXT PRIMARY KEY,
status TEXT NOT NULL,
queue_id TEXT NOT NULL,
data TEXT NOT NULL
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY CHECK (id = 1),
data TEXT NOT NULL
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS queues (
id TEXT PRIMARY KEY,
data TEXT NOT NULL
)",
[],
)?;
Ok(conn)
}
// Downloads CRUD
pub fn insert_download(conn: &Connection, id: &str, status: &str, queue_id: &str, data: &str) -> Result<()> {
conn.execute(
"INSERT OR REPLACE INTO downloads (id, status, queue_id, data) VALUES (?1, ?2, ?3, ?4)",
params![id, status, queue_id, data],
)?;
Ok(())
}
pub fn delete_download(conn: &Connection, id: &str) -> Result<()> {
conn.execute("DELETE FROM downloads WHERE id = ?1", params![id])?;
Ok(())
}
pub fn get_all_downloads(conn: &Connection) -> Result<Vec<String>> {
let mut stmt = conn.prepare("SELECT data FROM downloads")?;
let iter = stmt.query_map([], |row| row.get(0))?;
let mut res = Vec::new();
for data in iter {
res.push(data?);
}
Ok(res)
}
// Settings CRUD
pub fn get_settings(conn: &Connection) -> Result<Option<String>> {
let mut stmt = conn.prepare("SELECT data FROM settings WHERE id = 1")?;
let mut iter = stmt.query_map([], |row| row.get(0))?;
if let Some(row) = iter.next() {
Ok(Some(row?))
} else {
Ok(None)
}
}
pub fn save_settings(conn: &Connection, data: &str) -> Result<()> {
conn.execute(
"INSERT OR REPLACE INTO settings (id, data) VALUES (1, ?1)",
params![data],
)?;
Ok(())
}
// Queues CRUD
pub fn insert_queue(conn: &Connection, id: &str, data: &str) -> Result<()> {
conn.execute(
"INSERT OR REPLACE INTO queues (id, data) VALUES (?1, ?2)",
params![id, data],
)?;
Ok(())
}
pub fn delete_queue(conn: &Connection, id: &str) -> Result<()> {
conn.execute("DELETE FROM queues WHERE id = ?1", params![id])?;
Ok(())
}
pub fn get_all_queues(conn: &Connection) -> Result<Vec<String>> {
let mut stmt = conn.prepare("SELECT data FROM queues")?;
let iter = stmt.query_map([], |row| row.get(0))?;
let mut res = Vec::new();
for data in iter {
res.push(data?);
}
Ok(res)
}
+55 -61
View File
@@ -601,6 +601,8 @@ async fn start_download(
options.insert("max-connection-per-server".to_string(), serde_json::json!(conn.to_string()));
options.insert("max-tries".to_string(), serde_json::json!(mt.to_string()));
options.insert("continue".to_string(), serde_json::json!("true"));
if let Some(speed) = &speed_limit {
options.insert("max-download-limit".to_string(), serde_json::json!(speed));
}
@@ -780,9 +782,10 @@ pub(crate) async fn start_media_download_internal(
.arg("--retries").arg("3")
.arg("--extractor-retries").arg("3")
.arg("--downloader").arg("aria2c")
.arg("--downloader-args").arg("aria2c:-x 16 -s 16 -k 1M")
.arg("--downloader-args").arg("aria2c:-c -x 16 -s 16 -k 1M")
.arg("--concurrent-fragments").arg("4")
.arg("--no-warnings")
.arg("--continue")
.arg("--compat-options").arg("no-youtube-unavailable-videos")
.arg("-o").arg(out_path.to_string_lossy().to_string());
@@ -941,6 +944,8 @@ pub(crate) async fn start_media_download_internal(
println!("child exit status: {:?}", payload.code);
if payload.code == Some(0) {
let _ = app_handle.emit("download-complete", id.to_string());
use tauri_plugin_notification::NotificationExt;
let _ = app_handle.notification().builder().title("Download Complete").body(&safe_filename).show();
} else {
let _ = app_handle.emit("download-failed", id.to_string());
}
@@ -1362,6 +1367,49 @@ pub fn run() {
.plugin(tauri_plugin_deep_link::init())
.manage(Aria2DaemonGuard(std::sync::Mutex::new(None)))
.setup(move |app| {
use tauri::tray::{TrayIconBuilder, MouseButton, MouseButtonState, TrayIconEvent};
use tauri::menu::{Menu, MenuItem};
let show_i = MenuItem::with_id(app, "show", "Show Firelink", true, None::<&str>).unwrap();
let pause_all_i = MenuItem::with_id(app, "pause_all", "Pause All", true, None::<&str>).unwrap();
let resume_all_i = MenuItem::with_id(app, "resume_all", "Resume All", true, None::<&str>).unwrap();
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap();
let menu = Menu::with_items(app, &[&show_i, &pause_all_i, &resume_all_i, &quit_i]).unwrap();
let tray_icon = tauri::image::Image::from_bytes(include_bytes!("../icons/trayTemplate.png")).unwrap();
let _tray = TrayIconBuilder::with_id("main_startup")
.icon(tray_icon)
.icon_as_template(true)
.menu(&menu)
.show_menu_on_left_click(false)
.on_menu_event(|app, event| match event.id.as_ref() {
"show" => { restore_main_window(app); }
"pause_all" => {
use tauri::Emitter;
let _ = app.emit("tray-action", "pause-all");
}
"resume_all" => {
use tauri::Emitter;
let _ = app.emit("tray-action", "resume-all");
}
"quit" => { app.exit(0); }
_ => {}
})
.on_tray_icon_event(|tray, event| {
if matches!(
event,
TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
}
) {
restore_main_window(tray.app_handle());
}
})
.build(app)
.unwrap();
let aria2_gids = Arc::new(RwLock::new(std::collections::HashMap::new()));
let aria2_gids_clone1 = aria2_gids.clone();
let aria2_gids_clone2 = aria2_gids.clone();
@@ -1385,9 +1433,6 @@ pub fn run() {
Ok(None) => {}
Err(error) => eprintln!("Failed to read startup deep link: {error}"),
}
let db_conn = crate::db::init_db(app.handle()).expect("Failed to init db");
app.manage(crate::db::DbState { conn: tokio::sync::Mutex::new(db_conn) });
crate::scheduler::spawn_scheduler(app.handle().clone());
use tauri_plugin_shell::ShellExt;
@@ -1446,7 +1491,9 @@ pub fn run() {
use tauri::Emitter;
match method {
"aria2.onDownloadComplete" => {
let _ = app_handle_ws.emit("download-complete", id);
let _ = app_handle_ws.emit("download-complete", id.clone());
use tauri_plugin_notification::NotificationExt;
let _ = app_handle_ws.notification().builder().title("Download Complete").body(&format!("File downloaded successfully")).show();
}
"aria2.onDownloadError" => {
let _ = app_handle_ws.emit("download-failed", id);
@@ -1529,6 +1576,7 @@ pub fn run() {
});
Ok(())
})
.plugin(tauri_plugin_store::Builder::new().build())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_opener::init())
@@ -1536,10 +1584,8 @@ pub fn run() {
.on_window_event(|window, event| {
if window.label() == "main" {
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
if window.app_handle().tray_by_id("main").is_some() {
api.prevent_close();
let _ = window.hide();
}
api.prevent_close();
let _ = window.hide();
}
}
})
@@ -1552,62 +1598,10 @@ pub fn run() {
check_file_exists, delete_file, toggle_tray_icon, set_extension_pairing_token,
set_extension_frontend_ready, set_concurrent_limit, set_global_speed_limit, remove_download,
parity::get_system_proxy, parity::get_file_category, parity::check_for_updates, parity::is_supported_media, parity::get_supported_media_domains,
db_save_settings, db_load_settings, db_get_all_downloads, db_save_download, db_delete_download, db_get_all_queues, db_save_queue, db_delete_queue,
parity::create_category_directories
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
mod extension_server;
mod db;
mod scheduler;
#[tauri::command]
async fn db_save_settings(state: tauri::State<'_, crate::db::DbState>, data: String) -> Result<(), String> {
let conn = state.conn.lock().await;
crate::db::save_settings(&conn, &data).map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_load_settings(state: tauri::State<'_, crate::db::DbState>) -> Result<Option<String>, String> {
let conn = state.conn.lock().await;
crate::db::get_settings(&conn).map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_get_all_downloads(state: tauri::State<'_, crate::db::DbState>) -> Result<Vec<String>, String> {
let conn = state.conn.lock().await;
crate::db::get_all_downloads(&conn).map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_save_download(state: tauri::State<'_, crate::db::DbState>, id: String, status: crate::ipc::DownloadStatus, queue_id: String, data: String) -> Result<(), String> {
let conn = state.conn.lock().await;
crate::db::insert_download(&conn, &id, status.as_str(), &queue_id, &data)
.map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_delete_download(state: tauri::State<'_, crate::db::DbState>, id: String) -> Result<(), String> {
let conn = state.conn.lock().await;
crate::db::delete_download(&conn, &id).map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_get_all_queues(state: tauri::State<'_, crate::db::DbState>) -> Result<Vec<String>, String> {
let conn = state.conn.lock().await;
crate::db::get_all_queues(&conn).map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_save_queue(state: tauri::State<'_, crate::db::DbState>, id: String, data: String) -> Result<(), String> {
let conn = state.conn.lock().await;
crate::db::insert_queue(&conn, &id, &data).map_err(|e| e.to_string())
}
#[tauri::command]
async fn db_delete_queue(state: tauri::State<'_, crate::db::DbState>, id: String) -> Result<(), String> {
let conn = state.conn.lock().await;
crate::db::delete_queue(&conn, &id).map_err(|e| e.to_string())
}
+17 -9
View File
@@ -11,9 +11,13 @@ pub fn spawn_scheduler(app_handle: tauri::AppHandle) {
interval.tick().await;
let settings_opt = {
let state = app_handle.state::<crate::db::DbState>();
let conn = state.conn.lock().await;
crate::db::get_settings(&conn).unwrap_or(None)
use tauri_plugin_store::StoreExt;
let store = app_handle.store("store.bin");
if let Ok(store) = store {
store.get("settings").and_then(|v| v.as_str().map(|s| s.to_string()))
} else {
None
}
};
if let Some(settings_str) = settings_opt {
@@ -45,9 +49,11 @@ pub fn spawn_scheduler(app_handle: tauri::AppHandle) {
let _ = app_handle.emit("schedule-trigger", "start");
if let Ok(updated) = serde_json::to_string(&settings) {
let state = app_handle.state::<crate::db::DbState>();
let conn = state.conn.lock().await;
let _ = crate::db::save_settings(&conn, &updated);
use tauri_plugin_store::StoreExt;
if let Ok(store) = app_handle.store("store.bin") {
store.set("settings", serde_json::json!(updated));
let _ = store.save();
}
}
}
@@ -58,9 +64,11 @@ pub fn spawn_scheduler(app_handle: tauri::AppHandle) {
let _ = app_handle.emit("schedule-trigger", "stop");
if let Ok(updated) = serde_json::to_string(&settings) {
let state = app_handle.state::<crate::db::DbState>();
let conn = state.conn.lock().await;
let _ = crate::db::save_settings(&conn, &updated);
use tauri_plugin_store::StoreExt;
if let Ok(store) = app_handle.store("store.bin") {
store.set("settings", serde_json::json!(updated));
let _ = store.save();
}
}
}
}