99using Microsoft . UI . Windowing ;
1010using Microsoft . UI . Xaml ;
1111using Microsoft . UI . Xaml . Controls ;
12+ using Microsoft . UI . Xaml . Documents ;
1213using Microsoft . UI . Xaml . Media . Animation ;
1314using System ;
1415using System . Collections . Generic ;
@@ -35,8 +36,8 @@ public sealed partial class TrayWindow : Window
3536 private int _lastWindowHeight ;
3637 private Storyboard ? _currentSb ;
3738
38- private VpnLifecycle prevVpnLifecycle = VpnLifecycle . Stopped ;
39- private RpcLifecycle prevRpcLifecycle = RpcLifecycle . Disconnected ;
39+ private VpnLifecycle curVpnLifecycle = VpnLifecycle . Stopped ;
40+ private RpcLifecycle curRpcLifecycle = RpcLifecycle . Disconnected ;
4041
4142 private readonly IRpcController _rpcController ;
4243 private readonly ICredentialManager _credentialManager ;
@@ -151,54 +152,54 @@ private void SetPageByState(RpcModel rpcModel, CredentialModel credentialModel,
151152 }
152153 }
153154
154- private void NotifyUser ( RpcModel rpcModel )
155+ private void MaybeNotifyUser ( RpcModel rpcModel )
155156 {
156157 // This method is called when the state changes, but we don't want to notify
157158 // 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 ;
160161
161162 if ( ! isRpcLifecycleChanged && ! isVpnLifecycleChanged )
162163 {
163164 return ;
164165 }
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- } ;
173166
174- if ( message . Length > 0 && isVpnLifecycleChanged )
175- message += " " ;
167+ var oldRpcLifeycle = curRpcLifecycle ;
168+ var oldVpnLifecycle = curVpnLifecycle ;
169+ curRpcLifecycle = rpcModel . RpcLifecycle ;
170+ curVpnLifecycle = rpcModel . VpnLifecycle ;
176171
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 > ( ) ;
184173
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+ }
188178
189- if ( _aw . IsVisible )
179+ if ( oldVpnLifecycle != curVpnLifecycle )
190180 {
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+ }
192190 }
193191
194- // Trigger notification
192+ if ( messages . Count == 0 ) return ;
193+ if ( _aw . IsVisible ) return ;
194+
195+ var message = string . Join ( " " , messages ) ;
195196 _userNotifier . ShowActionNotification ( message , string . Empty , null , null , CancellationToken . None ) ;
196197 }
197198
198199 private void RpcController_StateChanged ( object ? _ , RpcModel model )
199200 {
200201 SetPageByState ( model , _credentialManager . GetCachedCredentials ( ) , _syncSessionController . GetState ( ) ) ;
201- NotifyUser ( model ) ;
202+ MaybeNotifyUser ( model ) ;
202203 }
203204
204205 private void CredentialManager_CredentialsChanged ( object ? _ , CredentialModel model )
0 commit comments