SwiftUI-HiddenAPI

此包允许您使用各种隐藏的 SwiftUI 功能。兼容 macOS 12.0+,iOS 15.0+

为什么是 xcframework?

这公开了 SwiftUI 中用于隐藏功能的函数,这些函数不在 swiftinterface 文件中。需要对库进行修改,才能让 Swift 编译器识别这些符号,这就是为什么它是在 SPM 之外构建的,并作为 binaryTarget 链接到主 snapshots 包。有关更深入的解释,请参阅此处

警告

这些功能未通过稳定接口公开,可能会在操作系统版本更新中被删除或更改。请注意,此包仅在 macOS 14 / iOS 17 上进行了测试。某些功能可能在旧版本上不可用,但缺少 @available 注释。如果您在较低版本上发现任何崩溃/问题,请创建一个 issue。

概述

所有公开的函数都在 hidden 变量之后。 这样,当 Apple 将它们公开时,就不会发生名称冲突。

WithCurrentWindow

从环境中获取当前的 NSWindow。

struct WithCurrentWindowTestView: View {
    @Environment(\.hidden.withCurrentWindow) var withCurrentWindow
    
    var body: some View {
        Text("Hello")
            .task {
                withCurrentWindow { window in
                    guard let window else { return }
                    print("Hello from \(window.title)")
                }
            }
    }
}

OpenSettings

打开 SwiftUI 设置,就像打开一个窗口一样。适用于 macOS 14.0、13.0 及更低版本。

struct OpenSettingsTestView: View {
    @Environment(\.hidden.openSettings) var openSettings
    
    var body: some View {
        Button("Open Settings") {
            openSettings()
        }
    }
}

AppActions

在视图之外打开 SwiftUI 窗口、文档和设置。

class AppActionsTestDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        AppActions.openSettings()
        AppActions.openWindow(id: "TestWindow")
    }
}

FloatingWindowStyle

struct FloatingWindowStyleTestScene: Scene {
    var body: some Scene {
        WindowGroup {
            Text("Hello")
        }
        .windowStyle(.hidden.floating)
    }
}

Scene modifiers (场景修饰符)

示例

struct WindowBackgroundTestScene: Scene {
    var body: some Scene {
        WindowGroup {
            Text("Hello")
        }
        .hidden.windowBackground(.regularMaterial)
    }
}
/// Customize the window background. Use `.clear` for a transparent window, use `.*Material` for a blurred background
func windowBackground(_ shape: some ShapeStyle) -> some Scene
/// Disable automatic window opening behavior..
/// For example, when set to `.hidden`, launching the app or clicking the app icon won't create a new window of that scene.
/// Note that previously created windows may reappear.
func defaultVisibility(_ visibility: Visibility) -> some Scene
/// Determine if a window should be closed when the user tries to close the window.
/// Returning `true` will close the window, `false` will keep the window open.
func windowShouldClose(_ perform: @escaping () -> Bool) -> some Scene

View modifiers (视图修饰符)

示例

struct OpenSettingsTestView: View {
    var body: some View {
        Form {
            List {
                Text("Hello")
            }
            .hidden.accessoryBar {
                HStack(spacing: .zero) {
                    Button("Add", systemImage: "plus") {}
                    Divider()
                    Button("Remove", systemImage: "minus") {}
                }
            }
        }
        .formStyle(.grouped)
    }
}
/// Apply a variable blur overlay to a view.
func variableBlur(maxRadius: CGFloat, mask: Image, opaque: Bool) -> some View
/// Change the toolbar behavior when the window is fullscreen.
/// `.showsWithWindow` is the default behavior. `.showsWithMenuBar` will hide the toolbar when fullscreen.
func fullScreenToolbarBehavior(_ behavior: FullScreenToolbarBehavior) -> some View
/// Get a trigger when a row has been selected by the user, and gets clicked again.
/// This differs from a TapGesture, as there is no time limit between the clicks.
func onReselect(enabled: Bool, perform: (() -> ())?) -> some View
/// Adjust the padding around a Form with FormStyle `.grouped`
func formInsets(_ edges: Edge.Set, _ value: CGFloat) -> some View
func formInsets(_ edges: Edge.Set, _ value: EdgeInsets) -> some View
/// Adjust the inset of an individual row in a Form with FormStyle `.grouped`
func formRowInsets(_ insets: EdgeInsets?) -> some View
/// Set the indentation level of a Form row.
func formRowIndentationLevel(_ level: Int?) -> some View
/// Change the background color of a Form.
func formBackground<S: ShapeStyle>(_ shape: S?) -> some View
/// Change the background color of an individual row in a Form.
func formRowBackground<S: ShapeStyle>(_ shape: S?) -> some View
/// Change the background color of a section.
func sectionBackground(_ visibility: Visibility) -> some View
/// Add a trailing info icon to a Form row.
/// As an example, see system bluetooth settings.
func formInfoAction(action: @escaping () -> Void) -> some View
/// Change the visibility of the trailing info icon in a Form row.
func formRowInfoVisibility(_ visibility: Visibility) -> some View
/// Add a trailing Form row view, which becomes visible when hovered over the row.
/// As an example, see system wifi settings.
func formAccessory(@ViewBuilder accessories: () -> some View) -> some View
/// Change the visibility of the trailing Form row view.
func formAccessoryVisibility(_ visibility: Visibility) -> some View
/// Add a bottom row to a Form.
func bottomBar(@ViewBuilder content: () -> some View) -> some View
/// Add a sticky footer to List or Table. The List or Table needs to be wrapped in a Form with style `grouped`.
/// As an example, see system privacy settings -> accessibility
func accessoryBar(@ViewBuilder content: () -> some View) -> some View
/// Control the visibility of the list reorder controls. Untested.
func listReorderControlVisibility(_ visibility: ListAccessoryVisibility) -> some View

Form initializers (表单初始化器)

public extension Form {
    /// Add a footer to a form.
    static func withFooter<C: View, F: View>(@ViewBuilder content: () -> C, @ViewBuilder footer: () -> F) -> some View
}