tauri-apps / tauri

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

[bug] Is this a bug in setPosition in a multi-monitor environment? #9323

Closed akumasign closed 6 months ago

akumasign commented 6 months ago

Describe the bug

<el-icon id="menu" size="25px" @click="titleMenu_onClick($event)">
async function titleMenu_onClick(event: PointerEvent) {
    const mainMenu = WebviewWindow.getByLabel('MainMenu');
    if (mainMenu) {
        await mainMenu.setPosition(new PhysicalPosition(event.screenX, event.screenY + 24));
        await mainMenu.show();
        await mainMenu.setAlwaysOnTop(true);
        await mainMenu.setFocus();
    }
}

This is a click event handler for the menu button on the main window to display the menu window. Every time you click this button after moving the main window to another monitor, the size of the menu window will increase significantly after setPosition, every time.

(Also, when clicking a button to display the menu window in the second monitor, the window appears to the left of the mouse pointer instead of the right as expected. Works fine in the first monitor)

This is my environment: Operating system: windows 11 x64 professional edition Monitor 1 resolution: 3440 x 1440 Monitor 2 resolution: 2560 x 1080

Configuration in Cargo.toml:

rust-version = "1.64"

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.6.1", features = ["window-all", "process-exit", "global-shortcut", "system-tray"] }
window-shadows = "0.2.2"
reqwest = { version = "0.12", features = ["json", "blocking"] }
tokio = { version = "1", features = ["full"] }
url="2.5.0"
error-chain = "0.12.4"
lazy_static = "1.4.0"
tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }

This is my current temporary solution:

const mainMenuSize = { inited: false, width: 0, height: 0};
async function titleMenu_onClick(event: PointerEvent) {
    const mainMenu = WebviewWindow.getByLabel('MainMenu');
    if (mainMenu) {
        if (!mainMenuSize.inited) {
            let size = await mainMenu.outerSize();
            mainMenuSize.width = size.width;
            mainMenuSize.height = size.height;
            mainMenuSize.inited = true;
        }
        await mainMenu.setPosition(new PhysicalPosition(event.screenX, event.screenY + 24));
        await mainMenu.setSize(new LogicalSize(mainMenuSize.width, mainMenuSize.height));
        await mainMenu.show();
        await mainMenu.setAlwaysOnTop(true);
        await mainMenu.setFocus();
    }
}

Reproduction

No response

Expected behavior

No response

Full tauri info output

> tauri info

WARNING: Only one package manager should be used, but found npm and yarn.
         Please remove unused package manager lock files, will use npm for now!

[✔] Environment
    - OS: Windows 10.0.22621 X64
    ✔ WebView2: 123.0.2420.65
    ✔ MSVC: Visual Studio Professional 2022
    ✔ rustc: 1.76.0 (07dca489a 2024-02-04)
    ✔ cargo: 1.76.0 (c84b36747 2024-01-18)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.11.1
    - yarn: 1.22.22
    - npm: 10.2.4

[-] Packages
    - tauri [RUST]: 1.6.1
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.7
    - @tauri-apps/api [NPM]: 1.5.3
    - @tauri-apps/cli [NPM]: 1.5.11

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

Stack trace

No response

Additional context

No response

pewsheen commented 6 months ago

Do these monitors have different DPI (scaling)?

akumasign commented 6 months ago

Do these monitors have different DPI (scaling)?

Thanks for the reminder, I forgot that the second monitor's scaling is 125% and the first monitor's 100%. After testing, when the scaling ratio is consistent, the problem no longer occurs. I'm really not sure if this is a BUG.

pewsheen commented 6 months ago

Personally, I think the window size should be adjusted when the DPI is changed. It's the default behavior in Windows, just like you drag a window to another monitor with a different DPI.