Skip to content

Commit

Permalink
Merge pull request #1940 from sophialee0416/sople/cherryPick
Browse files Browse the repository at this point in the history
[main_0.24] Cherry-picking PopupMenuController + iOS 17 changes
  • Loading branch information
sophialee0416 authored Dec 5, 2023
2 parents a5c2d0d + 787673f commit 0539af1
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ open class BottomCommandingController: UIViewController, TokenizedControlInterna
}
}

@available(iOS, deprecated: 17.0)
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Button/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ open class Button: UIButton, Shadowable, TokenizedControlInternal {
initialize()
}

@available(iOS, deprecated: 17.0)
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Card/CardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ open class CardView: UIView, Shadowable, TokenizedControlInternal {
preconditionFailure("init(coder:) has not been implemented")
}

@available(iOS, deprecated: 17.0)
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if let previousTraitCollection = previousTraitCollection {
Expand Down
21 changes: 19 additions & 2 deletions ios/FluentUI/Core/SwiftUI+ViewModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension View {

/// Adds a large content viewer for the view. If the text and image are both nil,
/// the default large content viewer will be used.
/// - Parameters
/// - Parameters:
/// - text: Optional String to display in the large content viewer.
/// - image: Optional UIImage to display in the large content viewer.
/// - Returns: The modified view.
Expand All @@ -52,12 +52,29 @@ extension View {
}

/// Applies multiple shadows on a View
/// - Parameters
/// - Parameters:
/// - shadowInfo: The values of the two shadows to be applied
/// - Returns: The modified view.
func applyShadow(shadowInfo: ShadowInfo) -> some View {
modifier(ShadowModifier(shadowInfo: shadowInfo))
}

/// Abstracts away differences in pre-iOS 17 `onChange(of:perform:)` versus post-iOS 17 `onChange(of:_:)`.
///
/// This function will be removed once FluentUI moves to iOS 17 as a minimum target.
/// - Parameters:
/// - value: The value to check against when determining whether to run the closure.
/// - action: A closure to run when the value changes.
/// - Returns: A view that fires an action when the specified value changes.
func onChange_iOS17<V>(of value: V, _ action: @escaping (V) -> Void) -> some View where V: Equatable {
if #available(iOS 17, *) {
return self.onChange(of: value) { _, newValue in
return action(newValue)
}
} else {
return self.onChange(of: value, perform: action)
}
}
}

/// PreferenceKey that will store the measured size of the view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class DatePickerController: UIViewController, GenericDateTimePicker {
navigationItem.leftBarButtonItem?.tintColor = view.fluentTheme.color(.foreground2)
}

@available(iOS, deprecated: 17.0)
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

