Skip to content

Commit

Permalink
feat: enhance registry key handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iholston committed Oct 18, 2024
1 parent 4502263 commit 01c4643
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,21 @@ pub fn is_in_startup() -> io::Result<bool> {
}
}

pub fn cleanup_stale_registry() -> io::Result<()> {
pub fn cleanup_stale_registry() -> io::Result<(bool)> {
let current_path = get_executable_path()?;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let run_key = hkcu.open_subkey_with_flags(RUN_REGISTRY_PATH, KEY_READ | KEY_WRITE)?;
match run_key.get_value::<String, _>(APP_NAME) {
Ok(stored_path) => {
if stored_path != current_path {
run_key.delete_value(APP_NAME)?;
return Ok(true);
}
},
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
// No entry exists, nothing to clean
},
Err(e) => return Err(e),
}
Ok(())
Ok(false)
}
9 changes: 7 additions & 2 deletions src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use tray_icon::{Icon, TrayIcon, TrayIconBuilder};
use tray_icon::menu::{Menu, MenuEvent, MenuItem, Submenu, PredefinedMenuItem};
use winit::event_loop::{ControlFlow, EventLoopBuilder};

use crate::reg;

const ICON_BYTES: &'static [u8] = include_bytes!("../assets/icon.ico");
Expand All @@ -19,8 +20,8 @@ fn load_icon() -> Icon {
Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}

#[allow(dead_code)]
pub struct TrayApp {
#[allow(dead_code)]
icon: TrayIcon,
menu_start: MenuItem,
menu_pause: MenuItem,
Expand All @@ -32,8 +33,12 @@ pub struct TrayApp {

impl TrayApp {
pub fn new() -> Self {
let _ = reg::cleanup_stale_registry();
// Checks registry and updates potentially stale path
let in_startup = reg::is_in_startup().unwrap_or(false);
let removed_stale_key = reg::cleanup_stale_registry().unwrap_or(false);
if in_startup && removed_stale_key {
let _ = reg::add_to_startup();
}

let tray_menu = Menu::new();
let menu_start = MenuItem::new("Start", false, None);
Expand Down

0 comments on commit 01c4643

Please sign in to comment.