Skip to content

Commit

Permalink
Improve macOS 11 compatibility (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 11, 2020
1 parent dfb7457 commit dbe099f
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 60 deletions.
14 changes: 7 additions & 7 deletions Example/AccountsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let AccountsPreferenceViewController: () -> PreferencePane = {
let paneView = Preferences.Pane(
identifier: .accounts,
title: "Accounts",
toolbarIcon: NSImage(named: NSImage.userAccountsName)!
toolbarIcon: NSImage(systemSymbolName: "person.crop.circle", accessibilityDescription: "Accounts preferences")!
) {
AccountsView()
}
Expand All @@ -32,23 +32,23 @@ struct AccountsView: View {
var body: some View {
Preferences.Container(contentWidth: contentWidth) {
Preferences.Section(title: "Permissions:") {
Toggle("Allow user to administer this computer", isOn: self.$isOn1)
Toggle("Allow user to administer this computer", isOn: $isOn1)
Text("Administrator has root access to this machine.")
.preferenceDescription()
Toggle("Allow user to access every file", isOn: self.$isOn2)
Toggle("Allow user to access every file", isOn: $isOn2)
}
Preferences.Section(title: "Show scroll bars:") {
Picker("", selection: self.$selection1) {
Picker("", selection: $selection1) {
Text("When scrolling").tag(0)
Text("Always").tag(1)
}
.labelsHidden()
.pickerStyle(RadioGroupPickerStyle())
}
Preferences.Section(label: {
Toggle("Some toggle", isOn: self.$isOn3)
Toggle("Some toggle", isOn: $isOn3)
}) {
Picker("", selection: self.$selection2) {
Picker("", selection: $selection2) {
Text("Automatic").tag(0)
Text("Manual").tag(1)
}
Expand All @@ -58,7 +58,7 @@ struct AccountsView: View {
.preferenceDescription()
}
Preferences.Section(title: "Preview mode:") {
Picker("", selection: self.$selection3) {
Picker("", selection: $selection3) {
Text("Automatic").tag(0)
Text("Manual").tag(1)
}
Expand Down
2 changes: 1 addition & 1 deletion Example/AdvancedPreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Preferences
final class AdvancedPreferenceViewController: NSViewController, PreferencePane {
let preferencePaneIdentifier = Preferences.PaneIdentifier.advanced
let preferencePaneTitle = "Advanced"
let toolbarItemIcon = NSImage(named: NSImage.advancedName)!
let toolbarItemIcon = NSImage(systemSymbolName: "gearshape.2", accessibilityDescription: "Advanced preferences")!

override var nibName: NSNib.Name? { "AdvancedPreferenceViewController" }

Expand Down
8 changes: 4 additions & 4 deletions Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Preferences

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

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

Expand All @@ -20,8 +20,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

lazy var preferences: [PreferencePane] = [
GeneralPreferenceViewController(),
AdvancedPreferenceViewController(),
AccountsPreferenceViewController()
AccountsPreferenceViewController(),
AdvancedPreferenceViewController()
]

lazy var preferencesWindowController = PreferencesWindowController(
Expand Down
2 changes: 1 addition & 1 deletion Example/GeneralPreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Preferences
final class GeneralPreferenceViewController: NSViewController, PreferencePane {
let preferencePaneIdentifier = Preferences.PaneIdentifier.general
let preferencePaneTitle = "General"
let toolbarItemIcon = NSImage(named: NSImage.preferencesGeneralName)!
let toolbarItemIcon = NSImage(systemSymbolName: "gearshape", accessibilityDescription: "General preferences")!

override var nibName: NSNib.Name? { "GeneralPreferenceViewController" }

Expand Down
15 changes: 15 additions & 0 deletions Example/Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Cocoa

extension NSApplication {
/// Relaunch the app.
func relaunch() {
let configuration = NSWorkspace.OpenConfiguration()
configuration.createsNewApplicationInstance = true

NSWorkspace.shared.openApplication(at: Bundle.main.bundleURL, configuration: configuration) { _, _ in
DispatchQueue.main.async {
NSApp.terminate(nil)
}
}
}
}
15 changes: 0 additions & 15 deletions Example/util.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.3
import PackageDescription

let package = Package(
Expand Down
2 changes: 1 addition & 1 deletion Preferences.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ Pod::Spec.new do |s|
s.authors = { 'Sindre Sorhus' => 'sindresorhus@gmail.com' }
s.source = { :git => 'https://github.com/sindresorhus/Preferences.git', :tag => "v#{s.version}" }
s.source_files = 'Sources/**/*.swift'
s.swift_version = '5.2'
s.swift_version = '5.3'
s.platform = :macos, '10.10'
end
28 changes: 15 additions & 13 deletions Preferences.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 53;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -13,7 +13,7 @@
50A412F62196E87900E4A5A8 /* SegmentedControlStyleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A412F52196E87900E4A5A8 /* SegmentedControlStyleViewController.swift */; };
50A412F82196EAF200E4A5A8 /* ToolbarItemStyleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50A412F72196EAF200E4A5A8 /* ToolbarItemStyleViewController.swift */; };
56392002228D453100F8804C /* Localization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56392001228D453100F8804C /* Localization.swift */; };
E3194E4122573FF3006FE775 /* util.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3194E4022573FF3006FE775 /* util.swift */; };
E3194E4122573FF3006FE775 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3194E4022573FF3006FE775 /* Utilities.swift */; };
E34E9EEA20E6149B002F8F86 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E34E9EE920E6149B002F8F86 /* AppDelegate.swift */; };
E34E9EEC20E6149D002F8F86 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E34E9EEB20E6149D002F8F86 /* Assets.xcassets */; };
E34E9EEF20E6149D002F8F86 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = E34E9EED20E6149D002F8F86 /* MainMenu.xib */; };
Expand All @@ -31,7 +31,7 @@
E752207623FCACC800117D41 /* Section.swift in Sources */ = {isa = PBXBuildFile; fileRef = E752207523FCACC800117D41 /* Section.swift */; };
E7E6B56823C7DB2000E51429 /* Container.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7E6B56723C7DB2000E51429 /* Container.swift */; };
OBJ_19 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* PreferencesWindowController.swift */; };
OBJ_20 /* util.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* util.swift */; };
OBJ_20 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* Utilities.swift */; };
OBJ_27 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -67,7 +67,7 @@
50A412F72196EAF200E4A5A8 /* ToolbarItemStyleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = ToolbarItemStyleViewController.swift; sourceTree = "<group>"; usesTabs = 1; };
50F9AE80225773A0005D7DC3 /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; lineEnding = 0; path = readme.md; sourceTree = "<group>"; usesTabs = 1; };
56392001228D453100F8804C /* Localization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Localization.swift; sourceTree = "<group>"; usesTabs = 1; };
E3194E4022573FF3006FE775 /* util.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = util.swift; sourceTree = "<group>"; usesTabs = 1; };
E3194E4022573FF3006FE775 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Utilities.swift; sourceTree = "<group>"; usesTabs = 1; };
E34E9EE720E6149B002F8F86 /* PreferencesExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PreferencesExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
E34E9EE920E6149B002F8F86 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = AppDelegate.swift; sourceTree = "<group>"; usesTabs = 1; };
E34E9EEB20E6149D002F8F86 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -84,7 +84,7 @@
E74F0B0623C2BD6D00B16841 /* Pane.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Pane.swift; sourceTree = "<group>"; usesTabs = 1; };
E752207523FCACC800117D41 /* Section.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Section.swift; sourceTree = "<group>"; usesTabs = 1; };
E7E6B56723C7DB2000E51429 /* Container.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Container.swift; sourceTree = "<group>"; usesTabs = 1; };
OBJ_10 /* util.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = util.swift; sourceTree = "<group>"; usesTabs = 1; };
OBJ_10 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Utilities.swift; sourceTree = "<group>"; usesTabs = 1; };
OBJ_6 /* Package.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = Package.swift; sourceTree = "<group>"; usesTabs = 1; };
OBJ_9 /* PreferencesWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = PreferencesWindowController.swift; sourceTree = "<group>"; usesTabs = 1; };
"Preferences::Preferences::Product" /* Preferences.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Preferences.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -120,7 +120,7 @@
E34E9F0220E61BC0002F8F86 /* AdvancedPreferenceViewController.swift */,
E34E9F0420E61C06002F8F86 /* AdvancedPreferenceViewController.xib */,
E7059F1123C2AC3700F84762 /* AccountsView.swift */,
E3194E4022573FF3006FE775 /* util.swift */,
E3194E4022573FF3006FE775 /* Utilities.swift */,
E34E9EEB20E6149D002F8F86 /* Assets.xcassets */,
E34E9EF020E6149D002F8F86 /* Info.plist */,
);
Expand Down Expand Up @@ -170,7 +170,7 @@
E7E6B56723C7DB2000E51429 /* Container.swift */,
E752207523FCACC800117D41 /* Section.swift */,
56392001228D453100F8804C /* Localization.swift */,
OBJ_10 /* util.swift */,
OBJ_10 /* Utilities.swift */,
);
name = Preferences;
path = Sources/Preferences;
Expand Down Expand Up @@ -236,7 +236,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0940;
LastUpgradeCheck = 1130;
LastUpgradeCheck = 1200;
TargetAttributes = {
E34E9EE620E6149B002F8F86 = {
CreatedOnToolsVersion = 9.4.1;
Expand All @@ -253,7 +253,7 @@
};
};
buildConfigurationList = OBJ_2 /* Build configuration list for PBXProject "Preferences" */;
compatibilityVersion = "Xcode 11.4";
compatibilityVersion = "Xcode 12.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
Expand Down Expand Up @@ -317,7 +317,7 @@
E34E9EEA20E6149B002F8F86 /* AppDelegate.swift in Sources */,
E7059F1323C2AC7400F84762 /* AccountsView.swift in Sources */,
E34E9F0320E61BC0002F8F86 /* AdvancedPreferenceViewController.swift in Sources */,
E3194E4122573FF3006FE775 /* util.swift in Sources */,
E3194E4122573FF3006FE775 /* Utilities.swift in Sources */,
502B68E72254947B00789D9F /* PreferencesStyle+UserDefaults.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -337,7 +337,7 @@
50A412F42196B70100E4A5A8 /* PreferencesStyle.swift in Sources */,
E3D64112248648EE0090D28E /* Preferences.swift in Sources */,
E34E9EFB20E61557002F8F86 /* PreferencePane.swift in Sources */,
OBJ_20 /* util.swift in Sources */,
OBJ_20 /* Utilities.swift in Sources */,
E7E6B56823C7DB2000E51429 /* Container.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -405,7 +405,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.sindresorhus.PreferencesExample;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -445,7 +445,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.sindresorhus.PreferencesExample;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -540,6 +540,7 @@
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -588,6 +589,7 @@
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1130"
LastUpgradeVersion = "1200"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1130"
LastUpgradeVersion = "1200"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 2 additions & 2 deletions Sources/Preferences/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension Preferences {

return VStack(alignment: .preferenceSectionLabel) {
ForEach(0..<sections.count, id: \.self) { index in
self.viewForSection(sections, index: index)
viewForSection(sections, index: index)
}
}
.modifier(Section.LabelWidthModifier(maxWidth: $maxLabelWidth))
Expand All @@ -59,7 +59,7 @@ extension Preferences {
Divider()
// Strangely doesn't work without width being specified. Probably because of custom alignment.
.frame(width: CGFloat(contentWidth), height: 20.0)
.alignmentGuide(.preferenceSectionLabel) { $0[.leading] + self.maxLabelWidth }
.alignmentGuide(.preferenceSectionLabel) { $0[.leading] + maxLabelWidth }
}
} else {
sections[index]
Expand Down
8 changes: 5 additions & 3 deletions Sources/Preferences/PreferencesTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Cocoa
final class PreferencesTabViewController: NSViewController, PreferencesStyleControllerDelegate {
private var activeTab: Int?
private var preferencePanes = [PreferencePane]()
private var style: Preferences.Style?
internal var preferencePanesCount: Int { preferencePanes.count }
private var preferencesStyleController: PreferencesStyleController!
private var isKeepingWindowCentered: Bool { preferencesStyleController.isKeepingWindowCentered }
Expand All @@ -22,6 +23,7 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont

func configure(preferencePanes: [PreferencePane], style: Preferences.Style) {
self.preferencePanes = preferencePanes
self.style = style
children = preferencePanes

let toolbar = NSToolbar(identifier: "PreferencesToolbar")
Expand Down Expand Up @@ -128,8 +130,8 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont
from: fromViewController,
to: toViewController,
options: options
) {
self.activeChildViewConstraints = toViewController.view.constrainToSuperviewBounds()
) { [self] in
activeChildViewConstraints = toViewController.view.constrainToSuperviewBounds()
}
}

Expand Down Expand Up @@ -207,7 +209,7 @@ extension PreferencesTabViewController: NSToolbarDelegate {
}

func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
toolbarItemIdentifiers
style == .segmentedControl ? [] : toolbarItemIdentifiers
}

public func toolbar(
Expand Down
6 changes: 5 additions & 1 deletion Sources/Preferences/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public final class PreferencesWindowController: NSWindowController {
}
}()

if #available(macOS 11.0, *), style == .toolbarItems {
window.toolbarStyle = .preference
}

tabViewController.isAnimated = animated
tabViewController.configure(preferencePanes: preferencePanes, style: style)
updateToolbarVisibility()
Expand Down Expand Up @@ -98,7 +102,7 @@ public final class PreferencesWindowController: NSWindowController {

private func restoreWindowPosition() {
guard
let window = self.window,
let window = window,
let screenContainingWindow = window.screen
else {
return
Expand Down
4 changes: 2 additions & 2 deletions Sources/Preferences/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ extension Preferences {

func body(content: Content) -> some View {
content
.onPreferenceChange(LabelWidthPreferenceKey.self) { maxWidth in
self.maxWidth = maxWidth
.onPreferenceChange(LabelWidthPreferenceKey.self) { newMaxWidth in
maxWidth = newMaxWidth
}
}
}
Expand Down
File renamed without changes.

0 comments on commit dbe099f

Please sign in to comment.