Expand Down
10 changes: 7 additions & 3 deletions ios/FluentUI/Drawer/DrawerPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class DrawerPresentationController: UIPresentationController {
}
case .up:
if actualPresentationOrigin == containerView.bounds.maxY {
return containerView.safeAreaInsets.bottom + keyboardHeight
return keyboardHeight == 0 ? containerView.safeAreaInsets.bottom : keyboardHeight
}
case .fromLeading:
if actualPresentationOrigin == containerView.bounds.minX {
Expand Down Expand Up @@ -344,6 +344,9 @@ class DrawerPresentationController: UIPresentationController {
}

private func frameForContentView(in bounds: CGRect) -> CGRect {
guard let containerView = containerView else {
return .zero
}
var contentFrame = bounds.inset(by: marginsForContentView())

var contentSize = presentedViewController.preferredContentSize
Expand Down Expand Up @@ -380,7 +383,8 @@ class DrawerPresentationController: UIPresentationController {

contentFrame.origin.x += (contentFrame.width - contentSize.width) / 2
if presentationDirection == .up {
contentFrame.origin.y = contentFrame.maxY - contentSize.height
contentFrame.origin.y = keyboardHeight != 0 ? max(landscapeMode ? 0 : sourceViewController.view.safeAreaInsets.top,
containerView.frame.maxY - keyboardHeight - contentSize.height) : contentFrame.maxY - contentSize.height
}
} else {
if actualPresentationOffset == 0 {
Expand Down Expand Up @@ -510,7 +514,7 @@ class DrawerPresentationController: UIPresentationController {
keyboardAnimationDuration = (notificationInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue

keyboardFrame = containerView.convert(keyboardFrame, from: nil)
keyboardHeight = max(0, containerView.bounds.maxY - containerView.safeAreaInsets.bottom - keyboardFrame.minY)
keyboardHeight = max(0, containerView.bounds.maxY - keyboardFrame.minY)
}
}

Expand Down
17 changes: 13 additions & 4 deletions ios/FluentUI/Extensions/UIColor+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,19 @@ extension UIColor {
private func resolvedColorValue(userInterfaceStyle: UIUserInterfaceStyle,
accessibilityContrast: UIAccessibilityContrast = .unspecified,
userInterfaceLevel: UIUserInterfaceLevel = .unspecified) -> UIColor {
let traitCollectionStyle = UITraitCollection(userInterfaceStyle: userInterfaceStyle)
let traitCollectionContrast = UITraitCollection(accessibilityContrast: accessibilityContrast)
let traitCollectionLevel = UITraitCollection(userInterfaceLevel: userInterfaceLevel)
let traitCollection = UITraitCollection(traitsFrom: [traitCollectionStyle, traitCollectionContrast, traitCollectionLevel])
let traitCollection: UITraitCollection
if #available(iOS 17, *) {
traitCollection = UITraitCollection { mutableTraits in
mutableTraits.userInterfaceStyle = userInterfaceStyle
mutableTraits.accessibilityContrast = accessibilityContrast
mutableTraits.userInterfaceLevel = userInterfaceLevel
}
} else {
let traitCollectionStyle = UITraitCollection(userInterfaceStyle: userInterfaceStyle)
let traitCollectionContrast = UITraitCollection(accessibilityContrast: accessibilityContrast)
let traitCollectionLevel = UITraitCollection(userInterfaceLevel: userInterfaceLevel)
traitCollection = UITraitCollection(traitsFrom: [traitCollectionStyle, traitCollectionContrast, traitCollectionLevel])
}
let resolvedColor = self.resolvedColor(with: traitCollection)
return resolvedColor
}
Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/MultilineCommandBar/MultilineCommandBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class MultilineCommandBar: UIViewController {
bottomSheetController = sheetController
}

@available(iOS, deprecated: 17.0)
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Navigation/NavigationBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ open class NavigationBar: UINavigationBar, TokenizedControlInternal, TwoLineTitl
contentStackView.point(inside: convert(point, to: contentStackView), with: event)
}

@available(iOS, deprecated: 17.0)
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass {
Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Navigation/Shy Header/ShyHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class ShyHeaderView: UIView, TokenizedControlInternal {
/// e.g. should cancel a search on scroll
private var cancelsContentFirstRespondingOnHide: Bool = false

@available(iOS, deprecated: 17.0)
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
updateContentInsets()
Expand Down
6 changes: 3 additions & 3 deletions ios/FluentUI/Notification/FluentNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ public struct FluentNotification: View, TokenizedControlView {
notification
.frame(idealWidth: isFlexibleWidthToast ? innerContentsSize.width - horizontalPadding : calculatedNotificationWidth,
maxWidth: isFlexibleWidthToast ? proposedWidth : calculatedNotificationWidth, alignment: .center)
.onChange(of: isPresented, perform: { present in
if present {
.onChange_iOS17(of: isPresented) { newPresent in
if newPresent {
presentAnimated()
} else {
dismissAnimated()
}
})
}
.padding(.bottom, tokenSet[.bottomPresentationPadding].float)
.onSizeChange { newSize in
bottomOffsetForDismissedState = newSize.height + (tokenSet[.shadow].shadowInfo.yKey / 2)
Expand Down
2 changes: 1 addition & 1 deletion ios/FluentUI/Popup Menu/PopupMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ open class PopupMenuController: DrawerController {
}

private func initTableView() {
tableView.backgroundColor = backgroundColor
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.alwaysBounceVertical = false
tableView.isAccessibilityElement = true
Expand Down
2 changes: 1 addition & 1 deletion ios/FluentUI/Popup Menu/PopupMenuItemTokenSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PopupMenuItemTokenSet: TableViewCellTokenSet {
self.fluentTheme.color(.foreground3)
},
.cellBackgroundColor: .uiColor {
self.fluentTheme.color(.background1)
.clear
}
])
}
Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/SegmentedControl/SegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ open class SegmentedControl: UIView, TokenizedControlInternal {
height: CGFloat.greatestFiniteMagnitude))
}

@available(iOS, deprecated: 17.0)
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
invalidateIntrinsicContentSize()
Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Tab Bar/TabBarItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class TabBarItemView: UIControl, TokenizedControlInternal {
return size
}

@available(iOS, deprecated: 17.0)
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if previousTraitCollection?.horizontalSizeClass != traitCollection.horizontalSizeClass || previousTraitCollection?.verticalSizeClass != traitCollection.verticalSizeClass {
Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Tab Bar/TabBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ open class TabBarView: UIView, TokenizedControlInternal {
preconditionFailure("init(coder:) has not been implemented")
}

@available(iOS, deprecated: 17.0)
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if previousTraitCollection?.horizontalSizeClass != traitCollection.horizontalSizeClass || previousTraitCollection?.verticalSizeClass != traitCollection.verticalSizeClass {
Expand Down
1 change: 1 addition & 0 deletions ios/FluentUI/Tooltip/TooltipViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TooltipViewController: UIViewController {
}
}

@available(iOS, deprecated: 17.0)
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)

Expand Down

0 comments on commit 0539af1

Please sign in to comment.