Skip to content

Commit

Permalink
Add auto launch option
Browse files Browse the repository at this point in the history
  • Loading branch information
makindotcc committed Mar 23, 2024
1 parent 5e83751 commit 48d0920
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ futures = "0.3.30"
notify-rust = "4.10.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.113"
auto-launch = "0.5"

[target.'cfg(target_os = "linux")'.dependencies]
gtk = "0.18"
Expand Down
16 changes: 15 additions & 1 deletion desktop/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct UiConfigState {
mixer_instance: String,
redirect_policy: HashMap<RuleName, RedirectWashPolicy>,
enable_clipboard_patcher: bool,
auto_start: bool,
}

fn apply_ui_config(app_config: &mut AppConfig, ui_config: &UiConfigState) {
Expand All @@ -46,10 +47,15 @@ impl ConfigWindow {
.as_ref()
.map(|url| url.to_string())
.unwrap_or_default();
let auto_start = app_state
.auto_launch
.is_enabled()
.expect("Could not check if autostart is enabled");
let ui_config_state = UiConfigState {
mixer_instance,
redirect_policy: config.url_washer.redirect_policy.clone(),
enable_clipboard_patcher: true,
enable_clipboard_patcher: config.enable_clipboard_patcher,
auto_start,
};
drop(app_state);
Self {
Expand All @@ -71,6 +77,14 @@ impl eframe::App for ConfigWindow {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Desktop settings");
ui.checkbox(&mut self.ui_config_state.enable_clipboard_patcher, "Automatically debloat URLs in your clipboard");
if ui.checkbox(&mut self.ui_config_state.auto_start, "Start debloater with system startup").clicked() {
let auto_launch = &self.app_state_flow.current().auto_launch;
if self.ui_config_state.auto_start {
auto_launch.enable().expect("Could not enable auto start");
} else {
auto_launch.disable().expect("Could not disable auto start");
}
}

ui.separator();
{
Expand Down
53 changes: 36 additions & 17 deletions desktop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use crate::{
gui::{ConfigWindow, TrayMenu},
};
use anyhow::Context;
use auto_launch::AutoLaunch;
use config::AppConfig;
use eframe::{egui, DetachedResult};
use futures::{stream::FuturesUnordered, StreamExt};
use notify_rust::Notification;
use std::env;
use std::{
io::{self, ErrorKind},
sync::Arc,
Expand All @@ -37,21 +39,19 @@ const CLIPBOARD_PAUSE_DURATION: Duration = Duration::from_secs(30);
pub struct AppState {
text_washer: TextWasher,
config: AppConfig,
auto_launch: AutoLaunch,
}

impl AppState {
pub fn new(config: AppConfig) -> Self {
pub fn new(config: AppConfig, auto_launch: AutoLaunch) -> Self {
Self {
text_washer: TextWasher {
url_washer: UrlWasher::new(config.url_washer.clone()),
},
config,
auto_launch,
}
}

pub fn with_config(&self, config: AppConfig) -> Self {
AppState::new(config)
}
}

#[derive(Clone)]
Expand All @@ -74,9 +74,15 @@ impl AppStateFlow {
}

pub fn modify_config(&self, apply_changes: impl FnOnce(&mut AppConfig)) {
let mut new_config = self.current().config.clone();
let (auto_launch, config) = {
let current = self.current();
(current.auto_launch.clone(), current.config.clone())
};
let mut new_config = config.clone();
apply_changes(&mut new_config);
let _ = self.tx.send(Arc::new(AppState::new(new_config)));
let _ = self
.tx
.send(Arc::new(AppState::new(new_config, auto_launch)));
}
}

Expand All @@ -90,16 +96,29 @@ async fn main() -> anyhow::Result<()> {
.init();
debug!("Hello, world!");

let config = config::from_file().await.unwrap_or_else(|err| {
if !err
.downcast_ref::<io::Error>()
.is_some_and(|err| err.kind() == ErrorKind::NotFound)
{
error!("Could not read config file: {err:?}. Using default...");
}
AppConfig::default()
});
let app_state = AppState::new(config);
let (first_launch, config) = config::from_file()
.await
.map(|config| (false, config))
.unwrap_or_else(|err| {
let config_not_found = err
.downcast_ref::<io::Error>()
.is_some_and(|err| err.kind() == ErrorKind::NotFound);
if !config_not_found {
error!("Could not read config file: {err:?}. Using default...");
}
(config_not_found, AppConfig::default())
});
let auto_launch = {
let app_path = env::current_exe().expect("Could not get current exe path");
let app_path = app_path.to_str().expect("Invalid current exe path");
AutoLaunch::new(APP_NAME, app_path, &[] as &[&str])
};
if first_launch {
auto_launch
.enable()
.expect("Could not enable auto launch on initial debloater startup");
}
let app_state = AppState::new(config, auto_launch);
let app_state_flow = AppStateFlow::new(app_state);
tokio::spawn(persist_config(app_state_flow.rx.clone()));
tokio::spawn(run_background_jobs_supervisor(app_state_flow.rx.clone()));
Expand Down

0 comments on commit 48d0920

Please sign in to comment.