fix: WindowProvider awful performance (#5119)
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import { getCurrentWindow } from "@tauri-apps/api/window";
|
import { getCurrentWindow } from "@tauri-apps/api/window";
|
||||||
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
|
|
||||||
|
import debounce from "@/utils/debounce";
|
||||||
|
|
||||||
import { WindowContext } from "./WindowContext";
|
import { WindowContext } from "./WindowContext";
|
||||||
|
|
||||||
export const WindowProvider: React.FC<{ children: React.ReactNode }> = ({
|
export const WindowProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||||
@@ -14,19 +16,14 @@ export const WindowProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||||||
const minimize = useCallback(() => currentWindow.minimize(), [currentWindow]);
|
const minimize = useCallback(() => currentWindow.minimize(), [currentWindow]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let active = true;
|
const checkMaximized = debounce(async () => {
|
||||||
|
|
||||||
const updateMaximized = async () => {
|
|
||||||
const value = await currentWindow.isMaximized();
|
const value = await currentWindow.isMaximized();
|
||||||
if (!active) return;
|
setMaximized(value);
|
||||||
setMaximized((prev) => (prev === value ? prev : value));
|
}, 300);
|
||||||
};
|
|
||||||
|
|
||||||
updateMaximized();
|
const unlistenPromise = currentWindow.onResized(checkMaximized);
|
||||||
const unlistenPromise = currentWindow.onResized(updateMaximized);
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
active = false;
|
|
||||||
unlistenPromise.then((unlisten) => unlisten());
|
unlistenPromise.then((unlisten) => unlisten());
|
||||||
};
|
};
|
||||||
}, [currentWindow]);
|
}, [currentWindow]);
|
||||||
@@ -47,7 +44,7 @@ export const WindowProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||||||
|
|
||||||
const refreshDecorated = useCallback(async () => {
|
const refreshDecorated = useCallback(async () => {
|
||||||
const val = await currentWindow.isDecorated();
|
const val = await currentWindow.isDecorated();
|
||||||
setDecorated((prev) => (prev === val ? prev : val));
|
setDecorated(val);
|
||||||
return val;
|
return val;
|
||||||
}, [currentWindow]);
|
}, [currentWindow]);
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from "./WindowContext";
|
export { WindowContext } from "./WindowContext";
|
||||||
export * from "./WindowProvider";
|
export type { WindowContextType } from "./WindowContext";
|
||||||
|
export { WindowProvider } from "./WindowProvider";
|
||||||
|
|||||||
Reference in New Issue
Block a user