Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct MatchedGeometryEffectUITests {
}
openSwiftUIAssertAnimationSnapshot(
of: ContentView(),
precision: 0.98, // FIXME: Text and button rendering differ in the middle frame
size: CGSize(width: 300, height: 150)
)
}
Expand All @@ -42,7 +41,6 @@ struct MatchedGeometryEffectUITests {
}
openSwiftUIAssertAnimationSnapshot(
of: ContentView(),
precision: 0.98, // FIXME: Text and button rendering differ in the middle frame
size: CGSize(width: 300, height: 150)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension DisplayList.ViewUpdater {
}
}
if !requirements.contains(.inheritedView),
state.transform != .identity,
!state.transform.isTranslation,
!item.canMergeWithTransform {
requirements.insert(.inheritedView)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,5 +586,9 @@ extension ResolvedStyledText {
}

// FIXME
package class TextLayoutManager: ResolvedStyledText {}
package class TextLayoutManager: ResolvedStyledText {
override package var majorAxis: Axis {
layoutProperties.writingMode == .horizontalTopToBottom ? .vertical : .horizontal
}
}
}
93 changes: 89 additions & 4 deletions Sources/OpenSwiftUICore/View/Text/Text/Text+View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,20 @@ package struct StyledTextContentView: UnaryView, PrimitiveView, ShapeStyledLeafV
outputs = .init()
} else {
let group = _ShapeStyle_InterpolatorGroup()
if inputs.needsGeometry {
newInputs.position = inputs.animatedPosition()
}
newInputs.containerPosition = inputs.animatedPosition()
outputs = makeLeafView(
view: view,
inputs: newInputs,
styles: shapeStyles,
interpolatorGroup: group
)
// TODO: outputs.applyInterpolatorGroup
outputs.applyInterpolatorGroup(
group,
content: view[offset: { .of(&$0.text) }].value,
inputs: inputs,
animatesSize: false,
defersRender: false
)
}
} else {
outputs = makeLeafView(
Expand Down Expand Up @@ -987,6 +991,87 @@ extension ResolvedStyledText {
@available(*, unavailable)
extension ResolvedStyledText: Sendable {}

// MARK: - ResolvedStyledText + InterpolatableContent

extension ResolvedStyledText: InterpolatableContent {
package static var defaultTransition: ContentTransition {
isLinkedOnOrAfter(.v4) ? .text : .identity
}

package func requiresTransition(to other: ResolvedStyledText) -> Bool {
guard self !== other else { return false }
guard features.contains(.checkInterpolationStrategy),
let storage,
let otherStorage = other.storage
else { return true }

typealias Strategy = AttributeScopes.OpenSwiftUIAttributes.InterpolationStrategy
let strategyKey = NSAttributedString.Key(Strategy.name)
var offset = 0
storage.enumerateAttributes(in: storage.range) { attributes, range, stop in
guard offset < otherStorage.length else {
stop.pointee = true
return
Comment on lines +1012 to +1014

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Detect remaining source runs when the target is exhausted

When .checkInterpolationStrategy is enabled and the destination removes a trailing ordinary run, this guard stops enumeration with offset == otherStorage.length, so the final comparison incorrectly returns false. For example, changing "Time: " + unanimated("9") + " seconds" to "Time: " + unanimated("9") suppresses the required transition for deleting " seconds", even though that run is not marked unanimated. Track whether source runs remain when the destination is exhausted rather than treating destination exhaustion alone as a successful match.

Useful? React with 👍 / 👎.

}
var otherRange = NSRange()
let otherAttributes = otherStorage.attributes(at: offset, effectiveRange: &otherRange)
guard Self.attributesMatch(attributes, otherAttributes) else {
stop.pointee = true
return
}
if attributes[strategyKey] as? Strategy != .unanimated,
storage.attributedSubstring(from: range).string != otherStorage.attributedSubstring(from: otherRange).string {
stop.pointee = true
return
}
offset += otherRange.length
}
return offset != otherStorage.length
}

private static func attributesMatch(_ lhs: NSAttributedStringAttributes, _ rhs: NSAttributedStringAttributes) -> Bool {
#if canImport(Darwin)
guard AttributeContainer(lhs) == AttributeContainer(rhs) else { return false }
// Foundation's default bridge does not load the OpenSwiftUI attribute scope.
if let lhs = try? AttributeContainer(lhs, including: \.openSwiftUI),
let rhs = try? AttributeContainer(rhs, including: \.openSwiftUI) {
return lhs == rhs
}
#endif
return NSDictionary(dictionary: lhs).isEqual(to: rhs)
}

package var appliesTransitionsForSizeChanges: Bool { true }

package var addsDrawingGroup: Bool { features.contains(.customRenderer) }

package func modifyTransition(state: inout ContentTransition.State, to other: ResolvedStyledText) {
if !state.options.contains(.animatesDifferentContent), storage?.string != other.storage?.string {
switch state.transition.storage {
case .named(var named):
switch named.name {
case .identity, .opacity, .numericText:
break
case .text:
named.name = .text(different: true)
state.transition.storage = .named(named)
case .default, .diff, .fadeIfDifferent:
state.transition.storage = ContentTransition.fadeIfDifferent.storage
}
case .custom, .symbolReplace:
state.transition.storage = ContentTransition.fadeIfDifferent.storage
}
}
if case var .named(named) = state.transition.storage,
case var .numericText(configuration) = named.name,
configuration.axis == nil {
configuration.axis = majorAxis.otherAxis
named.name = .numericText(configuration)
state.transition.storage = .named(named)
}
}
}

// FIXME: RB
package class ORBCGStyleHandler {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@

import Foundation
import OpenAttributeGraphShims
@_spi(ForOpenSwiftUIOnly) @testable import OpenSwiftUICore
import OpenCoreGraphicsShims
@_spi(DisplayList_ViewSystem) @_spi(ForOpenSwiftUIOnly) @testable import OpenSwiftUICore
import Testing

@MainActor
@Suite
struct DisplayListTransformsTests {
@Test(arguments: [(CGFloat(1), false), (CGFloat(2), true)])
func translatedRoundedClip(scale: CGFloat, needsInheritedView: Bool) {
typealias Model = DisplayList.ViewUpdater.Model
let updater = DisplayList.ViewUpdater(platform: .init(definition: PlatformViewDefinition.self), host: nil)
let globals = Model.State.Globals(updater: updater, time: .zero, maxVersion: .init(), environment: .invalid)
let path = Path(roundedRect: CGRect(x: 0, y: 0, width: 50, height: 50), cornerRadius: 8)
var clip = item(.effect(.clip(path, FillStyle()), DisplayList()), version: 1)
withUnsafePointer(to: globals) { pointer in
var state = Model.State(globals: pointer)
state.transform = CGAffineTransform(translationX: 8, y: 3).scaledBy(x: scale, y: scale)
let requirements = Model.merge(item: &clip, index: .init(), into: &state)
#expect(requirements.contains(.inheritedView) == needsInheritedView)
}
}

@Test
func stateSelectsMatchingDisplayList() {
let firstHash = StrongHash(of: 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// ResolvedStyledTextTests.swift
// OpenSwiftUICoreTests

#if canImport(Darwin)
import Foundation
@_spi(Private) @testable import OpenSwiftUICore
import Testing

struct ResolvedStyledTextTests {
@Test
func unanimatedRunCanChangeLengthBetweenOrdinaryRuns() throws {
let source = try timerText("9")
let next = try timerText("10")
let differentUnit = try timerText("10", unit: "minutes")

#expect(!source.requiresTransition(to: next))
#expect(source.requiresTransition(to: differentUnit))
}

@Test
func animatedRunRequiresTransitionWhenItsTextChanges() throws {
let source = try timerText("9", strategy: .animated)
let next = try timerText("10", strategy: .animated)

#expect(source.requiresTransition(to: next))
}

@Test
func changingInterpolationStrategyRequiresTransitionForTheSameText() throws {
let source = try timerText("9", strategy: .unanimated)
let next = try timerText("9", strategy: .animated)

#expect(source.requiresTransition(to: next))
}

@Test(arguments: [
(Text.WritingMode.horizontalTopToBottom, Axis.horizontal),
(.verticalRightToLeft, .vertical),
])
func numericTransitionSuppliesAxisForWritingMode(_ mode: Text.WritingMode, _ expectedAxis: Axis) throws {
let text = try timerText("9", writingMode: mode)
var state = ContentTransition.State(transition: .numericText(.fixed()))

text.modifyTransition(state: &state, to: text)

#expect(state.transition == .numericText(.fixed().axis(expectedAxis)))
}

private func timerText(
_ digits: String,
unit: String = "seconds",
strategy: AttributeScopes.OpenSwiftUIAttributes.InterpolationStrategy = .unanimated,
writingMode: Text.WritingMode = .horizontalTopToBottom
) throws -> ResolvedStyledText {
var time = AttributedString(digits)
time.interpolationStrategy = strategy
let text = AttributedString("Time: ") + time + AttributedString(" " + unit)
let storage = try NSAttributedString(text, including: \.openSwiftUI)
var layout = TextLayoutProperties()
layout.writingMode = writingMode
return ResolvedStyledText.styledText(
storage: storage,
layoutProperties: layout,
features: [.checkInterpolationStrategy]
)
}
}
#endif
Loading