Skip to content

On windows tray position on click doesn't respect scale factor #295

@quolpr

Description

@quolpr

Windows and Mac Os tray position values are different. The display's mac os scale factor applies, while for windows not.

On Windows, I get these values:

Tray pos: PhysicalPosition { x: 6140.0, y: 3920.0 }
Monitor { name: Some("\\\\.\\DISPLAY1"), size: PhysicalSize { width: 3456, height: 2160 }, position: PhysicalPosition { x: 0, y: 0 }, scale_factor: 2.0 }

(the tray is located at the bottom right of the screen)

On mac os I get this value:

Tray pos: PhysicalPosition { x: 1952.0, y: 74.0 }
Monitor { name: Some("Monitor #41040"), size: PhysicalSize { width: 3456, height: 2234 }, position: PhysicalPosition { x: 0, y: 0 }, scale_factor: 2.0 }

(the tray is located at the top right of the screen)

So you can notice that for the same screen size(see Monitor) the tray pos differs(for windows it should be divided by scale_factor)

Here is the code that uses Tauri:

#![cfg_attr(
  all(not(debug_assertions), target_os = "windows"),
  windows_subsystem = "windows"
)]

use tauri::Window;
use tauri::{Manager, SystemTray, SystemTrayEvent, WindowBuilder};

#[derive(thiserror::Error, Debug)]
pub enum WindowPosError {
  #[error("Tauri error")]
  Disconnect(#[from] tauri::Error),
  #[error("Failed to get monitor")]
  FailedToGetMonitor(),
}

fn handle_window_position(win: Window) -> Result<(), WindowPosError> {
  let monitor = win
    .current_monitor()?
    .ok_or(WindowPosError::FailedToGetMonitor())?;

  println!("{:?}", monitor);

  Ok(())
}

fn main() {
  let tray = SystemTray::new();

  tauri::Builder::default()
    .system_tray(tray)
    .on_system_tray_event(|app, event| match event {
      SystemTrayEvent::LeftClick { position, .. } => {
        println!("Tray pos: {:?}", position);

        if let Some(win) = app.get_window("main") {
          handle_window_position(win);
        }
      }
      SystemTrayEvent::RightClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a right click");
      }
      _ => {}
    })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

Sorry for the rough code. And it uses Tauri, but I hope you get the idea.

Tauri version is 1.0.0-beta.8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions