@@ -10,8 +10,9 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
1010 let prefs = UserDefaults . standard
1111
1212 var displays : [ Display ] = [ ]
13- enum DisplayCell : String {
13+ enum DisplayColumn : Int {
1414 case checkbox
15+ case ddc
1516 case name
1617 case friendlyName
1718 case identifier
@@ -50,27 +51,10 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
5051 for screen in NSScreen . screens {
5152 let id = screen. displayID
5253
53- // Disable built-in displays.
54- if screen. isBuiltin {
55- let display = Display ( id, name: screen. displayName ?? NSLocalizedString ( " Unknown " , comment: " Unknown display name " ) , isEnabled: false )
56- self . displays. append ( display)
57- continue
58- }
59-
60- guard let ddc = DDC ( for: id) else {
61- os_log ( " Display “%{public}@” cannot be controlled via DDC. " , screen. displayName ?? NSLocalizedString ( " Unknown " , comment: " Unknown display name " ) )
62- continue
63- }
64-
65- guard let edid = ddc. edid ( ) else {
66- os_log ( " Cannot read EDID information for display “%{public}@”. " , screen. displayName ?? NSLocalizedString ( " Unknown " , comment: " Unknown display name " ) )
67- continue
68- }
69-
70- let name = Utils . getDisplayName ( forEdid: edid)
54+ let name = screen. displayName ?? NSLocalizedString ( " Unknown " , comment: " Unknown display name " )
7155 let isEnabled = ( prefs. object ( forKey: " \( id) -state " ) as? Bool ) ?? true
7256
73- let display = Display ( id, name: name, isEnabled: isEnabled)
57+ let display = Display ( id, name: name, isBuiltin : screen . isBuiltin , isEnabled: isEnabled)
7458 self . displays. append ( display)
7559 }
7660
@@ -84,58 +68,57 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
8468 // MARK: - Table delegate
8569
8670 func tableView( _ tableView: NSTableView , viewFor tableColumn: NSTableColumn ? , row: Int ) -> NSView ? {
87- var cellType = DisplayCell . checkbox
88- var checked = false
89- var text = " "
71+ guard let tableColumn = tableColumn,
72+ let columnIndex = tableView. tableColumns. firstIndex ( of: tableColumn) ,
73+ let column = DisplayColumn ( rawValue: columnIndex) else {
74+ return nil
75+ }
9076 let display = self . displays [ row]
9177
92- if tableColumn == tableView. tableColumns [ 0 ] {
93- // Checkbox
94- checked = display. isEnabled
95- } else if tableColumn == tableView. tableColumns [ 1 ] {
96- // Name
97- text = display. name
98- cellType = DisplayCell . name
99- } else if tableColumn == tableView. tableColumns [ 2 ] {
100- // Friendly Name
101- text = display. getFriendlyName ( )
102- cellType = DisplayCell . friendlyName
103- } else if tableColumn == tableView. tableColumns [ 3 ] {
104- // Identifier
105- text = " \( display. identifier) "
106- cellType = DisplayCell . identifier
107- } else if tableColumn == tableView. tableColumns [ 4 ] {
108- // Vendor
109- text = display. identifier. vendorNumber. map { String ( format: " 0x%02X " , $0) } ?? NSLocalizedString ( " Unknown " , comment: " Unknown vendor " )
110- cellType = DisplayCell . vendor
111- } else if tableColumn == tableView. tableColumns [ 5 ] {
112- // Model
113- text = display. identifier. modelNumber. map { String ( format: " 0x%02X " , $0) } ?? NSLocalizedString ( " Unknown " , comment: " Unknown model " )
114- cellType = DisplayCell . model
115- }
116- if cellType == DisplayCell . checkbox {
117- if let cell = tableView. makeView ( withIdentifier: ( tableColumn? . identifier) !, owner: nil ) as? ButtonCellView {
118- cell. button. state = checked ? . on : . off
78+ switch column {
79+ case . checkbox:
80+ if let cell = tableView. makeView ( withIdentifier: tableColumn. identifier, owner: nil ) as? ButtonCellView {
81+ cell. display = display
82+ cell. button. state = display. isEnabled ? . on : . off
83+ cell. button. isEnabled = !display. isBuiltin
84+ return cell
85+ }
86+ case . ddc:
87+ if let cell = tableView. makeView ( withIdentifier: tableColumn. identifier, owner: nil ) as? ButtonCellView {
11988 cell. display = display
120- if display. name == " Mac built-in Display " {
121- cell. button. isEnabled = false
122- }
89+ cell. button. state = DDC ( for: display. identifier) != nil ? . on : . off
90+ cell. button. isEnabled = false
12391 return cell
12492 }
125- } else if cellType == DisplayCell . friendlyName {
126- if let cell = tableView. makeView ( withIdentifier: ( tableColumn? . identifier) ! , owner: nil ) as? FriendlyNameCellView {
93+ case . friendlyName:
94+ if let cell = tableView. makeView ( withIdentifier: tableColumn. identifier, owner: nil ) as? FriendlyNameCellView {
12795 cell. display = display
128- cell. textField? . stringValue = text
96+ cell. textField? . stringValue = display . getFriendlyName ( )
12997 cell. textField? . isEditable = true
13098 return cell
13199 }
132- } else {
133- if let cell = tableView. makeView ( withIdentifier: ( tableColumn? . identifier) ! , owner: nil ) as? NSTableCellView {
134- cell. textField? . stringValue = text
100+ default :
101+ if let cell = tableView. makeView ( withIdentifier: tableColumn. identifier, owner: nil ) as? NSTableCellView {
102+ cell. textField? . stringValue = self . getText ( for : column , with : display )
135103 return cell
136104 }
137105 }
138106
139107 return nil
140108 }
109+
110+ private func getText( for column: DisplayColumn , with display: Display ) -> String {
111+ switch column {
112+ case . name:
113+ return display. name
114+ case . identifier:
115+ return " \( display. identifier) "
116+ case . vendor:
117+ return display. identifier. vendorNumber. map { String ( format: " 0x%02X " , $0) } ?? NSLocalizedString ( " Unknown " , comment: " Unknown vendor " )
118+ case . model:
119+ return display. identifier. modelNumber. map { String ( format: " 0x%02X " , $0) } ?? NSLocalizedString ( " Unknown " , comment: " Unknown model " )
120+ default :
121+ return " "
122+ }
123+ }
141124}
0 commit comments