github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/updater/windows/WpfPrompter/WpfApplication1/MainWindow.xaml.cs (about)

     1  using System;
     2  using System.IO;
     3  using System.Windows;
     4  using System.Windows.Interop;
     5  using System.Web.Script.Serialization;
     6  using System.Runtime.InteropServices;
     7  
     8  public class Input
     9  {
    10      public string title { get; set; }
    11      public string message { get; set; }
    12      public string description { get; set; }
    13      public string outPath { get; set; }
    14      public bool auto { get; set; }
    15  }
    16  
    17  public class Result
    18  {
    19      public string action { get; set; }
    20      public bool autoUpdate { get; set; }
    21      public int snooze_duration { get; set; }
    22  }
    23  
    24  namespace WpfApplication1
    25  {
    26      /// <summary>
    27      /// Interaction logic for MainWindow.xaml
    28      /// </summary>
    29      public partial class MainWindow : Window
    30      {
    31          Input input;
    32          Result result;
    33  
    34          private const int GWL_STYLE = -16;
    35          private const int WS_SYSMENU = 0x80000;
    36          [DllImport("user32.dll", SetLastError = true)]
    37          private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    38          [DllImport("user32.dll")]
    39          private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    40  
    41          private const int snoozeDay = 60 * 60 * 24; // seconds per day
    42  
    43          public MainWindow()
    44          {
    45              InitializeComponent();
    46              result = new Result();
    47              string[] args = Environment.GetCommandLineArgs();
    48              if (args != null && args.Length > 1)
    49              {
    50                  JavaScriptSerializer serializer = new JavaScriptSerializer();
    51                  input = serializer.Deserialize<Input>(args[1]);
    52              } else
    53              {
    54                  input = new global::Input();
    55              }
    56              if (input.title != null && input.title.Length > 0)
    57              {
    58                  title.Text = input.title;
    59              }
    60              if (input.message != null && input.message.Length > 0)
    61              {
    62                  message.Text = input.message;
    63              }
    64              if (input.description != null && input.description.Length > 0)
    65              {
    66                  description.Text = input.description;
    67              }
    68              if (input.outPath == null || input.outPath.Length <= 0)
    69              {
    70                  input.outPath = "updaterPromptResult.txt";
    71              }
    72              silent.IsChecked = input.auto;
    73            
    74          }
    75  
    76          private void apply_Click(object sender, RoutedEventArgs e)
    77          {
    78              result.action = "apply";
    79              result.autoUpdate = (bool) silent.IsChecked;
    80              writeResult();
    81          }
    82  
    83          private void writeResult()
    84          {
    85              JavaScriptSerializer serializer = new JavaScriptSerializer();
    86              File.WriteAllText(input.outPath, serializer.Serialize(result));
    87              Application.Current.Shutdown();
    88          }
    89  
    90          private void Window_Loaded(object sender, RoutedEventArgs e)
    91          {
    92              var hwnd = new WindowInteropHelper(this).Handle;
    93              SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
    94          }
    95  
    96          private void snoozeDuration_DropDownClosed(object sender, EventArgs e)
    97          {
    98              var snoozeVal = (System.Windows.Controls.ComboBoxItem) snoozeDuration.SelectedItem;
    99              if (snoozeVal != null && snoozeDuration.SelectedIndex > 0)
   100              {
   101                  result.action = "snooze";
   102                  result.snooze_duration = (snoozeVal.Name == "snooze7") ? snoozeDay * 7 : snoozeDay;
   103                  writeResult();
   104              }
   105          }
   106      }
   107  }