Skip to content

Commit

Permalink
Add segmented control style (#6)
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
DivineDominion authored and sindresorhus committed Apr 3, 2019
1 parent e0ef252 commit 3b62df8
Show file tree
Hide file tree
Showing 21 changed files with 774 additions and 114 deletions.
3 changes: 2 additions & 1 deletion Example/AdvancedPreferenceViewController.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Cocoa
import Preferences

final class AdvancedPreferenceViewController: NSViewController, Preferenceable {
final class AdvancedPreferenceViewController: NSViewController, PreferencePane {
let preferencePaneIdentifier: PreferencePaneIdentifier = .advanced
let toolbarItemTitle = "Advanced"
let toolbarItemIcon = NSImage(named: NSImage.advancedName)!

Expand Down
20 changes: 12 additions & 8 deletions Example/AdvancedPreferenceViewController.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -13,20 +13,24 @@
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="580" height="380"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="485" height="380"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="bc0-pc-Sw8">
<rect key="frame" x="244" y="178" width="92" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textField horizontalHuggingPriority="750" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="bc0-pc-Sw8">
<rect key="frame" x="198" y="178" width="89" height="24"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Advanced" id="2Ht-pt-tsZ">
<font key="font" metaFont="system" size="20"/>
<color key="textColor" name="secondaryLabelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<constraints>
<constraint firstItem="bc0-pc-Sw8" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="200" id="0an-7b-VLp"/>
<constraint firstAttribute="trailing" secondItem="bc0-pc-Sw8" secondAttribute="trailing" constant="200" id="4gj-WO-sBw"/>
<constraint firstItem="bc0-pc-Sw8" firstAttribute="top" secondItem="c22-O7-iKe" secondAttribute="top" constant="178" id="YGc-8e-maw"/>
<constraint firstAttribute="bottom" secondItem="bc0-pc-Sw8" secondAttribute="bottom" constant="178" id="rkd-zg-yQS"/>
</constraints>
</customView>
</objects>
</document>
46 changes: 38 additions & 8 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
import Cocoa
import Preferences

extension PreferencePaneIdentifier {
static let general = PreferencePaneIdentifier("general")
static let advanced = PreferencePaneIdentifier("advanced")
}

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet private var window: NSWindow!

let preferencesWindowController = PreferencesWindowController(
viewControllers: [
GeneralPreferenceViewController(),
AdvancedPreferenceViewController()
]
)
var preferencesStyle: PreferencesStyle {
get {
return PreferencesStyle.preferencesStyleFromUserDefaults()
}
set(newPreferencesStyle) {
newPreferencesStyle.storeInUserDefaults()
}
}

lazy var preferences: [PreferencePane] = [
GeneralPreferenceViewController(),
AdvancedPreferenceViewController()
]
lazy var preferencesWindowController = PreferencesWindowController(preferencePanes: self.preferences, style: self.preferencesStyle, animated: true)

func applicationWillFinishLaunching(_ notification: Notification) {
window.orderOut(self)
}

func applicationDidFinishLaunching(_ notification: Notification) {
preferencesWindowController.showWindow()
preferencesWindowController.show(preferencePane: .advanced)
}

@IBAction private func preferencesMenuItemActionHandler(_ sender: NSMenuItem) {
preferencesWindowController.showWindow()
preferencesWindowController.show()
}

@IBAction private func switchStyle(_ sender: Any) {
self.preferencesStyle = (self.preferencesStyle == .segmentedControl)
? .toolbarItems
: .segmentedControl
relaunch()
}
}

private func relaunch() {
let appBundleIdentifier = Bundle.main.bundleIdentifier!
NSWorkspace.shared.launchApplication(
withBundleIdentifier: appBundleIdentifier,
options: NSWorkspace.LaunchOptions.newInstance,
additionalEventParamDescriptor: nil,
launchIdentifier: nil)
NSApp.terminate(nil)
}
7 changes: 4 additions & 3 deletions Example/GeneralPreferenceViewController.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Cocoa
import Preferences

final class GeneralPreferenceViewController: NSViewController, Preferenceable {
let toolbarItemTitle = "General"
let toolbarItemIcon = NSImage(named: NSImage.preferencesGeneralName)!
final class GeneralPreferenceViewController: NSViewController, PreferencePane {
let preferencePaneIdentifier: PreferencePaneIdentifier = .general
let toolbarItemTitle = "General"
let toolbarItemIcon = NSImage(named: NSImage.preferencesGeneralName)!

override var nibName: NSNib.Name? {
return "GeneralPreferenceViewController"
Expand Down
33 changes: 25 additions & 8 deletions Example/GeneralPreferenceViewController.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -13,20 +13,37 @@
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="480" height="272"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="666" height="272"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Xc1-oC-jbN">
<rect key="frame" x="204" y="124" width="72" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textField horizontalHuggingPriority="750" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Xc1-oC-jbN">
<rect key="frame" x="298" y="124" width="70" height="24"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="General" id="kEa-Ts-PrR">
<font key="font" metaFont="system" size="20"/>
<color key="textColor" name="secondaryLabelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AeJ-us-BJO">
<rect key="frame" x="241" y="88" width="185" height="32"/>
<buttonCell key="cell" type="push" title="Switch Preference Style" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="pPv-HR-smI">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="switchStyle:" target="-1" id="mbG-7D-LUH"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="AeJ-us-BJO" firstAttribute="centerX" secondItem="Xc1-oC-jbN" secondAttribute="centerX" id="0D5-yt-qbT"/>
<constraint firstItem="AeJ-us-BJO" firstAttribute="top" secondItem="Xc1-oC-jbN" secondAttribute="bottom" constant="8" id="PDh-Co-vOC"/>
<constraint firstAttribute="bottom" secondItem="Xc1-oC-jbN" secondAttribute="bottom" constant="124" id="Ygh-kW-pw7"/>
<constraint firstAttribute="trailing" secondItem="Xc1-oC-jbN" secondAttribute="trailing" constant="300" id="dPQ-fn-EVx"/>
<constraint firstItem="Xc1-oC-jbN" firstAttribute="top" secondItem="c22-O7-iKe" secondAttribute="top" constant="124" id="obX-e4-hYJ"/>
<constraint firstItem="Xc1-oC-jbN" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="300" id="sQd-RT-oXo"/>
</constraints>
<point key="canvasLocation" x="55" y="154"/>
</customView>
</objects>
</document>
39 changes: 39 additions & 0 deletions Example/PreferencesStyle+UserDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation
import Preferences

// Helpers to write styles to and read them from UserDefaults.

extension PreferencesStyle: RawRepresentable {
public var rawValue: Int {
switch self {
case .toolbarItems:
return 0
case .segmentedControl:
return 1
}
}

public init?(rawValue: Int) {
switch rawValue {
case 0:
self = .toolbarItems
case 1:
self = .segmentedControl
default:
return nil
}
}
}

extension PreferencesStyle {
static var userDefaultsKey: String { return "preferencesStyle" }

static func preferencesStyleFromUserDefaults(_ userDefaults: UserDefaults = .standard) -> PreferencesStyle {
return PreferencesStyle(rawValue: userDefaults.integer(forKey: PreferencesStyle.userDefaultsKey))
?? .toolbarItems
}

func storeInUserDefaults(_ userDefaults: UserDefaults = .standard) {
userDefaults.set(self.rawValue, forKey: PreferencesStyle.userDefaultsKey)
}
}

0 comments on commit 3b62df8

Please sign in to comment.