9
9
using Microsoft . UI . Windowing ;
10
10
using Microsoft . UI . Xaml ;
11
11
using Microsoft . UI . Xaml . Controls ;
12
+ using Microsoft . UI . Xaml . Documents ;
12
13
using Microsoft . UI . Xaml . Media . Animation ;
13
14
using System ;
14
15
using System . Collections . Generic ;
@@ -35,8 +36,8 @@ public sealed partial class TrayWindow : Window
35
36
private int _lastWindowHeight ;
36
37
private Storyboard ? _currentSb ;
37
38
38
- private VpnLifecycle prevVpnLifecycle = VpnLifecycle . Stopped ;
39
- private RpcLifecycle prevRpcLifecycle = RpcLifecycle . Disconnected ;
39
+ private VpnLifecycle curVpnLifecycle = VpnLifecycle . Stopped ;
40
+ private RpcLifecycle curRpcLifecycle = RpcLifecycle . Disconnected ;
40
41
41
42
private readonly IRpcController _rpcController ;
42
43
private readonly ICredentialManager _credentialManager ;
@@ -151,54 +152,54 @@ private void SetPageByState(RpcModel rpcModel, CredentialModel credentialModel,
151
152
}
152
153
}
153
154
154
- private void NotifyUser ( RpcModel rpcModel )
155
+ private void MaybeNotifyUser ( RpcModel rpcModel )
155
156
{
156
157
// This method is called when the state changes, but we don't want to notify
157
158
// the user if the state hasn't changed.
158
- var isRpcLifecycleChanged = rpcModel . RpcLifecycle == RpcLifecycle . Disconnected && prevRpcLifecycle != rpcModel . RpcLifecycle ;
159
- var isVpnLifecycleChanged = ( rpcModel . VpnLifecycle == VpnLifecycle . Started || rpcModel . VpnLifecycle == VpnLifecycle . Stopped ) && prevVpnLifecycle != rpcModel . VpnLifecycle ;
159
+ var isRpcLifecycleChanged = rpcModel . RpcLifecycle == RpcLifecycle . Disconnected && curRpcLifecycle != rpcModel . RpcLifecycle ;
160
+ var isVpnLifecycleChanged = ( rpcModel . VpnLifecycle == VpnLifecycle . Started || rpcModel . VpnLifecycle == VpnLifecycle . Stopped ) && curVpnLifecycle != rpcModel . VpnLifecycle ;
160
161
161
162
if ( ! isRpcLifecycleChanged && ! isVpnLifecycleChanged )
162
163
{
163
164
return ;
164
165
}
165
- var message = string . Empty ;
166
- // Compose the message based on the lifecycle changes
167
- if ( isRpcLifecycleChanged )
168
- message += rpcModel . RpcLifecycle switch
169
- {
170
- RpcLifecycle . Disconnected => "Disconnected from Coder background service." ,
171
- _ => "" // This will never be hit.
172
- } ;
173
166
174
- if ( message . Length > 0 && isVpnLifecycleChanged )
175
- message += " " ;
167
+ var oldRpcLifeycle = curRpcLifecycle ;
168
+ var oldVpnLifecycle = curVpnLifecycle ;
169
+ curRpcLifecycle = rpcModel . RpcLifecycle ;
170
+ curVpnLifecycle = rpcModel . VpnLifecycle ;
176
171
177
- if ( isVpnLifecycleChanged )
178
- message += rpcModel . VpnLifecycle switch
179
- {
180
- VpnLifecycle . Started => "Coder Connect started." ,
181
- VpnLifecycle . Stopped => "Coder Connect stopped." ,
182
- _ => "" // This will never be hit.
183
- } ;
172
+ var messages = new List < string > ( ) ;
184
173
185
- // Save state for the next notification check
186
- prevRpcLifecycle = rpcModel . RpcLifecycle ;
187
- prevVpnLifecycle = rpcModel . VpnLifecycle ;
174
+ if ( oldRpcLifeycle != RpcLifecycle . Disconnected && curRpcLifecycle == RpcLifecycle . Disconnected )
175
+ {
176
+ messages . Add ( "Disconnected from Coder background service." ) ;
177
+ }
188
178
189
- if ( _aw . IsVisible )
179
+ if ( oldVpnLifecycle != curVpnLifecycle )
190
180
{
191
- return ; // No need to notify if the window is not visible.
181
+ switch ( curVpnLifecycle )
182
+ {
183
+ case VpnLifecycle . Started :
184
+ messages . Add ( "Coder Connect started." ) ;
185
+ break ;
186
+ case VpnLifecycle . Stopped :
187
+ messages . Add ( "Coder Connect stopped." ) ;
188
+ break ;
189
+ }
192
190
}
193
191
194
- // Trigger notification
192
+ if ( messages . Count == 0 ) return ;
193
+ if ( _aw . IsVisible ) return ;
194
+
195
+ var message = string . Join ( " " , messages ) ;
195
196
_userNotifier . ShowActionNotification ( message , string . Empty , null , null , CancellationToken . None ) ;
196
197
}
197
198
198
199
private void RpcController_StateChanged ( object ? _ , RpcModel model )
199
200
{
200
201
SetPageByState ( model , _credentialManager . GetCachedCredentials ( ) , _syncSessionController . GetState ( ) ) ;
201
- NotifyUser ( model ) ;
202
+ MaybeNotifyUser ( model ) ;
202
203
}
203
204
204
205
private void CredentialManager_CredentialsChanged ( object ? _ , CredentialModel model )
0 commit comments