如何通知我的Cocoa应用程序NSScreen分辨率变化?

时间:2017-03-15 09:42:32

标签: objective-c swift cocoa screen-resolution

我正在使用浮动窗口制作Cocoa应用程序。浮动窗口应该以主屏幕为中心,大小为主屏幕的1/4。以下Swift是我的应用程序的精髓:

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var panel: NSPanel!
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let screenRect:CGRect = NSScreen.main()!.frame
        panel = NSPanel(
            contentRect: NSRect(
                x: screenRect.width/4,
                y: screenRect.height/4,
                width:  screenRect.width/2,
                height: screenRect.height/2
            ),
            styleMask: NSWindowStyleMask.nonactivatingPanel,
            backing: NSBackingStoreType.buffered,
            defer: false
        )
        panel.alphaValue = 0.5
        panel.backgroundColor = NSColor.red
        panel.level = Int(CGWindowLevelForKey(CGWindowLevelKey.maximumWindow))
        panel.orderFront(nil)
    }
}

这会产生一个这样的面板:

CORRECTLY POSITIONED

主屏幕分辨率更改时出现问题。显示此信息的一种方法是转到系统偏好设置>显示并设置分辨率为" Scaled"和更多空间"。完成后,面板看起来像这样:

INCORRECTLY POSITIONED AFTER RESOLUTION CHANGE

如您所见,更改分辨率后面板的位置不正确。我希望面板保持其位置:居中和屏幕尺寸的1/4。为此,我要检测屏幕分辨率(即frame的{​​{1}}属性)何时发生变化,以便我可以更改面板的大小和位置。

NSScreen的{​​{1}}属性发生变化时,是否会触发某些事件?或者有不同的方法来处理这个问题吗?

1 个答案:

答案 0 :(得分:3)

在@Adolfo的帮助下,这有效:

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var panel: NSPanel!

    func getPanelRect() -> NSRect {
        let screenRect:CGRect = NSScreen.main()!.frame
        return NSRect(
            x: screenRect.width/4,
            y: screenRect.height/4,
            width:  screenRect.width/2,
            height: screenRect.height/2
        )
    }

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        panel = NSPanel(
            contentRect: self.getPanelRect(),
            styleMask: NSWindowStyleMask.nonactivatingPanel,
            backing: NSBackingStoreType.buffered,
            defer: false
        )
        panel.alphaValue = 0.5
        panel.backgroundColor = NSColor.red
        panel.level = Int(CGWindowLevelForKey(CGWindowLevelKey.maximumWindow))
        panel.orderFront(nil)

        NotificationCenter.default.addObserver(
            forName: NSNotification.Name.NSApplicationDidChangeScreenParameters,
            object: NSApplication.shared(),
            queue: OperationQueue.main
        ) { notification -> Void in
            print("screen parameters changed")
            self.panel.setFrame(self.getPanelRect(), display: true)
        }
    }
}