From 8a0ec0122dbcadb746c56e27385a0599a50bbad7 Mon Sep 17 00:00:00 2001 From: FTCHD Date: Fri, 31 Jan 2025 16:21:06 +0200 Subject: [PATCH] delay when opening first window --- lib/store.ts | 2 ++ lib/window.ts | 53 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/store.ts b/lib/store.ts index 8e99094..c7cd697 100644 --- a/lib/store.ts +++ b/lib/store.ts @@ -23,6 +23,7 @@ export interface RemoteConfig { } interface State { + firstWindow: boolean rcloneLoaded: boolean rcloneAuth: string @@ -68,6 +69,7 @@ const getStorage = (store: LazyStore): StateStorage => ({ export const useStore = create()( shared( (set) => ({ + firstWindow: true, rcloneLoaded: false, rcloneAuth: '', diff --git a/lib/window.ts b/lib/window.ts index adf4f8a..fecdf13 100644 --- a/lib/window.ts +++ b/lib/window.ts @@ -1,6 +1,12 @@ import { WebviewWindow } from '@tauri-apps/api/webviewWindow' -import { LogicalSize, PhysicalPosition, PhysicalSize, currentMonitor, getAllWindows } from '@tauri-apps/api/window' -import { Position, moveWindow } from '@tauri-apps/plugin-positioner' +import { + LogicalSize, + PhysicalPosition, + PhysicalSize, + currentMonitor, + getAllWindows, +} from '@tauri-apps/api/window' +import { useStore } from './store' import { getTrayRect } from './tray' export async function resetMainWindow() { @@ -56,6 +62,8 @@ export async function openWindow({ width?: number height?: number }) { + const isFirstWindow = useStore.getState().firstWindow + const w = new WebviewWindow(name, { height: 0, width: 0, @@ -70,13 +78,15 @@ export async function openWindow({ // parent: 'main', }) - await new Promise((resolve) => setTimeout(resolve, 1000)) + await new Promise((resolve) => setTimeout(resolve, isFirstWindow ? 1000 : 100)) await w.hide() await w.setSize(new LogicalSize(width, height)) await w.center() await w.show() + useStore.setState({ firstWindow: false }) + return w } @@ -87,6 +97,8 @@ export async function openTrayWindow({ name: string url: string }) { + const isFirstWindow = useStore.getState().firstWindow + const w = new WebviewWindow(name, { height: 0, width: 0, @@ -104,22 +116,29 @@ export async function openTrayWindow({ // .then((w) => w.find((w) => w.label === 'main')) // ?.then((w) => w?.setSize(new LogicalSize(400, 600))) - await new Promise((resolve) => setTimeout(resolve, 1500)) + await new Promise((resolve) => setTimeout(resolve, isFirstWindow ? 1000 : 100)) await w.hide() - const windowSize = new LogicalSize(400, 600) - await w.setSize(windowSize) - const trayRect = getTrayRect() - const windowPhysicalSize = windowSize.toPhysical(await w.scaleFactor()) - await w.setPosition( - new PhysicalPosition( - trayRect.position.x + - trayRect.size.width / 2 - - windowPhysicalSize.width / 2, - trayRect.position.y - ) - ) - await w.show() + + const windowSize = new LogicalSize(400, 600) + await w.setSize(windowSize) + + const trayRect = getTrayRect() + const windowPhysicalSize = windowSize.toPhysical(await w.scaleFactor()) + await w.setPosition( + new PhysicalPosition( + trayRect.position.x + trayRect.size.width / 2 - windowPhysicalSize.width / 2, + trayRect.position.y + ) + ) + + await w.show() + + await w.listen('tauri://blur', async () => { + await w.destroy() + }) + + useStore.setState({ firstWindow: false }) return w }