PowerToys/Wox/App.xaml.cs

85 lines
2.8 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
using System.Collections.Generic;
2015-11-07 03:55:48 +08:00
using System.Diagnostics;
2016-01-07 06:18:27 +08:00
using System.IO;
2014-01-29 18:33:24 +08:00
using System.Linq;
2016-01-07 06:18:27 +08:00
using System.Reflection;
2015-11-02 21:43:19 +08:00
using System.Threading;
2015-10-31 07:17:34 +08:00
using System.Windows;
using Wox.CommandArgs;
2015-10-31 04:47:03 +08:00
using Wox.Core.Plugin;
using Wox.Core.UserSettings;
2014-03-11 23:54:37 +08:00
using Wox.Helper;
2016-01-07 06:18:27 +08:00
using Wox.Infrastructure;
using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch;
2014-01-29 18:33:24 +08:00
namespace Wox
{
public partial class App : Application, ISingleInstanceApp
{
private const string Unique = "Wox_Unique_Application_Mutex";
public static MainWindow Window { get; private set; }
public static PublicAPIInstance API { get; private set; }
[STAThread]
public static void Main()
{
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{
var application = new App();
application.InitializeComponent();
application.Run();
SingleInstance<App>.Cleanup();
}
}
protected override void OnStartup(StartupEventArgs e)
{
Stopwatch.Debug("Startup Time", () =>
2015-11-02 10:47:43 +08:00
{
base.OnStartup(e);
2016-01-07 06:18:27 +08:00
WoxDirectroy.Executable = Directory.GetParent(Assembly.GetExecutingAssembly().Location).ToString();
2015-11-07 03:55:48 +08:00
RegisterUnhandledException();
2016-02-21 23:26:57 +08:00
ThreadPool.SetMaxThreads(30, 10);
ThreadPool.SetMinThreads(10, 5);
ThreadPool.QueueUserWorkItem(_ => { ImageLoader.ImageLoader.PreloadImages(); });
MainViewModel mainVM = new MainViewModel();
API = new PublicAPIInstance(mainVM);
2016-02-21 23:26:57 +08:00
Window = new MainWindow {DataContext = mainVM};
2016-02-12 15:22:01 +08:00
NotifyIconManager notifyIconManager = new NotifyIconManager(API);
PluginManager.Init(API);
2015-11-02 10:47:43 +08:00
CommandArgsFactory.Execute(e.Args.ToList());
// happlebao todo: the whole setting releated initialization should be put into seperate class/method
API.SetHotkey(UserSettingStorage.Instance.Hotkey, API.OnHotkey);
API.SetCustomPluginHotkey();
});
}
2015-11-11 08:33:33 +08:00
[Conditional("RELEASE")]
2015-11-07 03:55:48 +08:00
private void RegisterUnhandledException()
{
// let exception throw as normal is better for Debug
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle;
}
public void OnActivate(IList<string> args)
{
if (args.Count > 0 && args[0] == SingleInstance<App>.Restart)
{
API.CloseApp();
}
else
{
CommandArgsFactory.Execute(args);
}
}
}
2014-01-29 18:33:24 +08:00
}