fix(view): 🐛 状态栏菜单bug修复

修复状态栏右键菜单位置不正确的bug
修复状态栏右键菜单在失去焦点不隐藏的bug
This commit is contained in:
ZOL中关村 2024-10-10 21:27:14 +08:00
parent 2f0e4bf6c7
commit 845b83ebcd
3 changed files with 68 additions and 20 deletions

54
src-tauri/src/init.rs Normal file
View File

@ -0,0 +1,54 @@
use tauri::{Manager, Runtime, WindowEvent};
use tauri_plugin_autostart::MacosLauncher;
pub trait CustomInit {
fn init_plugin(self) -> Self;
fn init_webwindow_event(self) -> Self;
fn init_window_event(self) -> Self;
}
impl<R: Runtime> CustomInit for tauri::Builder<R> {
// 初始化插件
fn init_plugin(self) -> Self {
self.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_websocket::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_upload::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec!["--flag1"])))
}
// 初始化web窗口事件
fn init_webwindow_event(self) -> Self {
self.on_webview_event(|_, event| match event {
_ => (),
})
}
// 初始化系统窗口事件
fn init_window_event(self) -> Self {
self.on_window_event(|window, event| match event {
WindowEvent::Focused(flag) => {
// "自定义系统托盘-实现托盘菜单失去焦点时隐藏"
if window.label() != "tray" && *flag {
let _ = window
.app_handle()
.get_webview_window("tray")
.unwrap()
.hide();
}
if window.label() == "tray" && !flag {
let _ = window.hide();
}
}
_ => (),
})
}
}

View File

@ -2,21 +2,16 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod tray;
mod user_cmd;
mod init;
use user_cmd::{get_user_info, save_user_info, default_window_icon, screenshot, audio};
use tauri_plugin_autostart::MacosLauncher;
use init::CustomInit;
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_websocket::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_upload::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, Some(vec!["--flag1"])))
.init_plugin()
.init_webwindow_event()
.init_window_event()
.setup(move |app| {
app.handle().plugin(tauri_plugin_global_shortcut::Builder::new().build())?;
tray::create_tray(app.handle())?;

View File

@ -101,16 +101,15 @@ onMounted(async () => {
if (!homeWindow) return
const position = event.payload as any
let scaleFactor = await homeWindow.scaleFactor()
let logicalPosition = new PhysicalPosition(position.x, position.y).toLogical(scaleFactor)
logicalPosition.y = logicalPosition.y - 360
let trayWindow = WebviewWindow.getByLabel('tray')
let trayWindow = await WebviewWindow.getByLabel('tray')
if (trayWindow) {
await trayWindow.setAlwaysOnTop(true)
await trayWindow.setPosition(logicalPosition)
await trayWindow.show()
await trayWindow.setFocus()
let size = await trayWindow.outerSize()
let logicalPosition = new PhysicalPosition(position.x, position.y - size.height)
trayWindow.setAlwaysOnTop(true)
trayWindow.setPosition(logicalPosition)
trayWindow.show()
trayWindow.setFocus()
}
})
await listen('login_success', () => {