Skip to content

Commit

Permalink
finish update
Browse files Browse the repository at this point in the history
  • Loading branch information
jqntn committed Feb 8, 2024
1 parent f98afa3 commit f29c783
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ panic = "abort"
native-windows-gui = "1.0.13"
native-windows-derive = "1.0.5"
single-instance = "0.3.3"
self_update = "0.39.0"
auto-launch = "0.5.0"
winapi = "0.3.9"
winreg = "0.52.0"
self_update = { version = "0.39.0", features = [
"archive-zip",
"compression-zip-deflate",
] }

[build-dependencies]
winapi = { version = "0.3.9", features = ["wincontypes"] }
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #![windows_subsystem = "windows"]
#![windows_subsystem = "windows"]

extern crate auto_launch as al;
extern crate native_windows_derive as nwd;
Expand Down
49 changes: 23 additions & 26 deletions src/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl SystemTray {
let system_tray: system_tray_ui::SystemTrayUi =
SystemTray::build_ui(SystemTray::default()).expect("Failed to build UI");

system_tray.first_time_init();
system_tray.auto_update();
Self::first_time_init();
Self::auto_update();

nwg::dispatch_thread_events();

Expand All @@ -66,21 +66,21 @@ impl SystemTray {
}

fn show_window(&self) {
if self.set_foreground_window() {
if Self::set_foreground_window() {
return;
}

let basic_app = BasicApp::build();

unsafe { *WINDOW_HANDLE.lock().unwrap() = Some(basic_app.window().handle.hwnd().unwrap()) };

self.set_foreground_window();
Self::set_foreground_window();

nwg::dispatch_thread_events();
}

fn toggle_startup(&self) {
let Ok(auto_launch) = self.get_auto_launch() else {
let Ok(auto_launch) = Self::get_auto_launch() else {
return;
};

Expand All @@ -92,18 +92,18 @@ impl SystemTray {
}

fn toggle_update(&self) {
let Ok((key, _)) = self.get_reg_key() else {
let Ok((key, _)) = Self::get_reg_key() else {
return;
};

let _: Result<(), std::io::Error> =
key.set_value("AutoUpdate", &(!self.tray_item_update.checked() as u32));

self.auto_update();
Self::auto_update();
}

fn refresh_startup(&self) {
let Ok(auto_launch) = self.get_auto_launch() else {
let Ok(auto_launch) = Self::get_auto_launch() else {
return;
};

Expand All @@ -113,7 +113,7 @@ impl SystemTray {
}

fn refresh_update(&self) {
let Ok((key, _)) = self.get_reg_key() else {
let Ok((key, _)) = Self::get_reg_key() else {
return;
};

Expand All @@ -122,7 +122,7 @@ impl SystemTray {
}
}

fn get_auto_launch(&self) -> Result<al::AutoLaunch, ()> {
fn get_auto_launch() -> Result<al::AutoLaunch, ()> {
let Ok(exe_path) = std::env::current_exe() else {
return Err(());
};
Expand All @@ -134,23 +134,21 @@ impl SystemTray {
.unwrap())
}

fn get_reg_key(
&self,
) -> Result<(winreg::RegKey, winreg::enums::RegDisposition), std::io::Error> {
fn get_reg_key() -> Result<(winreg::RegKey, winreg::enums::RegDisposition), std::io::Error> {
winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER)
.create_subkey(std::path::Path::new("SOFTWARE").join(APP_NAME))
}

fn set_foreground_window(&self) -> bool {
fn set_foreground_window() -> bool {
if let Some(hwnd) = unsafe { *WINDOW_HANDLE.lock().unwrap() } {
unsafe { winapi::um::winuser::SetForegroundWindow(hwnd) };
return true;
}
false
}

fn first_time_init(&self) {
let Ok((key, _)) = self.get_reg_key() else {
fn first_time_init() {
let Ok((key, _)) = Self::get_reg_key() else {
return;
};

Expand All @@ -172,15 +170,15 @@ impl SystemTray {
return;
}

if let Ok(auto_launch) = self.get_auto_launch() {
if let Ok(auto_launch) = Self::get_auto_launch() {
let _: Result<(), al::Error> = auto_launch.enable();
}

let _: Result<(), std::io::Error> = key.set_value("AutoUpdate", &(true as u32));
}

fn auto_update(&self) {
let Ok((key, _)) = self.get_reg_key() else {
fn auto_update() {
let Ok((key, _)) = Self::get_reg_key() else {
return;
};

Expand All @@ -189,22 +187,21 @@ impl SystemTray {
};

if auto_update > 0 {
let _: Result<(), Box<su::errors::Error>> = self.update();
let _: Result<(), Box<su::errors::Error>> = Self::update();
}
}

fn update(&self) -> Result<(), Box<su::errors::Error>> {
let status: su::Status = su::backends::github::Update::configure()
fn update() -> Result<(), Box<su::errors::Error>> {
su::backends::github::Update::configure()
.repo_owner("jqntn")
.repo_name(APP_NAME)
.bin_name(APP_NAME)
.show_download_progress(true)
.target("zip")
.current_version(su::cargo_crate_version!())
.show_download_progress(true)
.no_confirm(true)
.build()?
.update()?;

println!("Update status: `{}`!", status.version());

Ok(())
}

Expand Down

0 comments on commit f29c783

Please sign in to comment.