Skip to content

Commit ca8fb4a

Browse files
karkarlCopilot
andauthored
fix(chat): stabilize bottom anchoring (#1089)
Use stable native anchoring for streamed chat growth and validate immutable queued tail requests against current identity, bounds, item count, and generation before native navigation. Co-authored-by: Copilot App <[email protected]>
1 parent ff19012 commit ca8fb4a

5 files changed

Lines changed: 324 additions & 50 deletions

File tree

‎src/OpenClaw.Tray.WinUI/Chat/ReactorChatTimeline.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ async Task ToggleSpeechAsync(ChatTimelineItem entry)
100100
var rows = BuildRows(props);
101101
var initialTailRequestKey =
102102
$"{props.Timeline.SessionId ?? "none"}|{props.Timeline.TimelineGeneration}|{props.HistoryRevision}|{props.Timeline.ScrollToBottomToken}";
103+
var displayedTailKey = rows.Count > 0 ? rows[^1].Key : null;
103104
void SetEntryHovered(string entryId, bool isHovered)
104105
{
105106
if (isHovered)
@@ -159,7 +160,9 @@ void SetEntryHovered(string entryId, bool isHovered)
159160
.BindVerticalScrollController(
160161
annotatedScrollBarRef,
161162
rows.Count - 1,
162-
initialTailRequestKey)
163+
rows.Count,
164+
initialTailRequestKey,
165+
displayedTailKey)
163166
.Grid(column: 0)
164167
.AutomationName("Chat messages")
165168
.HAlign(HorizontalAlignment.Stretch)

‎src/OpenClaw.Tray.WinUI/Chat/ReactorItemsViewScrollController.cs‎

Lines changed: 145 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Microsoft.UI.Reactor;
22
using Microsoft.UI.Reactor.Core;
33
using Microsoft.UI.Reactor.Core.V1Protocol;
4-
using Microsoft.UI.Reactor.Hooks;
54
using Microsoft.UI.Reactor.Input;
65
using Microsoft.UI.Xaml;
76
using System.Runtime.CompilerServices;
@@ -15,7 +14,9 @@ file sealed record ItemsViewVerticalScrollControllerElement(
1514
Element Child,
1615
ElementRef<WinUIAnnotatedScrollBar> ScrollBarRef,
1716
int InitialTailIndex,
18-
string InitialTailRequestKey) : Element
17+
int ItemCount,
18+
string InitialTailRequestKey,
19+
string? DisplayedTailKey) : Element
1920
{
2021
static ItemsViewVerticalScrollControllerElement() =>
2122
ControlRegistry.RegisterDecorator<ItemsViewVerticalScrollControllerElement>(
@@ -39,7 +40,11 @@ public UIElement Mount(MountContext context, ItemsViewVerticalScrollControllerEl
3940
((WinUIItemsView)value).VerticalScrollController = scrollBar?.ScrollController);
4041
var positioner = new InitialTailPositioner(itemsView);
4142
Positioners.Add(itemsView, positioner);
42-
positioner.Request(element.InitialTailIndex, element.InitialTailRequestKey);
43+
positioner.Request(
44+
element.InitialTailIndex,
45+
element.ItemCount,
46+
element.InitialTailRequestKey,
47+
element.DisplayedTailKey);
4348
return itemsView;
4449
}
4550

@@ -54,9 +59,16 @@ public UIElement Update(
5459
throw new InvalidOperationException("ItemsView scroll controller binding requires an ItemsView child.");
5560
if (!string.Equals(oldElement.InitialTailRequestKey, newElement.InitialTailRequestKey, StringComparison.Ordinal)
5661
&& Positioners.TryGetValue(itemsView, out var positioner))
57-
positioner.Request(newElement.InitialTailIndex, newElement.InitialTailRequestKey);
62+
positioner.Request(
63+
newElement.InitialTailIndex,
64+
newElement.ItemCount,
65+
newElement.InitialTailRequestKey,
66+
newElement.DisplayedTailKey);
5867
else if (Positioners.TryGetValue(itemsView, out var existingPositioner))
59-
existingPositioner.UpdateTailIndex(newElement.InitialTailIndex);
68+
existingPositioner.UpdateTail(
69+
newElement.InitialTailIndex,
70+
newElement.ItemCount,
71+
newElement.DisplayedTailKey);
6072
return itemsView;
6173
}
6274

@@ -73,15 +85,20 @@ public V1UnmountDisposition Unmount(UnmountContext context, ItemsViewVerticalScr
7385

7486
file sealed class InitialTailPositioner : IDisposable
7587
{
88+
private const double FollowThreshold = 60;
89+
7690
private readonly WinUIItemsView itemsView;
7791
private string? _requestKey;
92+
private string? _displayedTailKey;
7893
private int _tailIndex;
94+
private int _itemCount;
7995
private int _version;
8096
private bool _valid;
8197
private bool _awaitingLayout;
8298
private WinUIScrollView? _awaitingScrollView;
8399
private WinUIScrollView? _scrollView;
84100
private bool _following;
101+
private readonly TailNavigationQueue _tailNavigationQueue = new();
85102
private bool _disposed;
86103

87104
public InitialTailPositioner(WinUIItemsView itemsView)
@@ -91,46 +108,72 @@ public InitialTailPositioner(WinUIItemsView itemsView)
91108
itemsView.Unloaded += OnUnloaded;
92109
}
93110

94-
public void Request(int tailIndex, string requestKey)
111+
public void Request(int tailIndex, int itemCount, string requestKey, string? displayedTailKey)
95112
{
96113
if (_disposed || string.Equals(_requestKey, requestKey, StringComparison.Ordinal))
97114
return;
115+
98116
_requestKey = requestKey;
99117
_version++;
100118
DetachLayout();
101-
_valid = tailIndex >= 0;
102-
if (!_valid) return;
103119
_tailIndex = tailIndex;
104-
SetFollowing(true);
105-
if (itemsView.IsLoaded) AwaitLayout();
120+
_itemCount = itemCount;
121+
_displayedTailKey = displayedTailKey;
122+
_following = true;
123+
_valid = TailNavigationPolicy.TryCapture(tailIndex, displayedTailKey, itemCount, out _);
124+
if (!_valid)
125+
return;
126+
127+
if (itemsView.IsLoaded)
128+
AwaitLayout();
106129
}
107130

108-
public void UpdateTailIndex(int tailIndex)
131+
public void UpdateTail(int tailIndex, int itemCount, string? displayedTailKey)
109132
{
133+
var changed = !string.Equals(_displayedTailKey, displayedTailKey, StringComparison.Ordinal);
110134
_tailIndex = tailIndex;
135+
_itemCount = itemCount;
136+
_displayedTailKey = displayedTailKey;
137+
_valid = TailNavigationPolicy.TryCapture(tailIndex, displayedTailKey, itemCount, out var request);
138+
if (!_valid)
139+
{
140+
_tailNavigationQueue.Clear();
141+
return;
142+
}
143+
144+
if (changed && _following && itemsView.IsLoaded)
145+
{
146+
QueueTailRequest(_version, request);
147+
}
111148
}
112149

113150
private void OnLoaded(object sender, RoutedEventArgs args)
114151
{
115-
if (_valid) AwaitLayout();
152+
if (_valid)
153+
AwaitLayout();
116154
}
117155

118156
private void AwaitLayout()
119157
{
120-
if (_disposed || !_valid || !itemsView.IsLoaded || _awaitingLayout) return;
158+
if (_disposed || !_valid || !itemsView.IsLoaded || _awaitingLayout)
159+
return;
160+
121161
if (itemsView.ScrollView is { IsLoaded: false } scrollView)
122162
{
123163
_awaitingScrollView = scrollView;
124164
scrollView.Loaded += OnScrollViewLoaded;
125165
return;
126166
}
167+
127168
_awaitingLayout = true;
128169
itemsView.LayoutUpdated += OnLayoutUpdated;
129170
}
130171

131172
private void OnScrollViewLoaded(object sender, RoutedEventArgs args)
132173
{
133-
if (sender is WinUIScrollView scrollView) scrollView.Loaded -= OnScrollViewLoaded;
174+
if (sender is WinUIScrollView scrollView)
175+
scrollView.Loaded -= OnScrollViewLoaded;
176+
134177
_awaitingScrollView = null;
135178
AwaitLayout();
136179
}
@@ -143,68 +186,103 @@ private void OnLayoutUpdated(object? sender, object args)
143186
AwaitLayout();
144187
return;
145188
}
189+
146190
var version = _version;
147-
var index = _tailIndex;
191+
if (!TailNavigationPolicy.TryCapture(_tailIndex, _displayedTailKey, _itemCount, out var request))
192+
return;
193+
148194
itemsView.DispatcherQueue.TryEnqueue(() =>
149195
{
150196
if (_disposed || !_valid || !itemsView.IsLoaded || version != _version
151197
|| itemsView.ScrollView is not { IsLoaded: true })
152198
{
153-
if (!_disposed && _valid) AwaitLayout();
199+
if (!_disposed && _valid)
200+
AwaitLayout();
154201
return;
155202
}
156-
itemsView.StartBringItemIntoView(index, new BringIntoViewOptions
157-
{
158-
AnimationDesired = false,
159-
VerticalAlignmentRatio = 1.0,
160-
});
203+
161204
AttachScrollView();
162-
ApplyFollowAnchor();
205+
if (!StartTailRequest(request) && !_disposed && _valid)
206+
AwaitLayout();
163207
});
164208
}
165209

