From a53b4e084b8fc3055011808397e3544e781369b7 Mon Sep 17 00:00:00 2001 From: david-swift Date: Sat, 30 Mar 2024 10:33:34 +0100 Subject: [PATCH] Add additional options for navigation split view - Whether it is collapsed - Whether the content is visible if it is collapsed --- .../Adwaita/View/NavigationSplitView.swift | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Sources/Adwaita/View/NavigationSplitView.swift b/Sources/Adwaita/View/NavigationSplitView.swift index 6747f6c..f24cde7 100644 --- a/Sources/Adwaita/View/NavigationSplitView.swift +++ b/Sources/Adwaita/View/NavigationSplitView.swift @@ -14,6 +14,10 @@ public struct NavigationSplitView: Widget { 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? /// The sidebar content's id. let sidebarID = "sidebar" @@ -48,7 +52,14 @@ public struct NavigationSplitView: Widget { adw_navigation_split_view_set_content(.init(splitView), mainPage?.cast()) content[contentID] = [mainContent] - return .init(.init(splitView), content: content) + let storage = ViewStorage(.init(splitView), content: content) + update(storage, modifiers: modifiers, updateProperties: true) + + storage.notify(name: "show-content") { + showContent?.wrappedValue = adw_navigation_split_view_get_show_content(storage.pointer) != 0 + } + + return storage } /// Update the view storage of the navigation split view widget. @@ -67,6 +78,35 @@ public struct NavigationSplitView: Widget { .widget(modifiers: modifiers) .update(storage, modifiers: modifiers, updateProperties: updateProperties) } + guard updateProperties else { + return + } + let collapsed = adw_navigation_split_view_get_collapsed(storage.pointer) != 0 + if collapsed != self.collapsed { + adw_navigation_split_view_set_collapsed(storage.pointer, self.collapsed.cBool) + } + let showContent = adw_navigation_split_view_get_show_content(storage.pointer) != 0 + if let binding = self.showContent, showContent != binding.wrappedValue { + adw_navigation_split_view_set_show_content(storage.pointer, 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) -> Self { + var newSelf = self + newSelf.showContent = showContent + return newSelf } }