[bugfix] 1: To do not restore and save the data x,y_pos and its size when the window is minimized.

2: To do not restore and save the message box ratio data when the window is minimized either.
This commit is contained in:
Sakamoto Shiina
2024-11-05 21:26:13 +09:00
parent ef5799cd9b
commit 0f599ce679
3 changed files with 58 additions and 18 deletions

View File

@@ -1,7 +1,6 @@
import { useStdoutToPython } from "@logics/useStdoutToPython";
import { useEffect } from "react";
import { appWindow, currentMonitor, LogicalPosition, LogicalSize } from "@tauri-apps/api/window";
import { appWindow, currentMonitor, availableMonitors, LogicalPosition, LogicalSize } from "@tauri-apps/api/window";
export const useWindow = () => {
const { asyncStdoutToPython } = useStdoutToPython();
const asyncGetWindowGeometry = async () => {
@@ -24,25 +23,57 @@ export const useWindow = () => {
};
const asyncSaveWindowGeometry = async () => {
const minimized = await appWindow.isMinimized();
if (minimized === true) return; // don't save while the window is minimized.
const data = await asyncGetWindowGeometry();
asyncStdoutToPython("/set/data/main_window_geometry", data);
};
const restoreWindowGeometry = async (data) => {
try {
const monitor = await currentMonitor();
const monitors = await availableMonitors();
const { x_pos, y_pos, width, height } = data;
if (monitor) {
const { width: monitorWidth, height: monitorHeight } = monitor.size;
// ウィンドウが属するモニターを特定
const targetMonitor = monitors.find(monitor =>
x_pos >= monitor.position.x &&
y_pos >= monitor.position.y &&
x_pos < monitor.position.x + monitor.size.width &&
y_pos < monitor.position.y + monitor.size.height
) || await currentMonitor();
let width = Math.min(parseInt(data.width), monitorWidth);
let height = Math.min(parseInt(data.height), monitorHeight);
if (targetMonitor) {
const { width: monitorWidth, height: monitorHeight } = targetMonitor.size;
const { x: monitorX, y: monitorY } = targetMonitor.position;
const x = parseInt(data.x_pos);
const y = parseInt(data.y_pos);
// ウィンドウのサイズをモニターサイズ内に収める
let adjustedWidth = Math.min(parseInt(width), monitorWidth);
let adjustedHeight = Math.min(parseInt(height), monitorHeight);
await appWindow.setPosition(new LogicalPosition(x, y));
await appWindow.setSize(new LogicalSize(width, height));
// ウィンドウの位置をモニターの範囲内に収める
let adjustedX = parseInt(x_pos);
let adjustedY = parseInt(y_pos);
// X座標がモニター左にはみ出ている場合
if (adjustedX < monitorX) {
adjustedX = monitorX;
}
// X座標がモニター右にはみ出ている場合
else if (adjustedX + adjustedWidth > monitorX + monitorWidth) {
adjustedX = monitorX + monitorWidth - adjustedWidth;
}
// Y座標がモニター上にはみ出ている場合
if (adjustedY < monitorY) {
adjustedY = monitorY;
}
// Y座標がモニター下にはみ出ている場合
else if (adjustedY + adjustedHeight > monitorY + monitorHeight) {
adjustedY = monitorY + monitorHeight - adjustedHeight;
}
await appWindow.setPosition(new LogicalPosition(adjustedX, adjustedY));
await appWindow.setSize(new LogicalSize(adjustedWidth, adjustedHeight));
} else {
console.error("Monitor information could not be retrieved.");
}