/ src / modules / cmdpal / ext / SamplePagesExtension / ExplodeInFiveSeconds.cs
ExplodeInFiveSeconds.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Timers;
 6  using Microsoft.CommandPalette.Extensions;
 7  using Microsoft.CommandPalette.Extensions.Toolkit;
 8  
 9  namespace SamplePagesExtension;
10  
11  internal sealed partial class ExplodeInFiveSeconds : ListPage
12  {
13      private readonly bool _repeat;
14  
15      private IListItem[] Commands => [
16        new ListItem(new NoOpCommand())
17             {
18                 Title = "This page will explode in five seconds!",
19                 Subtitle = _repeat ? "Not only that, I'll _keep_ exploding every 5 seconds after that" : string.Empty,
20             },
21          ];
22  
23      private bool shouldExplode;
24      private static Timer timer;
25  
26      public ExplodeInFiveSeconds(bool repeat)
27      {
28          _repeat = repeat;
29          Icon = new IconInfo(string.Empty);
30          Name = "Open";
31      }
32  
33      public override IListItem[] GetItems()
34      {
35          if (shouldExplode)
36          {
37              _ = Commands[9001]; // Throws
38          }
39          else
40          {
41              timer = new Timer(5000);
42              timer.Elapsed += (object source, ElapsedEventArgs e) => { RaiseItemsChanged(9000); };
43              timer.AutoReset = _repeat; // Keep repeating
44              timer.Enabled = true;
45          }
46  
47          shouldExplode = true;
48          return Commands;
49      }
50  }