tauri-apps / tauri

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

[bug] Incorrect and inconsistent window positions #10021

Open thewh1teagle opened 3 months ago

thewh1teagle commented 3 months ago

Describe the bug

I use tauri on v2 project on macOS When creating new window with specific position, the position is incorrect and inconsistent comparing of doing it with global tauri.

Reproduction

Example on Rust side, it creates the window with invalid offset.

#[tauri::command]
async fn test(app_handle: AppHandle) {
  if let Some(window) = app_handle.get_webview_window("main") {
    let position = window.outer_position().unwrap();
    println!("position: {:?}", position);
    let window = WebviewWindowBuilder::new(&app_handle, "main1", WebviewUrl::App("/index.html".into()))
      .position(position.x as f64 + 30.0, position.y as f64 + 30.0) // 30px offset x+y
      .build();
  }
}

Example correct position using global tauri:

// needs  window:allow-create permission
(async () => {
    const position = await window.__TAURI__.window.getCurrent().outerPosition()
    console.log(position.x, position.y)
    new window.__TAURI__.window.Window("main1", {url: "/index.html", x: position.x + 30, y: position.y + 30})
})()

Expected behavior

The window should be created with offset of 30px on x,y

Full tauri info output

npx tauri info
WARNING: no lock files found, defaulting to npm

[✔] Environment
    - OS: Mac OS 14.4.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.75.0 (82e1608df 2023-12-21)
    ✔ cargo: 1.75.0 (1d8b05cdd 2023-11-20)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 18.19.1
    - yarn: 1.22.22
    - npm: 10.2.4
    - bun: 1.1.6

[-] Packages
    - tauri [RUST]: 2.0.0-beta.22
    - tauri-build [RUST]: 2.0.0-beta.17
    - wry [RUST]: 0.40.1
    - tao [RUST]: 0.28.0
    - tauri-cli [RUST]: 2.0.0-beta.17
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 2.0.0-beta.9

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../src

Stack trace

No response

Additional context

  1. Position values are inconsistent window.outer_position returns PhysicalPosition<i32> while WebviewWindowBuilder::new().position() expects f64
  2. Using window.set_position(position); right after creating it move it to correct position
pewsheen commented 3 months ago

WebviewWindowBuilder::new().position() expects a LogicalPosition. Could you try offsetting the position and then convert it to the LogicalPosition to see if it's correct?

It may look like:

let mut position = window.outer_position().unwrap();
println!("Physical position: {:?}", position);
position.x += 30;
position.y += 30;
let logical_position = position.to_logical::<f64>(window.scale_factor().unwrap());
println!("Logical position: {:?}", &logical_position);
let _window =
      WebviewWindowBuilder::new(&app_handle, "main1", WebviewUrl::App("/index.html".into()))
          .position(logical_position.x as f64, logical_position.y as f64) // 30px offset x+y
          .build();