Files
PowerToys/Wox/App.xaml.cs

126 lines
3.9 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
2015-11-06 19:55:48 +00:00
using System.Diagnostics;
2015-10-30 23:17:34 +00:00
using System.Windows;
using Squirrel;
2015-10-30 20:47:03 +00:00
using Wox.Core.Plugin;
2014-03-11 23:54:37 +08:00
using Wox.Helper;
using Wox.Infrastructure.Image;
using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch;
using Wox.Infrastructure.Logger;
2014-01-29 18:33:24 +08:00
namespace Wox
{
public partial class App : ISingleInstanceApp, IDisposable
{
private const string Unique = "Wox_Unique_Application_Mutex";
public static MainWindow Window { get; private set; }
public static PublicAPIInstance API { get; private set; }
private static bool _disposed;
public static UpdateManager Updater;
[STAThread]
public static void Main()
{
RegisterAppDomainUnhandledException();
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{
using (var application = new App())
{
application.InitializeComponent();
application.Run();
}
}
}
private void OnStartup(object sender, StartupEventArgs e)
{
Stopwatch.Debug("Startup Time", () =>
2015-11-02 02:47:43 +00:00
{
RegisterDispatcherUnhandledException();
2016-02-21 15:26:57 +00:00
ImageLoader.PreloadImages();
MainViewModel mainVM = new MainViewModel();
var pluginsSettings = mainVM._settings.PluginSettings;
API = new PublicAPIInstance(mainVM, mainVM._settings);
PluginManager.InitializePlugins(API, pluginsSettings);
Window = new MainWindow(mainVM._settings, mainVM);
var _notifyIconManager = new NotifyIconManager(API);
RegisterExitEvents();
});
}
private async void OnActivated(object sender, EventArgs e)
{
try
{
using (Updater = await UpdateManager.GitHubUpdateManager(Infrastructure.Wox.Github, prerelease: true))
{
await Updater.UpdateApp();
}
}
catch (Exception exception)
{
const string info = "Update.exe not found, not a Squirrel-installed app?";
if (exception.Message == info)
{
Log.Warn("info");
}
else
{
throw;
}
}
}
private void RegisterExitEvents()
{
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
Current.Exit += (s, e) => Dispose();
Current.SessionEnding += (s, e) => Dispose();
}
2015-11-11 00:33:33 +00:00
[Conditional("RELEASE")]
private void RegisterDispatcherUnhandledException()
2015-11-06 19:55:48 +00:00
{
// let exception throw as normal is better for Debug
2015-11-06 19:55:48 +00:00
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
}
[Conditional("RELEASE")]
private static void RegisterAppDomainUnhandledException()
{
// let exception throw as normal is better for Debug
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
}
public void OnActivate()
{
API.ShowApp();
}
private static void Save()
{
var vm = (MainViewModel)Window.DataContext;
vm.Save();
PluginManager.Save();
ImageLoader.Save();
_disposed = true;
}
public void Dispose()
{
// if sessionending is called, exit proverbially be called when log off / shutdown
// but if sessionending is not called, exit won't be called when log off / shutdown
if (!_disposed)
{
Save();
Updater?.Dispose();
SingleInstance<App>.Cleanup();
}
}
}
}