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

View File

@@ -2,36 +2,34 @@
mod app;
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::{
invoke,
invoke::{download_file, download_file_by_binary, send_notification},
setup::{set_global_shortcut, set_system_tray},
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;
pub fn run_app() {
let (pake_config, tauri_config) = get_pake_config();
let tauri_app = tauri::Builder::default();
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 init_fullscreen = pake_config.windows[0].fullscreen;
let window_state_plugin = if init_fullscreen {
windowStatePlugin::default()
.with_state_flags(tauri_plugin_window_state::StateFlags::FULLSCREEN)
.build()
} else {
windowStatePlugin::default().build()
};
let window_state_plugin = WindowStatePlugin::default()
.with_state_flags(if init_fullscreen {
StateFlags::FULLSCREEN
} else {
// Prevent flickering on the first open.
StateFlags::all() & !StateFlags::VISIBLE
})
.build();
tauri_app
.plugin(window_state_plugin)
@@ -46,29 +44,25 @@ pub fn run_app() {
send_notification,
])
.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_global_shortcut(app.app_handle(), activation_shortcut).unwrap();
// Prevent flickering on the first open.
window.show().unwrap();
Ok(())
})
.on_window_event(|_window, _event| {
#[cfg(target_os = "macos")]
if let tauri::WindowEvent::CloseRequested { api, .. } = _event {
let window = _window.clone();
{
tauri::async_runtime::spawn(async move {
if window.is_fullscreen().unwrap_or(false) {
window.set_fullscreen(false).unwrap();
// 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();
});
}
tauri::async_runtime::spawn(async move {
if window.is_fullscreen().unwrap_or(false) {
window.set_fullscreen(false).unwrap();
tokio::time::sleep(Duration::from_millis(900)).await;
}
window.minimize().unwrap();
window.hide().unwrap();
});
api.prevent_close();
}
})