[feat] start hidden + hidden on close + basic tray function

This commit is contained in:
Purp1e
2025-03-26 03:00:18 +08:00
parent e0a84a0570
commit ee03bf0160
20 changed files with 266 additions and 40 deletions

View File

@@ -54,7 +54,7 @@ pub fn get_powerplan() -> Result<i32, String> {
#[cfg(target_os = "windows")]
let powerplan = powerplan::get_powerplan()?;
#[cfg(not(target_os = "windows"))]
#[cfg(not(target_os = "windows"))]
let powerplan = powerplan::PowerPlanMode::Other.into();
Ok(powerplan)
@@ -70,7 +70,7 @@ pub fn set_powerplan(plan: i32) -> Result<(), String> {
#[tauri::command]
pub fn get_steam_users(steam_dir: &str) -> Result<Vec<preset::User>, String> {
wrap_err!(preset::get_users(steam_dir))
wrap_err!(preset::get_users(steam_dir))
}
#[tauri::command]

View File

@@ -4,12 +4,14 @@
)]
use tauri::Manager;
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_cli::CliExt;
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_store::StoreExt;
// Window Vibrancy
#[cfg(target_os = "windows")]
use window_vibrancy::apply_mica;
use window_vibrancy::apply_acrylic;
#[cfg(target_os = "macos")]
use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial};
@@ -50,7 +52,10 @@ fn main() {
.plugin(tauri_plugin_store::Builder::new().build())
.plugin(tauri_plugin_theme::init(ctx.config_mut()))
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec![]) /* arbitrary number of args to pass to your app */))
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
Some(vec!["hidden"]), /* arbitrary number of args to pass to your app */
))
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
@@ -60,9 +65,29 @@ fn main() {
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_system_info::init())
.plugin(tauri_plugin_cli::init())
// .plugin(tauri_plugin_store::Builder::default().build())
// .plugin(tauri_plugin_updater::Builder::new().build())
.setup(|app| {
// Get Window
let window = app.get_webview_window("main").unwrap();
let store = app.store("cstb.json")?;
// 获取boolean类型的hidden值Err时设置为False
let hidden: bool = store.get("hidden").and_then(|v| v.as_bool()).unwrap_or(false);
// Vibrant Window
#[cfg(target_os = "macos")]
apply_vibrancy(&window, NSVisualEffectMaterial::HudWindow, None, Some(10.0))
.expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");
#[cfg(target_os = "windows")]
apply_acrylic(&window, None)
.expect("Unsupported platform! 'apply_acrylic' is only supported on Windows");
// apply_blur(&window, Some((18, 18, 18, 0)))
// .expect("Unsupported platform! 'apply_blur' is only supported on Windows");
// Deep Link
#[cfg(desktop)]
app.deep_link().register("cstb")?;
@@ -74,20 +99,22 @@ fn main() {
tray::create_tray(handle)?;
}
// Get Window
let window = app.get_webview_window("main").unwrap();
// CLI
match app.cli().matches() {
// `matches` here is a Struct with { args, subcommand }.
// `args` is `HashMap<String, ArgData>` where `ArgData` is a struct with { value, occurrences }.
// `subcommand` is `Option<Box<SubcommandMatches>>` where `SubcommandMatches` is a struct with { name, matches }.
Ok(matches) => {
println!("{:?}", matches);
if matches.args.contains_key("hidden") && matches.args["hidden"].value == true && hidden {
window.hide().unwrap();
} else {
window.show().unwrap();
}
}
Err(_) => {}
}
// Vibrant Window
#[cfg(target_os = "macos")]
apply_vibrancy(&window, NSVisualEffectMaterial::HudWindow, None, Some(10.0))
.expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");
#[cfg(target_os = "windows")]
apply_mica(&window, Some(false))
.expect("Unsupported platform! 'apply_mica' is only supported on Windows");
// apply_blur(&window, Some((18, 18, 18, 0)))
// .expect("Unsupported platform! 'apply_blur' is only supported on Windows");
Ok(())
})
.invoke_handler(tauri::generate_handler![
@@ -100,7 +127,7 @@ fn main() {
cmds::open_path,
cmds::get_powerplan,
cmds::set_powerplan,
cmds::get_steam_users,
cmds::get_steam_users,
cmds::set_auto_login_user,
cmds::check_path,
on_button_clicked

View File

@@ -21,7 +21,6 @@ pub fn run_steam() -> std::io::Result<std::process::Output> {
.output()
}
pub fn get_exe_path(name: &str) -> Result<String, std::io::Error> {
// [原理]
// Powershell 运行 Get-Process name | Select-Object path

View File

@@ -42,4 +42,4 @@ macro_rules! wrap_err {
}
}
};
}
}

View File

@@ -1,3 +1,3 @@
pub mod common;
pub mod macros;
pub mod powerplan;
pub mod powerplan;

View File

@@ -5,8 +5,12 @@ use tauri::{
};
pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
let menu = Menu::with_items(app, &[&quit_i])?;
// 托盘菜单项目
let show_i = MenuItem::with_id(app, "show", "显示主界面", true, None::<&str>)?;
let quit_i = MenuItem::with_id(app, "quit", "退出", true, None::<&str>)?;
// 创建托盘菜单
let menu = Menu::with_items(app, &[&show_i, &quit_i])?;
let _ = TrayIconBuilder::with_id("tray")
.icon(app.default_window_icon().unwrap().clone())
@@ -16,6 +20,12 @@ pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
"quit" => {
app.exit(0);
}
"show" => {
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
}
// Add more events here
_ => {}
})

View File

@@ -1,2 +1,2 @@
pub mod parse;
pub mod preset;
pub mod preset;