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;
|
2020-08-18 01:00:56 +08:00
|
|
|
using System.Windows;
|
|
|
|
using ManagedCommon;
|
|
|
|
using Microsoft.PowerLauncher.Telemetry;
|
|
|
|
using Microsoft.PowerToys.Telemetry;
|
|
|
|
using PowerLauncher.Helper;
|
|
|
|
using PowerLauncher.ViewModel;
|
|
|
|
using Wox;
|
|
|
|
using Wox.Core.Plugin;
|
|
|
|
using Wox.Infrastructure;
|
|
|
|
using Wox.Infrastructure.Http;
|
|
|
|
using Wox.Infrastructure.Image;
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
|
|
|
using Wox.Plugin;
|
|
|
|
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
|
|
|
|
|
|
|
namespace PowerLauncher
|
|
|
|
{
|
|
|
|
public partial class App : IDisposable, ISingleInstanceApp
|
|
|
|
{
|
|
|
|
public static PublicAPIInstance API { get; private set; }
|
|
|
|
|
|
|
|
private readonly Alphabet _alphabet = new Alphabet();
|
|
|
|
|
|
|
|
private const string Unique = "PowerLauncher_Unique_Application_Mutex";
|
2020-08-22 03:40:31 +08:00
|
|
|
private static bool _disposed;
|
2020-08-18 01:00:56 +08:00
|
|
|
private Settings _settings;
|
|
|
|
private MainViewModel _mainVM;
|
|
|
|
private MainWindow _mainWindow;
|
|
|
|
private ThemeManager _themeManager;
|
|
|
|
private SettingWindowViewModel _settingsVM;
|
|
|
|
private StringMatcher _stringMatcher;
|
|
|
|
private SettingsWatcher _settingsWatcher;
|
|
|
|
|
|
|
|
[STAThread]
|
2020-09-21 18:44:16 +08:00
|
|
|
public static void Main()
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
|
|
|
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
|
|
|
|
{
|
|
|
|
using (var application = new App())
|
|
|
|
{
|
|
|
|
application.InitializeComponent();
|
|
|
|
application.Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnStartup(object sender, StartupEventArgs e)
|
|
|
|
{
|
2020-09-21 18:44:16 +08:00
|
|
|
for (int i = 0; i + 1 < e.Args.Length; i++)
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2020-09-21 18:44:16 +08:00
|
|
|
if (e.Args[i] == "-powerToysPid")
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2020-09-21 18:44:16 +08:00
|
|
|
int powerToysPid;
|
|
|
|
if (int.TryParse(e.Args[i + 1], out powerToysPid))
|
|
|
|
{
|
|
|
|
RunnerHelper.WaitForPowerToysRunner(powerToysPid, () =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
2020-09-21 18:44:16 +08:00
|
|
|
}
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
var bootTime = new System.Diagnostics.Stopwatch();
|
|
|
|
bootTime.Start();
|
|
|
|
Stopwatch.Normal("|App.OnStartup|Startup cost", () =>
|
|
|
|
{
|
2020-09-24 07:32:06 +08:00
|
|
|
Log.Info("Begin PowerToys Run startup ----------------------------------------------------", GetType());
|
|
|
|
Log.Info($"Runtime info:{ErrorReporting.RuntimeInfo()}", GetType());
|
|
|
|
|
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
|
|
|
|
|
|
|
_alphabet.Initialize(_settings);
|
|
|
|
_stringMatcher = new StringMatcher(_alphabet);
|
|
|
|
StringMatcher.Instance = _stringMatcher;
|
|
|
|
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
|
|
|
|
|
|
|
PluginManager.LoadPlugins(_settings.PluginSettings);
|
|
|
|
_mainVM = new MainViewModel(_settings);
|
|
|
|
_mainWindow = new MainWindow(_settings, _mainVM);
|
|
|
|
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet, _themeManager);
|
|
|
|
PluginManager.InitializePlugins(API);
|
|
|
|
|
|
|
|
Current.MainWindow = _mainWindow;
|
|
|
|
Current.MainWindow.Title = Constant.ExeFileName;
|
|
|
|
|
|
|
|
// main windows needs initialized before theme change because of blur settings
|
|
|
|
Http.Proxy = _settings.Proxy;
|
|
|
|
|
|
|
|
RegisterExitEvents();
|
|
|
|
|
|
|
|
_settingsWatcher = new SettingsWatcher(_settings);
|
|
|
|
|
|
|
|
_mainVM.MainWindowVisibility = Visibility.Visible;
|
|
|
|
_mainVM.ColdStartFix();
|
|
|
|
_themeManager.ThemeChanged += OnThemeChanged;
|
2020-09-24 07:32:06 +08:00
|
|
|
Log.Info("End PowerToys Run startup ---------------------------------------------------- ", GetType());
|
2020-08-18 01:00:56 +08:00
|
|
|
|
|
|
|
bootTime.Stop();
|
|
|
|
|
|
|
|
PowerToysTelemetry.Log.WriteEvent(new LauncherBootEvent() { BootTimeMs = bootTime.ElapsedMilliseconds });
|
|
|
|
|
|
|
|
// [Conditional("RELEASE")]
|
|
|
|
// check update every 5 hours
|
|
|
|
|
|
|
|
// check updates on startup
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RegisterExitEvents()
|
|
|
|
{
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
|
|
|
|
Current.Exit += (s, e) => Dispose();
|
|
|
|
Current.SessionEnding += (s, e) => Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
Stopwatch.Normal("|App.OnExit|Exit cost", () =>
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|