Skip to content

[bug] onResized sends { width, height } instead of PhysicalSize #6507

@djeikyb

Description

@djeikyb

Describe the bug

Similar to #2599 .. The onResized payload is written PhysicalSize, but only the width and height properties are being received by my handler.

  async onResized(handler: EventCallback<PhysicalSize>): Promise<UnlistenFn> {
    return this.listen<PhysicalSize>(TauriEvent.WINDOW_RESIZED, handler)
  }

If I'm following right, rust sends back a struct with just the width and height, and then the window.ts function above should be constructing a PhysicalSize object before passing it to the handler.

Reproduction

Unfortunately I don't have a runnable repro ready to go.. but here's a vue3 composable that demonstrates the problem and a workaround. You see the onResized handler constructs the PhysicalSize object instead of relying on the event payload to be the documented type.

import {
  computed,
  onMounted,
  onUnmounted,
  ref,
} from "vue";
import {
  appWindow,
  PhysicalSize,
} from "@tauri-apps/api/window";

export function useInnerSize() {
  const unlisten = ref<(() => void) | null>(null);
  const innerSize = ref<PhysicalSize | null>(null);
  const scaleFactor = ref<number>(1);

  const innerSizeLogical = computed(() => {
    if (innerSize.value == null) return null;
    return innerSize.value.toLogical(scaleFactor.value);
  });

  const width = computed(() => {
    if (innerSizeLogical.value == null) return 0;
    return innerSizeLogical.value.width;
  });

  const height = computed(() => {
    if (innerSizeLogical.value == null) return 0;
    return innerSizeLogical.value.height;
  });

  onMounted(() => {
    appWindow.innerSize().then(p => innerSize.value = p);
    appWindow.scaleFactor().then(sf => scaleFactor.value = sf);
    appWindow
      .onResized(({ payload: size }) => innerSize.value = new PhysicalSize(size.width, size.height))
      .then(u => unlisten.value = u);
  });

  onUnmounted(() => {
    if (unlisten.value) {
      unlisten.value();
    }
  });

  return { width, height };
}

Expected behavior

No response

Platform and versions

yarn run v1.22.19
$ tauri info

Environment
  › OS: Mac OS 13.2.1 X64
  › Node.js: 16.14.2
  › npm: 8.5.0
  › pnpm: 4.12.1
  › yarn: 1.22.19
  › rustup: 1.25.2
  › rustc: 1.68.0
  › cargo: 1.68.0
  › Rust toolchain: stable-aarch64-apple-darwin

Packages
  › @tauri-apps/cli [NPM]: 1.2.3
  › @tauri-apps/api [NPM]: 1.2.0
  › tauri [RUST]: 1.2.4,
  › tauri-build [RUST]: 1.2.1,
  › tao [RUST]: 0.15.8,
  › wry [RUST]: 0.23.4,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:1420/
  › framework: Vue.js
  › bundler: Vite

App directory structure
  ├─ dist
  ├─ node_modules
  ├─ public
  ├─ src-tauri
  ├─ api
  ├─ .vscode
  ├─ .idea
  └─ src
✨  Done in 6.43s.


### Stack trace

_No response_

### Additional context

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions