PowerToys/Wox/App.xaml.cs

121 lines
3.7 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
2015-11-07 03:55:48 +08:00
using System.Diagnostics;
2015-10-31 07:17:34 +08:00
using System.Windows;
using Wox.Core;
2015-10-31 04:47:03 +08:00
using Wox.Core.Plugin;
using Wox.Core.UserSettings;
2014-03-11 23:54:37 +08:00
using Wox.Helper;
using Wox.Infrastructure.Image;
2016-05-16 00:03:06 +08:00
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch;
2014-01-29 18:33:24 +08:00
namespace Wox
{
2016-05-10 05:45:20 +08:00
public partial class App : IDisposable, ISingleInstanceApp
{
public static PublicAPIInstance API { get; private set; }
2016-05-10 05:45:20 +08:00
private const string Unique = "Wox_Unique_Application_Mutex";
private static bool _disposed;
2016-05-12 10:01:33 +08:00
private Settings _settings;
[STAThread]
public static void Main()
{
2016-05-16 00:03:06 +08:00
RegisterAppDomainExceptions();
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 10:47:43 +08:00
{
RegisterDispatcherUnhandledException();
2016-02-21 23:26:57 +08:00
ImageLoader.PreloadImages();
var storage = new JsonStrorage<Settings>();
2016-05-12 10:01:33 +08:00
_settings = storage.Load();
2016-05-12 10:01:33 +08:00
PluginManager.LoadPlugins(_settings.PluginSettings);
var vm = new MainViewModel(_settings, storage);
var pluginsSettings = _settings.PluginSettings;
var window = new MainWindow(_settings, vm);
API = new PublicAPIInstance(_settings, vm);
PluginManager.InitializePlugins(API);
RegisterExitEvents();
2016-05-10 05:45:20 +08:00
Current.MainWindow = window;
Current.MainWindow.Title = Infrastructure.Wox.Name;
window.Show();
});
}
2016-05-14 06:54:41 +08:00
private void OnActivated(object sender, EventArgs e)
{
2016-05-12 10:01:33 +08:00
if (_settings.AutoUpdates)
{
Updater.UpdateApp();
}
}
private void RegisterExitEvents()
{
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
Current.Exit += (s, e) => Dispose();
Current.SessionEnding += (s, e) => Dispose();
}
2016-05-16 00:03:06 +08:00
/// <summary>
/// let exception throw as normal is better for Debug
/// </summary>
2015-11-11 08:33:33 +08:00
[Conditional("RELEASE")]
private void RegisterDispatcherUnhandledException()
2015-11-07 03:55:48 +08:00
{
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
}
2016-05-16 00:03:06 +08:00
/// <summary>
/// let exception throw as normal is better for Debug
/// </summary>
[Conditional("RELEASE")]
2016-05-16 00:03:06 +08:00
private static void RegisterAppDomainExceptions()
{
2016-05-16 00:03:06 +08:00
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
2016-05-16 00:03:06 +08:00
AppDomain.CurrentDomain.FirstChanceException += (s, e) =>
{
Log.Error("First Chance Exception:");
Log.Exception(e.Exception);
};
}
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)
{
2016-05-16 00:19:33 +08:00
Current.Dispatcher.Invoke(() => ((MainViewModel) Current.MainWindow?.DataContext)?.Save());
2016-05-10 05:45:20 +08:00
_disposed = true;
}
}
2016-05-10 05:45:20 +08:00
public void OnSecondAppStarted()
{
API.ShowApp();
}
}
}