start on login

This commit is contained in:
Joseph Montanaro
2023-04-27 14:24:08 -07:00
parent ebc00a5df6
commit 741169d807
9 changed files with 101 additions and 27 deletions

View File

@ -1,6 +1,7 @@
use std::net::Ipv4Addr;
use std::path::PathBuf;
use auto_launch::AutoLaunchBuilder;
use serde::{Serialize, Deserialize};
use sqlx::SqlitePool;
@ -17,6 +18,8 @@ pub struct AppConfig {
pub rehide_ms: u64,
#[serde(default = "default_start_minimized")]
pub start_minimized: bool,
#[serde(default = "default_start_on_login")]
pub start_on_login: bool,
}
@ -27,6 +30,7 @@ impl Default for AppConfig {
listen_port: default_listen_port(),
rehide_ms: default_rehide_ms(),
start_minimized: default_start_minimized(),
start_on_login: default_start_on_login(),
}
}
}
@ -62,6 +66,28 @@ impl AppConfig {
}
pub fn set_auto_launch(enable: bool) -> Result<(), SetupError> {
let path_buf = std::env::current_exe()
.map_err(|e| auto_launch::Error::Io(e))?;
let path = path_buf
.to_string_lossy();
let auto = AutoLaunchBuilder::new()
.set_app_name("Creddy")
.set_app_path(&path)
.build()?;
if enable {
auto.enable()?;
}
else {
auto.disable()?;
}
Ok(())
}
pub fn get_or_create_db_path() -> PathBuf {
if cfg!(debug_assertions) {
return PathBuf::from("./creddy.db");
@ -90,7 +116,7 @@ fn default_listen_port() -> u16 {
}
fn default_listen_addr() -> Ipv4Addr { Ipv4Addr::LOCALHOST }
fn default_rehide_ms() -> u64 { 1000 }
fn default_start_minimized() -> bool { !cfg!(debug_assertions) } // default to start-minimized in production only
// start minimized and on login only in production mode
fn default_start_minimized() -> bool { !cfg!(debug_assertions) }
fn default_start_on_login() -> bool { !cfg!(debug_assertions) }