-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathAnimatedListHandler.cs
194 lines (161 loc) · 5.48 KB
/
AnimatedListHandler.cs
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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using Telegram.Controls;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
namespace Telegram.Common
{
public enum AnimatedListType
{
Stickers,
Animations,
Emoji,
Other // Inline bots, chat list,
}
public enum AnimatedImageType
{
Sticker,
Animation,
Emoji,
Other
}
public partial class AnimatedListHandler
{
private readonly ListViewBase _listView;
private readonly DispatcherTimer _debouncer;
private readonly AnimatedListType _type;
private bool _unloaded;
public AnimatedListHandler(ListViewBase listView, AnimatedListType type)
{
_listView = listView;
_listView.SizeChanged += OnSizeChanged;
_listView.Unloaded += OnUnloaded;
_debouncer = new DispatcherTimer();
_debouncer.Interval = TimeSpan.FromMilliseconds(Constants.AnimatedThrottle);
_debouncer.Tick += (s, args) =>
{
_debouncer.Stop();
LoadVisibleItems();
};
_type = type;
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (sender is ListViewBase)
{
_listView.SizeChanged -= OnSizeChanged;
_listView.Items.VectorChanged += OnVectorChanged;
var scrollViewer = _listView.GetScrollViewer();
if (scrollViewer != null)
{
scrollViewer.ViewChanged += OnViewChanged;
}
var panel = _listView.ItemsPanelRoot;
if (panel != null)
{
panel.SizeChanged += OnSizeChanged;
}
}
else if (e.PreviousSize.Width < _listView.ActualWidth || e.PreviousSize.Height < _listView.ActualHeight)
{
ThrottleVisibleItems();
}
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
UnloadItems();
}
private void OnVectorChanged(IObservableVector<object> sender, IVectorChangedEventArgs e)
{
if (_unloaded)
{
return;
}
ThrottleVisibleItems();
}
private void OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
LoadVisibleItems();
ThrottleVisibleItems();
}
//private bool _throttling;
public void ThrottleVisibleItems()
{
//if (_throttling)
//{
// return;
//}
//_throttling = true;
//VisualUtilities.QueueCallbackForCompositionRendering(LoadVisibleItems);
if (_debouncer.IsEnabled)
{
return;
}
_debouncer.Stop();
_debouncer.Start();
}
public bool IsDisabledByPolicy
{
get => _type switch
{
AnimatedListType.Stickers => !PowerSavingPolicy.AutoPlayStickers,
AnimatedListType.Animations => !PowerSavingPolicy.AutoPlayAnimations,
AnimatedListType.Emoji => !PowerSavingPolicy.AutoPlayEmoji,
_ => false
};
}
public void LoadVisibleItems() => UpdateVisibleItems(true);
public void UnloadVisibleItems() => UpdateVisibleItems(false);
public void UnloadItems() => UpdateVisibleItems(false);
public void UpdateVisibleItems(bool load)
{
//_throttling = false;
int lastVisibleIndex;
int firstVisibleIndex;
int lastCacheIndex;
int firstCacheIndex;
if (_listView.ItemsPanelRoot is ItemsStackPanel stack)
{
lastCacheIndex = stack.LastCacheIndex;
firstCacheIndex = stack.FirstCacheIndex;
lastVisibleIndex = stack.LastVisibleIndex;
firstVisibleIndex = stack.FirstVisibleIndex;
}
else if (_listView.ItemsPanelRoot is ItemsWrapGrid wrap)
{
lastCacheIndex = wrap.LastCacheIndex;
firstCacheIndex = wrap.FirstCacheIndex;
lastVisibleIndex = wrap.LastVisibleIndex;
firstVisibleIndex = wrap.FirstVisibleIndex;
}
else
{
return;
}
if (lastCacheIndex < firstCacheIndex || firstCacheIndex < 0)
{
return;
}
for (int i = firstCacheIndex; i <= lastCacheIndex; i++)
{
var container = _listView.ContainerFromIndex(i) as SelectorItem;
if (container == null || container.ContentTemplateRoot is not FrameworkElement content)
{
continue;
}
var within = load && i >= firstVisibleIndex && i <= lastVisibleIndex;
var player = content as IPlayerView;
player ??= content.FindName("Player") as IPlayerView;
player?.ViewportChanged(within);
}
_unloaded = !load;
}
}
}