166210
private void AttachScrollView()
167211
{
168-
if (ReferenceEquals(_scrollView, itemsView.ScrollView))
212+
var nextScrollView = itemsView.ScrollView;
213+
if (ReferenceEquals(_scrollView, nextScrollView))
169214
return;
170215

216+
DetachScrollView();
217+
_scrollView = nextScrollView;
171218
if (_scrollView is not null)
172-
_scrollView.ViewChanged -= OnViewChanged;
173-
_scrollView = itemsView.ScrollView;
174-
if (_scrollView is not null)
219+
{
220+
_scrollView.VerticalAnchorRatio = 1.0;
175221
_scrollView.ViewChanged += OnViewChanged;
222+
}
176223
}
177224

178225
private void OnViewChanged(WinUIScrollView sender, object args)
179226
{
180-
if (_tailIndex < 0
181-
|| !itemsView.TryGetItemIndex(0.5, 1.0, out var bottomIndex))
182-
{
183-
return;
184-
}
185-
186-
SetFollowing(bottomIndex >= _tailIndex);
227+
_following = IsNearBottom(sender);
187228
}
188229

189-
private void SetFollowing(bool following)
230+
private void QueueTailRequest(int version, TailNavigationRequest request)
190231
{
191-
if (_following == following)
232+
if (!_tailNavigationQueue.Enqueue(version, request))
192233
return;
193234

194-
_following = following;
195-
ApplyFollowAnchor();
235+
if (!itemsView.DispatcherQueue.TryEnqueue(() =>
236+
{
237+
if (!_tailNavigationQueue.TryDequeue(_version, out var queuedRequest)
238+
|| _disposed
239+
|| !_valid
240+
|| !itemsView.IsLoaded
241+
|| !_following)
242+
{
243+
return;
244+
}
245+
246+
StartTailRequest(queuedRequest);
247+
}))
248+
{
249+
_tailNavigationQueue.SchedulingFailed();
250+
_following = false;
251+
}
196252
}
197253

