PowerToys/Wox/App.xaml.cs

145 lines
4.5 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;
2016-05-18 05:23:37 +08:00
using System.Timers;
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;
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;
2016-06-19 23:18:43 +08:00
using Wox.Infrastructure.UserSettings;
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.Normal("Startup Time", () =>
2015-11-02 10:47:43 +08:00
{
2016-11-30 09:15:15 +08:00
Log.Info("-------------------------- Begin Wox startup --------------------------");
RegisterDispatcherUnhandledException();
2016-02-21 23:26:57 +08:00
2016-05-22 05:44:27 +08:00
var settingVM = new SettingWindowViewModel();
_settings = settingVM.Settings;
2016-05-12 10:01:33 +08:00
PluginManager.LoadPlugins(_settings.PluginSettings);
2016-05-22 05:44:27 +08:00
var mainVM = new MainViewModel(_settings);
var window = new MainWindow(_settings, mainVM);
API = new PublicAPIInstance(settingVM, mainVM);
PluginManager.InitializePlugins(API);
2016-05-21 05:20:41 +08:00
ImageLoader.PreloadImages();
2016-05-10 05:45:20 +08:00
Current.MainWindow = window;
2016-05-19 02:38:43 +08:00
Current.MainWindow.Title = Infrastructure.Constant.Wox;
2016-05-18 05:23:37 +08:00
RegisterExitEvents();
2016-05-19 02:35:34 +08:00
AutoStartup();
2016-05-18 05:23:37 +08:00
AutoUpdates();
mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible;
2016-11-30 09:15:15 +08:00
Log.Info("-------------------------- End Wox startup --------------------------");
});
}
2016-05-19 02:35:34 +08:00
private void AutoStartup()
{
if (_settings.StartWoxOnSystemStartup)
{
if (!SettingWindow.StartupSet())
{
SettingWindow.SetStartup();
}
}
}
2016-05-18 05:23:37 +08:00
private void AutoUpdates()
{
2016-05-12 10:01:33 +08:00
if (_settings.AutoUpdates)
{
2016-05-18 05:23:37 +08: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 10:01:33 +08:00
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
2016-05-16 00:03:06 +08:00
/// </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
2016-05-16 00:03:06 +08:00
/// </summary>
[Conditional("RELEASE")]
2016-05-16 00:03:06 +08:00
private static void RegisterAppDomainExceptions()
{
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)
{
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()
{
Current.MainWindow.Visibility = Visibility.Visible;
2016-05-10 05:45:20 +08:00
}
}
}