Skip to content

Commit 29dc07d

Browse files
authored
Add ability to calculate intrinsic height of the command bar (#2244)
1 parent e38cc19 commit 29dc07d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Sources/FluentUI_iOS/Components/CommandBar/CommandBar.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public class CommandBar: UIView, Shadowable, TokenizedControl {
146146
// MARK: Overrides
147147

148148
public override var intrinsicContentSize: CGSize {
149-
.zero
149+
shouldCalculateIntrinsicHeight ? CGSize(width: UIView.noIntrinsicMetric, height: intrinsicHeight) : .zero
150150
}
151151

152152
public override func layoutSubviews() {
@@ -197,6 +197,7 @@ public class CommandBar: UIView, Shadowable, TokenizedControl {
197197
}
198198
set {
199199
mainCommandGroupsView.itemGroups = newValue
200+
invalidateIntrinsicContentSize()
200201
}
201202
}
202203

@@ -207,6 +208,7 @@ public class CommandBar: UIView, Shadowable, TokenizedControl {
207208
}
208209
set {
209210
setupGroupsView(leadingCommandGroupsView, with: newValue)
211+
invalidateIntrinsicContentSize()
210212
}
211213
}
212214

@@ -217,6 +219,7 @@ public class CommandBar: UIView, Shadowable, TokenizedControl {
217219
}
218220
set {
219221
setupGroupsView(trailingCommandGroupsView, with: newValue)
222+
invalidateIntrinsicContentSize()
220223
}
221224
}
222225

@@ -240,6 +243,15 @@ public class CommandBar: UIView, Shadowable, TokenizedControl {
240243
}
241244
}
242245

246+
/// Controls whether intrinsic height should be calculated. When false, intrinsicContentSize.height returns 0.
247+
@objc public var shouldCalculateIntrinsicHeight: Bool = true {
248+
didSet {
249+
if shouldCalculateIntrinsicHeight != oldValue {
250+
invalidateIntrinsicContentSize()
251+
}
252+
}
253+
}
254+
243255
/// Delegate object that notifies consumers of events occuring inside the `CommandBar`
244256
public weak var delegate: CommandBarDelegate?
245257

@@ -259,6 +271,24 @@ public class CommandBar: UIView, Shadowable, TokenizedControl {
259271

260272
private var mainCommandGroupsViewConstraints: [NSLayoutConstraint] = []
261273

274+
private var intrinsicHeight: CGFloat {
275+
var maxButtonHeight: CGFloat = 0
276+
277+
let allGroups = (leadingItemGroups ?? []) + itemGroups + (trailingItemGroups ?? [])
278+
for group in allGroups {
279+
for item in group where !item.isHidden {
280+
if let button = leadingCommandGroupsView.button(for: item) ??
281+
mainCommandGroupsView.button(for: item) ??
282+
trailingCommandGroupsView.button(for: item) {
283+
let buttonHeight = button.intrinsicContentSize.height
284+
maxButtonHeight = max(maxButtonHeight, buttonHeight)
285+
}
286+
}
287+
}
288+
289+
return maxButtonHeight + CommandBarTokenSet.barInsets * 2
290+
}
291+
262292
// MARK: Views and Layers
263293

264294
private lazy var containerView: CommandBarContainerView = {

Sources/FluentUI_iOS/Components/CommandBar/CommandBarButton.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ class CommandBarButton: UIButton {
8080
preconditionFailure("init(coder:) has not been implemented")
8181
}
8282

83+
override var intrinsicContentSize: CGSize {
84+
if let customView = item.customControlView?() {
85+
return customView.intrinsicContentSize
86+
}
87+
return super.intrinsicContentSize
88+
}
89+
8390
func updateState() {
8491
isEnabled = item.isEnabled
8592
isSelected = isPersistSelection && item.isSelected

Sources/FluentUI_iOS/Components/CommandBar/CommandBarCommandGroupsView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ class CommandBarCommandGroupsView: UIView {
108108
}
109109
}
110110

111+
/// Returns the button associated with the given item, if it exists.
112+
func button(for item: CommandBarItem) -> CommandBarButton? {
113+
return itemsToButtonsMap[item]
114+
}
115+
111116
var equalWidthGroups: Bool = false {
112117
didSet {
113118
buttonGroupsStackView.distribution = equalWidthGroups ? .fillEqually : .fill

0 commit comments

Comments
 (0)