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

Commit

Permalink
Add support for setting a view's visibility
Browse files Browse the repository at this point in the history
Use the visibility instead of a ViewStack for optional views
  • Loading branch information
david-swift committed Feb 5, 2024
1 parent 3ee1cf3 commit e86aa73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Sources/Adwaita/Model/User Interface/View/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ public enum ViewBuilder {
/// - Parameter component: An optional component.
/// - Returns: A nonoptional component.
public static func buildOptional(_ component: Component?) -> Component {
if let component {
return .element(ViewStack(id: true) { _ in buildFinalResult(component) })
} else {
return .element(ViewStack(id: false) { _ in [] })
}
.element(
VStack {
if let component {
buildFinalResult(component)
} else {
[]
}
}
.visible(component != nil)
)
}

/// Enables support for `if`-`else` and `switch` statements.
Expand Down
7 changes: 7 additions & 0 deletions Sources/Adwaita/View/Modifiers/InspectorWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,11 @@ extension View {
inspect { gtk_widget_set_sensitive($0.pointer?.cast(), insensitive ? 0 : 1) }
}

/// Set the view's visibility.
/// - Parameter visible: Whether the view is visible.
/// - Returns: A view.
public func visible(_ visible: Bool = true) -> View {
inspect { gtk_widget_set_visible($0.pointer?.cast(), visible.cBool) }
}

}

0 comments on commit e86aa73

Please sign in to comment.