2019-12-03 21:55:22 +08:00
|
|
|
using System;
|
2015-11-07 03:55:48 +08:00
|
|
|
using System.Diagnostics;
|
2017-03-06 07:59:10 +08:00
|
|
|
using System.Threading.Tasks;
|
2016-05-18 05:23:37 +08:00
|
|
|
using System.Timers;
|
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;
|
2017-02-21 10:19:50 +08:00
|
|
|
using Wox.Core.Resource;
|
2014-03-11 23:54:37 +08:00
|
|
|
using Wox.Helper;
|
2017-02-13 00:57:24 +08:00
|
|
|
using Wox.Infrastructure;
|
2017-02-21 10:19:50 +08:00
|
|
|
using Wox.Infrastructure.Http;
|
2016-04-26 08:20:10 +08:00
|
|
|
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;
|
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-05-12 10:01:33 +08:00
|
|
|
private Settings _settings;
|
2017-03-02 07:47:50 +08:00
|
|
|
private MainViewModel _mainVM;
|
|
|
|
private SettingWindowViewModel _settingsVM;
|
2020-01-07 08:34:46 +08:00
|
|
|
private readonly Updater _updater = new Updater(Wox.Properties.Settings.Default.GithubRepo);
|
2020-01-20 07:06:16 +08:00
|
|
|
private readonly Alphabet _alphabet = new Alphabet();
|
|
|
|
private StringMatcher _stringMatcher;
|
2016-02-18 19:35:17 +08:00
|
|
|
|
2014-10-21 18:16:05 +08:00
|
|
|
[STAThread]
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
Stopwatch.Normal("|App.OnStartup|Startup cost", () =>
|
2015-11-02 10:47:43 +08:00
|
|
|
{
|
2017-01-24 08:24:20 +08:00
|
|
|
Log.Info("|App.OnStartup|Begin Wox startup ----------------------------------------------------");
|
2017-04-01 20:02:07 +08:00
|
|
|
Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}");
|
2017-04-01 19:49:02 +08:00
|
|
|
RegisterAppDomainExceptions();
|
2016-05-08 05:44:38 +08:00
|
|
|
RegisterDispatcherUnhandledException();
|
2016-02-21 23:26:57 +08:00
|
|
|
|
2017-02-13 01:10:42 +08:00
|
|
|
ImageLoader.Initialize();
|
|
|
|
|
2020-01-07 08:34:46 +08:00
|
|
|
_settingsVM = new SettingWindowViewModel(_updater);
|
2017-03-02 07:47:50 +08:00
|
|
|
_settings = _settingsVM.Settings;
|
2016-05-10 08:08:54 +08:00
|
|
|
|
2020-01-20 07:06:16 +08:00
|
|
|
_alphabet.Initialize(_settings);
|
2020-01-25 06:12:32 +08:00
|
|
|
_stringMatcher = new StringMatcher(_alphabet);
|
|
|
|
StringMatcher.Instance = _stringMatcher;
|
2020-01-20 07:06:16 +08:00
|
|
|
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
2019-09-29 12:27:46 +08:00
|
|
|
|
2016-05-12 10:01:33 +08:00
|
|
|
PluginManager.LoadPlugins(_settings.PluginSettings);
|
2017-03-02 07:47:50 +08:00
|
|
|
_mainVM = new MainViewModel(_settings);
|
|
|
|
var window = new MainWindow(_settings, _mainVM);
|
2020-01-20 07:06:16 +08:00
|
|
|
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
|
2016-05-10 08:08:54 +08:00
|
|
|
PluginManager.InitializePlugins(API);
|
2017-04-01 20:02:07 +08:00
|
|
|
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");
|
2016-03-28 08:09:40 +08:00
|
|
|
|
2016-05-10 05:45:20 +08:00
|
|
|
Current.MainWindow = window;
|
2017-02-13 01:10:42 +08:00
|
|
|
Current.MainWindow.Title = Constant.Wox;
|
2016-05-18 05:23:37 +08:00
|
|
|
|
2017-02-21 10:19:50 +08: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-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();
|
|
|
|
|
2017-03-02 07:47:50 +08:00
|
|
|
_mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible;
|
2017-01-24 08:24:20 +08:00
|
|
|
Log.Info("|App.OnStartup|End Wox startup ---------------------------------------------------- ");
|
2015-11-05 05:35:04 +08:00
|
|
|
});
|
2014-10-21 18:16:05 +08:00
|
|
|
}
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
|
2016-05-19 02:35:34 +08:00
|
|
|
private void AutoStartup()
|
|
|
|
{
|
|
|
|
if (_settings.StartWoxOnSystemStartup)
|
|
|
|
{
|
|
|
|
if (!SettingWindow.StartupSet())
|
|
|
|
{
|
|
|
|
SettingWindow.SetStartup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:25:17 +08:00
|
|
|
//[Conditional("RELEASE")]
|
2016-05-18 05:23:37 +08:00
|
|
|
private void AutoUpdates()
|
2016-05-08 05:44:38 +08:00
|
|
|
{
|
2017-03-06 07:59:10 +08:00
|
|
|
Task.Run(async () =>
|
2016-05-12 10:01:33 +08:00
|
|
|
{
|
2017-03-06 07:59:10 +08:00
|
|
|
if (_settings.AutoUpdates)
|
|
|
|
{
|
|
|
|
// check udpate every 5 hours
|
|
|
|
var timer = new Timer(1000 * 60 * 60 * 5);
|
|
|
|
timer.Elapsed += async (s, e) =>
|
|
|
|
{
|
2020-01-07 08:34:46 +08:00
|
|
|
await _updater.UpdateApp();
|
2017-03-06 07:59:10 +08:00
|
|
|
};
|
|
|
|
timer.Start();
|
|
|
|
|
|
|
|
// check updates on startup
|
2020-01-07 08:34:46 +08:00
|
|
|
await _updater.UpdateApp();
|
2017-03-06 07:59:10 +08:00
|
|
|
}
|
|
|
|
});
|
2016-05-08 05:44:38 +08:00
|
|
|
}
|
2017-03-02 07:47:50 +08:00
|
|
|
|
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();
|
|
|
|
}
|
2016-05-16 00:03:06 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2016-07-20 07:24:28 +08:00
|
|
|
/// 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")]
|
2016-05-08 05:44:38 +08:00
|
|
|
private void RegisterDispatcherUnhandledException()
|
2015-11-07 03:55:48 +08:00
|
|
|
{
|
|
|
|
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
|
|
|
|
}
|
|
|
|
|
2016-05-16 00:03:06 +08:00
|
|
|
|
|
|
|
/// <summary>
|
2016-07-20 07:24:28 +08:00
|
|
|
/// let exception throw as normal is better for Debug
|
2016-05-16 00:03:06 +08:00
|
|
|
/// </summary>
|
2016-05-08 05:44:38 +08:00
|
|
|
[Conditional("RELEASE")]
|
2016-05-16 00:03:06 +08:00
|
|
|
private static void RegisterAppDomainExceptions()
|
2014-10-21 18:16:05 +08:00
|
|
|
{
|
2016-05-08 05:44:38 +08:00
|
|
|
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
|
|
|
{
|
2020-01-20 07:06:16 +08:00
|
|
|
API.SaveAppAllSettings();
|
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()
|
|
|
|
{
|
2016-05-25 08:00:10 +08:00
|
|
|
Current.MainWindow.Visibility = Visibility.Visible;
|
2016-05-10 05:45:20 +08:00
|
|
|
}
|
2014-10-21 18:16:05 +08:00
|
|
|
}
|
2016-05-08 05:44:38 +08:00
|
|
|
}
|