pub fn notify_completion(summary: &str) {
let result = notify_rust::Notification::new()
.summary("CleanSys")
.body(summary)
.appname("CleanSys")
.show();
if let Err(e) = result {
log::debug!("desktop notification failed (non-fatal): {e}");
}
}
pub fn relaunch_as_admin() {
#[cfg(target_os = "windows")]
{
if let Err(e) = windows_impl::relaunch_elevated() {
log::warn!("Failed to relaunch as Administrator: {e}");
}
}
#[cfg(not(target_os = "windows"))]
{
log::debug!("relaunch_as_admin() is a no-op on this platform");
}
}
#[cfg(target_os = "windows")]
mod windows_impl {
use anyhow::{Context, Result};
use std::os::windows::ffi::OsStrExt;
use windows::core::PCWSTR;
use windows::Win32::UI::Shell::ShellExecuteW;
use windows::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
fn to_wide(s: &std::ffi::OsStr) -> Vec<u16> {
s.encode_wide().chain(std::iter::once(0)).collect()
}
pub fn relaunch_elevated() -> Result<()> {
let exe = std::env::current_exe().context("failed to determine current executable")?;
let exe_wide = to_wide(exe.as_os_str());
let verb_wide = to_wide(std::ffi::OsStr::new("runas"));
let result = unsafe {
ShellExecuteW(
None,
PCWSTR(verb_wide.as_ptr()),
PCWSTR(exe_wide.as_ptr()),
PCWSTR::null(),
PCWSTR::null(),
SW_SHOWNORMAL,
)
};
if (result.0 as isize) <= 32 {
anyhow::bail!("ShellExecuteW returned error code {}", result.0 as isize);
}
std::process::exit(0);
}
}