-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathGroupCallActiveHeader.xaml.cs
228 lines (200 loc) · 7.41 KB
/
GroupCallActiveHeader.xaml.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
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
226
227
228
//
// 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 System.Collections.Generic;
using System.Numerics;
using Telegram.Common;
using Telegram.Composition;
using Telegram.Native.Calls;
using Telegram.Services.Calls;
using Telegram.Td.Api;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Telegram.Controls
{
public sealed partial class GroupCallActiveHeader : UserControl
{
private readonly CompositionCurveVisual _curveVisual;
private VoipCallBase _call;
public GroupCallActiveHeader()
{
InitializeComponent();
// This should be used as a mask to apply a gradient to another visual
_curveVisual = new CompositionCurveVisual(Curve, 0, 0, 1.5f);
_curveVisual.SetColorStops(0xFF59c7f8, 0xFF0078ff);
}
public void Update(VoipCallBase value)
{
if (_call is VoipCall oldPrivateCall)
{
oldPrivateCall.MediaStateChanged -= OnMediaStateChanged;
oldPrivateCall.AudioLevelUpdated -= OnAudioLevelUpdated;
}
else if (_call is VoipGroupCall oldGroupCall)
{
oldGroupCall.MutedChanged -= OnMutedChanged;
oldGroupCall.AudioLevelsUpdated -= OnAudioLevelsUpdated;
}
_call = value;
if (_call is VoipCall newPrivateCall)
{
newPrivateCall.MediaStateChanged += OnMediaStateChanged;
newPrivateCall.AudioLevelUpdated += OnAudioLevelUpdated;
StartAnimating();
UpdateCurveColors(newPrivateCall.AudioState == VoipAudioState.Muted);
Audio.Visibility = Visibility.Visible;
Dismiss.Visibility = Visibility.Visible;
try
{
if (newPrivateCall.ClientService.TryGetUser(newPrivateCall.UserId, out User user))
{
TitleInfo.Text = user.FullName();
}
}
catch
{
// TODO: there's a race condition happening here for obvious reasons.
// Try-catching until the code is actually properly refactored.
}
}
else if (_call is VoipGroupCall newGroupCall)
{
newGroupCall.MutedChanged += OnMutedChanged;
newGroupCall.AudioLevelsUpdated += OnAudioLevelsUpdated;
StartAnimating();
UpdateCurveColors(newGroupCall.IsMuted);
Audio.Visibility = Visibility.Visible;
Dismiss.Visibility = Visibility.Visible;
TitleInfo.Text = newGroupCall.GetTitle();
Audio.IsChecked = !newGroupCall.IsMuted;
Automation.SetToolTip(Audio, newGroupCall.IsMuted ? Strings.VoipGroupUnmute : Strings.VoipGroupMute);
}
else
{
_curveVisual.StopAnimating();
_curveVisual.Clear();
}
}
private void UpdateCurveColors(bool muted)
{
// TODO: there are multiple states to be supported: connecting, active, speaking, can't speak, late
if (PowerSavingPolicy.AreMaterialsEnabled && ApiInfo.CanAnimatePaths)
{
if (muted)
{
_curveVisual.SetColorStops(0xFF59c7f8, 0xFF0078ff);
}
else
{
_curveVisual.SetColorStops(0xFF0078ff, 0xFF33c659);
}
}
else
{
if (muted)
{
RootGrid.Background = ColorsHelper.LinearGradient(0xFF59c7f8, 0xFF0078ff);
}
else
{
RootGrid.Background = ColorsHelper.LinearGradient(0xFF0078ff, 0xFF33c659);
}
}
}
private void OnMutedChanged(object sender, EventArgs e)
{
if (sender is VoipGroupCall groupCall && groupCall.IsMuted is bool groupMuted)
{
this.BeginOnUIThread(() =>
{
UpdateCurveColors(groupMuted);
Audio.IsChecked = !groupMuted;
Automation.SetToolTip(Audio, groupMuted ? Strings.VoipGroupUnmute : Strings.VoipGroupMute);
});
}
}
private void OnMediaStateChanged(VoipCall sender, VoipCallMediaStateChangedEventArgs args)
{
this.BeginOnUIThread(() => UpdateCurveColors(args.Audio == VoipAudioState.Muted));
}
private void OnAudioLevelsUpdated(VoipGroupCall sender, IList<VoipGroupParticipant> args)
{
if (PowerSavingPolicy.AreMaterialsEnabled && ApiInfo.CanAnimatePaths)
{
var average = 0f;
foreach (var level in args)
{
if (level.AudioSource == 0)
{
if (sender.IsMuted)
{
average = MathF.Max(average, 0);
}
else
{
average = MathF.Max(average, level.Level); // MathF.Max(average, Math.Min(8500, level.Level * 4000) / 8500);
}
}
else
{
average = MathF.Max(average, Math.Clamp(level.Level * 15f / 80f, 0, 1));
}
}
_curveVisual.UpdateLevel(average);
}
}
private void OnAudioLevelUpdated(VoipCall sender, VoipCallAudioLevelUpdatedEventArgs args)
{
if (PowerSavingPolicy.AreMaterialsEnabled && ApiInfo.CanAnimatePaths)
{
_curveVisual.UpdateLevel(args.AudioLevel);
}
}
private void Audio_Click(object sender, RoutedEventArgs e)
{
if (_call is VoipCall privateCall)
{
privateCall.AudioState = privateCall.AudioState == VoipAudioState.Muted
? VoipAudioState.Active
: VoipAudioState.Muted;
}
else if (_call is VoipGroupCall groupCall)
{
groupCall.IsMuted = !groupCall.IsMuted;
}
}
private void Dismiss_Click(object sender, RoutedEventArgs e)
{
_call?.Discard();
}
private void Title_Click(object sender, RoutedEventArgs e)
{
_call?.Show();
}
private void Curve_SizeChanged(object sender, SizeChangedEventArgs e)
{
_curveVisual.ActualSize = e.NewSize.ToVector2();
StartAnimating();
}
private void StartAnimating()
{
if (_curveVisual.ActualSize.X == 0 || _curveVisual.ActualSize.Y == 0)
{
return;
}
if (PowerSavingPolicy.AreMaterialsEnabled && ApiInfo.CanAnimatePaths)
{
_curveVisual.StartAnimating();
}
else
{
_curveVisual.StopAnimating();
_curveVisual.Clear();
}
}
}
}