Skip to content

Commit 8c574e4

Browse files
committed
mac/common: fix window position on multi monitor setups
on multi monitor setups the position of the window was wrongly computed when the position was forced, eg the calculated rectangle was screen relative but the function expected absolute coordinates. this is a regression from 50042f5 and #15500, where support for --force-window-position was introduced. this worked in simple one monitor setups and in some cases on multi monitor setups by sheer coincidence. calculate the absolute position of the window by taking the screen into account when needed. Fixes #16929
1 parent 233e896 commit 8c574e4

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

video/out/mac/window.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ class Window: NSWindow, NSWindowDelegate {
7878

7979
// workaround for an AppKit bug where the NSWindow can't be placed on a
8080
// none Main screen NSScreen outside the Main screen's frame bounds
81-
if let wantedScreen = screen, screen != NSScreen.main {
82-
var absoluteWantedOrigin = contentRect.origin
83-
absoluteWantedOrigin.x += wantedScreen.frame.origin.x
84-
absoluteWantedOrigin.y += wantedScreen.frame.origin.y
81+
if screen != NSScreen.main {
82+
let absolutePos = absolutePosition(for: contentRect, screen: screen)
8583

86-
if absoluteWantedOrigin != self.frame.origin {
87-
self.setFrameOrigin(absoluteWantedOrigin)
84+
if absolutePos.origin != self.frame.origin {
85+
self.setFrameOrigin(absolutePos.origin)
8886
}
8987
}
9088

@@ -342,11 +340,11 @@ class Window: NSWindow, NSWindowDelegate {
342340
}
343341
}
344342

345-
func updateFrame(_ rect: NSRect) {
343+
func updateFrame(_ rect: NSRect, _ screen: NSScreen? = nil) {
346344
if rect != frame {
347345
unfsContentFrame = rect
348346
if !isInFullscreen {
349-
let cRect = frameRect(forContentRect: rect)
347+
let cRect = absolutePosition(for: frameRect(forContentRect: rect), screen: screen)
350348
setFrame(cRect, display: true)
351349
common.windowDidUpdateFrame()
352350
}
@@ -371,6 +369,14 @@ class Window: NSWindow, NSWindowDelegate {
371369
if keepAspect { contentAspectRatio = unfsContentFrame.size }
372370
}
373371

372+
func absolutePosition(for rect: NSRect, screen: NSScreen?) -> NSRect {
373+
guard let tScreen = screen else { return rect }
374+
var absolutePosition = rect
375+
absolutePosition.origin.x += tScreen.frame.origin.x
376+
absolutePosition.origin.y += tScreen.frame.origin.y
377+
return absolutePosition
378+
}
379+
374380
func centeredContentSize(for rect: NSRect, size sz: NSSize) -> NSRect {
375381
let cRect = contentRect(forFrameRect: rect)
376382
let dx = (cRect.size.width - sz.width) / 2

video/out/mac_common.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MacCommon: Common {
4747
let previousActiveApp = getActiveApp()
4848
initApp()
4949

50-
let (_, wr, forcePosition) = getInitProperties(vo)
50+
let (screen, wr, forcePosition) = getInitProperties(vo)
5151
guard let layer = self.layer else {
5252
log.error("Something went wrong, no MetalLayer was initialized")
5353
exit(1)
@@ -60,7 +60,7 @@ class MacCommon: Common {
6060
}
6161

6262
if forcePosition {
63-
window?.updateFrame(wr)
63+
window?.updateFrame(wr, screen)
6464
} else if option.vo.auto_window_resize {
6565
window?.updateSize(wr.size)
6666
}

0 commit comments

Comments
 (0)