diff --git a/MonitorControl.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/MonitorControl.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/MonitorControl.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/MonitorControl/AppDelegate.swift b/MonitorControl/AppDelegate.swift index 04a14bd..fa97301 100644 --- a/MonitorControl/AppDelegate.swift +++ b/MonitorControl/AppDelegate.swift @@ -153,7 +153,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.displays.append(display) let monitorMenuItem = NSMenuItem() - monitorMenuItem.title = "\(name)" + monitorMenuItem.title = "\(display.getFriendlyName())" if asSubMenu { monitorMenuItem.submenu = monitorSubMenu } @@ -177,6 +177,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { // subscribe KeyTap event listener NotificationCenter.default.addObserver(self, selector: #selector(handleListenForChanged), name: NSNotification.Name(Utils.PrefKeys.listenFor.rawValue), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(handleShowContrastChanged), name: NSNotification.Name(Utils.PrefKeys.showContrast.rawValue), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleFriendlyNameChanged), name: NSNotification.Name(Utils.PrefKeys.friendlyName.rawValue), object: nil) // subscribe Audio output detector (AMCoreAudio) AMCoreAudio.NotificationCenter.defaultCenter.subscribe(self, eventType: AudioHardwareEvent.self, dispatchQueue: DispatchQueue.main) @@ -226,6 +227,10 @@ extension AppDelegate: MediaKeyTapDelegate { self.updateDisplays() } + @objc func handleFriendlyNameChanged() { + self.updateDisplays() + } + private func startOrRestartMediaKeyTap() { var keys: [MediaKey] diff --git a/MonitorControl/Display.swift b/MonitorControl/Display.swift index c3cca81..49dd685 100644 --- a/MonitorControl/Display.swift +++ b/MonitorControl/Display.swift @@ -137,6 +137,14 @@ class Display { return max == 0 ? 100 : max } + func setFriendlyName(_ value: String) { + self.prefs.set(value, forKey: "friendlyName-\(self.identifier)") + } + + func getFriendlyName() -> String { + return self.prefs.string(forKey: "friendlyName-\(self.identifier)") ?? self.name + } + private func showOsd(command: DDC.Command, value: Int) { guard let manager = OSDManager.sharedManager() as? OSDManager else { return diff --git a/MonitorControl/Support/Utils.swift b/MonitorControl/Support/Utils.swift index 1482d09..225dc57 100644 --- a/MonitorControl/Support/Utils.swift +++ b/MonitorControl/Support/Utils.swift @@ -151,6 +151,9 @@ class Utils: NSObject { /// Change Brightness/Volume for all screens case allScreens + + /// Friendly name changed + case friendlyName } /// Keys for the value of listenFor option diff --git a/MonitorControl/UI/Base.lproj/Main.storyboard b/MonitorControl/UI/Base.lproj/Main.storyboard index 3d9ff53..507f4ea 100644 --- a/MonitorControl/UI/Base.lproj/Main.storyboard +++ b/MonitorControl/UI/Base.lproj/Main.storyboard @@ -79,7 +79,7 @@ - + @@ -91,13 +91,13 @@ - + - + - + @@ -167,6 +167,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -181,7 +217,7 @@ - + @@ -214,7 +250,7 @@ - + @@ -233,7 +269,7 @@ - + @@ -247,11 +283,11 @@ - + - + @@ -284,7 +320,7 @@ - + @@ -319,7 +355,7 @@ - + diff --git a/MonitorControl/UI/FriendlyNameCellView.swift b/MonitorControl/UI/FriendlyNameCellView.swift new file mode 100644 index 0000000..e2ee87e --- /dev/null +++ b/MonitorControl/UI/FriendlyNameCellView.swift @@ -0,0 +1,33 @@ +import Cocoa +import os.log + +class FriendlyNameCellView: NSTableCellView { + var display: Display? + let prefs = UserDefaults.standard + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + } + + @IBAction func valueChanged(_ sender: NSTextFieldCell) { + if let display = display { + let newValue = sender.stringValue + let originalValue = display.getFriendlyName() + + if newValue.isEmpty { + self.textField?.stringValue = originalValue + return + } + + if newValue != originalValue, + !newValue.isEmpty { + display.setFriendlyName(newValue) + NotificationCenter.default.post(name: Notification.Name(Utils.PrefKeys.friendlyName.rawValue), object: nil) + + #if DEBUG + os_log("Value changed for friendly name: %{public}@", type: .info, "from `\(originalValue)` to `\(newValue)`") + #endif + } + } + } +} diff --git a/MonitorControl/UI/en.lproj/Main.strings b/MonitorControl/UI/en.lproj/Main.strings index 4e79ac8..373fdfa 100644 --- a/MonitorControl/UI/en.lproj/Main.strings +++ b/MonitorControl/UI/en.lproj/Main.strings @@ -8,6 +8,9 @@ /* Class = "NSTableColumn"; headerCell.title = "Display Name"; ObjectID = "CHc-s5-4MN"; */ "CHc-s5-4MN.headerCell.title" = "Display Name"; +/* Class = "NSTableColumn"; headerCell.title = "Friendly Name"; ObjectID = "uoI-1J-RdD"; */ +"uoI-1J-RdD.headerCell.title" = "Friendly Name"; + /* Class = "NSTextFieldCell"; title = "Keys"; ObjectID = "Dcz-GG-1li"; */ "Dcz-GG-1li.title" = "Keys"; diff --git a/MonitorControl/View Controllers/DisplayPrefsViewController.swift b/MonitorControl/View Controllers/DisplayPrefsViewController.swift index c0fc65d..8cfb2d7 100644 --- a/MonitorControl/View Controllers/DisplayPrefsViewController.swift +++ b/MonitorControl/View Controllers/DisplayPrefsViewController.swift @@ -13,6 +13,7 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController enum DisplayCell: String { case checkbox case name + case friendlyName case identifier case vendor case model @@ -96,14 +97,18 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController text = display.name cellType = DisplayCell.name } else if tableColumn == tableView.tableColumns[2] { + // Friendly Name + text = display.getFriendlyName() + cellType = DisplayCell.friendlyName + } else if tableColumn == tableView.tableColumns[3] { // Identifier text = "\(display.identifier)" cellType = DisplayCell.identifier - } else if tableColumn == tableView.tableColumns[3] { + } else if tableColumn == tableView.tableColumns[4] { // Vendor text = display.identifier.vendorNumber.map { String(format: "0x%02X", $0) } ?? NSLocalizedString("unknown", comment: "unknown vendor") cellType = DisplayCell.vendor - } else if tableColumn == tableView.tableColumns[4] { + } else if tableColumn == tableView.tableColumns[5] { // Model text = display.identifier.modelNumber.map { String(format: "0x%02X", $0) } ?? NSLocalizedString("unknown", comment: "unknown model") cellType = DisplayCell.model @@ -117,6 +122,13 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController } return cell } + } else if cellType == DisplayCell.friendlyName { + if let cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: nil) as? FriendlyNameCellView { + cell.display = display + cell.textField?.stringValue = text + cell.textField?.isEditable = true + return cell + } } else { if let cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: nil) as? NSTableCellView { cell.textField?.stringValue = text