Files
cstb-next/src-tauri/src/main.rs

146 lines
5.0 KiB
Rust
Raw Normal View History

2024-09-20 02:28:25 +08:00
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
2024-09-20 10:14:36 +08:00
use tauri::Manager;
2025-03-23 23:25:28 +08:00
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_cli::CliExt;
2025-11-04 23:33:36 +08:00
use tauri_plugin_deep_link::DeepLinkExt;
use tauri_plugin_store::StoreExt;
2025-03-23 23:25:28 +08:00
2024-09-20 09:52:13 +08:00
// Window Vibrancy
#[cfg(target_os = "windows")]
use window_vibrancy::apply_acrylic;
2024-09-20 09:52:13 +08:00
#[cfg(target_os = "macos")]
use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial};
2024-09-20 02:28:25 +08:00
use std::time::{SystemTime, UNIX_EPOCH};
2024-09-20 10:14:36 +08:00
// System Tray
#[cfg(desktop)]
mod tray;
2024-09-20 09:52:13 +08:00
// Local Modules
mod cmds;
mod steam;
mod tool;
mod vdf;
2024-09-20 09:52:13 +08:00
2024-09-20 02:28:25 +08:00
#[tauri::command]
fn on_button_clicked() -> String {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_millis();
format!("on_button_clicked called from Rust! (timestamp: {since_the_epoch}ms)")
}
fn main() {
tauri::Builder::default()
2025-03-19 22:03:32 +08:00
.plugin(tauri_plugin_single_instance::init(|app, _, _| {
let window = app
2025-03-19 22:03:32 +08:00
.get_webview_window("main")
.expect("no main window");
window.show().expect("no main window, can't show");
window.set_focus().expect("no main window, can't set focus")
2025-03-19 22:03:32 +08:00
}))
.plugin(tauri_plugin_deep_link::init())
.plugin(tauri_plugin_valtio::init())
2024-09-26 16:58:40 +08:00
.plugin(tauri_plugin_store::Builder::new().build())
2024-09-20 10:14:36 +08:00
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
Some(vec!["hidden"]), /* arbitrary number of args to pass to your app */
))
2024-09-20 10:14:36 +08:00
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_system_info::init())
.plugin(tauri_plugin_cli::init())
2024-09-20 10:14:36 +08:00
// .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();
2024-09-20 09:52:13 +08:00
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);
2024-09-20 10:14:36 +08:00
// 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");
2024-09-20 09:52:13 +08:00
2024-09-20 10:14:36 +08:00
#[cfg(target_os = "windows")]
apply_acrylic(&window, None)
.expect("Unsupported platform! 'apply_acrylic' is only supported on Windows");
2024-09-20 09:52:13 +08:00
2024-09-20 10:14:36 +08:00
// apply_blur(&window, Some((18, 18, 18, 0)))
// .expect("Unsupported platform! 'apply_blur' is only supported on Windows");
// Deep Link
2025-11-05 11:21:13 +08:00
#[cfg(desktop)]
app.deep_link().register("cstb")?;
// Tray
#[cfg(all(desktop))]
{
let handle = app.handle();
tray::create_tray(handle)?;
}
// 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(_) => {}
}
2024-09-20 10:14:36 +08:00
Ok(())
})
.invoke_handler(tauri::generate_handler![
cmds::greet,
cmds::launch_game,
cmds::kill_game,
2025-11-05 11:21:13 +08:00
cmds::check_process_running,
2024-09-20 10:14:36 +08:00
cmds::kill_steam,
cmds::get_steam_path,
cmds::get_cs_path,
cmds::open_path,
cmds::get_powerplan,
cmds::set_powerplan,
cmds::get_steam_users,
2024-09-20 10:14:36 +08:00
cmds::set_auto_login_user,
2025-03-27 13:32:30 +08:00
cmds::get_cs2_video_config,
cmds::set_cs2_video_config,
cmds::check_path,
cmds::analyze_replay,
2025-11-05 02:24:17 +08:00
cmds::get_console_log_path,
cmds::read_vprof_report,
2025-11-05 11:21:13 +08:00
cmds::check_app_update,
cmds::download_app_update,
cmds::install_app_update,
2024-09-20 10:14:36 +08:00
on_button_clicked
])
2025-10-28 00:45:54 +08:00
.run(tauri::generate_context!())
2024-09-20 10:14:36 +08:00
.expect("error while running tauri application");
2024-09-20 02:28:25 +08:00
}