[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

@@ -8,7 +8,7 @@ import { useMessageInputBoxRatio } from "@logics_main";
import { useUiScaling } from "@logics_configs";
export const MessageContainer = () => {
const { currentMessageInputBoxRatio, setMessageInputBoxRatio } = useMessageInputBoxRatio();
const { currentMessageInputBoxRatio, asyncSetMessageInputBoxRatio } = useMessageInputBoxRatio();
const { currentUiScaling } = useUiScaling();
const [message_box_height_in_rem, setMessageBoxHeightInRem] = useState(10);
const FONT_SIZE_STANDARD = 10 * currentUiScaling.data / 100; // 10px = 1rem
@@ -17,6 +17,12 @@ export const MessageContainer = () => {
const log_box_ref = useRef(null);
const message_box_wrapper_ref = useRef(null);
const asyncSetMessageBoxHeightInRem = async (data) => {
const minimized = await appWindow.isMinimized();
if (minimized === true) return; // don't save while the window is minimized.
setMessageBoxHeightInRem(data);
};
const calculateMessageBoxRatioAndHeight = () => {
if (log_box_ref.current && message_box_wrapper_ref.current) {
const container_height = container_ref.current.offsetHeight;
@@ -26,10 +32,10 @@ export const MessageContainer = () => {
const message_box_height = message_box_wrapper_ref.current.offsetHeight;
const message_box_ratio = (message_box_height / total_height) * 100;
setMessageInputBoxRatio(message_box_ratio);
asyncSetMessageInputBoxRatio(message_box_ratio);
const height_in_rem = convertRatioToRem(message_box_ratio);
setMessageBoxHeightInRem(height_in_rem);
asyncSetMessageBoxHeightInRem(height_in_rem);
}
};
@@ -42,12 +48,12 @@ export const MessageContainer = () => {
useEffect(() => {
// Note: I thought the part "1.4" is message box bottom padding + (message box separator height/2)
// but it should be fixed at 1.4. Idk why, tho.
setMessageBoxHeightInRem((position / FONT_SIZE_STANDARD) - 1.4);
asyncSetMessageBoxHeightInRem((position / FONT_SIZE_STANDARD) - 1.4);
}, [position]);
useEffect(() => {
setMessageBoxHeightInRem(convertRatioToRem(currentMessageInputBoxRatio.data));
asyncSetMessageBoxHeightInRem(convertRatioToRem(currentMessageInputBoxRatio.data));
}, [currentMessageInputBoxRatio.data]);
const convertRatioToRem = (ratio) => {

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.");
}

View File

@@ -1,3 +1,4 @@
import { appWindow } from "@tauri-apps/api/window";
import { useStore_MessageInputBoxRatio } from "@store";
import { useStdoutToPython } from "@logics/useStdoutToPython";
import { clampMinMax } from "@utils/clampMinMax";
@@ -9,7 +10,9 @@ export const useMessageInputBoxRatio = () => {
asyncStdoutToPython("/get/data/message_box_ratio");
};
const setMessageInputBoxRatio = (ratio) => {
const asyncSetMessageInputBoxRatio = async (ratio) => {
const minimized = await appWindow.isMinimized();
if (minimized === true) return; // don't save while the window is minimized.
const parsed = parseFloat(ratio.toFixed(2));
const valid_ratio = clampMinMax(parsed, 1, 99);
asyncStdoutToPython("/set/data/message_box_ratio", valid_ratio);
@@ -19,6 +22,6 @@ export const useMessageInputBoxRatio = () => {
currentMessageInputBoxRatio,
getMessageInputBoxRatio,
updateMessageInputBoxRatio,
setMessageInputBoxRatio,
asyncSetMessageInputBoxRatio,
};
};