From d3b7732aaa3c9c94bee94937c8bb69cddfc09a2d Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 24 Apr 2021 19:17:36 +0200 Subject: 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. --- video/out/mac/common.swift | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'video/out') diff --git a/video/out/mac/common.swift b/video/out/mac/common.swift index 7ede81cbda..f439088642 100644 --- a/video/out/mac/common.swift +++ b/video/out/mac/common.swift @@ -438,31 +438,38 @@ class Common: NSObject { NSScreen.main } - func getWindowGeometry(forScreen targetScreen: NSScreen, - videoOut vo: UnsafeMutablePointer) -> 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) -> 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) -> (MPVHelper, NSScreen, NSRect) { -- cgit v1.2.3