Add ToolbarView extension

This commit is contained in:
Brendan Szymanski 2026-08-05 18:38:06 -04:00
parent f3bcae1f74
commit 667400ea71

View file

@ -0,0 +1,69 @@
import Adw
import Gtk
extension ToolbarView {
/// Creates a toolbar view with content and optional top and bottom bars.
///
/// The content closure's first view is mounted as the content widget. Every
/// view returned by the top and bottom closures is mounted directly as a
/// corresponding toolbar bar.
///
/// - Parameters:
/// - content: A closure producing the content widget.
/// - top: A closure producing widgets to add as top bars.
/// - bottom: A closure producing widgets to add as bottom bars.
public init(
@ViewBuilder content: () -> [AnyView],
@ViewBuilder top: () -> [AnyView] = { [] },
@ViewBuilder bottom: () -> [AnyView] = { [] }
) {
let contentViews = content()
let topViews = top()
let bottomViews = bottom()
self.init()
if let contentView = contentViews.first {
self = self.appending { toolbar, context in
toolbar.setContent(content: contentView.makeWidget(context))
}
}
for view in topViews {
self = self.appending { toolbar, context in
toolbar.addTopBar(widget: view.makeWidget(context))
}
}
for view in bottomViews {
self = self.appending { toolbar, context in
toolbar.addBottomBar(widget: view.makeWidget(context))
}
}
}
/// Adds widgets produced by `content` as top bars.
///
/// - Parameter content: A closure producing widgets to add as top bars.
/// - Returns: A copy of this view with the bars added at mount time.
public func top(@ViewBuilder _ content: () -> [AnyView]) -> Self {
var view = self
for child in content() {
view = view.appending { toolbar, context in
toolbar.addTopBar(widget: child.makeWidget(context))
}
}
return view
}
/// Adds widgets produced by `content` as bottom bars.
///
/// - Parameter content: A closure producing widgets to add as bottom bars.
/// - Returns: A copy of this view with the bars added at mount time.
public func bottom(@ViewBuilder _ content: () -> [AnyView]) -> Self {
var view = self
for child in content() {
view = view.appending { toolbar, context in
toolbar.addBottomBar(widget: child.makeWidget(context))
}
}
return view
}
}