refactor: eliminate startup flicker — defer window show until overlay renders

- Remove Rust-side `eval(INITIAL_LOADING_OVERLAY)` that prematurely
  dismissed the overlay before React/MUI theme was ready
- Defer `window.show()` from Rust `activate_window` to an inline
  `<script>` in index.html, executed after the themed overlay is in DOM
- Remove `useAppInitialization` hook (duplicate of `useLoadingOverlay`
  with no themeReady gate)
- Simplify overlay to pure theme-colored background — no spinner or
  loading text — so fast startup feels instant
- Simplify `hideInitialOverlay` API and reduce overlay fade to 0.2s
- Clean up unused CSS variables (spinner-track, spinner-top, etc.)
This commit is contained in:
Tunglies
2026-04-06 01:53:40 +08:00
parent 04ce3d1772
commit ec82b69786
9 changed files with 43 additions and 201 deletions
+16 -48
View File
@@ -11,27 +11,15 @@
<title>Clash Verge</title>
<style>
:root {
--initial-bg: #f5f5f5;
--initial-text: #333;
--initial-spinner-track: #e3e3e3;
--initial-spinner-top: #3498db;
--bg-color: var(--initial-bg);
--text-color: var(--initial-text);
--spinner-track: var(--initial-spinner-track);
--spinner-top: var(--initial-spinner-top);
--bg-color: #f5f5f5;
--text-color: #333;
color-scheme: light;
}
@media (prefers-color-scheme: dark) {
:root {
--initial-bg: #2e303d;
--initial-text: #ffffff;
--initial-spinner-track: #3a3a3a;
--initial-spinner-top: #0a84ff;
--bg-color: var(--initial-bg);
--text-color: var(--initial-text);
--spinner-track: var(--initial-spinner-track);
--spinner-top: var(--initial-spinner-top);
--bg-color: #2e303d;
--text-color: #ffffff;
color-scheme: dark;
}
}
@@ -53,48 +41,28 @@
position: fixed;
inset: 0;
background: var(--bg-color);
color: var(--text-color);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 9999;
font-family:
-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
transition: opacity 0.3s ease;
transition: opacity 0.2s ease-out;
}
#initial-loading-overlay[data-hidden='true'] {
opacity: 0;
pointer-events: none;
}
.initial-spinner {
width: 40px;
height: 40px;
border: 3px solid var(--spinner-track);
border-top: 3px solid var(--spinner-top);
border-radius: 50%;
animation: initial-spin 1s linear infinite;
}
@keyframes initial-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div id="initial-loading-overlay">
<div class="initial-spinner"></div>
<div style="font-size: 14px; opacity: 0.7; margin-top: 20px">
Loading Clash Verge...
</div>
</div>
<div id="initial-loading-overlay"></div>
<script>
if (window.__TAURI_INTERNALS__) {
window.__TAURI_INTERNALS__
.invoke('plugin:window|show', { label: 'main' })
.catch(function () {});
window.__TAURI_INTERNALS__
.invoke('plugin:window|set_focus', { label: 'main' })
.catch(function () {});
}
</script>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
-2
View File
@@ -44,7 +44,6 @@ import { useThemeMode } from '@/services/states'
import getSystem from '@/utils/get-system'
import {
useAppInitialization,
useCustomTheme,
useLayoutEvents,
useLoadingOverlay,
@@ -217,7 +216,6 @@ const Layout = () => {
)
useLoadingOverlay(themeReady)
useAppInitialization()
const handleNotice = useCallback(
(payload: [string, string]) => {
-1
View File
@@ -1,4 +1,3 @@
export { useAppInitialization } from './use-app-initialization'
export { useLayoutEvents } from './use-layout-events'
export { useLoadingOverlay } from './use-loading-overlay'
export { useNavMenuOrder } from './use-nav-menu-order'
@@ -1,51 +0,0 @@
import { useEffect, useRef } from 'react'
import { hideInitialOverlay } from '../utils'
export const useAppInitialization = () => {
const initRef = useRef(false)
useEffect(() => {
if (initRef.current) return
initRef.current = true
let isCancelled = false
const timers = new Set<number>()
const scheduleTimeout = (handler: () => void, delay: number) => {
if (isCancelled) return -1
const id = window.setTimeout(() => {
if (!isCancelled) {
handler()
}
timers.delete(id)
}, delay)
timers.add(id)
return id
}
const removeLoadingOverlay = () => {
hideInitialOverlay({ schedule: scheduleTimeout })
}
const performInitialization = () => {
if (isCancelled) return
removeLoadingOverlay()
}
scheduleTimeout(performInitialization, 100)
scheduleTimeout(performInitialization, 5000)
return () => {
isCancelled = true
timers.forEach((id) => {
try {
window.clearTimeout(id)
} catch (error) {
console.warn('[Initialization] Failed to clear timer:', error)
}
})
timers.clear()
}
}, [])
}
+5 -36
View File
@@ -3,46 +3,15 @@ import { useEffect, useRef } from 'react'
import { hideInitialOverlay } from '../utils'
export const useLoadingOverlay = (themeReady: boolean) => {
const overlayRemovedRef = useRef(false)
const doneRef = useRef(false)
useEffect(() => {
if (!themeReady || overlayRemovedRef.current) return
let removalTimer: number | undefined
let retryTimer: number | undefined
let attempts = 0
const maxAttempts = 50
let stopped = false
const tryRemoveOverlay = () => {
if (stopped || overlayRemovedRef.current) return
const { removed, removalTimer: timerId } = hideInitialOverlay({
assumeMissingAsRemoved: true,
})
if (typeof timerId === 'number') {
removalTimer = timerId
}
if (removed) {
overlayRemovedRef.current = true
return
}
if (attempts < maxAttempts) {
attempts += 1
retryTimer = window.setTimeout(tryRemoveOverlay, 100)
} else {
console.warn('[Loading Overlay] Element not found')
}
}
tryRemoveOverlay()
if (!themeReady || doneRef.current) return
doneRef.current = true
const timer = hideInitialOverlay()
return () => {
stopped = true
if (typeof removalTimer === 'number') window.clearTimeout(removalTimer)
if (typeof retryTimer === 'number') window.clearTimeout(retryTimer)
if (timer !== undefined) window.clearTimeout(timer)
}
}, [themeReady])
}
@@ -1,45 +1,17 @@
const OVERLAY_ID = 'initial-loading-overlay'
const REMOVE_DELAY = 300
let removed = false
let overlayRemoved = false
export const hideInitialOverlay = (): number | undefined => {
if (removed) return undefined
type HideOverlayOptions = {
schedule?: (handler: () => void, delay: number) => number
assumeMissingAsRemoved?: boolean
}
type HideOverlayResult = {
removed: boolean
removalTimer?: number
}
export const hideInitialOverlay = (
options: HideOverlayOptions = {},
): HideOverlayResult => {
if (overlayRemoved) {
return { removed: true }
}
const overlay = document.getElementById(OVERLAY_ID)
const overlay = document.getElementById('initial-loading-overlay')
if (!overlay) {
if (options.assumeMissingAsRemoved) {
overlayRemoved = true
return { removed: true }
}
return { removed: false }
removed = true
return undefined
}
overlayRemoved = true
removed = true
overlay.dataset.hidden = 'true'
const schedule = options.schedule ?? window.setTimeout
const removalTimer = schedule(() => {
try {
overlay.remove()
} catch (error) {
console.warn('[Loading Overlay] Removal failed:', error)
}
}, REMOVE_DELAY)
return { removed: true, removalTimer }
const timer = window.setTimeout(() => overlay.remove(), 200)
return timer
}