mac: fix window geometry calculation on secondary screens

this is a regression of af26402, where we changed the geometry
calculation to use the visible frame and consider its origin. though the
origin is not screen relative but relative to the main screen. the rest
of the code expects a screen relative rectangle. because of that windows
would be placed out of the screen bounds when not on the main screen.

recalculate to origin to be screen relative and use those values for the
rest of the calculations. to make the code a bit more comprehensible
be a bit more explicit about what is done where with temporary variables
and comments. also move the mp_rect calculation that moves the origin
and flips the y position to a separate function, so we can reuse it
later.
This commit is contained in:
der richter 2021-04-24 19:17:36 +02:00
parent 01482c63ca
commit d3b7732aaa
1 changed files with 25 additions and 18 deletions

View File

@ -438,31 +438,38 @@ class Common: NSObject {
NSScreen.main
}
func getWindowGeometry(forScreen targetScreen: NSScreen,
videoOut vo: UnsafeMutablePointer<vo>) -> NSRect {
let r = targetScreen.convertRectToBacking(targetScreen.frame)
let targetFrame =
(mpv?.macOpts.macos_geometry_calculation ?? Int32(FRAME_VISIBLE)) == FRAME_VISIBLE ?
targetScreen.visibleFrame : targetScreen.frame
let rv = targetScreen.convertRectToBacking(targetFrame)
func calculateRect(forScreen screen: NSScreen, visible: Bool) -> mp_rect {
let r = screen.convertRectToBacking(screen.frame)
let targetFrame = visible ? screen.visibleFrame : screen.frame
let rv = screen.convertRectToBacking(targetFrame)
// convert origin to be relative to target screen
var originY = rv.origin.y - r.origin.y
var originX = rv.origin.x - r.origin.x
// flip the y origin, mp_rect expects the origin at the top-left
// macOS' windowing system operates from the bottom-left
var originY = r.size.height - rv.origin.y - rv.size.height
var screenRC: mp_rect = mp_rect(x0: Int32(rv.origin.x),
y0: Int32(originY),
x1: Int32(rv.origin.x + rv.size.width),
y1: Int32(originY + rv.size.height))
originY = -(originY + rv.size.height)
return mp_rect(x0: Int32(originX),
y0: Int32(originY),
x1: Int32(originX + rv.size.width),
y1: Int32(originY + rv.size.height))
}
func getWindowGeometry(forScreen screen: NSScreen,
videoOut vo: UnsafeMutablePointer<vo>) -> NSRect {
let visible: Bool = (mpv?.macOpts.macos_geometry_calculation ?? Int32(FRAME_VISIBLE)) == FRAME_VISIBLE
var screenRC: mp_rect = calculateRect(forScreen: screen, visible: visible)
var geo: vo_win_geometry = vo_win_geometry()
vo_calc_window_geometry2(vo, &screenRC, Double(targetScreen.backingScaleFactor), &geo)
vo_calc_window_geometry2(vo, &screenRC, Double(screen.backingScaleFactor), &geo)
vo_apply_window_geometry(vo, &geo)
// flip the y origin again
let height = CGFloat(geo.win.y1 - geo.win.y0)
originY = r.size.height - CGFloat(geo.win.y0) - height
let wr = NSMakeRect(CGFloat(geo.win.x0), originY,
CGFloat(geo.win.x1 - geo.win.x0), height)
return targetScreen.convertRectFromBacking(wr)
let width = CGFloat(geo.win.x1 - geo.win.x0)
// flip the y origin again
let y = CGFloat(-geo.win.y1)
let x = CGFloat(geo.win.x0)
return screen.convertRectFromBacking(NSMakeRect(x, y, width, height))
}
func getInitProperties(_ vo: UnsafeMutablePointer<vo>) -> (MPVHelper, NSScreen, NSRect) {