🔧 Fix cyclomatic complexity warning

This commit is contained in:
Guillaume Broder 2019-06-05 00:01:55 +02:00
parent bfc91fd5ec
commit afd82e3a29
No known key found for this signature in database
GPG key ID: 66FB02D063D9E08F
2 changed files with 80 additions and 66 deletions

View file

@ -97,7 +97,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<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">
<rect key="frame" x="0.0" y="0.0" width="635" height="76"/>
<rect key="frame" x="0.0" y="0.0" width="687" height="76"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@ -134,6 +134,37 @@
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn width="49" minWidth="40" maxWidth="1000" id="xFw-if-3FU">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="DDC">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
<customCell key="dataCell" alignment="left" id="Zxr-PK-6Na"/>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="1tR-yk-dKV" customClass="ButtonCellView" customModule="MonitorControl" customModuleProvider="target">
<rect key="frame" x="53" y="1" width="49" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4a9-J0-ruf">
<rect key="frame" x="0.0" y="0.0" width="49" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="overlaps" state="on" inset="2" id="NBx-SJ-4YC">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="buttonToggled:" target="1tR-yk-dKV" id="i9X-im-3gv"/>
</connections>
</button>
</subviews>
<connections>
<outlet property="button" destination="4a9-J0-ruf" id="UGd-xq-uRA"/>
</connections>
</tableCellView>
</prototypeCellViews>
</tableColumn>
<tableColumn width="140" minWidth="40" maxWidth="1000" id="CHc-s5-4MN">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Name">
<font key="font" metaFont="smallSystem"/>
@ -148,7 +179,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="iYj-Ud-yGU">
<rect key="frame" x="53" y="1" width="140" height="17"/>
<rect key="frame" x="105" y="1" width="140" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qsc-jC-vJQ">
@ -181,7 +212,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="aGt-d9-Qcx" customClass="FriendlyNameCellView" customModule="MonitorControl" customModuleProvider="target">
<rect key="frame" x="196" y="1" width="140" height="17"/>
<rect key="frame" x="248" y="1" width="140" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CB1-x8-d9G">
@ -217,7 +248,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="vw4-Us-VHk">
<rect key="frame" x="339" y="1" width="80" height="17"/>
<rect key="frame" x="391" y="1" width="80" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="z5b-Gr-EmC">
@ -250,7 +281,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="eVa-Md-Avh">
<rect key="frame" x="422" y="1" width="80" height="17"/>
<rect key="frame" x="474" y="1" width="80" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NK1-rO-I0Z">
@ -283,7 +314,7 @@
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
<prototypeCellViews>
<tableCellView id="C4F-JT-GfP">
<rect key="frame" x="505" y="1" width="128" height="17"/>
<rect key="frame" x="557" y="1" width="128" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="4Vl-7d-eft">
@ -320,7 +351,7 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
<tableHeaderView key="headerView" id="ckY-Px-mJn">
<rect key="frame" x="0.0" y="0.0" width="635" height="23"/>
<rect key="frame" x="0.0" y="0.0" width="687" height="23"/>
<autoresizingMask key="autoresizingMask"/>
</tableHeaderView>
</scrollView>

View file

@ -10,8 +10,9 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
let prefs = UserDefaults.standard
var displays: [Display] = []
enum DisplayCell: String {
enum DisplayColumn: Int {
case checkbox
case ddc
case name
case friendlyName
case identifier
@ -50,27 +51,10 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
for screen in NSScreen.screens {
let id = screen.displayID
// Disable built-in displays.
if screen.isBuiltin {
let display = Display(id, name: screen.displayName ?? NSLocalizedString("Unknown", comment: "Unknown display name"), isEnabled: false)
self.displays.append(display)
continue
}
guard let ddc = DDC(for: id) else {
os_log("Display “%{public}@” cannot be controlled via DDC.", screen.displayName ?? NSLocalizedString("Unknown", comment: "Unknown display name"))
continue
}
guard let edid = ddc.edid() else {
os_log("Cannot read EDID information for display “%{public}@”.", screen.displayName ?? NSLocalizedString("Unknown", comment: "Unknown display name"))
continue
}
let name = Utils.getDisplayName(forEdid: edid)
let name = screen.displayName ?? NSLocalizedString("Unknown", comment: "Unknown display name")
let isEnabled = (prefs.object(forKey: "\(id)-state") as? Bool) ?? true
let display = Display(id, name: name, isEnabled: isEnabled)
let display = Display(id, name: name, isBuiltin: screen.isBuiltin, isEnabled: isEnabled)
self.displays.append(display)
}
@ -84,58 +68,57 @@ class DisplayPrefsViewController: NSViewController, MASPreferencesViewController
// MARK: - Table delegate
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cellType = DisplayCell.checkbox
var checked = false
var text = ""
guard let tableColumn = tableColumn,
let columnIndex = tableView.tableColumns.firstIndex(of: tableColumn),
let column = DisplayColumn(rawValue: columnIndex) else {
return nil
}
let display = self.displays[row]
if tableColumn == tableView.tableColumns[0] {
// Checkbox
checked = display.isEnabled
} else if tableColumn == tableView.tableColumns[1] {
// Name
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[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[5] {
// Model
text = display.identifier.modelNumber.map { String(format: "0x%02X", $0) } ?? NSLocalizedString("Unknown", comment: "Unknown model")
cellType = DisplayCell.model
}
if cellType == DisplayCell.checkbox {
if let cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: nil) as? ButtonCellView {
cell.button.state = checked ? .on : .off
switch column {
case .checkbox:
if let cell = tableView.makeView(withIdentifier: tableColumn.identifier, owner: nil) as? ButtonCellView {
cell.display = display
if display.name == "Mac built-in Display" {
cell.button.isEnabled = false
}
cell.button.state = display.isEnabled ? .on : .off
cell.button.isEnabled = !display.isBuiltin
return cell
}
} else if cellType == DisplayCell.friendlyName {
if let cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: nil) as? FriendlyNameCellView {
case .ddc:
if let cell = tableView.makeView(withIdentifier: tableColumn.identifier, owner: nil) as? ButtonCellView {
cell.display = display
cell.textField?.stringValue = text
cell.button.state = DDC(for: display.identifier) != nil ? .on : .off
cell.button.isEnabled = false
return cell
}
case .friendlyName:
if let cell = tableView.makeView(withIdentifier: tableColumn.identifier, owner: nil) as? FriendlyNameCellView {
cell.display = display
cell.textField?.stringValue = display.getFriendlyName()
cell.textField?.isEditable = true
return cell
}
} else {
if let cell = tableView.makeView(withIdentifier: (tableColumn?.identifier)!, owner: nil) as? NSTableCellView {
cell.textField?.stringValue = text
default:
if let cell = tableView.makeView(withIdentifier: tableColumn.identifier, owner: nil) as? NSTableCellView {
cell.textField?.stringValue = self.getText(for: column, with: display)
return cell
}
}
return nil
}
private func getText(for column: DisplayColumn, with display: Display) -> String {
switch column {
case .name:
return display.name
case .identifier:
return "\(display.identifier)"
case .vendor:
return display.identifier.vendorNumber.map { String(format: "0x%02X", $0) } ?? NSLocalizedString("Unknown", comment: "Unknown vendor")
case .model:
return display.identifier.modelNumber.map { String(format: "0x%02X", $0) } ?? NSLocalizedString("Unknown", comment: "Unknown model")
default:
return ""
}
}
}