Add ToolbarView extension
This commit is contained in:
parent
f3bcae1f74
commit
667400ea71
1 changed files with 69 additions and 0 deletions
69
Sources/Portico/Extensions/ToolbarView+Extras.swift
Normal file
69
Sources/Portico/Extensions/ToolbarView+Extras.swift
Normal 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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue