Skip to content

Commit afd82e3

Browse files
committed
🔧 Fix cyclomatic complexity warning
1 parent bfc91fd commit afd82e3

File tree

2 files changed

+80
-66
lines changed

2 files changed

+80
-66
lines changed

MonitorControl/UI/Base.lproj/Main.storyboard

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
9898
<subviews>
9999
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnReordering="NO" columnResizing="NO" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" typeSelect="NO" rowSizeStyle="automatic" headerView="ckY-Px-mJn" viewBased="YES" id="dyo-uY-pMe">
100-
<rect key="frame" x="0.0" y="0.0" width="635" height="76"/>
100+
<rect key="frame" x="0.0" y="0.0" width="687" height="76"/>
101101
<autoresizingMask key="autoresizingMask"/>
102102
<size key="intercellSpacing" width="3" height="2"/>
103103
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -134,6 +134,37 @@
134134
</tableCellView>
135135
</prototypeCellViews>
136136
</tableColumn>
137+
<tableColumn width="49" minWidth="40" maxWidth="1000" id="xFw-if-3FU">
138+
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="DDC">
139+
<font key="font" metaFont="smallSystem"/>
140+
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
141+
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
142+
</tableHeaderCell>
143+
<customCell key="dataCell" alignment="left" id="Zxr-PK-6Na"/>
144+
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
145+
<prototypeCellViews>
146+
<tableCellView id="1tR-yk-dKV" customClass="ButtonCellView" customModule="MonitorControl" customModuleProvider="target">
147+
<rect key="frame" x="53" y="1" width="49" height="17"/>
148+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
149+
<subviews>
150+
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4a9-J0-ruf">
151+
<rect key="frame" x="0.0" y="0.0" width="49" height="18"/>
152+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
153+
<buttonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="overlaps" state="on" inset="2" id="NBx-SJ-4YC">
154+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
155+
<font key="font" metaFont="system"/>
156+
</buttonCell>
157+
<connections>
158+
<action selector="buttonToggled:" target="1tR-yk-dKV" id="i9X-im-3gv"/>
159+
</connections>
160+
</button>
161+
</subviews>
162+
<connections>
163+
<outlet property="button" destination="4a9-J0-ruf" id="UGd-xq-uRA"/>
164+
</connections>
165+
</tableCellView>
166+
</prototypeCellViews>
167+
</tableColumn>
137168
<tableColumn width="140" minWidth="40" maxWidth="1000" id="CHc-s5-4MN">
138169
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Name">
139170
<font key="font" metaFont="smallSystem"/>
@@ -148,7 +179,7 @@
148179
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
149180
<prototypeCellViews>
150181
<tableCellView id="iYj-Ud-yGU">
151-
<rect key="frame" x="53" y="1" width="140" height="17"/>
182+
<rect key="frame" x="105" y="1" width="140" height="17"/>
152183
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
153184
<subviews>
154185
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qsc-jC-vJQ">
@@ -181,7 +212,7 @@
181212
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
182213
<prototypeCellViews>
183214
<tableCellView id="aGt-d9-Qcx" customClass="FriendlyNameCellView" customModule="MonitorControl" customModuleProvider="target">
184-
<rect key="frame" x="196" y="1" width="140" height="17"/>
215+
<rect key="frame" x="248" y="1" width="140" height="17"/>
185216
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
186217
<subviews>
187218
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CB1-x8-d9G">
@@ -217,7 +248,7 @@
217248
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
218249
<prototypeCellViews>
219250
<tableCellView id="vw4-Us-VHk">
220-
<rect key="frame" x="339" y="1" width="80" height="17"/>
251+
<rect key="frame" x="391" y="1" width="80" height="17"/>
221252
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
222253
<subviews>
223254
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="z5b-Gr-EmC">
@@ -250,7 +281,7 @@
250281
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
251282
<prototypeCellViews>
252283
<tableCellView id="eVa-Md-Avh">
253-
<rect key="frame" x="422" y="1" width="80" height="17"/>
284+
<rect key="frame" x="474" y="1" width="80" height="17"/>
254285
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
255286
<subviews>
256287
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NK1-rO-I0Z">
@@ -283,7 +314,7 @@
283314
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
284315
<prototypeCellViews>
285316
<tableCellView id="C4F-JT-GfP">
286-
<rect key="frame" x="505" y="1" width="128" height="17"/>
317+
<rect key="frame" x="557" y="1" width="128" height="17"/>
287318
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
288319
<subviews>
289320
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4Vl-7d-eft">
@@ -320,7 +351,7 @@
320351
<autoresizingMask key="autoresizingMask"/>
321352
</scroller>
322353
<tableHeaderView key="headerView" id="ckY-Px-mJn">
323-
<rect key="frame" x="0.0" y="0.0" width="635" height="23"/>
354+
<rect key="frame" x="0.0" y="0.0" width="687" height="23"/>
324355
<autoresizingMask key="autoresizingMask"/>
325356
</tableHeaderView>
326357
</scrollView>

MonitorControl/View Controllers/DisplayPrefsViewController.swift

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)