Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: Build binary
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
Expand Down
67 changes: 67 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(release, windows_subsystem = "windows")]

Check warning on line 1 in src/main.rs

View workflow job for this annotation

GitHub Actions / Build binary

unexpected `cfg` condition name: `release`

use core::mem::MaybeUninit;
use std::{
Expand Down Expand Up @@ -38,6 +38,7 @@
IconCat,
IconParrot,
RunTaskmgr,
ToggleRunOnStart,
}

pub fn wchar(string: &str) -> Vec<u16> {
Expand All @@ -52,6 +53,7 @@
.map(icons::load_icons)
.collect::<Vec<_>>();
fn build_menu(icon_id: usize) -> MenuBuilder<Events> {
let run_on_start_enabled = is_run_on_start_enabled(); // Call internally
MenuBuilder::new()
.submenu(
"&Theme",
Expand All @@ -66,6 +68,8 @@
.checkable("&Parrot", !is_cat(icon_id), Events::IconParrot),
)
.separator()
.checkable(".&Run on Start", run_on_start_enabled, Events::ToggleRunOnStart) // Use internal state
.separator()
.item("E&xit", Events::Exit)
}

Expand Down Expand Up @@ -136,6 +140,16 @@
Events::ThemeLight => update_icon(icon_id.load(Ordering::Relaxed) | 1),
Events::IconCat => update_icon(icon_id.load(Ordering::Relaxed) & 1),
Events::IconParrot => update_icon(icon_id.load(Ordering::Relaxed) | 2),
Events::ToggleRunOnStart => {
let current_state = is_run_on_start_enabled();
set_run_on_start(!current_state);
// new_run_on_start_state variable removed
tray_icon
.lock()
.unwrap()
.set_menu(&build_menu(icon_id.load(Ordering::Relaxed)))
.expect("set_menu for ToggleRunOnStart");
}
}
}
});
Expand Down Expand Up @@ -200,6 +214,59 @@
}
}

fn is_run_on_start_enabled() -> bool {
use winreg::enums::*;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
if let Ok(run_key) = hkcu.open_subkey_with_flags("Software\\Microsoft\\Windows\\CurrentVersion\\Run", KEY_READ) {
// Attempt to get the value. The type of the value doesn't matter as much as its existence.
// We expect it to be a String (REG_SZ) if it exists.
if run_key.get_value::<String, _>("RustCat").is_ok() {
// Optionally, you could check if the value (path) is not empty,
// but for simplicity, existence is enough.
return true;
}
}
false
}

fn set_run_on_start(enable: bool) {
use std::env;
use winreg::enums::*;
use winreg::RegKey;

const RUN_KEY_PATH: &str = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
const VALUE_NAME: &str = "RustCat";

let hkcu = RegKey::predef(HKEY_CURRENT_USER);

match hkcu.open_subkey_with_flags(RUN_KEY_PATH, KEY_WRITE | KEY_READ) {
Ok(run_key) => {
if enable {
match env::current_exe() {
Ok(exe_path) => {
let exe_path_str = exe_path.to_string_lossy().to_string();
if let Err(e) = run_key.set_value(VALUE_NAME, &exe_path_str) {
eprintln!("Failed to set registry value '{}': {}", VALUE_NAME, e);
}
}
Err(e) => {
eprintln!("Failed to get current executable path: {}", e);
}
}
} else {
if let Err(e) = run_key.delete_value(VALUE_NAME) {

Check warning on line 257 in src/main.rs

View workflow job for this annotation

GitHub Actions / Build binary

unused variable: `e`
// It's okay if the value doesn't exist when trying to delete.
// You might want to log this for debugging if it's unexpected.
// eprintln!("Failed to delete registry value '{}' (this may be okay if it didn't exist): {}", VALUE_NAME, e);
}
}
}
Err(e) => {
eprintln!("Failed to open or create registry subkey '{}': {}", RUN_KEY_PATH, e);
}
}
}

fn is_dark(id: usize) -> bool {
id & 1 == 0
}
Expand Down
Loading