Files
PowerToys/Wox/App.xaml.cs

174 lines
5.9 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
2015-11-06 19:55:48 +00:00
using System.Diagnostics;
using System.Threading.Tasks;
2016-05-17 22:23:37 +01:00
using System.Timers;
2015-10-30 23:17:34 +00:00
using System.Windows;
using Wox.Core;
2015-10-30 20:47:03 +00:00
using Wox.Core.Plugin;
using Wox.Core.Resource;
2014-03-11 23:54:37 +08:00
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
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;
using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch;
2014-01-29 18:33:24 +08:00
namespace Wox
{
2016-05-09 22:45:20 +01:00
public partial class App : IDisposable, ISingleInstanceApp
{
public static PublicAPIInstance API { get; private set; }
2016-05-09 22:45:20 +01:00
private const string Unique = "Wox_Unique_Application_Mutex";
private static bool _disposed;
2016-05-12 03:01:33 +01:00
private Settings _settings;
2017-03-01 23:47:50 +00:00
private MainViewModel _mainVM;
private SettingWindowViewModel _settingsVM;
[STAThread]
public static void Main()
{
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{
using (var application = new App())
{
application.InitializeComponent();
application.Run();
}
}
}
private void OnStartup(object sender, StartupEventArgs e)
{
2017-01-24 00:24:20 +00:00
Stopwatch.Normal("|App.OnStartup|Startup cost", () =>
2015-11-02 02:47:43 +00:00
{
2017-01-24 00:24:20 +00:00
Log.Info("|App.OnStartup|Begin Wox startup ----------------------------------------------------");
Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}");
RegisterAppDomainExceptions();
RegisterDispatcherUnhandledException();
2016-02-21 15:26:57 +00:00
2017-02-12 17:10:42 +00:00
ImageLoader.Initialize();
Alphabet.Initialize();
2017-03-01 23:47:50 +00:00
_settingsVM = new SettingWindowViewModel();
_settings = _settingsVM.Settings;
StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
2016-05-12 03:01:33 +01:00
PluginManager.LoadPlugins(_settings.PluginSettings);
2017-03-01 23:47:50 +00:00
_mainVM = new MainViewModel(_settings);
var window = new MainWindow(_settings, _mainVM);
API = new PublicAPIInstance(_settingsVM, _mainVM);
PluginManager.InitializePlugins(API);
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");
2016-05-09 22:45:20 +01:00
Current.MainWindow = window;
2017-02-12 17:10:42 +00:00
Current.MainWindow.Title = Constant.Wox;
2016-05-17 22:23:37 +01:00
// happlebao todo temp fix for instance code logic
// load plugin before change language, because plugin language also needs be changed
InternationalizationManager.Instance.Settings = _settings;
InternationalizationManager.Instance.ChangeLanguage(_settings.Language);
// main windows needs initialized before theme change because of blur settigns
ThemeManager.Instance.Settings = _settings;
ThemeManager.Instance.ChangeTheme(_settings.Theme);
Http.Proxy = _settings.Proxy;
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();
2017-03-01 23:47:50 +00:00
_mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible;
2017-01-24 00:24:20 +00:00
Log.Info("|App.OnStartup|End Wox startup ---------------------------------------------------- ");
});
}
2016-05-18 19:35:34 +01:00
private void AutoStartup()
{
if (_settings.StartWoxOnSystemStartup)
{
if (!SettingWindow.StartupSet())
{
SettingWindow.SetStartup();
}
}
}
2017-03-06 00:25:17 +00:00
//[Conditional("RELEASE")]
2016-05-17 22:23:37 +01:00
private void AutoUpdates()
{
Task.Run(async () =>
2016-05-12 03:01:33 +01:00
{
if (_settings.AutoUpdates)
{
// check udpate every 5 hours
var timer = new Timer(1000 * 60 * 60 * 5);
timer.Elapsed += async (s, e) =>
{
await Updater.UpdateApp();
};
timer.Start();
// check updates on startup
await Updater.UpdateApp();
}
});
}
2017-03-01 23:47:50 +00: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>
/// 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")]
private void RegisterDispatcherUnhandledException()
2015-11-06 19:55:48 +00:00
{
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
}
2016-05-15 17:03:06 +01:00
/// <summary>
/// let exception throw as normal is better for Debug
2016-05-15 17:03:06 +01:00
/// </summary>
[Conditional("RELEASE")]
2016-05-15 17:03:06 +01:00
private static void RegisterAppDomainExceptions()
{
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
2017-04-01 18:50:22 +01:00
AppDomain.CurrentDomain.FirstChanceException += (_, e) =>
{
Log.Exception("|App.RegisterAppDomainExceptions|First Chance 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)
{
2017-03-01 23:47:50 +00:00
_mainVM.Save();
_settingsVM.Save();
PluginManager.Save();
ImageLoader.Save();
Alphabet.Save();
2016-05-09 22:45:20 +01:00
_disposed = true;
}
}
2016-05-09 22:45:20 +01:00
public void OnSecondAppStarted()
{
Current.MainWindow.Visibility = Visibility.Visible;
2016-05-09 22:45:20 +01:00
}
}
}