198-
private void ApplyFollowAnchor()
254+
private bool StartTailRequest(TailNavigationRequest request)
199255
{
200-
if (itemsView.ScrollView is { IsLoaded: true } scrollView)
201-
scrollView.VerticalAnchorRatio = _following ? 1.0 : double.NaN;
256+
if (itemsView.ScrollView is not { IsLoaded: true })
257+
return false;
258+
259+
if (!TailNavigationPolicy.CanExecute(
260+
request,
261+
_tailIndex,
262+
_displayedTailKey,
263+
_itemCount))
264+
{
265+
return false;
266+
}
267+
268+
_following = true;
269+
itemsView.StartBringItemIntoView(request.Index, new BringIntoViewOptions
270+
{
271+
AnimationDesired = false,
272+
VerticalAlignmentRatio = 1.0,
273+
});
274+
return true;
202275
}
203276

277+
private static bool IsNearBottom(WinUIScrollView scrollView) =>
278+
scrollView.ScrollableHeight - scrollView.VerticalOffset <= FollowThreshold;
279+
204280
private void OnUnloaded(object sender, RoutedEventArgs args)
205281
{
206282
_version++;
283+
_tailNavigationQueue.Clear();
207284
DetachLayout();
285+
DetachScrollView();
208286
}
209287

210288
private void DetachLayout()
@@ -214,22 +292,35 @@ private void DetachLayout()
214292
scrollView.Loaded -= OnScrollViewLoaded;
215293
_awaitingScrollView = null;
216294
}
295+
217296
if (_awaitingLayout)
218297
{
219298
itemsView.LayoutUpdated -= OnLayoutUpdated;
220299
_awaitingLayout = false;
221300
}
222301
}
223302

303+
private void DetachScrollView()
304+
{
305+
if (_scrollView is not null)
306+
{
307+
_scrollView.VerticalAnchorRatio = double.NaN;
308+
_scrollView.ViewChanged -= OnViewChanged;
309+
}
310+
311+
_scrollView = null;
312+
}
313+
224314
public void Dispose()
225315
{
226-
if (_disposed) return;
316+
if (_disposed)
317+
return;
318+
227319
_disposed = true;
228320
_version++;
321+
_tailNavigationQueue.Clear();
229322
DetachLayout();
230-
if (_scrollView is not null)
231-
_scrollView.ViewChanged -= OnViewChanged;
232-
_scrollView = null;
323+
DetachScrollView();
233324
itemsView.Loaded -= OnLoaded;
234325
itemsView.Unloaded -= OnUnloaded;
235326
}
@@ -241,6 +332,14 @@ public static Element BindVerticalScrollController<T>(
241332
this ItemsViewElement<T> itemsView,
242333
ElementRef<WinUIAnnotatedScrollBar> scrollBarRef,
243334
int initialTailIndex,
244-
string initialTailRequestKey) =>
245-
new ItemsViewVerticalScrollControllerElement(itemsView, scrollBarRef, initialTailIndex, initialTailRequestKey);
335+
int itemCount,
336+
string initialTailRequestKey,
337+
string? displayedTailKey) =>
338+
new ItemsViewVerticalScrollControllerElement(
339+
itemsView,
340+
scrollBarRef,
341+
initialTailIndex,
342+
itemCount,
343+
initialTailRequestKey,
344+
displayedTailKey);
246345
}

0 commit comments

Comments
 (0)