From cf60b8103f4884ee285e50a1e2bb22747bccd42d Mon Sep 17 00:00:00 2001 From: david-swift Date: Tue, 30 Dec 2025 16:32:24 +0100 Subject: [PATCH] Add homogeneous option to ForEach #74 --- Sources/Adwaita/View/ForEach.swift | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Sources/Adwaita/View/ForEach.swift b/Sources/Adwaita/View/ForEach.swift index fe5caaa..1fd8cb1 100644 --- a/Sources/Adwaita/View/ForEach.swift +++ b/Sources/Adwaita/View/ForEach.swift @@ -17,6 +17,8 @@ public struct ForEach: AdwaitaWidget where Element: Identifiable { var content: (Element) -> Body /// Whether the list is horizontal. var horizontal: Bool + /// Whether the children should all be the same size. + var homogeneous: Bool? /// Initialize `ForEach`. public init(_ elements: [Element], horizontal: Bool = false, @ViewBuilder content: @escaping (Element) -> Body) { @@ -73,10 +75,16 @@ public struct ForEach: AdwaitaWidget where Element: Identifiable { } ) if updateProperties { - gtk_orientable_set_orientation( - widget?.opaque(), - horizontal ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL - ) + if (storage.previousState as? Self)?.horizontal != horizontal { + gtk_orientable_set_orientation( + widget?.opaque(), + horizontal ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL + ) + } + if let homogeneous, (storage.previousState as? Self)?.homogeneous != homogeneous { + gtk_box_set_homogeneous(widget?.cast(), homogeneous.cBool) + } + storage.previousState = self } storage.fields["element"] = elements storage.content[.mainContent] = contentStorage @@ -91,4 +99,11 @@ public struct ForEach: AdwaitaWidget where Element: Identifiable { } } + /// Whether the children should all be the same size. + /// - Parameter homogeneous: Whether the children should all be the same size. + /// - Returns: The for each view. + public func homogeneous(_ homogeneous: Bool? = true) -> Self { + modify { $0.homogeneous = homogeneous } + } + }