Skip to content

Commit

Permalink
SegmentedControl code clean up (#1432)
Browse files Browse the repository at this point in the history
* fix rotation bug on segmented control (#1411)

* Fix pillMaskedLabelContainerView comment

* Fix update logic

* Make tokenSink and animation duration private

* Update selectionView on token update

Co-authored-by: Harrie Shin <hyshin@microsoft.com>
  • Loading branch information
huwilkes and harrieshin committed Dec 15, 2022
1 parent d6f3578 commit 9cc9773
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions ios/FluentUI/SegmentedControl/SegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
// |--pillContainerView (used to create 16pt inset on either side)
// | |--stackView (fill container view, uses restTabColor)
// | | |--buttons (uses restLabelColor)
// | |--pillMaskedLabelsContainerView (fill container view, uses selectedTabColor)
// | |--pillMaskedContentContainerView (fill container view, uses selectedTabColor)
// | | |.mask -> selectionView
// | | |--pillMaskedLabels (uses selectedLabelColor)
// | | |--pillMaskedImages (uses selectedLabelColor)
Expand All @@ -88,7 +88,7 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
// pillContainerView (used to create 16pt inset on either side)
// |--stackView (fill container view, uses restTabColor)
// | |--buttons (uses restLabelColor)
// |--pillMaskedLabelsContainerView (fill container view, uses selectedTabColor)
// |--pillMaskedContentContainerView (fill container view, uses selectedTabColor)
// | |.mask -> selectionView
// | |--pillMaskedLabels (uses selectedLabelColor)
// | |--pillMaskedImages (uses selectedLabelColor)
Expand Down Expand Up @@ -195,9 +195,9 @@ open class SegmentedControl: UIView, TokenizedControlInternal {

// Update appearance whenever overrideTokens changes.
tokenSetSink = tokenSet.sinkChanges { [weak self] in
self?.updateColors()
self?.updateButtons()
self?.updateTokenizedValues()
}
updateTokenizedValues()
}

public required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -355,7 +355,7 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
invalidateIntrinsicContentSize()
layoutSubviews()
setNeedsLayout()
}

open override func sizeThatFits(_ size: CGSize) -> CGSize {
Expand Down Expand Up @@ -399,10 +399,6 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
}
}

func intrinsicContentSizeInvalidatedForChildView() {
invalidateIntrinsicContentSize()
}

/// Used to retrieve the view from the segment at the specified index
open func segmentView(at index: Int) -> UIView? {
guard index <= buttons.count else {
Expand All @@ -416,9 +412,9 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
lazy public var tokenSet: SegmentedControlTokenSet = .init(style: { [weak self] in
return self?.style ?? .primaryPill
})
var tokenSetSink: AnyCancellable?
private var tokenSetSink: AnyCancellable?

var selectionChangeAnimationDuration: TimeInterval { return 0.2 }
private let selectionChangeAnimationDuration: TimeInterval = 0.2

private func updateButtons() {
let contentColor = isEnabled ? UIColor(dynamicColor: tokenSet[.restLabelColor].dynamicColor) : UIColor(dynamicColor: tokenSet[.disabledLabelColor].dynamicColor)
Expand Down Expand Up @@ -561,12 +557,10 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
}

@objc private func themeDidChange(_ notification: Notification) {
guard let window = window, window.isEqual(notification.object) else {
guard let themeView = notification.object as? UIView, self.isDescendant(of: themeView) else {
return
}
tokenSet.update(window.fluentTheme)
updateColors()
updateButtons()
tokenSet.update(fluentTheme)
}

private func layoutSelectionView() {
Expand All @@ -579,6 +573,13 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
selectionView.layer.cornerRadius = Constants.pillButtonCornerRadius
}

private func updateTokenizedValues() {
updateColors()
updateButtons()
layoutSelectionView()
setNeedsLayout()
}

private func updateAccessibilityHints() {
for (index, button) in buttons.enumerated() {
button.accessibilityHint = String.localizedStringWithFormat("Accessibility.MSPillButtonBar.Hint".localized,
Expand Down

0 comments on commit 9cc9773

Please sign in to comment.