Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add additional options for navigation split view
Browse files Browse the repository at this point in the history
- Whether it is collapsed
- Whether the content is visible if it is collapsed
  • Loading branch information
david-swift committed Mar 30, 2024
1 parent 84ac558 commit e488391
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion Sources/Adwaita/View/NavigationSplitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: Binding<Bool>?
/// Whether the content is visible (if the split view is collapsed).
var showContent: Binding<Bool>?

/// The sidebar content's id.
let sidebarID = "sidebar"
Expand Down Expand Up @@ -48,7 +52,17 @@ 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: "collapsed") {
collapsed?.wrappedValue = adw_navigation_split_view_get_collapsed(storage.pointer) != 0
}
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.
Expand All @@ -67,6 +81,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 let binding = self.collapsed, collapsed != binding.wrappedValue {
adw_navigation_split_view_set_collapsed(storage.pointer, binding.wrappedValue.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: Binding<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
}

}

0 comments on commit e488391

Please sign in to comment.