PreferencesExtension // Clang-Format.
This commit is contained in:
parent
e5d8e8fe98
commit
db555640eb
|
@ -23,8 +23,8 @@ import SwiftUI
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
extension Preferences {
|
extension Preferences {
|
||||||
/**
|
/**
|
||||||
Function builder for `Preferences` components used in order to restrict types of child views to be of type `Section`.
|
Function builder for `Preferences` components used in order to restrict types of child views to be of type `Section`.
|
||||||
*/
|
*/
|
||||||
@resultBuilder
|
@resultBuilder
|
||||||
public enum SectionBuilder {
|
public enum SectionBuilder {
|
||||||
public static func buildBlock(_ sections: Section...) -> [Section] {
|
public static func buildBlock(_ sections: Section...) -> [Section] {
|
||||||
|
@ -33,8 +33,8 @@ extension Preferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A view which holds `Preferences.Section` views and does all the alignment magic similar to `NSGridView` from AppKit.
|
A view which holds `Preferences.Section` views and does all the alignment magic similar to `NSGridView` from AppKit.
|
||||||
*/
|
*/
|
||||||
public struct Container: View {
|
public struct Container: View {
|
||||||
private let sectionBuilder: () -> [Section]
|
private let sectionBuilder: () -> [Section]
|
||||||
private let contentWidth: Double
|
private let contentWidth: Double
|
||||||
|
@ -42,15 +42,15 @@ extension Preferences {
|
||||||
@State private var maximumLabelWidth = 0.0
|
@State private var maximumLabelWidth = 0.0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Creates an instance of container component, which handles layout of stacked `Preferences.Section` views.
|
Creates an instance of container component, which handles layout of stacked `Preferences.Section` views.
|
||||||
|
|
||||||
Custom alignment requires content width to be specified beforehand.
|
Custom alignment requires content width to be specified beforehand.
|
||||||
|
|
||||||
- Parameters:
|
- Parameters:
|
||||||
- contentWidth: A fixed width of the container's content (excluding paddings).
|
- contentWidth: A fixed width of the container's content (excluding paddings).
|
||||||
- minimumLabelWidth: A minimum width for labels within this container. By default, it will fit to the largest label.
|
- minimumLabelWidth: A minimum width for labels within this container. By default, it will fit to the largest label.
|
||||||
- builder: A view builder that creates `Preferences.Section`'s of this container.
|
- builder: A view builder that creates `Preferences.Section`'s of this container.
|
||||||
*/
|
*/
|
||||||
public init(
|
public init(
|
||||||
contentWidth: Double,
|
contentWidth: Double,
|
||||||
minimumLabelWidth: Double = 0,
|
minimumLabelWidth: Double = 0,
|
||||||
|
|
|
@ -112,12 +112,12 @@ struct Localization {
|
||||||
]
|
]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Returns the localized version of the given string.
|
Returns the localized version of the given string.
|
||||||
|
|
||||||
- Parameter identifier: Identifier of the string to localize.
|
- Parameter identifier: Identifier of the string to localize.
|
||||||
|
|
||||||
- Note: If the system's locale can't be determined, the English localization of the string will be returned.
|
- Note: If the system's locale can't be determined, the English localization of the string will be returned.
|
||||||
*/
|
*/
|
||||||
static subscript(identifier: Identifier) -> String {
|
static subscript(identifier: Identifier) -> String {
|
||||||
// Force-unwrapped since all of the involved code is under our control.
|
// Force-unwrapped since all of the involved code is under our control.
|
||||||
let localizedDict = Localization.localizedStrings[identifier]!
|
let localizedDict = Localization.localizedStrings[identifier]!
|
||||||
|
|
|
@ -25,18 +25,18 @@ import SwiftUI
|
||||||
/// Acts as type-eraser for `Preferences.Pane<T>`.
|
/// Acts as type-eraser for `Preferences.Pane<T>`.
|
||||||
public protocol PreferencePaneConvertible {
|
public protocol PreferencePaneConvertible {
|
||||||
/**
|
/**
|
||||||
Convert `self` to equivalent `PreferencePane`.
|
Convert `self` to equivalent `PreferencePane`.
|
||||||
*/
|
*/
|
||||||
func asPreferencePane() -> PreferencePane
|
func asPreferencePane() -> PreferencePane
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
extension Preferences {
|
extension Preferences {
|
||||||
/**
|
/**
|
||||||
Create a SwiftUI-based preference pane.
|
Create a SwiftUI-based preference pane.
|
||||||
|
|
||||||
SwiftUI equivalent of the `PreferencePane` protocol.
|
SwiftUI equivalent of the `PreferencePane` protocol.
|
||||||
*/
|
*/
|
||||||
public struct Pane<Content: View>: View, PreferencePaneConvertible {
|
public struct Pane<Content: View>: View, PreferencePaneConvertible {
|
||||||
let identifier: PaneIdentifier
|
let identifier: PaneIdentifier
|
||||||
let title: String
|
let title: String
|
||||||
|
@ -63,8 +63,8 @@ extension Preferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Hosting controller enabling `Preferences.Pane` to be used alongside AppKit `NSViewController`'s.
|
Hosting controller enabling `Preferences.Pane` to be used alongside AppKit `NSViewController`'s.
|
||||||
*/
|
*/
|
||||||
public final class PaneHostingController<Content: View>: NSHostingController<Content>, PreferencePane {
|
public final class PaneHostingController<Content: View>: NSHostingController<Content>, PreferencePane {
|
||||||
public let preferencePaneIdentifier: PaneIdentifier
|
public let preferencePaneIdentifier: PaneIdentifier
|
||||||
public let preferencePaneTitle: String
|
public let preferencePaneTitle: String
|
||||||
|
@ -102,8 +102,8 @@ extension Preferences {
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
extension View {
|
extension View {
|
||||||
/**
|
/**
|
||||||
Applies font and color for a label used for describing a preference.
|
Applies font and color for a label used for describing a preference.
|
||||||
*/
|
*/
|
||||||
public func preferenceDescription() -> some View {
|
public func preferenceDescription() -> some View {
|
||||||
font(.system(size: 11.0))
|
font(.system(size: 11.0))
|
||||||
// TODO: Use `.foregroundStyle` when targeting macOS 12.
|
// TODO: Use `.foregroundStyle` when targeting macOS 12.
|
||||||
|
|
|
@ -97,17 +97,17 @@ public final class PreferencesWindowController: NSWindowController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Show the preferences window and brings it to front.
|
Show the preferences window and brings it to front.
|
||||||
|
|
||||||
If you pass a `Preferences.PaneIdentifier`, the window will activate the corresponding tab.
|
If you pass a `Preferences.PaneIdentifier`, the window will activate the corresponding tab.
|
||||||
|
|
||||||
- Parameter preferencePane: Identifier of the preference pane to display, or `nil` to show the tab that was open when the user last closed the window.
|
- Parameter preferencePane: Identifier of the preference pane to display, or `nil` to show the tab that was open when the user last closed the window.
|
||||||
|
|
||||||
- Note: Unless you need to open a specific pane, prefer not to pass a parameter at all or `nil`.
|
- Note: Unless you need to open a specific pane, prefer not to pass a parameter at all or `nil`.
|
||||||
|
|
||||||
- See `close()` to close the window again.
|
- See `close()` to close the window again.
|
||||||
- See `showWindow(_:)` to show the window without the convenience of activating the app.
|
- See `showWindow(_:)` to show the window without the convenience of activating the app.
|
||||||
*/
|
*/
|
||||||
public func show(preferencePane preferenceIdentifier: Preferences.PaneIdentifier? = nil) {
|
public func show(preferencePane preferenceIdentifier: Preferences.PaneIdentifier? = nil) {
|
||||||
if let preferenceIdentifier = preferenceIdentifier {
|
if let preferenceIdentifier = preferenceIdentifier {
|
||||||
tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
|
tabViewController.activateTab(preferenceIdentifier: preferenceIdentifier, animated: false)
|
||||||
|
@ -168,8 +168,8 @@ extension PreferencesWindowController {
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
extension PreferencesWindowController {
|
extension PreferencesWindowController {
|
||||||
/**
|
/**
|
||||||
Create a preferences window from only SwiftUI-based preference panes.
|
Create a preferences window from only SwiftUI-based preference panes.
|
||||||
*/
|
*/
|
||||||
public convenience init(
|
public convenience init(
|
||||||
panes: [PreferencePaneConvertible],
|
panes: [PreferencePaneConvertible],
|
||||||
style: Preferences.Style = .toolbarItems,
|
style: Preferences.Style = .toolbarItems,
|
||||||
|
|
|
@ -23,13 +23,13 @@ import SwiftUI
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
extension Preferences {
|
extension Preferences {
|
||||||
/**
|
/**
|
||||||
Represents a section with right-aligned title and optional bottom divider.
|
Represents a section with right-aligned title and optional bottom divider.
|
||||||
*/
|
*/
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
public struct Section: View {
|
public struct Section: View {
|
||||||
/**
|
/**
|
||||||
Preference key holding max width of section labels.
|
Preference key holding max width of section labels.
|
||||||
*/
|
*/
|
||||||
private struct LabelWidthPreferenceKey: PreferenceKey {
|
private struct LabelWidthPreferenceKey: PreferenceKey {
|
||||||
typealias Value = Double
|
typealias Value = Double
|
||||||
|
|
||||||
|
@ -42,8 +42,8 @@ extension Preferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Convenience overlay for finding a label's dimensions using `GeometryReader`.
|
Convenience overlay for finding a label's dimensions using `GeometryReader`.
|
||||||
*/
|
*/
|
||||||
private struct LabelOverlay: View {
|
private struct LabelOverlay: View {
|
||||||
var body: some View {
|
var body: some View {
|
||||||
GeometryReader { geometry in
|
GeometryReader { geometry in
|
||||||
|
@ -54,8 +54,8 @@ extension Preferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Convenience modifier for applying `LabelWidthPreferenceKey`.
|
Convenience modifier for applying `LabelWidthPreferenceKey`.
|
||||||
*/
|
*/
|
||||||
struct LabelWidthModifier: ViewModifier {
|
struct LabelWidthModifier: ViewModifier {
|
||||||
@Binding var maximumWidth: Double
|
@Binding var maximumWidth: Double
|
||||||
|
|
||||||
|
@ -73,15 +73,15 @@ extension Preferences {
|
||||||
public let verticalAlignment: VerticalAlignment
|
public let verticalAlignment: VerticalAlignment
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A section is responsible for controlling a single preference.
|
A section is responsible for controlling a single preference.
|
||||||
|
|
||||||
- Parameters:
|
- Parameters:
|
||||||
- bottomDivider: Whether to place a `Divider` after the section content. Default is `false`.
|
- bottomDivider: Whether to place a `Divider` after the section content. Default is `false`.
|
||||||
- verticalAlignement: The vertical alignment of the section content.
|
- verticalAlignement: The vertical alignment of the section content.
|
||||||
- verticalAlignment:
|
- verticalAlignment:
|
||||||
- label: A view describing preference handled by this section.
|
- label: A view describing preference handled by this section.
|
||||||
- content: A content view.
|
- content: A content view.
|
||||||
*/
|
*/
|
||||||
public init<Label: View, Content: View>(
|
public init<Label: View, Content: View>(
|
||||||
bottomDivider: Bool = false,
|
bottomDivider: Bool = false,
|
||||||
verticalAlignment: VerticalAlignment = .firstTextBaseline,
|
verticalAlignment: VerticalAlignment = .firstTextBaseline,
|
||||||
|
@ -98,15 +98,15 @@ extension Preferences {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Creates instance of section, responsible for controling single preference with `Text` as a `Label`.
|
Creates instance of section, responsible for controling single preference with `Text` as a `Label`.
|
||||||
|
|
||||||
- Parameters:
|
- Parameters:
|
||||||
- title: A string describing preference handled by this section.
|
- title: A string describing preference handled by this section.
|
||||||
- bottomDivider: Whether to place a `Divider` after the section content. Default is `false`.
|
- bottomDivider: Whether to place a `Divider` after the section content. Default is `false`.
|
||||||
- verticalAlignement: The vertical alignment of the section content.
|
- verticalAlignement: The vertical alignment of the section content.
|
||||||
- verticalAlignment:
|
- verticalAlignment:
|
||||||
- content: A content view.
|
- content: A content view.
|
||||||
*/
|
*/
|
||||||
public init<Content: View>(
|
public init<Content: View>(
|
||||||
title: String,
|
title: String,
|
||||||
bottomDivider: Bool = false,
|
bottomDivider: Bool = false,
|
||||||
|
|
|
@ -130,8 +130,8 @@ class UserInteractionPausableWindow: NSWindow {
|
||||||
@available(macOS 10.15, *)
|
@available(macOS 10.15, *)
|
||||||
extension View {
|
extension View {
|
||||||
/**
|
/**
|
||||||
Equivalent to `.eraseToAnyPublisher()` from the Combine framework.
|
Equivalent to `.eraseToAnyPublisher()` from the Combine framework.
|
||||||
*/
|
*/
|
||||||
func eraseToAnyView() -> AnyView {
|
func eraseToAnyView() -> AnyView {
|
||||||
AnyView(self)
|
AnyView(self)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue