tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
81.35k stars 2.43k forks source link

[bug] isMaximized seems to trigger resize event #5812

Open ottosson opened 1 year ago

ottosson commented 1 year ago

Describe the bug

I'm checking isMaximized with the resize event. This seems to trigger an infinite loop of sorts since I can see thousands of console.logs instead of one per pixel (or whatever). I have window decorations set to false in tauri.conf.

  useEffect(() => {
    let unlisten: UnlistenFn;
    const window = getCurrent();

    listen("tauri://resize", async () => {
      console.log("resize");
      const _isWindowMaximized = await window.isMaximized();
      //...
    }).then((data) => {
      unlisten = data;
    });

    return () => {
      if (unlisten != null) {
        unlisten();
      }
    };
  }, []);

Removing the isMaximized or trying with some other functions on window does not result in the same behaviour.

I've tested on Macos and Windows and it seems it works just fine on Windows.

Reproduction

use window.isMaximized() in listen("tauri://resize")

Expected behavior

No response

Platform and versions

Environment › OS: Mac OS 13.0.0 X64 › Node.js: 18.5.0 › npm: 8.12.1 › pnpm: Not installed! › yarn: 1.22.19 › rustup: 1.25.1 › rustc: 1.64.0 › cargo: 1.64.0 › Rust toolchain: stable-x86_64-apple-darwin

Packages › @tauri-apps/cli [NPM]: 1.2.2 › @tauri-apps/api [NPM]: 1.2.0 › tauri [RUST]: 1.2.2, › tauri-build [RUST]: 1.1.1, › tao [RUST]: 0.15.7, › wry [RUST]: 0.23.3,

App › build-type: bundle › CSP: unset › distDir: ../dist › devPath: http://localhost:5173/ › framework: React › bundler: Vite

App directory structure ├─ dist ├─ node_modules ├─ src-tauri ├─ .git ├─ .vscode └─ src

Stack trace

No response

Additional context

No response

letrungdo commented 1 year ago

I also have the same issue

keadex commented 9 months ago

Same issue here. I also noticed, in my case, that this happens only in dev mode (tauri dev) and not in production (tauri build).

This issue makes it impossible to use isMaximized() in combination with onResized() (https://tauri.app/it/v1/api/js/window#onresized) on MacOS.

The only workaround I found is to use a flag to temporarily disable the onResized() callback during the isMaximized() execution:

const isOnResizedDisabled = useRef(true)

function isMaximizedWorkaround(){
  isOnResizedDisabled.current = true
  appWindow.isMaximized().then((isMaximized) => {
    isOnResizedDisabled.current = false
    // your stuff
  })
}

useEffect(() => {
  unlisten = appWindow.listen(TauriEvent.WINDOW_RESIZED, () => {
    if (!isOnResizedDisabled.current) {
      isMaximizedWorkaround()
    }
  })
  return () => {
    if (unlisten) unlisten.then((f) => f())
  }
}, [])