2014-01-29 18:33:24 +08:00
|
|
|
|
using System;
|
2015-11-06 19:55:48 +00:00
|
|
|
|
using System.Diagnostics;
|
2016-05-17 22:23:37 +01:00
|
|
|
|
using System.Timers;
|
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;
|
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-15 17:03:06 +01:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-06-19 16:18:43 +01:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
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-15 17:03:06 +01:00
|
|
|
|
RegisterAppDomainExceptions();
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2016-05-22 05:30:38 +01:00
|
|
|
|
Stopwatch.Normal("Startup Time", () =>
|
2015-11-02 02:47:43 +00:00
|
|
|
|
{
|
2016-11-30 01:15:15 +00:00
|
|
|
|
Log.Info("-------------------------- Begin Wox startup --------------------------");
|
2016-05-07 22:44:38 +01:00
|
|
|
|
RegisterDispatcherUnhandledException();
|
2016-02-21 15:26:57 +00:00
|
|
|
|
|
2016-05-21 22:44:27 +01:00
|
|
|
|
var settingVM = new SettingWindowViewModel();
|
|
|
|
|
|
_settings = settingVM.Settings;
|
2016-05-10 01:08:54 +01:00
|
|
|
|
|
2016-05-12 03:01:33 +01:00
|
|
|
|
PluginManager.LoadPlugins(_settings.PluginSettings);
|
2016-05-21 22:44:27 +01:00
|
|
|
|
var mainVM = new MainViewModel(_settings);
|
|
|
|
|
|
var window = new MainWindow(_settings, mainVM);
|
|
|
|
|
|
API = new PublicAPIInstance(settingVM, mainVM);
|
2016-05-10 01:08:54 +01:00
|
|
|
|
PluginManager.InitializePlugins(API);
|
2016-03-28 01:09:40 +01:00
|
|
|
|
|
2016-05-20 22:20:41 +01:00
|
|
|
|
ImageLoader.PreloadImages();
|
|
|
|
|
|
|
2016-05-09 22:45:20 +01:00
|
|
|
|
Current.MainWindow = window;
|
2016-05-18 19:38:43 +01:00
|
|
|
|
Current.MainWindow.Title = Infrastructure.Constant.Wox;
|
2016-05-17 22:23:37 +01:00
|
|
|
|
|
|
|
|
|
|
RegisterExitEvents();
|
|
|
|
|
|
|
2016-05-18 19:35:34 +01:00
|
|
|
|
AutoStartup();
|
2016-05-17 22:23:37 +01:00
|
|
|
|
AutoUpdates();
|
|
|
|
|
|
|
2016-07-20 01:24:28 +02:00
|
|
|
|
mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible;
|
2016-11-30 01:15:15 +00:00
|
|
|
|
Log.Info("-------------------------- End Wox startup --------------------------");
|
2015-11-04 21:35:04 +00:00
|
|
|
|
});
|
2014-10-21 18:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-18 19:35:34 +01:00
|
|
|
|
private void AutoStartup()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_settings.StartWoxOnSystemStartup)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!SettingWindow.StartupSet())
|
|
|
|
|
|
{
|
|
|
|
|
|
SettingWindow.SetStartup();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-17 22:23:37 +01:00
|
|
|
|
private void AutoUpdates()
|
2016-05-07 22:44:38 +01:00
|
|
|
|
{
|
2016-05-12 03:01:33 +01:00
|
|
|
|
if (_settings.AutoUpdates)
|
|
|
|
|
|
{
|
2016-05-17 22:23:37 +01:00
|
|
|
|
// check udpate every 5 hours
|
|
|
|
|
|
var timer = new Timer(1000 * 60 * 60 * 5);
|
|
|
|
|
|
timer.Elapsed += (s, e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Updater.UpdateApp();
|
|
|
|
|
|
};
|
|
|
|
|
|
timer.Start();
|
|
|
|
|
|
|
|
|
|
|
|
// check updates on startup
|
2016-05-12 03:01:33 +01:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
2016-05-15 17:03:06 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-07-20 01:24:28 +02:00
|
|
|
|
/// let exception throw as normal is better for Debug
|
2016-05-15 17:03:06 +01:00
|
|
|
|
/// </summary>
|
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
|
|
|
|
{
|
|
|
|
|
|
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-15 17:03:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-07-20 01:24:28 +02:00
|
|
|
|
/// let exception throw as normal is better for Debug
|
2016-05-15 17:03:06 +01:00
|
|
|
|
/// </summary>
|
2016-05-07 22:44:38 +01:00
|
|
|
|
[Conditional("RELEASE")]
|
2016-05-15 17:03:06 +01:00
|
|
|
|
private static void RegisterAppDomainExceptions()
|
2014-10-21 18:16:05 +08:00
|
|
|
|
{
|
2016-05-25 01:00:10 +01:00
|
|
|
|
|
2016-05-07 22:44:38 +01:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
|
2016-05-15 17:03:06 +01:00
|
|
|
|
AppDomain.CurrentDomain.FirstChanceException += (s, e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Error("First Chance Exception:");
|
|
|
|
|
|
Log.Exception(e.Exception);
|
|
|
|
|
|
};
|
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-25 01:00:10 +01:00
|
|
|
|
Current.Dispatcher.Invoke(() => ((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()
|
|
|
|
|
|
{
|
2016-05-25 01:00:10 +01:00
|
|
|
|
Current.MainWindow.Visibility = Visibility.Visible;
|
2016-05-09 22:45:20 +01:00
|
|
|
|
}
|
2014-10-21 18:16:05 +08:00
|
|
|
|
}
|
2016-05-07 22:44:38 +01:00
|
|
|
|
}
|