2020-08-18 01:00:56 +08:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
2020-09-21 18:44:16 +08:00
|
|
|
using System.Linq;
|
2021-02-09 21:54:39 +08:00
|
|
|
using System.Text;
|
2020-08-18 01:00:56 +08:00
|
|
|
using System.Windows;
|
2021-05-24 17:05:17 +08:00
|
|
|
using interop;
|
2020-08-18 01:00:56 +08:00
|
|
|
using ManagedCommon;
|
|
|
|
using Microsoft.PowerLauncher.Telemetry;
|
2021-01-05 22:54:32 +08:00
|
|
|
using Microsoft.PowerToys.Common.UI;
|
2020-08-18 01:00:56 +08:00
|
|
|
using Microsoft.PowerToys.Telemetry;
|
|
|
|
using PowerLauncher.Helper;
|
2020-11-04 04:45:01 +08:00
|
|
|
using PowerLauncher.Plugin;
|
2020-08-18 01:00:56 +08:00
|
|
|
using PowerLauncher.ViewModel;
|
|
|
|
using Wox;
|
|
|
|
using Wox.Infrastructure;
|
|
|
|
using Wox.Infrastructure.Http;
|
|
|
|
using Wox.Infrastructure.Image;
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
|
|
|
using Wox.Plugin;
|
2020-10-24 04:06:22 +08:00
|
|
|
using Wox.Plugin.Logger;
|
2020-08-18 01:00:56 +08:00
|
|
|
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
|
|
|
|
|
|
|
namespace PowerLauncher
|
|
|
|
{
|
|
|
|
public partial class App : IDisposable, ISingleInstanceApp
|
|
|
|
{
|
|
|
|
public static PublicAPIInstance API { get; private set; }
|
|
|
|
|
2020-08-22 03:40:31 +08:00
|
|
|
private static bool _disposed;
|
2020-10-30 08:52:35 +08:00
|
|
|
private PowerToysRunSettings _settings;
|
2020-08-18 01:00:56 +08:00
|
|
|
private MainViewModel _mainVM;
|
|
|
|
private MainWindow _mainWindow;
|
|
|
|
private ThemeManager _themeManager;
|
|
|
|
private SettingWindowViewModel _settingsVM;
|
|
|
|
private StringMatcher _stringMatcher;
|
2021-04-05 22:57:22 +08:00
|
|
|
private SettingsReader _settingsReader;
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
[STAThread]
|
2020-09-21 18:44:16 +08:00
|
|
|
public static void Main()
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2021-05-21 22:24:33 +08:00
|
|
|
Log.Info($"Starting PowerToys Run with PID={Process.GetCurrentProcess().Id}", typeof(App));
|
2021-05-24 17:05:17 +08:00
|
|
|
if (SingleInstance<App>.InitializeAsFirstInstance())
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
|
|
|
using (var application = new App())
|
|
|
|
{
|
|
|
|
application.InitializeComponent();
|
2021-05-24 17:05:17 +08:00
|
|
|
NativeEventWaiter.WaitForEventLoop(Constants.RunExitEvent(), () =>
|
|
|
|
{
|
|
|
|
Log.Warn("RunExitEvent was signaled. Exiting PowerToys", typeof(App));
|
|
|
|
ExitPowerToys(application);
|
|
|
|
});
|
|
|
|
|
|
|
|
int powerToysPid = GetPowerToysPId();
|
|
|
|
if (powerToysPid != 0)
|
|
|
|
{
|
|
|
|
Log.Info($"Runner pid={powerToysPid}", typeof(App));
|
|
|
|
RunnerHelper.WaitForPowerToysRunner(powerToysPid, () =>
|
|
|
|
{
|
|
|
|
Log.Info($"Runner with pid={powerToysPid} exited. Exiting PowerToys Run", typeof(App));
|
|
|
|
ExitPowerToys(application);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
application.Run();
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 22:24:33 +08:00
|
|
|
else
|
|
|
|
{
|
2021-05-24 17:05:17 +08:00
|
|
|
Log.Error("There is already running PowerToys Run instance. Exiting PowerToys Run", typeof(App));
|
2021-05-21 22:24:33 +08:00
|
|
|
}
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnStartup(object sender, StartupEventArgs e)
|
|
|
|
{
|
2021-05-21 22:24:33 +08:00
|
|
|
Log.Info("On Startup.", GetType());
|
2020-08-18 01:00:56 +08:00
|
|
|
var bootTime = new System.Diagnostics.Stopwatch();
|
|
|
|
bootTime.Start();
|
2021-02-09 21:54:39 +08:00
|
|
|
Stopwatch.Normal("App.OnStartup - Startup cost", () =>
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2021-02-09 21:54:39 +08:00
|
|
|
var textToLog = new StringBuilder();
|
|
|
|
textToLog.AppendLine("Begin PowerToys Run startup ----------------------------------------------------");
|
|
|
|
textToLog.AppendLine($"Runtime info:{ErrorReporting.RuntimeInfo()}");
|
2020-09-24 07:32:06 +08:00
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
RegisterAppDomainExceptions();
|
|
|
|
RegisterDispatcherUnhandledException();
|
|
|
|
|
|
|
|
_themeManager = new ThemeManager(this);
|
|
|
|
ImageLoader.Initialize(_themeManager.GetCurrentTheme());
|
|
|
|
|
|
|
|
_settingsVM = new SettingWindowViewModel();
|
|
|
|
_settings = _settingsVM.Settings;
|
2020-09-21 18:44:16 +08:00
|
|
|
_settings.UsePowerToysRunnerKeyboardHook = e.Args.Contains("--centralized-kb-hook");
|
2020-08-18 01:00:56 +08:00
|
|
|
|
2020-10-24 01:34:24 +08:00
|
|
|
_stringMatcher = new StringMatcher();
|
2020-08-18 01:00:56 +08:00
|
|
|
StringMatcher.Instance = _stringMatcher;
|
|
|
|
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
|
|
|
|
|
|
|
_mainVM = new MainViewModel(_settings);
|
|
|
|
_mainWindow = new MainWindow(_settings, _mainVM);
|
2020-10-24 01:34:24 +08:00
|
|
|
API = new PublicAPIInstance(_settingsVM, _mainVM, _themeManager);
|
2021-04-05 22:57:22 +08:00
|
|
|
_settingsReader = new SettingsReader(_settings, _themeManager);
|
|
|
|
_settingsReader.ReadSettings();
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
PluginManager.InitializePlugins(API);
|
|
|
|
|
|
|
|
Current.MainWindow = _mainWindow;
|
|
|
|
Current.MainWindow.Title = Constant.ExeFileName;
|
|
|
|
|
|
|
|
// main windows needs initialized before theme change because of blur settings
|
2020-10-30 08:52:35 +08:00
|
|
|
HttpClient.Proxy = _settings.Proxy;
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
RegisterExitEvents();
|
|
|
|
|
2021-04-05 22:57:22 +08:00
|
|
|
_settingsReader.ReadSettingsOnChange();
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
_mainVM.MainWindowVisibility = Visibility.Visible;
|
|
|
|
_mainVM.ColdStartFix();
|
|
|
|
_themeManager.ThemeChanged += OnThemeChanged;
|
2021-02-09 21:54:39 +08:00
|
|
|
textToLog.AppendLine("End PowerToys Run startup ---------------------------------------------------- ");
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
bootTime.Stop();
|
|
|
|
|
2021-02-09 21:54:39 +08:00
|
|
|
Log.Info(textToLog.ToString(), GetType());
|
2020-08-18 01:00:56 +08:00
|
|
|
PowerToysTelemetry.Log.WriteEvent(new LauncherBootEvent() { BootTimeMs = bootTime.ElapsedMilliseconds });
|
|
|
|
|
|
|
|
// [Conditional("RELEASE")]
|
|
|
|
// check update every 5 hours
|
|
|
|
|
|
|
|
// check updates on startup
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-24 17:05:17 +08:00
|
|
|
private static void ExitPowerToys(App app)
|
|
|
|
{
|
|
|
|
SingleInstance<App>.SingleInstanceMutex.Close();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
app.Dispose();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetPowerToysPId()
|
|
|
|
{
|
|
|
|
var args = Environment.GetCommandLineArgs();
|
|
|
|
for (int i = 0; i + 1 < args.Length; i++)
|
|
|
|
{
|
|
|
|
if (args[i] == "-powerToysPid")
|
|
|
|
{
|
|
|
|
int powerToysPid;
|
|
|
|
if (int.TryParse(args[i + 1], out powerToysPid))
|
|
|
|
{
|
|
|
|
return powerToysPid;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
private void RegisterExitEvents()
|
|
|
|
{
|
2021-05-31 18:00:06 +08:00
|
|
|
AppDomain.CurrentDomain.ProcessExit += (s, e) =>
|
|
|
|
{
|
|
|
|
Log.Info("AppDomain.CurrentDomain.ProcessExit", GetType());
|
|
|
|
Dispose();
|
|
|
|
};
|
|
|
|
|
|
|
|
Current.Exit += (s, e) =>
|
|
|
|
{
|
|
|
|
Log.Info("Application.Current.Exit", GetType());
|
|
|
|
Dispose();
|
|
|
|
};
|
|
|
|
|
|
|
|
Current.SessionEnding += (s, e) =>
|
|
|
|
{
|
|
|
|
Log.Info("Application.Current.SessionEnding", GetType());
|
|
|
|
Dispose();
|
|
|
|
};
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Callback when windows theme is changed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="oldTheme">Previous Theme</param>
|
|
|
|
/// <param name="newTheme">Current Theme</param>
|
|
|
|
private void OnThemeChanged(Theme oldTheme, Theme newTheme)
|
|
|
|
{
|
|
|
|
ImageLoader.UpdateIconPath(newTheme);
|
|
|
|
_mainVM.Query();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// let exception throw as normal is better for Debug
|
|
|
|
/// </summary>
|
|
|
|
[Conditional("RELEASE")]
|
|
|
|
private void RegisterDispatcherUnhandledException()
|
|
|
|
{
|
|
|
|
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// let exception throw as normal is better for Debug
|
|
|
|
/// </summary>
|
|
|
|
[Conditional("RELEASE")]
|
|
|
|
private static void RegisterAppDomainExceptions()
|
|
|
|
{
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnSecondAppStarted()
|
|
|
|
{
|
|
|
|
Current.MainWindow.Visibility = Visibility.Visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
2021-02-09 21:54:39 +08:00
|
|
|
Stopwatch.Normal("App.OnExit - Exit cost", () =>
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2020-09-24 07:32:06 +08:00
|
|
|
Log.Info("Start PowerToys Run Exit---------------------------------------------------- ", GetType());
|
2020-08-18 01:00:56 +08:00
|
|
|
if (disposing)
|
|
|
|
{
|
2020-09-15 02:03:17 +08:00
|
|
|
if (_themeManager != null)
|
|
|
|
{
|
|
|
|
_themeManager.ThemeChanged -= OnThemeChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
API?.SaveAppAllSettings();
|
2020-08-18 01:00:56 +08:00
|
|
|
PluginManager.Dispose();
|
2020-09-15 02:03:17 +08:00
|
|
|
_mainWindow?.Dispose();
|
|
|
|
API?.Dispose();
|
|
|
|
_mainVM?.Dispose();
|
|
|
|
_themeManager?.Dispose();
|
2020-08-18 01:00:56 +08:00
|
|
|
_disposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
|
|
|
|
// TODO: set large fields to null
|
|
|
|
_disposed = true;
|
2020-09-24 07:32:06 +08:00
|
|
|
Log.Info("End PowerToys Run Exit ---------------------------------------------------- ", GetType());
|
2020-08-18 01:00:56 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
|
|
|
// ~App()
|
|
|
|
// {
|
|
|
|
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
// Dispose(disposing: false);
|
|
|
|
// }
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
|
|
|
Dispose(disposing: true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|