The exe carried no icon resource and the tray drew a stand-in disc, so Explorer, the taskbar and the tray each guessed at what to show. The old front end's medallion was already in the tree and identical to the installer's icon.ico, so the whole shell now draws it: set_app_icon feeds icons/icon_full.png to rune before the event loop, the tray decodes the bundled 32 px, and build.rs bakes icon.ico into the exe for Explorer and shortcuts. The 128 px variants stay in tauri-app, which rune's own resizing makes redundant here.
128 lines
4.2 KiB
Rust
128 lines
4.2 KiB
Rust
//! Windows integration that the 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(¬ification);
|
|
}
|
|
|
|
/// 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, decoded to the straight RGBA rows the tray takes.
|
|
pub fn tray_icon() -> rune::TrayIcon {
|
|
let (width, height, pixels) = tray_icon_pixels();
|
|
rune::TrayIcon::rgba(width, height, pixels)
|
|
}
|
|
|
|
/// The medallion at its bundled 32 px: straight RGBA rows for [`rune::TrayIcon`].
|
|
fn tray_icon_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())
|
|
}
|
|
|
|
/// 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) = tray_icon_pixels();
|
|
assert_eq!((width, height), (32, 32));
|
|
assert_eq!(pixels.len(), (32 * 32 * 4) as usize);
|
|
}
|
|
}
|