App.xaml.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;
 6  
 7  using ManagedCommon;
 8  using Microsoft.UI.Dispatching;
 9  using Microsoft.UI.Xaml;
10  
11  namespace MeasureToolUI
12  {
13      /// <summary>
14      /// Provides application-specific behavior to supplement the default Application class.
15      /// </summary>
16      public partial class App : Application
17      {
18          /// <summary>
19          /// Initializes a new instance of the <see cref="App"/> class.
20          /// Initializes the singleton application object.  This is the first line of authored code
21          /// executed, and as such is the logical equivalent of main() or WinMain().
22          /// </summary>
23          public App()
24          {
25              Logger.InitializeLogger("\\Measure Tool\\MeasureToolUI\\Logs");
26  
27              string appLanguage = LanguageHelper.LoadLanguage();
28              if (!string.IsNullOrEmpty(appLanguage))
29              {
30                  Microsoft.Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = appLanguage;
31              }
32  
33              this.InitializeComponent();
34          }
35  
36          /// <summary>
37          /// Invoked when the application is launched normally by the end user.  Other entry points
38          /// will be used such as when the application is launched to open a specific file.
39          /// </summary>
40          /// <param name="args">Details about the launch request and process.</param>
41          protected override void OnLaunched(LaunchActivatedEventArgs args)
42          {
43              if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredScreenRulerEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
44              {
45                  Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
46                  Environment.Exit(0); // Current.Exit won't work until there's a window opened.
47                  return;
48              }
49  
50              var cmdArgs = Environment.GetCommandLineArgs();
51              if (cmdArgs?.Length > 1)
52              {
53                  if (int.TryParse(cmdArgs[cmdArgs.Length - 1], out int powerToysRunnerPid))
54                  {
55                      var dispatcher = DispatcherQueue.GetForCurrentThread();
56                      RunnerHelper.WaitForPowerToysRunner(powerToysRunnerPid, () =>
57                      {
58                          dispatcher.TryEnqueue(App.Current.Exit);
59                      });
60                  }
61              }
62  
63              PowerToys.MeasureToolCore.Core core = null;
64              try
65              {
66                  core = new PowerToys.MeasureToolCore.Core();
67              }
68              catch (Exception ex)
69              {
70                  Logger.LogError($"MeasureToolCore failed to initialize: {ex}");
71                  App.Current.Exit();
72                  return;
73              }
74  
75              _window = new MainWindow(core);
76              _window.Activate();
77          }
78  
79          private Window _window;
80      }
81  }