Update menu when diplays are added/removed

This commit is contained in:
Asger Hautop Drewsen 2017-01-12 06:19:16 +01:00
parent b072626dad
commit a6422c017a

View file

@ -15,6 +15,8 @@ struct Display {
var serial: String
}
var app : AppDelegate! = nil
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@ -27,6 +29,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let keycode = UInt16(0x07)
var monitorItems : [NSMenuItem] = []
var displays : [Display] = []
@IBAction func quitClicked(_ sender: AnyObject) {
@ -70,9 +73,26 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
app = self
statusItem.title = ""
statusItem.menu = statusMenu
acquirePrivileges();
CGDisplayRegisterReconfigurationCallback({_,_,_ in app.updateDisplays()}, nil)
updateDisplays()
}
func updateDisplays() {
for m in monitorItems {
statusMenu.removeItem(m)
}
monitorItems = []
displays = []
sleep(1)
var firstDisplay : Display? = nil
for s in NSScreen.screens()! {
@ -209,6 +229,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
monitorMenuItem.title = "\(name)";
monitorMenuItem.submenu = monitorSubMenu;
monitorItems.append(monitorMenuItem)
statusMenu.insertItem(monitorMenuItem, at: i)
if firstDisplay == nil {
@ -216,8 +237,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
acquirePrivileges();
if firstDisplay == nil {
return
}