-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTeleprompterPage.ReaderAlignment.cs
More file actions
225 lines (192 loc) · 7.49 KB
/
TeleprompterPage.ReaderAlignment.cs
File metadata and controls
225 lines (192 loc) · 7.49 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System.Globalization;
using PrompterOne.Shared.Contracts;
namespace PrompterOne.Shared.Pages;
public partial class TeleprompterPage
{
private const double ReaderAlignmentChangeThresholdPixels = 0.5d;
private const string ReaderTextNoTransitionStyle = "transition:none;";
private readonly Dictionary<int, string> _readerCardTextStyles = [];
private readonly HashSet<int> _readerCardsPendingTransitionRestore = [];
private readonly HashSet<int> _readerCardsWithoutTransition = [];
private bool _pendingReaderAlignment;
private bool _pendingReaderAlignmentInstant;
private async Task AlignActiveReaderTextAsync()
{
if (!_pendingReaderAlignment || _cards.Count == 0 || _activeReaderCardIndex >= _cards.Count)
{
return;
}
var instant = _pendingReaderAlignmentInstant;
_pendingReaderAlignment = false;
_pendingReaderAlignmentInstant = false;
await AlignReaderCardTextAsync(
_activeReaderCardIndex,
Math.Max(_activeReaderWordIndex, 0),
neutralizeCard: false,
rerender: true,
instantTransition: instant);
// Font-size, text-width, or focal-point changes rewrite every
// word's pixel position. The focus lens caches the last word's
// bounding rect, so without a reposition call here it would
// point at stale pixels after the settings change. Re-slide to
// the current active word (or active pause) so the lens
// follows the new layout.
if (_activeReaderPauseChunkIndex is { } pauseChunkIndex)
{
await SlideFocusLensToPauseAsync(pauseChunkIndex);
}
else if (_activeReaderWordIndex >= 0)
{
await SlideFocusLensToActiveWordAsync(_activeReaderWordIndex);
}
}
private string BuildReaderCardTextStyle(int cardIndex)
{
var hasStyle = _readerCardTextStyles.TryGetValue(cardIndex, out var style);
var disableTransition = _readerCardsWithoutTransition.Contains(cardIndex);
if (!hasStyle && !disableTransition)
{
return string.Empty;
}
if (!hasStyle)
{
return ReaderTextNoTransitionStyle;
}
var resolvedStyle = style ?? string.Empty;
return disableTransition
? resolvedStyle + ReaderTextNoTransitionStyle
: resolvedStyle;
}
private void RequestReaderAlignment(bool instant = false)
{
_pendingReaderAlignment = true;
_pendingReaderAlignmentInstant |= instant;
}
private void ResetReaderAlignmentState()
{
_readerCardTextStyles.Clear();
_readerCardsPendingTransitionRestore.Clear();
_readerCardsWithoutTransition.Clear();
_pendingReaderAlignment = true;
_pendingReaderAlignmentInstant = true;
}
private Task PrepareReaderCardAlignmentAsync(int cardIndex, int wordOrdinal) =>
// Card preparation positions the incoming card's cluster text so
// word 0 already sits on the focal line BEFORE the card fades in.
// `instantTransition: true` prevents the text from visibly sliding
// from y=0 to its final offset while the card is already on
// screen — otherwise the user sees the text "jump back" once the
// first word activates.
AlignReaderCardTextAsync(cardIndex, wordOrdinal, neutralizeCard: true, rerender: false, instantTransition: true);
private Task AlignReaderWordBeforeActivationAsync(int cardIndex, int wordOrdinal) =>
AlignReaderCardTextAsync(cardIndex, wordOrdinal, neutralizeCard: false, rerender: false, instantTransition: false);
private async Task RestorePendingReaderTextTransitionsAsync()
{
if (_readerCardsPendingTransitionRestore.Count == 0)
{
return;
}
var cardsToRestore = _readerCardsPendingTransitionRestore.ToArray();
_readerCardsPendingTransitionRestore.Clear();
var changed = false;
foreach (var cardIndex in cardsToRestore)
{
changed |= _readerCardsWithoutTransition.Remove(cardIndex);
}
if (changed)
{
await InvokeAsync(StateHasChanged);
}
}
private async Task AlignReaderCardTextAsync(
int cardIndex,
int wordOrdinal,
bool neutralizeCard,
bool rerender,
bool instantTransition)
{
if (!TryGetAlignmentWordId(cardIndex, wordOrdinal, out var wordId))
{
return;
}
var offsetPixels = await ReaderInterop.MeasureClusterOffsetAsync(
UiDomIds.Teleprompter.Stage,
UiDomIds.Teleprompter.CardText(cardIndex),
wordId,
_readerFocalPointPercent,
neutralizeCard);
if (!offsetPixels.HasValue)
{
return;
}
var nextStyle = BuildReaderCardTextStyleValue(offsetPixels.Value);
var currentStyle = _readerCardTextStyles.TryGetValue(cardIndex, out var existingStyle)
? existingStyle
: string.Empty;
var currentTransitionMode = _readerCardsWithoutTransition.Contains(cardIndex);
if (string.Equals(currentStyle, nextStyle, StringComparison.Ordinal) &&
currentTransitionMode == instantTransition)
{
return;
}
_readerCardTextStyles[cardIndex] = nextStyle;
if (instantTransition)
{
_readerCardsWithoutTransition.Add(cardIndex);
_readerCardsPendingTransitionRestore.Add(cardIndex);
}
else
{
_readerCardsWithoutTransition.Remove(cardIndex);
_readerCardsPendingTransitionRestore.Remove(cardIndex);
}
if (rerender)
{
await InvokeAsync(StateHasChanged);
}
}
private bool TryGetAlignmentWordId(int cardIndex, int wordOrdinal, out string wordId)
{
wordId = string.Empty;
if (!TryGetAlignmentWordPosition(cardIndex, wordOrdinal, out var position))
{
return false;
}
wordId = UiDomIds.Teleprompter.CardWord(position.CardIndex, position.ChunkIndex, position.WordIndex);
return true;
}
private bool TryGetAlignmentWordPosition(int cardIndex, int wordOrdinal, out (int CardIndex, int ChunkIndex, int WordIndex) position)
{
position = default;
if (cardIndex < 0 || cardIndex >= _cards.Count)
{
return false;
}
var remainingWords = Math.Max(wordOrdinal, 0);
var chunks = _cards[cardIndex].Chunks;
for (var chunkIndex = 0; chunkIndex < chunks.Count; chunkIndex++)
{
if (chunks[chunkIndex] is not ReaderGroupViewModel group)
{
continue;
}
for (var wordIndex = 0; wordIndex < group.Words.Count; wordIndex++)
{
if (remainingWords == 0)
{
position = (cardIndex, chunkIndex, wordIndex);
return true;
}
remainingWords--;
}
}
return false;
}
private static string BuildReaderCardTextStyleValue(double offsetPixels)
{
var roundedOffset = Math.Round(offsetPixels, 2, MidpointRounding.AwayFromZero);
return Math.Abs(roundedOffset) <= ReaderAlignmentChangeThresholdPixels
? string.Empty
: $"transform:translateY({roundedOffset.ToString("0.##", CultureInfo.InvariantCulture)}px);";
}
}