File size: 2,537 Bytes
a21c316 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import { getCurrentWindow, LogicalSize } from '@tauri-apps/api/window';
import { isTauri } from './env';
/**
* Enter mini view mode
* @param contentHeight The height of the content to fit
* @param shouldCenter Whether to center the window (default: false)
*/
export const enterMiniMode = async (contentHeight: number, shouldCenter: boolean = false) => {
if (!isTauri()) return;
try {
const win = getCurrentWindow();
// Hide window decorations (title bar) first to ensure accurate sizing
await win.setDecorations(false);
// Set window size: width 300, height = content height
await win.setSize(new LogicalSize(300, contentHeight+2));
await win.setAlwaysOnTop(true);
// Enable window shadow
await win.setShadow(true);
// Disable resizing in mini mode
await win.setResizable(false);
// Center window only if requested (usually on first load)
if (shouldCenter) {
await win.center();
}
} catch (error) {
console.error('Failed to enter mini mode:', error);
}
};
/**
* Exit mini view mode and restore default window state
*/
export const exitMiniMode = async () => {
if (!isTauri()) return;
try {
const win = getCurrentWindow();
// Restore to a reasonable default size
await win.setSize(new LogicalSize(1200, 800));
await win.setAlwaysOnTop(false);
await win.center();
// Restore window decorations (title bar)
await win.setDecorations(true);
// Re-enable resizing
await win.setResizable(true);
} catch (error) {
console.error('Failed to exit mini mode:', error);
}
};
/**
* Ensure window is in valid full view state (Self-healing)
* Used on app startup to recover from improper shutdown in mini mode
*/
export const ensureFullViewState = async () => {
if (!isTauri()) return;
try {
const win = getCurrentWindow();
const size = await win.outerSize();
// If window is suspiciously narrow (likely leftover from Mini View), restore default size
if (size.width < 500) {
await win.setSize(new LogicalSize(1200, 800));
await win.center();
}
// Always enforce standard window properties for Full View
await win.setDecorations(true);
await win.setResizable(true);
await win.setAlwaysOnTop(false);
} catch (error) {
console.error('Failed to ensure full view state:', error);
}
};
|