Skip to content

Commit

Permalink
feat: add run-on-startup functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
iholston committed Oct 18, 2024
1 parent dde1e93 commit 4502263
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 6 deletions.
11 changes: 11 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ powershell_script = "1.1.0"
serde = { version = "1.0.205", features = ["derive"] }
strum = "0.26.3"
strum_macros = "0.26.4"
winreg = "0.52.0"


[build-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
LoL-Accept is a Windows tray application that Auto Accepts your League of Legends games ☕ </br>
- Doesn't use your 🖱️ or ⌨️
- Extremely lightweight so you can leave it running in the background
- Works even if League is minimized or in the background </br>
- Works even if League is minimized or in the background
- Can be toggled to run on startup</br>

## Getting Started
- Download the [latest release](https://github.com/iholston/lol-play/releases)
- Download the [latest release](https://github.com/iholston/lol-accept/releases)
- Double click on the .exe
- An icon will appear in the tray, right click to pause or exit 👍

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod cmd;
mod lcu;
mod tray;
mod reg;

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down
68 changes: 68 additions & 0 deletions src/reg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::env;
use std::io;
use std::path::PathBuf;
use winreg::enums::*;
use winreg::RegKey;

const APP_NAME: &str = "LoL-Accept";
const RUN_REGISTRY_PATH: &str = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";

fn get_executable_path() -> io::Result<String> {
let path: PathBuf = env::current_exe()?;
path.into_os_string()
.into_string()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Failed to convert path to string"))
}

pub fn add_to_startup() -> io::Result<()> {
let app_path = get_executable_path()?;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let (run_key, _) = hkcu.create_subkey(RUN_REGISTRY_PATH)?;
run_key.set_value(APP_NAME, &app_path)?;
Ok(())
}

pub fn remove_from_startup() -> io::Result<()> {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let run_key = hkcu.open_subkey_with_flags(RUN_REGISTRY_PATH, KEY_WRITE)?;
match run_key.delete_value(APP_NAME) {
Ok(_) => Ok(()),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
Ok(())
},
Err(e) => Err(e),
}
}

pub fn is_in_startup() -> io::Result<bool> {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
match hkcu.open_subkey_with_flags(RUN_REGISTRY_PATH, KEY_READ) {
Ok(run_key) => {
match run_key.get_value::<String, _>(APP_NAME) {
Ok(_) => Ok(true),
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
},
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
Err(e) => Err(e),
}
}

pub fn cleanup_stale_registry() -> io::Result<()> {
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)?;
}
},
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
// No entry exists, nothing to clean
},
Err(e) => return Err(e),
}
Ok(())
}
40 changes: 36 additions & 4 deletions src/tray.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tray_icon::{Icon, TrayIcon, TrayIconBuilder};
use tray_icon::menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem};
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 @@ -23,21 +24,39 @@ pub struct TrayApp {
icon: TrayIcon,
menu_start: MenuItem,
menu_pause: MenuItem,
submenu_startup: Submenu,
submenu_yes: MenuItem,
submenu_no: MenuItem,
menu_quit: MenuItem,
}

impl TrayApp {
pub fn new() -> Self {
let _ = reg::cleanup_stale_registry();
let in_startup = reg::is_in_startup().unwrap_or(false);

let tray_menu = Menu::new();
let menu_start = MenuItem::new("Start", false, None);
let menu_pause = MenuItem::new("Pause", true, None);
let submenu_startup = Submenu::new("Run on Startup", true);
let submenu_yes = MenuItem::new("Yes", !in_startup, None);
let submenu_no = MenuItem::new("No", in_startup, None);
let menu_quit = MenuItem::new("Quit", true, None);
let tray_menu = Menu::new();

submenu_startup
.append_items(&[
&submenu_yes,
&submenu_no,
])
.unwrap();

tray_menu
.append_items(&[
&menu_start,
&PredefinedMenuItem::separator(),
&menu_pause,
&PredefinedMenuItem::separator(),
&submenu_startup,
&PredefinedMenuItem::separator(),
&menu_quit,
])
.unwrap();
Expand All @@ -53,6 +72,9 @@ impl TrayApp {
icon,
menu_start,
menu_pause,
submenu_startup,
submenu_yes,
submenu_no,
menu_quit,
}
}
Expand All @@ -73,7 +95,17 @@ impl TrayApp {
pause.store(true, Ordering::SeqCst);
self.menu_start.set_enabled(true);
self.menu_pause.set_enabled(false);
} else if event.id() == self.menu_quit.id() {
} else if event.id() == self.submenu_yes.id() {
self.submenu_yes.set_enabled(false);
self.submenu_no.set_enabled(true);
let _ = reg::cleanup_stale_registry();
let _ = reg::add_to_startup();
} else if event.id() == self.submenu_no.id() {
self.submenu_no.set_enabled(false);
self.submenu_yes.set_enabled(true);
let _ = reg::cleanup_stale_registry();
let _ = reg::remove_from_startup();
}else if event.id() == self.menu_quit.id() {
terminate.store(true, Ordering::SeqCst);
event_loop.exit();
}
Expand Down

0 comments on commit 4502263

Please sign in to comment.