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;
|
2016-05-10 03:40:59 +08:00
|
|
|
|
using Wox.Core;
|
2015-10-31 04:47:03 +08:00
|
|
|
|
using Wox.Core.Plugin;
|
2014-03-11 23:54:37 +08:00
|
|
|
|
using Wox.Helper;
|
2016-04-26 08:20:10 +08:00
|
|
|
|
using Wox.Infrastructure.Image;
|
2016-02-18 19:35:17 +08:00
|
|
|
|
using Wox.ViewModel;
|
2015-11-05 06:49:40 +08:00
|
|
|
|
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
|
2014-10-21 18:16:05 +08:00
|
|
|
|
namespace Wox
|
|
|
|
|
{
|
2016-05-10 05:45:20 +08:00
|
|
|
|
public partial class App : IDisposable, ISingleInstanceApp
|
2014-10-21 18:16:05 +08:00
|
|
|
|
{
|
2016-03-25 09:22:24 +08:00
|
|
|
|
public static PublicAPIInstance API { get; private set; }
|
2016-05-10 05:45:20 +08:00
|
|
|
|
private const string Unique = "Wox_Unique_Application_Mutex";
|
2016-05-08 05:44:38 +08:00
|
|
|
|
private static bool _disposed;
|
2016-02-18 19:35:17 +08:00
|
|
|
|
|
2014-10-21 18:16:05 +08:00
|
|
|
|
[STAThread]
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
2016-05-08 05:44:38 +08:00
|
|
|
|
RegisterAppDomainUnhandledException();
|
2014-10-21 18:16:05 +08:00
|
|
|
|
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
|
|
|
|
|
{
|
2016-05-08 05:44:38 +08:00
|
|
|
|
using (var application = new App())
|
|
|
|
|
{
|
|
|
|
|
application.InitializeComponent();
|
|
|
|
|
application.Run();
|
|
|
|
|
}
|
2014-10-21 18:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 05:44:38 +08:00
|
|
|
|
private void OnStartup(object sender, StartupEventArgs e)
|
2014-10-21 18:16:05 +08:00
|
|
|
|
{
|
2015-11-05 05:49:36 +08:00
|
|
|
|
Stopwatch.Debug("Startup Time", () =>
|
2015-11-02 10:47:43 +08:00
|
|
|
|
{
|
2016-05-08 05:44:38 +08:00
|
|
|
|
RegisterDispatcherUnhandledException();
|
2016-02-21 23:26:57 +08:00
|
|
|
|
|
2016-05-04 04:18:26 +08:00
|
|
|
|
ImageLoader.PreloadImages();
|
2016-04-25 09:33:55 +08:00
|
|
|
|
|
2016-05-10 06:35:20 +08:00
|
|
|
|
var vm = new MainViewModel();
|
|
|
|
|
var pluginsSettings = vm._settings.PluginSettings;
|
|
|
|
|
var window = new MainWindow(vm._settings, vm);
|
|
|
|
|
API = new PublicAPIInstance(vm._settings, vm);
|
2016-05-05 08:57:03 +08:00
|
|
|
|
PluginManager.InitializePlugins(API, pluginsSettings);
|
2016-03-28 08:09:40 +08:00
|
|
|
|
|
2016-05-08 05:44:38 +08:00
|
|
|
|
RegisterExitEvents();
|
2016-05-10 05:45:20 +08:00
|
|
|
|
|
|
|
|
|
Current.MainWindow = window;
|
|
|
|
|
Current.MainWindow.Title = Infrastructure.Wox.Name;
|
|
|
|
|
window.Show();
|
2015-11-05 05:35:04 +08:00
|
|
|
|
});
|
2014-10-21 18:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 05:44:38 +08:00
|
|
|
|
private async void OnActivated(object sender, EventArgs e)
|
|
|
|
|
{
|
2016-05-10 03:40:59 +08:00
|
|
|
|
Updater.UpdateApp();
|
2016-05-08 05:44:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterExitEvents()
|
|
|
|
|
{
|
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
|
|
|
|
|
Current.Exit += (s, e) => Dispose();
|
|
|
|
|
Current.SessionEnding += (s, e) => Dispose();
|
|
|
|
|
}
|
2015-11-11 08:33:33 +08:00
|
|
|
|
[Conditional("RELEASE")]
|
2016-05-08 05:44:38 +08:00
|
|
|
|
private void RegisterDispatcherUnhandledException()
|
2015-11-07 03:55:48 +08:00
|
|
|
|
{
|
2016-05-08 05:44:38 +08:00
|
|
|
|
// let exception throw as normal is better for Debug
|
2015-11-07 03:55:48 +08:00
|
|
|
|
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 05:44:38 +08:00
|
|
|
|
[Conditional("RELEASE")]
|
|
|
|
|
private static void RegisterAppDomainUnhandledException()
|
2014-10-21 18:16:05 +08:00
|
|
|
|
{
|
2016-05-08 05:44:38 +08:00
|
|
|
|
// let exception throw as normal is better for Debug
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
|
2014-10-21 18:16:05 +08:00
|
|
|
|
}
|
2016-05-03 05:37:01 +08:00
|
|
|
|
|
2016-05-08 05:44:38 +08:00
|
|
|
|
public void Dispose()
|
2016-05-03 05:37:01 +08:00
|
|
|
|
{
|
|
|
|
|
// 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
|
2016-05-08 05:44:38 +08:00
|
|
|
|
if (!_disposed)
|
2016-05-03 05:37:01 +08:00
|
|
|
|
{
|
2016-05-10 05:56:05 +08:00
|
|
|
|
((MainViewModel)Current.MainWindow?.DataContext)?.Save();
|
2016-05-10 05:45:20 +08:00
|
|
|
|
_disposed = true;
|
2016-05-03 05:37:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-10 05:45:20 +08:00
|
|
|
|
|
|
|
|
|
public void OnSecondAppStarted()
|
|
|
|
|
{
|
|
|
|
|
API.ShowApp();
|
|
|
|
|
}
|
2014-10-21 18:16:05 +08:00
|
|
|
|
}
|
2016-05-08 05:44:38 +08:00
|
|
|
|
}
|