目录
目录README.md

CustomTitlebar Swift Buy Me a Coffee

Create custom titlebars for your SwiftUI applications.

Requirements

  • macOS 11.0+

Installation

Add https://github.com/JochenBe/CustomTitlebar in the Swift Package Manager.

Usage

CustomTitlebar uses an observable version of NSWindowDelegate called ObservableWindowDelegate to lower the opacity of the titlebar when the window resigns key. This ObservableWindowDelegate is passed down using environment objects. If you do not wish to use this feature, pass ignoreIsKeyWindow: true to your CustomTitlebar instance.

Initiating and passing down the ObservableWindowDelegate:

myWindowDelegate = ObservableWindowDelegate()
myWindow.delegate = myWindowDelegate

myWindow.titlebarAppearsTransparent = true
myWindow.contentView = NSHostingView(
    rootView: ContentView().environmentObject(myWindowDelegate)
)

Using the CustomTitlebar:

CustomTitlebar(MyCustomTitlebar()) {
    MyContent()
}

For more information about the parameters, check the CustomTitlebar reference.

Reference

CustomTitlebar

A SwiftUI View aligning your custom titlebar and window content.

Parameters

  • titlebar: The contents of the titlebar.

  • withToolbar: Whether the titlebar contains a toolbar. This will result in a taller titlebar.

  • hideDivider: Whether the divider between the titlebar and the window content should be hidden.

  • ignoreIsKeyWindow: Whether isKeyWindow should be ignored. Setting this to true allows you to not pass an ObservableWindowDelegate.

  • content: The contents of the window.

struct CustomTitlebar<TitlebarContent, WindowContent> : View
where TitlebarContent : View, WindowContent : View {
    init(
        _ titlebar: TitlebarContent,
        withToolbar: Bool = false,
        hideDivider: Bool = false,
        ignoreIsKeyWindow: Bool = false,
        content: () -> WindowContent
    )

    var body: some View { get }
}

ObservableWindowDelegate

An observable version of NSWindowDelegate.

Parameters

  • isKeyWindow: The initial value of isKeyWindow.
class ObservableWindowDelegate : NSObject, NSWindowDelegate, ObservableObject {
    /// A Boolean value that indicates whether the window is the key window for the application.
    var isKeyWindow: Bool { get }

    init(_ isKeyWindow: Bool = false)

    func windowDidBecomeKey(_ notification: Notification)

    func windowDidResignKey(_ notification: Notification)
}

TitlebarDimensions

An object containing constant titlebar dimensions.

struct TitlebarDimensions {
    /// An object containing constant titlebar heights.
    struct height {
        /// The height of a statusbar without toolbar.
        static let withoutToolbar: CGFloat
        /// The height of a statusbar with toolbar.
        static let withToolbar: CGFloat
    }

    /// An object containing constant titlebar paddings.
    struct padding {
        /// An object containing constant titlebar left paddings.
        struct left {
            /// The left padding of a titlebar without toolbar.
            static let withoutToolbar: CGFloat
            /// The left padding of a titlebar with toolbar.
            static let withToolbar: CGFloat
        }
    }
}
关于
61.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号