[bugfix] Fix window geometry bug that was not save and restore. changes the way how to get appWindow instance from Tauri.

This commit is contained in:
Sakamoto Shiina
2025-05-11 15:26:05 +09:00
parent 61e333401f
commit b2bc1e62cb
10 changed files with 53 additions and 68 deletions

View File

@@ -1,5 +1,5 @@
import { useEffect, useRef } from "react";
import { getCurrentWindow, currentMonitor, availableMonitors, PhysicalPosition, PhysicalSize } from "@tauri-apps/api/window";
import { currentMonitor, availableMonitors, PhysicalPosition, PhysicalSize } from "@tauri-apps/api/window";
import { useStdoutToPython } from "@logics/useStdoutToPython";
import { useStore_IsBreakPoint } from "@store";
import { useUiScaling } from "@logics_configs";
@@ -10,9 +10,9 @@ export const useWindow = () => {
const { currentUiScaling } = useUiScaling();
const { updateIsBreakPoint } = useStore_IsBreakPoint();
const appWindow = store.appWindow;
const asyncGetWindowGeometry = async () => {
const appWindow = await getCurrentWindow();
try {
const position = await appWindow.outerPosition();
const { x: x_pos, y: y_pos } = position;
@@ -32,7 +32,6 @@ export const useWindow = () => {
};
const asyncSaveWindowGeometry = async () => {
const appWindow = await getCurrentWindow();
const minimized = await appWindow.isMinimized();
if (minimized === true) return; // don't save while the window is minimized.
const data = await asyncGetWindowGeometry();
@@ -40,8 +39,6 @@ export const useWindow = () => {
};
const restoreWindowGeometry = async (data) => {
const appWindow = await getCurrentWindow();
try {
const monitors = await availableMonitors();
const { x_pos, y_pos, width, height } = data;
@@ -95,7 +92,6 @@ export const useWindow = () => {
};
const asyncUpdateBreakPoint = async () => {
const appWindow = await getCurrentWindow();
const size = await appWindow.innerSize();
const dynamicBreakPoint = 800 * (currentUiScaling.data / 100);
updateIsBreakPoint(size.width <= dynamicBreakPoint);
@@ -109,10 +105,7 @@ export const useWindow = () => {
const unlistenMove = useRef(null);
useEffect(() => {
const setup = async () => {
if (store.is_register_window_geometry_controller) return;
const appWindow = await getCurrentWindow();
const setup = () => {
unlistenResize.current = appWindow.onResized(() => {
clearTimeout(resizeTimeout.current);
resizeTimeout.current = setTimeout(() => {
@@ -127,7 +120,6 @@ export const useWindow = () => {
asyncSaveWindowGeometry();
}, 200);
});
store.is_register_window_geometry_controller = true;
};
setup();
@@ -148,10 +140,30 @@ export const useWindow = () => {
return null;
};
const asyncToggleMaximizeApp = async () => {
const maximizeState = await appWindow.isMaximized();
if (!maximizeState) {
await appWindow.maximize();
} else {
await appWindow.unmaximize();
}
};
const asyncMinimizeApp = async () => {
await appWindow.minimize();
};
const asyncCloseApp = async () => {
await appWindow.close();
};
return {
WindowGeometryController,
asyncSaveWindowGeometry,
restoreWindowGeometry,
asyncUpdateBreakPoint,
asyncCloseApp,
asyncToggleMaximizeApp,
asyncMinimizeApp,
};
};

View File

@@ -1,5 +1,3 @@
import { getCurrentWindow } from "@tauri-apps/api/window";
import { store, useStore_Hotkeys } from "@store";
import { useStdoutToPython } from "@logics/useStdoutToPython";
import { useNotificationStatus } from "@logics_common";
@@ -7,6 +5,8 @@ import { useMainFunction } from "@logics_main";
import { register, unregisterAll, isRegistered } from "@tauri-apps/plugin-global-shortcut";
export const useHotkeys = () => {
const appWindow = store.appWindow;
const { asyncStdoutToPython } = useStdoutToPython();
const { currentHotkeys, updateHotkeys, pendingHotkeys } = useStore_Hotkeys();
const {
@@ -68,8 +68,6 @@ export const useHotkeys = () => {
if (!isAlreadyRegistered) {
await register(shortcut, async (event) => {
if (event.state !== "Pressed") return;
const appWindow = await getCurrentWindow();
switch (actionKey) {
case "toggle_vrct_visibility": {
const minimized = await appWindow.isMinimized();

View File

@@ -1,4 +1,4 @@
import { getCurrentWindow } from "@tauri-apps/api/window";
import { store } from "@store";
import {
useStore_TranslationStatus,
@@ -6,10 +6,11 @@ import {
useStore_TranscriptionReceiveStatus,
useStore_ForegroundStatus,
} from "@store";
import { useCallback } from "react";
import { useStdoutToPython } from "@logics/useStdoutToPython";
export const useMainFunction = () => {
const appWindow = store.appWindow;
const {
currentTranslationStatus,
updateTranslationStatus,
@@ -76,9 +77,8 @@ export const useMainFunction = () => {
const toggleForeground = async () => {
const main_page = await getCurrentWindow();
const is_foreground_enabled = !currentForegroundStatus.data;
main_page.setAlwaysOnTop(is_foreground_enabled);
await appWindow.setAlwaysOnTop(is_foreground_enabled);
updateForegroundStatus(is_foreground_enabled);
};

View File

@@ -1,8 +1,9 @@
import { getCurrentWindow } from "@tauri-apps/api/window";
import { store } from "@store";
import { useStore_MessageInputBoxRatio } from "@store";
import { useStdoutToPython } from "@logics/useStdoutToPython";
import { clampMinMax } from "@utils";
export const useMessageInputBoxRatio = () => {
const appWindow = store.appWindow;
const { asyncStdoutToPython } = useStdoutToPython();
const { currentMessageInputBoxRatio, updateMessageInputBoxRatio } = useStore_MessageInputBoxRatio();
@@ -12,7 +13,6 @@ export const useMessageInputBoxRatio = () => {
};
const asyncSetMessageInputBoxRatio = async (ratio) => {
const appWindow = getCurrentWindow();
const minimized = await appWindow.isMinimized();
if (minimized === true) return; // don't save while the window is minimized.
const parsed = parseFloat(ratio.toFixed(2));