Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SegmentedControl code clean up #1432

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
huwilkes marked this conversation as resolved.
Show resolved Hide resolved
}

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