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