Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Example/AdvancedPreferenceViewController.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Cocoa
import Settings

final class AdvancedSettingsViewController: NSViewController, SettingsPane {
let paneIdentifier = Settings.PaneIdentifier.advanced
let paneTitle = "Advanced"
let toolbarItemIcon = NSImage(systemSymbolName: "gearshape.2", accessibilityDescription: "Advanced settings")!

@IBOutlet private var fontLabel: NSTextField!
private var font = NSFont.systemFont(ofSize: 14)
Expand Down
36 changes: 36 additions & 0 deletions Example/AdvancedPreferenceViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="14490.70"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="AdvancedPreferenceViewController" customModule="PreferencesExample" customModuleProvider="target">
<connections>
<outlet property="view" destination="c22-O7-iKe" id="buC-Sz-vlD"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="485" height="380"/>
<subviews>
<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>
53 changes: 13 additions & 40 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,55 +1,28 @@
import Cocoa
import Settings

extension Settings.PaneIdentifier {
static let general = Self("general")
static let accounts = Self("accounts")
static let advanced = Self("advanced")
}

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

private var settingsStyle: Settings.Style {
get { .settingsStyleFromUserDefaults() }
set {
newValue.storeInUserDefaults()
}
}

private lazy var panes: [SettingsPane] = [
GeneralSettingsViewController(),
AccountsSettingsViewController(),
AdvancedSettingsViewController()
]

private lazy var settingsWindowController = SettingsWindowController(
panes: panes,
style: settingsStyle,
animated: true,
hidesToolbarForSingleItem: true
)

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

func applicationDidFinishLaunching(_ notification: Notification) {
settingsWindowController.show(pane: .accounts)
}

@IBAction private func settingsMenuItemActionHandler(_ sender: NSMenuItem) {
settingsWindowController.show()
}

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

Task {
try! await NSApp.relaunch() // swiftlint:disable:this force_try
}
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)
}
18 changes: 18 additions & 0 deletions Example/GeneralPreferenceViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Cocoa
import Preferences

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"
}

override func viewDidLoad() {
super.viewDidLoad()

// Setup stuff here
}
}
49 changes: 49 additions & 0 deletions Example/GeneralPreferenceViewController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="14490.70"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="GeneralPreferenceViewController" customModule="PreferencesExample" customModuleProvider="target">
<connections>
<outlet property="view" destination="c22-O7-iKe" id="ZQe-UH-LCX"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="c22-O7-iKe">
<rect key="frame" x="0.0" y="0.0" width="666" height="272"/>
<subviews>
<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)
}
}
Loading