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

143 lines
4.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::Manager;
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_acrylic;
#[cfg(target_os = "macos")]
use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial};
use std::time::{SystemTime, UNIX_EPOCH};
// System Tray
#[cfg(desktop)]
mod tray;
// Local Modules
mod cmds;
mod steam;
mod tool;
mod vdf;
#[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() {
let mut ctx = tauri::generate_context!();
tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, _, _| {
let window = app
.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")
}))
.plugin(tauri_plugin_deep_link::init())
.plugin(tauri_plugin_valtio::init())
.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!["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())
.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())
// .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")?;
// 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(_) => {}
}
Ok(())
})
.invoke_handler(tauri::generate_handler![
cmds::greet,
cmds::launch_game,
cmds::kill_game,
cmds::kill_steam,
cmds::get_steam_path,
cmds::get_cs_path,
cmds::open_path,
cmds::get_powerplan,
cmds::set_powerplan,
cmds::get_steam_users,
cmds::set_auto_login_user,
cmds::get_cs2_video_config,
cmds::set_cs2_video_config,
cmds::check_path,
cmds::analyze_replay,
on_button_clicked
])
.run(ctx)
.expect("error while running tauri application");
}