Skip to content

Commit f2b3cc4

Browse files
committed
Add rendering animation.
1 parent 64b4f9d commit f2b3cc4

File tree

5 files changed

+43
-57
lines changed

5 files changed

+43
-57
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ A SwiftUI view that renders LaTeX equations.
2323
- [Format Equation Number](#format-equation-number)
2424
- [Unencode HTML](#🔗-unencode-html)
2525
- [Rendering Style](#🕶️-rendering-style)
26-
- [Animated](#🪩-animated)
26+
- [Rendering Animation](#🪩-animated)
2727
- [Caching](#🗄️-caching)
2828
- [Preloading](#🏃‍♀️-preloading)
2929

@@ -47,7 +47,7 @@ It won't
4747
Add the dependency to your package manifest file.
4848

4949
```swift
50-
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.1")
50+
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.2")
5151
```
5252

5353
## ⌨️ Usage
@@ -237,14 +237,14 @@ The view has four rendering styles. The `wait` style is the default style, and l
237237
| `wait` | No | *(default)* The view blocks the main queue until its finished rendering. |
238238

239239

240-
#### 🪩 Animated
240+
#### 🪩 Rendering Animation
241241

242-
The `animated` modifier applies to the view when using the asynchronous rendering styles `empty`, `original`, or `progress`.
242+
When using the asynchronous rendering styles `empty`, `original`, or `progress`, use this modifier to determine the animation applied to the transition between views. The default value is `none`.
243243

244244
```swift
245245
LaTeX(input)
246246
.renderingStyle(.original)
247-
.animated()
247+
.renderingAnimation(.easeIn)
248248
```
249249

250250
> In the above example, the input text will be displayed until the SVGs have been rendered at which point the rendered views will animate in to view.

Sources/LaTeXSwiftUI/Extensions/EnvironmentValues+Extensions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ private struct RenderingStyleKey: EnvironmentKey {
7070
static let defaultValue: LaTeX.RenderingStyle = .wait
7171
}
7272

73-
private struct AnimatedKey: EnvironmentKey {
74-
static let defaultValue: Bool = false
73+
private struct RenderingAnimationKey: EnvironmentKey {
74+
static let defaultValue: Animation? = .none
7575
}
7676

7777
extension EnvironmentValues {
@@ -143,9 +143,9 @@ extension EnvironmentValues {
143143
}
144144

145145
/// Whether or not rendering should be animated.
146-
var animated: Bool {
147-
get { self[AnimatedKey.self] }
148-
set { self[AnimatedKey.self] = newValue }
146+
var renderingAnimation: Animation? {
147+
get { self[RenderingAnimationKey.self] }
148+
set { self[RenderingAnimationKey.self] = newValue }
149149
}
150150

151151
}

Sources/LaTeXSwiftUI/Extensions/View+Extensions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ public extension View {
154154
environment(\.renderingStyle, style)
155155
}
156156

157-
/// Sets whether or not the view will animate its render state changes.
157+
/// Sets the animation the view should apply to its rendered images.
158158
///
159-
/// - Parameter animate: Whether animations are enabled.
160-
/// - Returns: A view that animates its rendering.
161-
func animated(_ animate: Bool = true) -> some View {
162-
environment(\.animated, animate)
159+
/// - Parameter animation: The animation.
160+
/// - Returns: A view that animates its rendered state.
161+
func renderingAnimation(_ animation: Animation?) -> some View {
162+
environment(\.renderingAnimation, animation)
163163
}
164164

165165
}

Sources/LaTeXSwiftUI/LaTeX.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public struct LaTeX: View {
145145
/// The view's rendering style.
146146
@Environment(\.renderingStyle) private var renderingStyle
147147

148-
/// Whether or not rendering should be animated.
149-
@Environment(\.animated) private var animated
148+
/// The animation the view should apply to its rendered images.
149+
@Environment(\.renderingAnimation) private var renderingAnimation
150150

151151
/// The view's current display scale.
152152
@Environment(\.displayScale) private var displayScale
@@ -188,24 +188,27 @@ public struct LaTeX: View {
188188
// MARK: View body
189189

190190
public var body: some View {
191-
if renderState.rendered {
192-
bodyWithBlocks(renderState.blocks)
193-
}
194-
else {
195-
switch renderingStyle {
196-
case .empty:
197-
Text("")
198-
.task(render)
199-
case .original:
200-
Text(latex)
201-
.task(render)
202-
case .progress:
203-
ProgressView()
204-
.task(render)
205-
case .wait:
206-
bodyWithBlocks(syncBlocks)
191+
VStack(spacing: 0) {
192+
if renderState.rendered {
193+
bodyWithBlocks(renderState.blocks)
194+
}
195+
else {
196+
switch renderingStyle {
197+
case .empty:
198+
Text("")
199+
.task(render)
200+
case .original:
201+
Text(latex)
202+
.task(render)
203+
case .progress:
204+
ProgressView()
205+
.task(render)
206+
case .wait:
207+
bodyWithBlocks(syncBlocks)
208+
}
207209
}
208210
}
211+
.animation(renderingAnimation, value: renderState.rendered)
209212
}
210213

211214
}
@@ -229,7 +232,6 @@ extension LaTeX {
229232
/// Renders the view's components.
230233
@Sendable private func render() async {
231234
await renderState.render(
232-
animated: animated,
233235
unencodeHTML: unencodeHTML,
234236
parsingMode: parsingMode,
235237
font: font,
@@ -375,11 +377,12 @@ struct LaTeX_Previews: PreviewProvider {
375377

376378
LaTeX("Hello, $\\LaTeX$!")
377379
.renderingStyle(.original)
380+
.renderingAnimation(.default)
378381

379382
LaTeX("Hello, $\\LaTeX$!")
380383
.renderingStyle(.progress)
384+
.renderingAnimation(.easeIn)
381385
}
382-
.animated()
383386
.previewDisplayName("Rendering Style and Animated")
384387
}
385388

Sources/LaTeXSwiftUI/Models/LaTeXRenderState.swift

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,19 @@ extension LaTeXRenderState {
5959
/// Renders the views components.
6060
///
6161
/// - Parameters:
62-
/// - animated: The `animated` environment variable.
6362
/// - unencodeHTML: The `unencodeHTML` environment variable.
6463
/// - parsingMode: The `parsingMode` environment variable.
6564
/// - font: The `font environment` variable.
6665
/// - displayScale: The `displayScale` environment variable.
6766
/// - texOptions: The `texOptions` environment variable.
68-
func render(animated: Bool, unencodeHTML: Bool, parsingMode: LaTeX.ParsingMode, font: Font?, displayScale: CGFloat, texOptions: TeXInputProcessorOptions) async {
67+
func render(unencodeHTML: Bool, parsingMode: LaTeX.ParsingMode, font: Font?, displayScale: CGFloat, texOptions: TeXInputProcessorOptions) async {
6968
let isRen = await isRendering
7069
let ren = await rendered
7170
guard !isRen && !ren else {
7271
return
7372
}
7473
await MainActor.run {
75-
if animated {
76-
withAnimation {
77-
isRendering = true
78-
}
79-
}
80-
else {
81-
isRendering = true
82-
}
74+
isRendering = true
8375
}
8476

8577
let renderedBlocks = await Renderer.shared.render(
@@ -89,18 +81,9 @@ extension LaTeXRenderState {
8981
texOptions: texOptions)
9082

9183
await MainActor.run {
92-
if animated {
93-
withAnimation {
94-
blocks = renderedBlocks
95-
isRendering = false
96-
rendered = true
97-
}
98-
}
99-
else {
100-
blocks = renderedBlocks
101-
isRendering = false
102-
rendered = true
103-
}
84+
blocks = renderedBlocks
85+
isRendering = false
86+
rendered = true
10487
}
10588
}
10689

0 commit comments

Comments
 (0)