adwaita-swift/Sources/Adwaita/View/NavigationSplitView.swift
david-swift 8f6a83dfc3
Some checks are pending
Deploy Docs / publish (push) Waiting to run
SwiftLint / SwiftLint (push) Waiting to run
Move update responsibility to constructor
When constructing views, the top-level constructor should now update
those views directly after the construction process. In adwaita-swift,
this applies only for EitherViews (as Meta will now automatically update
when constructing new scene elements).
2026-02-02 22:44:44 +01:00

130 lines
5.0 KiB
Swift

//
// NavigationSplitView.swift
// Adwaita
//
// Created by david-swift on 24.09.23.
//
import CAdw
/// A navigation split view widget.
public struct NavigationSplitView: AdwaitaWidget {
/// The sidebar's content.
var sidebar: Body
/// The split view's main content.
var content: Body
/// Whether the split view is collapsed.
var collapsed = false
/// Whether the content is visible (if the split view is collapsed).
var showContent: Binding<Bool>?
/// The sidebar content's id.
let sidebarID = "sidebar"
/// The main content's id.
let contentID = "content"
/// Initialize a navigation split view.
/// - Parameters:
/// - sidebar: The sidebar content.
/// - content: The main content.
public init(@ViewBuilder sidebar: @escaping () -> Body, @ViewBuilder content: @escaping () -> Body) {
self.sidebar = sidebar()
self.content = content()
}
/// The view storage.
/// - Parameters:
/// - modifiers: Modify views before being updated.
/// - type: The view render data type.
/// - Returns: The view storage.
public func container<Data>(
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let splitView = adw_navigation_split_view_new()
var content: [String: [ViewStorage]] = [:]
let sidebar = sidebar.storage(data: data, type: type)
let label = sidebar.fields[.navigationLabel] as? String ?? ""
let sidebarPage = adw_navigation_page_new(sidebar.opaquePointer?.cast(), label)
adw_navigation_split_view_set_sidebar(.init(splitView), sidebarPage?.cast())
content[sidebarID] = [sidebar]
let mainContent = self.content.storage(data: data, type: type)
let mainLabel = mainContent.fields[.navigationLabel] as? String ?? ""
let mainPage = adw_navigation_page_new(mainContent.opaquePointer?.cast(), mainLabel)
adw_navigation_split_view_set_content(.init(splitView), mainPage?.cast())
content[contentID] = [mainContent]
let storage = ViewStorage(splitView?.opaque(), content: content)
storage.fields["sidebar-page"] = sidebarPage?.opaque()
storage.fields["main-page"] = mainPage?.opaque()
storage.notify(name: "show-content") {
showContent?.wrappedValue = adw_navigation_split_view_get_show_content(storage.opaquePointer) != 0
}
return storage
}
/// Update the stored content.
/// - Parameters:
/// - storage: The storage to update.
/// - modifiers: Modify views before being updated
/// - updateProperties: Whether to update the view's properties.
/// - type: The view render data type.
public func update<Data>(
_ storage: ViewStorage,
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
if let cStorage = storage.content[contentID]?[safe: 0] {
content
.updateStorage(cStorage, data: data, updateProperties: updateProperties, type: type)
if updateProperties, let mainPage = storage.fields["main-page"] as? OpaquePointer {
adw_navigation_page_set_title(mainPage.cast(), cStorage.fields[.navigationLabel] as? String ?? "")
}
}
if let sStorage = storage.content[sidebarID]?[safe: 0] {
sidebar
.updateStorage(sStorage, data: data, updateProperties: updateProperties, type: type)
if updateProperties, let sidebarPage = storage.fields["sidebar-page"] as? OpaquePointer {
adw_navigation_page_set_title(sidebarPage.cast(), sStorage.fields[.navigationLabel] as? String ?? "")
}
}
guard updateProperties else {
return
}
let collapsed = adw_navigation_split_view_get_collapsed(storage.opaquePointer) != 0
if collapsed != self.collapsed {
adw_navigation_split_view_set_collapsed(storage.opaquePointer, self.collapsed.cBool)
}
let showContent = adw_navigation_split_view_get_show_content(storage.opaquePointer) != 0
if let binding = self.showContent, showContent != binding.wrappedValue {
adw_navigation_split_view_set_show_content(storage.opaquePointer, binding.wrappedValue.cBool)
}
}
/// Whether the navigation split view is collapsed, meaning in its compact form.
/// - Parameter collapsed: Whether the view is collapsed.
/// - Returns: The navigation split view.
public func collapsed(_ collapsed: Bool) -> Self {
var newSelf = self
newSelf.collapsed = collapsed
return newSelf
}
/// Whether the content view is visible if the split view is collapsed.
/// - Parameter showContent: Whether the content view is visible.
/// - Returns: The navigation split view.
public func showContent(_ showContent: Binding<Bool>) -> Self {
var newSelf = self
newSelf.showContent = showContent
return newSelf
}
}