migrate: v2 basically working

This commit is contained in:
Purp1e
2024-09-20 10:14:36 +08:00
parent 7229e79cd4
commit b12c6279d5
18 changed files with 16950 additions and 1259 deletions

View File

@@ -53,5 +53,5 @@ pub fn set_auto_login_user(user: &str) -> String {
#[tauri::command]
pub fn check_file_exists(file: &str) -> bool {
std::path::Path::new(&file).exists()
std::path::Path::new(&file).exists()
}

View File

@@ -2,26 +2,20 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use tauri::Manager;
use tauri::Manager;
// Window Vibrancy
#[cfg(target_os = "windows")]
use window_vibrancy::apply_mica;
#[cfg(target_os = "macos")]
use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial};
// System Tray
use tauri::{
menu::{MenuBuilder, MenuItemBuilder},
tray::{ClickType, TrayIconBuilder},
};
// Auto Start
use tauri_plugin_autostart::MacosLauncher;
// use tauri_plugin_autostart::ManagerExt;
use std::time::{SystemTime, UNIX_EPOCH};
// System Tray
#[cfg(desktop)]
mod tray;
// Local Modules
mod cmds;
mod steam;
@@ -39,109 +33,53 @@ fn on_button_clicked() -> String {
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
Some(vec![]),
))
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_store::Builder::default().build())
// .plugin(tauri_plugin_updater::Builder::new().build())
.invoke_handler(tauri::generate_handler![on_button_clicked])
.setup(|app| {
// Tray Menu
// let quit = CustomMenuItem::new("quit".to_string(), "Quit");
// let hide = CustomMenuItem::new("hide".to_string(), "Hide");
// let tray_menu = SystemTrayMenu::new() // insert the menu items here
// .add_item(hide)
// .add_item(quit);
// .add_native_item(SystemTrayMenuItem::Separator)
let toggle = MenuItemBuilder::with_id("toggle", "Toggle").build(app)?;
let menu = MenuBuilder::new(app).items(&[&toggle]).build()?;
.plugin(tauri_plugin_notification::init())
.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_store::Builder::default().build())
// .plugin(tauri_plugin_updater::Builder::new().build())
.invoke_handler(tauri::generate_handler![on_button_clicked])
.setup(|app| {
// Tray
#[cfg(all(desktop))]
{
let handle = app.handle();
tray::create_tray(handle)?;
}
// Setup Tray
// let tray = tauri::tray::TrayIconBuilder::with_id("my-tray").build(app)?;
let _ = TrayIconBuilder::new()
.menu(&menu)
.on_menu_event(move |_, event| {
match event.id().as_ref() {
"toggle" => {
println!("toggle clicked");
}
_ => (),
}
// match event {
// SystemTrayEvent::LeftClick { position: _, size: _, .. } => {
// let window = app.get_window("main").unwrap();
// window.show().unwrap();
// window.set_focus().unwrap();
// Get Window
let window = app.get_webview_window("main").unwrap();
// // thread::sleep(Duration::from_millis(100));
// // window.set_always_on_top(false).unwrap();
// println!("system tray received a left click");
// }
// SystemTrayEvent::RightClick { position: _, size: _, .. } => {
// // let window = app.get_window("main").unwrap();
// // window.hide().unwrap();
// println!("system tray received a right click");
// }
// SystemTrayEvent::DoubleClick { position: _, size: _, .. } => {
// println!("system tray received a double click");
// }
// SystemTrayEvent::MenuItemClick { id, .. } =>
// match id.as_str() {
// "quit" => {
// std::process::exit(0);
// }
// "hide" => {
// let window = app.get_window("main").unwrap();
// window.hide().unwrap();
// }
// _ => {}
// }
// _ => {}
// }
})
.on_tray_icon_event(|tray, event| {
if event.click_type == ClickType::Left {
let app = tray.app_handle();
if let Some(webview_window) = app.get_webview_window("main") {
let _ = webview_window.show();
let _ = webview_window.set_focus();
}
}
})
.build(app)
.unwrap();
// 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");
// Get Window
let window = app.get_webview_window("main").unwrap();
#[cfg(target_os = "windows")]
apply_mica(&window, Some(false))
.expect("Unsupported platform! 'apply_mica' is only supported on Windows");
// 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");
// apply_blur(&window, Some((18, 18, 18, 0)))
// .expect("Unsupported platform! 'apply_blur' is only supported on Windows");
#[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![
cmds::greet,
cmds::launch_game,
cmds::kill_game,
cmds::kill_steam,
cmds::get_steam_path,
cmds::set_auto_login_user,
cmds::check_file_exists
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Ok(())
})
.invoke_handler(tauri::generate_handler![
cmds::greet,
cmds::launch_game,
cmds::kill_game,
cmds::kill_steam,
cmds::get_steam_path,
cmds::set_auto_login_user,
cmds::check_file_exists,
on_button_clicked
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@@ -24,16 +24,16 @@ pub fn get_steam_path<'a>() -> Result<String, &'a str> {
}
pub fn get_cs_path<'a>(name: &str) -> Result<String, &'a str> {
if name != "csgo" || name != "cs2" {
return Err("invalid cs name");
}
if name != "csgo" || name != "cs2" {
return Err("invalid cs name");
}
#[cfg(target_os = "windows")]
if let Ok(cs_path) = get_exe_path(&(name.to_owned() + ".exe")) {
return Ok(cs_path);
}
#[cfg(target_os = "windows")]
if let Ok(cs_path) = get_exe_path(&(name.to_owned() + ".exe")) {
return Ok(cs_path);
}
Err("no cs path found")
Err("no cs path found")
}
#[cfg(test)]

104
src-tauri/src/tray.rs Normal file
View File

@@ -0,0 +1,104 @@
use tauri::{
menu::{Menu, MenuItem},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
Manager, Runtime,
};
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 _ = TrayIconBuilder::with_id("tray")
.icon(app.default_window_icon().unwrap().clone())
.menu(&menu)
.menu_on_left_click(false)
.on_menu_event(move |app, event| match event.id.as_ref() {
"quit" => {
app.exit(0);
}
// Add more events here
_ => {}
})
.on_tray_icon_event(|tray, event| {
if let TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} = event
{
let app = tray.app_handle();
if let Some(window) = app.get_webview_window("main") {
let _ = window.show();
let _ = window.set_focus();
}
}
})
.build(app);
Ok(())
}
// Tray Menu
// let quit = CustomMenuItem::new("quit".to_string(), "Quit");
// let hide = CustomMenuItem::new("hide".to_string(), "Hide");
// let tray_menu = SystemTrayMenu::new() // insert the menu items here
// .add_item(hide)
// .add_item(quit);
// .add_native_item(SystemTrayMenuItem::Separator)
// let toggle = MenuItemBuilder::with_id("toggle", "Toggle").build(app)?;
// let menu = MenuBuilder::new(app).items(&[&toggle]).build()?;
// Setup Tray
// let tray = tauri::tray::TrayIconBuilder::with_id("my-tray").build(app)?;
// let _ = TrayIconBuilder::new()
// .menu(&menu)
// .on_menu_event(move |_, event| {
// match event.id().as_ref() {
// "toggle" => {
// println!("toggle clicked");
// }
// _ => (),
// }
// // match event {
// // SystemTrayEvent::LeftClick { position: _, size: _, .. } => {
// // let window = app.get_window("main").unwrap();
// // window.show().unwrap();
// // window.set_focus().unwrap();
// // // thread::sleep(Duration::from_millis(100));
// // // window.set_always_on_top(false).unwrap();
// // println!("system tray received a left click");
// // }
// // SystemTrayEvent::RightClick { position: _, size: _, .. } => {
// // // let window = app.get_window("main").unwrap();
// // // window.hide().unwrap();
// // println!("system tray received a right click");
// // }
// // SystemTrayEvent::DoubleClick { position: _, size: _, .. } => {
// // println!("system tray received a double click");
// // }
// // SystemTrayEvent::MenuItemClick { id, .. } =>
// // match id.as_str() {
// // "quit" => {
// // std::process::exit(0);
// // }
// // "hide" => {
// // let window = app.get_window("main").unwrap();
// // window.hide().unwrap();
// // }
// // _ => {}
// // }
// // _ => {}
// // }
// })
// .on_tray_icon_event(|tray, event| {
// if event.click_type == ClickType::Left {
// let app = tray.app_handle();
// if let Some(webview_window) = app.get_webview_window("main") {
// let _ = webview_window.show();
// let _ = webview_window.set_focus();
// }
// }
// })
// .build(app)
// .unwrap();