🐛 Prevent flickering on the first open

This commit is contained in:
Tw93
2024-12-26 14:59:52 +08:00
parent a695b41a29
commit 991f718fba
2 changed files with 44 additions and 45 deletions

View File

@@ -18,21 +18,28 @@ pub fn set_system_tray(app: &AppHandle, show_system_tray: bool) -> tauri::Result
let hide_app = MenuItemBuilder::with_id("hide_app", "Hide").build(app)?; let hide_app = MenuItemBuilder::with_id("hide_app", "Hide").build(app)?;
let show_app = MenuItemBuilder::with_id("show_app", "Show").build(app)?; let show_app = MenuItemBuilder::with_id("show_app", "Show").build(app)?;
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?; let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
let menu = MenuBuilder::new(app) let menu = MenuBuilder::new(app)
.items(&[&hide_app, &show_app, &quit]) .items(&[&hide_app, &show_app, &quit])
.build()?; .build()?;
app.app_handle().remove_tray_by_id("pake-tray"); app.app_handle().remove_tray_by_id("pake-tray");
let tray = TrayIconBuilder::new() let tray = TrayIconBuilder::new()
.menu(&menu) .menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() { .on_menu_event(move |app, event| match event.id().as_ref() {
"hide_app" => { "hide_app" => {
app.get_webview_window("pake").unwrap().minimize().unwrap(); if let Some(window) = app.get_webview_window("pake") {
window.minimize().unwrap();
}
} }
"show_app" => { "show_app" => {
app.get_webview_window("pake").unwrap().show().unwrap(); if let Some(window) = app.get_webview_window("pake") {
window.show().unwrap();
}
} }
"quit" => { "quit" => {
let _res = app.save_window_state(StateFlags::all()); app.save_window_state(StateFlags::all()).unwrap();
std::process::exit(0); std::process::exit(0);
} }
_ => (), _ => (),
@@ -48,8 +55,9 @@ pub fn set_global_shortcut(app: &AppHandle, shortcut: String) -> tauri::Result<(
if shortcut.is_empty() { if shortcut.is_empty() {
return Ok(()); return Ok(());
} }
let app_handle = app.clone(); let app_handle = app.clone();
let shortcut_hotkey = Shortcut::from_str(shortcut.as_str()).unwrap(); let shortcut_hotkey = Shortcut::from_str(&shortcut).unwrap();
let last_triggered = Arc::new(Mutex::new(Instant::now())); let last_triggered = Arc::new(Mutex::new(Instant::now()));
app_handle app_handle
@@ -59,21 +67,17 @@ pub fn set_global_shortcut(app: &AppHandle, shortcut: String) -> tauri::Result<(
let last_triggered = Arc::clone(&last_triggered); let last_triggered = Arc::clone(&last_triggered);
move |app, event, _shortcut| { move |app, event, _shortcut| {
let mut last_triggered = last_triggered.lock().unwrap(); let mut last_triggered = last_triggered.lock().unwrap();
if Instant::now().duration_since(*last_triggered) if Instant::now().duration_since(*last_triggered) < Duration::from_millis(300) {
< Duration::from_millis(300)
{
return; return;
} }
*last_triggered = Instant::now(); *last_triggered = Instant::now();
if shortcut_hotkey.eq(event) {
let window = app.get_webview_window("pake").unwrap();
let is_visible = window.is_visible().unwrap();
match is_visible { if shortcut_hotkey.eq(event) {
true => { if let Some(window) = app.get_webview_window("pake") {
let is_visible = window.is_visible().unwrap();
if is_visible {
window.hide().unwrap(); window.hide().unwrap();
} } else {
false => {
window.show().unwrap(); window.show().unwrap();
window.set_focus().unwrap(); window.set_focus().unwrap();
} }
@@ -84,6 +88,7 @@ pub fn set_global_shortcut(app: &AppHandle, shortcut: String) -> tauri::Result<(
.build(), .build(),
) )
.expect("Failed to set global shortcut"); .expect("Failed to set global shortcut");
app.global_shortcut().register(shortcut_hotkey).unwrap(); app.global_shortcut().register(shortcut_hotkey).unwrap();
Ok(()) Ok(())

View File

@@ -2,36 +2,34 @@
mod app; mod app;
mod util; mod util;
use std::time::Duration;
use tauri::Manager;
use tauri_plugin_window_state::Builder as WindowStatePlugin;
use tauri_plugin_window_state::StateFlags;
use app::{ use app::{
invoke, invoke::{download_file, download_file_by_binary, send_notification},
setup::{set_global_shortcut, set_system_tray}, setup::{set_global_shortcut, set_system_tray},
window::set_window, window::set_window,
}; };
use invoke::{download_file, download_file_by_binary, send_notification};
use std::time::Duration;
use tauri::Manager;
use tauri_plugin_window_state::Builder as windowStatePlugin;
use util::get_pake_config; use util::get_pake_config;
pub fn run_app() { pub fn run_app() {
let (pake_config, tauri_config) = get_pake_config(); let (pake_config, tauri_config) = get_pake_config();
let tauri_app = tauri::Builder::default(); let tauri_app = tauri::Builder::default();
let show_system_tray = pake_config.show_system_tray(); let show_system_tray = pake_config.show_system_tray();
// Save the value of toggle_app_shortcut before pake_config is moved
let activation_shortcut = pake_config.windows[0].activation_shortcut.clone(); let activation_shortcut = pake_config.windows[0].activation_shortcut.clone();
let init_fullscreen = pake_config.windows[0].fullscreen; let init_fullscreen = pake_config.windows[0].fullscreen;
let window_state_plugin = if init_fullscreen { let window_state_plugin = WindowStatePlugin::default()
windowStatePlugin::default() .with_state_flags(if init_fullscreen {
.with_state_flags(tauri_plugin_window_state::StateFlags::FULLSCREEN) StateFlags::FULLSCREEN
.build() } else {
} else { // Prevent flickering on the first open.
windowStatePlugin::default().build() StateFlags::all() & !StateFlags::VISIBLE
}; })
.build();
tauri_app tauri_app
.plugin(window_state_plugin) .plugin(window_state_plugin)
@@ -46,29 +44,25 @@ pub fn run_app() {
send_notification, send_notification,
]) ])
.setup(move |app| { .setup(move |app| {
set_window(app, &pake_config, &tauri_config); let window = set_window(app, &pake_config, &tauri_config);
set_system_tray(app.app_handle(), show_system_tray).unwrap(); set_system_tray(app.app_handle(), show_system_tray).unwrap();
set_global_shortcut(app.app_handle(), activation_shortcut).unwrap(); set_global_shortcut(app.app_handle(), activation_shortcut).unwrap();
// Prevent flickering on the first open.
window.show().unwrap();
Ok(()) Ok(())
}) })
.on_window_event(|_window, _event| { .on_window_event(|_window, _event| {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
if let tauri::WindowEvent::CloseRequested { api, .. } = _event { if let tauri::WindowEvent::CloseRequested { api, .. } = _event {
let window = _window.clone(); let window = _window.clone();
{ tauri::async_runtime::spawn(async move {
tauri::async_runtime::spawn(async move { if window.is_fullscreen().unwrap_or(false) {
if window.is_fullscreen().unwrap_or(false) { window.set_fullscreen(false).unwrap();
window.set_fullscreen(false).unwrap(); tokio::time::sleep(Duration::from_millis(900)).await;
// Give a small delay to ensure the full-screen exit operation is completed. }
tokio::time::sleep(Duration::from_millis(900)).await; window.minimize().unwrap();
} window.hide().unwrap();
window.minimize().unwrap(); });
window.hide().unwrap();
});
}
api.prevent_close(); api.prevent_close();
} }
}) })