Files
leaguerecorder/app/src/os.rs
T
vhaudiquet 47510c88b5 app: drop the old Tauri front end
tauri-app/ was kept while the native app caught up, and the screens it had
are covered now. Its 48 tracked files go; the 7.6 GB of node_modules,
dist and target beside them were never tracked and are gone from the
working copy too.

The docs that said it was still in the tree name two programs now, and
the comments that pointed at a file inside it name the old app's file
instead, so nothing refers to a folder that is no longer there. The design
plans under plans/ keep their wording: they are dated documents, not
instructions.
2026-11-24 00:18:31 +01:00

171 lines
5.7 KiB
Rust

//! Windows integration that the old Tauri app got from plugins: launch at login, one instance,
//! toast notifications and the tray icon.
/// The app's reverse-DNS id, used for single-instance and notifications.
pub const APP_ID: &str = "v.leaguerecorder";
pub const APP_NAME: &str = "League Recorder";
#[cfg(windows)]
const RUN_KEY: &str = r"Software\Microsoft\Windows\CurrentVersion\Run";
#[cfg(windows)]
const RUN_VALUE: &str = "LeagueRecorder";
/// Launch at login writes the executable into the user's `Run` key.
#[cfg(windows)]
pub fn autostart_enabled() -> bool {
use winreg::RegKey;
use winreg::enums::HKEY_CURRENT_USER;
let Ok(key) = RegKey::predef(HKEY_CURRENT_USER).open_subkey(RUN_KEY) else {
return false;
};
key.get_value::<String, _>(RUN_VALUE).is_ok()
}
#[cfg(not(windows))]
pub fn autostart_enabled() -> bool {
false
}
#[cfg(windows)]
pub fn set_autostart(on: bool) -> Result<(), String> {
use winreg::RegKey;
use winreg::enums::HKEY_CURRENT_USER;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let (key, _) = hkcu
.create_subkey(RUN_KEY)
.map_err(|e| format!("cannot open the Run key: {e}"))?;
if on {
let exe =
std::env::current_exe().map_err(|e| format!("cannot find the executable: {e}"))?;
let value = format!("\"{}\" --hidden", exe.display());
key.set_value(RUN_VALUE, &value)
.map_err(|e| format!("cannot write the Run key: {e}"))
} else {
match key.delete_value(RUN_VALUE) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(format!("cannot remove the Run value: {e}")),
}
}
}
#[cfg(not(windows))]
pub fn set_autostart(_on: bool) -> Result<(), String> {
Err("launch at login is implemented for Windows only".into())
}
/// Register the app so Windows can show its toasts, and group its windows on the taskbar.
pub fn register_notifications() {
let identity = rune_notify::Identity::new(APP_ID, APP_NAME);
if let Err(e) = rune_notify::register(&identity) {
eprintln!("notifications unavailable: {e}");
}
}
/// Post a toast, unless a window already has the user's attention.
pub fn notify(id: &str, title: &str, body: &str) {
if rune_notify::app_focused() {
return;
}
let notification = rune_notify::Notification::new(id, title).body(body);
rune_notify::post(&notification);
}
/// The app's icon for the title bar, the taskbar and Alt+Tab: the same medallion the exe
/// carries and the installer shows. Call once at startup, before the event loop; rune sizes
/// it for each use and a headless run sets nothing.
pub fn set_app_icon() {
let bytes = include_bytes!("../icons/icon_full.png").as_slice();
if let Err(e) = rune::set_app_icon(bytes) {
eprintln!("app icon unavailable: {e}");
}
}
/// A 32 px tray icon: the app's medallion, or a plain red disc while the daemon records.
pub fn tray_icon(recording: bool) -> rune::TrayIcon {
let (width, height, pixels) = if recording {
recording_pixels()
} else {
medallion_pixels()
};
rune::TrayIcon::rgba(width, height, pixels)
}
/// The medallion at its bundled 32 px: straight RGBA rows for [`rune::TrayIcon`].
fn medallion_pixels() -> (u32, u32, Vec<u8>) {
let image = image::load_from_memory(include_bytes!("../icons/32x32.png"))
.expect("the bundled tray icon decodes")
.into_rgba8();
(image.width(), image.height(), image.into_raw())
}
/// A red disc on clear pixels. The edge gets one pixel of coverage, so a tray that scales the
/// icon down to 16 px does not show a jagged ring.
fn recording_pixels() -> (u32, u32, Vec<u8>) {
const SIZE: u32 = 32;
const RADIUS: f32 = 11.0;
const RED: [u8; 3] = [220, 38, 38];
let center = SIZE as f32 / 2.0;
let mut pixels = Vec::with_capacity((SIZE * SIZE * 4) as usize);
for y in 0..SIZE {
for x in 0..SIZE {
let dx = x as f32 + 0.5 - center;
let dy = y as f32 + 0.5 - center;
let distance = (dx * dx + dy * dy).sqrt();
let alpha = ((RADIUS + 0.5 - distance).clamp(0.0, 1.0) * 255.0).round() as u8;
pixels.extend_from_slice(&[RED[0], RED[1], RED[2], alpha]);
}
}
(SIZE, SIZE, pixels)
}
/// The tray menu. `recording` flips the record item's label.
pub fn tray_menu(recording: bool) -> Vec<rune::TrayItem> {
vec![
rune::TrayItem::action("show", "Open League Recorder"),
rune::TrayItem::action("hide", "Hide window"),
rune::TrayItem::Separator,
rune::TrayItem::action(
"record",
if recording {
"Stop recording"
} else {
"Start recording"
},
),
rune::TrayItem::Separator,
rune::TrayItem::action("quit", "Quit"),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_tray_icon_decodes_to_32_px_of_rgba() {
let (width, height, pixels) = medallion_pixels();
assert_eq!((width, height), (32, 32));
assert_eq!(pixels.len(), (32 * 32 * 4) as usize);
}
#[test]
fn the_recording_icon_is_a_red_disc_on_a_clear_square() {
let (width, height, pixels) = recording_pixels();
assert_eq!((width, height), (32, 32));
let at = |x: usize, y: usize| {
let i = (y * 32 + x) * 4;
[pixels[i], pixels[i + 1], pixels[i + 2], pixels[i + 3]]
};
assert_eq!(at(16, 16), [220, 38, 38, 255]);
assert_eq!(at(0, 0)[3], 0);
assert_eq!(at(31, 16)[3], 0);
assert!(
pixels
.chunks_exact(4)
.filter(|p| p[3] == 255)
.all(|p| p[..3] == [220, 38, 38])
);
}
}