Skip to content

Commit 110336c

Browse files
polw1lucasfernog
andauthored
fix(macOS): fix incorrect window position on multi-monitor setups (#15250)
* fix(macOS): fix incorrect window position on multi-monitor setups * avoid 'flash' on current monitor when targeting another display for fullscreen * simplify fullscreen monitor selection * change file --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent c00a3db commit 110336c

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime-wry": patch:bug
3+
"tauri": patch:bug
4+
---
5+
6+
Fix initial window position when positioning it to another monitor.

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,25 @@ impl From<Position> for PositionWrapper {
692692
}
693693
}
694694

695+
#[cfg(desktop)]
696+
fn find_monitor_for_position(
697+
monitors: impl Iterator<Item = MonitorHandle>,
698+
window_position: TaoPosition,
699+
) -> Option<MonitorHandle> {
700+
monitors.into_iter().find(|m| {
701+
let monitor_pos = m.position();
702+
let monitor_size = m.size();
703+
704+
// type annotations required for 32bit targets.
705+
let window_position = window_position.to_physical::<i32>(m.scale_factor());
706+
707+
monitor_pos.x <= window_position.x
708+
&& window_position.x < monitor_pos.x + monitor_size.width as i32
709+
&& monitor_pos.y <= window_position.y
710+
&& window_position.y < monitor_pos.y + monitor_size.height as i32
711+
})
712+
}
713+
695714
#[derive(Debug, Clone)]
696715
pub struct UserAttentionTypeWrapper(pub TaoUserAttentionType);
697716

@@ -4489,18 +4508,7 @@ fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
44894508
#[cfg(desktop)]
44904509
if window_builder.prevent_overflow.is_some() || window_builder.center {
44914510
let monitor = if let Some(window_position) = &window_builder.inner.window.position {
4492-
event_loop.available_monitors().find(|m| {
4493-
let monitor_pos = m.position();
4494-
let monitor_size = m.size();
4495-
4496-
// type annotations required for 32bit targets.
4497-
let window_position = window_position.to_physical::<i32>(m.scale_factor());
4498-
4499-
monitor_pos.x <= window_position.x
4500-
&& window_position.x < monitor_pos.x + monitor_size.width as i32
4501-
&& monitor_pos.y <= window_position.y
4502-
&& window_position.y < monitor_pos.y + monitor_size.height as i32
4503-
})
4511+
find_monitor_for_position(event_loop.available_monitors(), *window_position)
45044512
} else {
45054513
event_loop.primary_monitor()
45064514
};
@@ -4567,12 +4575,38 @@ fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
45674575
}
45684576
};
45694577

4578+
#[cfg(any(target_os = "macos", target_os = "linux"))]
4579+
let (initial_position, is_fullscreen) = (
4580+
window_builder.inner.window.position,
4581+
window_builder.inner.window.fullscreen.is_some(),
4582+
);
4583+
4584+
// If fullscreen is requested with an explicit position, resolve the target
4585+
// monitor up front so the window is created fullscreen on that display.
4586+
#[cfg(any(target_os = "macos", target_os = "linux"))]
4587+
if let (true, Some(position)) = (is_fullscreen, initial_position) {
4588+
if let Some(target_monitor) =
4589+
find_monitor_for_position(event_loop.available_monitors(), position)
4590+
{
4591+
window_builder.inner.window.fullscreen = Some(Fullscreen::Borderless(Some(target_monitor)));
4592+
}
4593+
}
4594+
45704595
let window = window_builder
45714596
.inner
45724597
.build(event_loop)
45734598
.inspect_err(|e| log::error!("Error creating window: {e:?}"))
45744599
.map_err(|_| Error::CreateWindow)?;
45754600

4601+
// On macOS, `with_position` uses the content origin; the title bar is added
4602+
// above it. `set_outer_position` is needed for precise window placement.
4603+
#[cfg(target_os = "macos")]
4604+
if !is_fullscreen {
4605+
if let Some(position) = initial_position {
4606+
window.set_outer_position(position);
4607+
}
4608+
}
4609+
45764610
#[cfg(feature = "tracing")]
45774611
{
45784612
drop(window_create_span);

0 commit comments

Comments
 (0)