1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
// Copyright (C) 2024 Apple Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
#if compiler(>=6.0)
#if ENABLE_WRITING_TOOLS && canImport(UIKit)
import OSLog
#if USE_APPLE_INTERNAL_SDK
@_spi(TextEffects) import UIKit
#else
import UIKit_SPI
#endif // USE_APPLE_INTERNAL_SDK
@objc
@implementation
extension WKTextAnimationManager {
private static let logger = Logger(subsystem: "com.apple.WebKit", category: "WKTextAnimationManager")
private final class TextEffectChunk: UITextEffectTextChunk {
let uuid: UUID
init(uuid: UUID) {
self.uuid = uuid
}
}
@MainActor
private final class Base {
var currentEffect: UITextEffectView.EffectID?
}
// Used to workaround the fact that `@objc @implementation` does not support stored properties whose size can change
// due to Library Evolution. Do not use this property directly.
@nonobjc
private let base = Base()
@nonobjc
private final var currentEffect: UITextEffectView.EffectID? {
get { base.currentEffect }
set { base.currentEffect = newValue }
}
@nonobjc
private lazy var effectView = UITextEffectView(source: self)
@nonobjc
private var chunkToEffect: [UUID: UITextEffectView.EffectID] = [:]
private weak var delegate: WKTextAnimationSourceDelegate?
init(delegate: any WKTextAnimationSourceDelegate) {
super.init()
self.delegate = delegate
delegate.containingViewForTextAnimationType().addSubview(self.effectView)
}
@objc(addTextAnimationForAnimationID:withStyleType:)
func addTextAnimation(forAnimationID uuid: UUID, withStyleType style: WKTextAnimationType) {
switch style {
case .initial:
let newEffect = effectView.addEffect(
UITextEffectView.PonderingEffect(chunk: TextEffectChunk(uuid: uuid), view: effectView)
)
chunkToEffect[uuid] = newEffect
case .source:
let newEffect = effectView.addEffect(
UITextEffectView.ReplacementTextEffect(chunk: TextEffectChunk(uuid: uuid), view: effectView, delegate: self)
)
chunkToEffect[uuid] = newEffect
case .final:
break
// Discard `.final` since we don't manually start the 2nd part of the animation on iOS.
@unknown default:
fatalError()
}
}
func removeTextAnimation(forAnimationID uuid: UUID) {
if let effectID = chunkToEffect.removeValue(forKey: uuid) {
effectView.removeEffect(effectID)
}
}
}
@_spi(TextEffects)
extension WKTextAnimationManager: @preconcurrency UITextEffectViewSource {
// This can be made non-public once the retroactive conformance is removed.
// It is currently safe due to the fact that WebKitSwift is not itself public.
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
public func targetedPreview(for chunk: UITextEffectTextChunk) async -> UITargetedPreview {
guard let delegate else {
Self.logger.debug("Can't obtain Targeted Preview. Missing delegate.")
return UITargetedPreview(view: UIView(frame: .zero))
}
let container = delegate.containingViewForTextAnimationType()
let defaultPreview = UITargetedPreview(
view: UIView(frame: .zero),
parameters: .init(),
target: UIPreviewTarget(container: container, center: container.center)
)
guard let uuidChunk = chunk as? TextEffectChunk else {
Self.logger.debug("Can't get text preview. Incorrect UITextEffectTextChunk subclass")
return defaultPreview
}
guard let preview = await delegate.targetedPreview(for: uuidChunk.uuid) else {
Self.logger.debug("Could not generate a UITargetedPreview")
return defaultPreview
}
return preview
}
// This can be made non-public once the retroactive conformance is removed.
// It is currently safe due to the fact that WebKitSwift is not itself public.
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
public func updateTextChunkVisibilityForAnimation(_ chunk: UITextEffectTextChunk, visible: Bool) async {
guard let uuidChunk = chunk as? TextEffectChunk else {
Self.logger.debug("Can't update text visibility. Incorrect UITextEffectTextChunk subclass")
return
}
guard let delegate else {
Self.logger.debug("Can't update Chunk Visibility. Missing delegate.")
return
}
await delegate.updateUnderlyingTextVisibility(forTextAnimationID: uuidChunk.uuid, visible: visible)
}
}
@_spi(TextEffects)
extension WKTextAnimationManager: @preconcurrency UITextEffectView.ReplacementTextEffect.Delegate {
// This can be made non-public once the retroactive conformance is removed.
// It is currently safe due to the fact that WebKitSwift is not itself public.
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
public func performReplacementAndGeneratePreview(
for chunk: UITextEffectTextChunk,
effect: UITextEffectView.ReplacementTextEffect,
animation: UITextEffectView.ReplacementTextEffect.AnimationParameters
) async -> UITargetedPreview? {
guard let uuidChunk = chunk as? TextEffectChunk else {
Self.logger.debug("Can't get text preview. Incorrect UITextEffectTextChunk subclass")
return nil
}
guard let delegate else {
Self.logger.debug("Can't obtain Targeted Preview. Missing delegate.")
return nil
}
let preview = await withCheckedContinuation { continuation in
delegate.callCompletionHandler(forAnimationID: uuidChunk.uuid) { preview in
continuation.resume(returning: preview)
}
}
return preview
}
// This can be made non-public once the retroactive conformance is removed.
// It is currently safe due to the fact that WebKitSwift is not itself public.
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
public func replacementEffectDidComplete(_ effect: UITextEffectView.ReplacementTextEffect) {
effectView.removeEffect(effect.id)
guard let (animationID, _) = chunkToEffect.first(where: { (_, value) in value == effect.id }) else {
return
}
chunkToEffect[animationID] = nil
guard let delegate else {
Self.logger.debug("Missing delegate.")
return
}
delegate.callCompletionHandler(forAnimationID: animationID)
delegate.replacementEffectDidComplete()
}
}
#endif // ENABLE_WRITING_TOOLS && canImport(UIKit)
#endif // compiler(>=6.0)
|