66using VisualStudioDiscordRPC . Shared . Plugs ;
77using VisualStudioDiscordRPC . Shared . Nests ;
88using VisualStudioDiscordRPC . Shared . Nests . Base ;
9+ using System . Windows ;
10+ using VisualStudioDiscordRPC . Shared . Plugs . TimerPlugs ;
911
1012namespace VisualStudioDiscordRPC . Shared
1113{
@@ -14,13 +16,15 @@ public class DiscordRpcController
1416 public const string DefaultApplicationId = "914622396630175855" ;
1517
1618 private static readonly RichPresence HiddenRichPresence ;
19+ private static readonly RichPresence IdlingRichPresence ;
1720
18- private readonly DiscordRpcClient _discordRpcClient ;
19-
2021 private bool _isDirty ;
22+ private DateTime _lastDirtyTime ;
2123 private readonly object _dirtyFlagSync = new object ( ) ;
2224
25+ private readonly DiscordRpcClient _discordRpcClient ;
2326 private readonly LocalizationService _localizationService ;
27+ private readonly SettingsService _settingsService ;
2428
2529 private readonly RichPresence _sharedRichPresence ;
2630 private readonly object _richPresenceSync = new object ( ) ;
@@ -29,6 +33,8 @@ public class DiscordRpcController
2933 private readonly int _sendDataMillisecondsTimeout ;
3034 private bool _sendingThreadCancellation ;
3135
36+ private bool _isIdling ;
37+
3238 private bool _enabled = true ;
3339 public bool Enabled
3440 {
@@ -63,7 +69,7 @@ public bool Secret
6369 set
6470 {
6571 _secret = value ;
66- _isDirty = true ;
72+ SetDirty ( ) ;
6773 }
6874 }
6975
@@ -81,12 +87,23 @@ static DiscordRpcController()
8187 } ,
8288 Details = "This solution is hidden"
8389 } ;
90+
91+ IdlingRichPresence = new RichPresence
92+ {
93+ Assets = new Assets ( )
94+ {
95+ LargeImageKey = "zzz" ,
96+ LargeImageText = "Idling..."
97+ } ,
98+ Details = "Idling..."
99+ } ;
84100 }
85101
86102 public DiscordRpcController ( int updateMillisecondsTimeout )
87103 {
88- var settingsService = ServiceRepository . Default . GetService < SettingsService > ( ) ;
89- var applicationId = settingsService . Read < string > ( SettingsKeys . ApplicationID ) ;
104+ _settingsService = ServiceRepository . Default . GetService < SettingsService > ( ) ;
105+
106+ var applicationId = _settingsService . Read < string > ( SettingsKeys . ApplicationID ) ;
90107
91108 _discordRpcClient = new DiscordRpcClient ( applicationId )
92109 {
@@ -194,10 +211,7 @@ private void OnLocalizationChanged()
194211
195212 private void OnNestChanged ( )
196213 {
197- lock ( _dirtyFlagSync )
198- {
199- _isDirty = true ;
200- }
214+ SetDirty ( ) ;
201215 }
202216
203217 public void RefreshAll ( )
@@ -206,10 +220,17 @@ public void RefreshAll()
206220 {
207221 nest . BasePlug ? . Update ( ) ;
208222 }
223+
224+ SetDirty ( ) ;
225+ }
209226
227+ private void SetDirty ( )
228+ {
210229 lock ( _dirtyFlagSync )
211230 {
212231 _isDirty = true ;
232+ _isIdling = false ;
233+ _lastDirtyTime = DateTime . Now ;
213234 }
214235 }
215236
@@ -228,22 +249,50 @@ private void SendRichPresenceData()
228249
229250 lock ( _dirtyFlagSync )
230251 {
231- if ( _enabled && _isDirty )
252+ if ( _enabled )
232253 {
233- if ( _secret )
254+ if ( ! _isIdling )
234255 {
235- _discordRpcClient . SetPresence ( HiddenRichPresence ) ;
256+ bool detectIdling = _settingsService . Read < bool > ( SettingsKeys . DetectIdling ) ;
257+ if ( detectIdling )
258+ {
259+ TimeSpan timeDiff = DateTime . Now - _lastDirtyTime ;
260+ long idleMinutes = _settingsService . Read ( SettingsKeys . IdleTime , SettingsDefaults . DefaultIdleTime ) ;
261+
262+ if ( timeDiff > TimeSpan . FromMinutes ( idleMinutes ) )
263+ {
264+ OnIdle ( ) ;
265+ _isIdling = true ;
266+ }
267+ }
236268 }
237- else
269+
270+ if ( _isDirty && ! _isIdling )
238271 {
239- _discordRpcClient . SetPresence ( _sharedRichPresence ) ;
272+ OnRpcUpdate ( ) ;
273+ _isDirty = false ;
240274 }
241-
242- _isDirty = false ;
243275 }
244276 }
245277 }
246278 }
247279 }
280+
281+ private void OnRpcUpdate ( )
282+ {
283+ if ( _secret )
284+ {
285+ _discordRpcClient . SetPresence ( HiddenRichPresence ) ;
286+ }
287+ else
288+ {
289+ _discordRpcClient . SetPresence ( _sharedRichPresence ) ;
290+ }
291+ }
292+
293+ private void OnIdle ( )
294+ {
295+ _discordRpcClient . SetPresence ( IdlingRichPresence ) ;
296+ }
248297 }
249298}
0 commit comments