-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
App.xaml.cs
213 lines (177 loc) · 7.32 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Collections.Generic;
using System.Threading;
using System.Globalization;
using CommandLine;
using NLog;
using Wox.Core;
using Wox.Core.Configuration;
using Wox.Core.Plugin;
using Wox.Core.Resource;
using Wox.Helper;
using Wox.Infrastructure;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.UserSettings;
using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch;
using Wox.Infrastructure.Exception;
namespace Wox
{
public partial class App : IDisposable, ISingleInstanceApp
{
public static PublicAPIInstance API { get; private set; }
private const string Unique = "Wox_Unique_Application_Mutex";
private static bool _disposed;
public Settings Settings => _settings;
private Settings _settings;
private MainViewModel _mainVM;
private SettingWindowViewModel _settingsVM;
private readonly Updater _updater = new Updater(Wox.Properties.Settings.Default.GithubRepo);
private readonly Portable _portable = new Portable();
private readonly Alphabet _alphabet = new Alphabet();
private StringMatcher _stringMatcher;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private class Options
{
[Option('q', "query", Required = false, HelpText = "Specify text to query on startup.")]
public string QueryText { get; set; }
}
private void ParseCommandLineArgs(IList<string> args)
{
if (args == null)
return;
Parser.Default.ParseArguments<Options>(args)
.WithParsed(o =>
{
if (o.QueryText != null && _mainVM != null)
_mainVM.ChangeQueryText(o.QueryText);
});
}
[STAThread]
public static void Main()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{
using (var application = new App())
{
application.InitializeComponent();
application.Run();
}
}
}
private void OnStartup(object sender, StartupEventArgs e)
{
Logger.StopWatchNormal("Startup cost", () =>
{
Constant.Initialize();
Logger.WoxInfo("Begin Wox startup----------------------------------------------------");
Logger.WoxInfo($"Runtime info:{ExceptionFormatter.RuntimeInfo()}");
RegisterAppDomainExceptions();
RegisterDispatcherUnhandledException();
//throw new Exception("sentry wox exception");
_portable.PreStartCleanUpAfterPortabilityUpdate();
ImageLoader.Initialize();
_settingsVM = new SettingWindowViewModel(_updater, _portable);
_settings = _settingsVM.Settings;
_alphabet.Initialize(_settings);
_stringMatcher = new StringMatcher(_alphabet);
StringMatcher.Instance = _stringMatcher;
_stringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
PluginManager.LoadPlugins(_settings.PluginSettings);
_mainVM = new MainViewModel(_settings);
var window = new MainWindow(_settings, _mainVM);
API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet);
PluginManager.InitializePlugins(API);
Current.MainWindow = window;
Current.MainWindow.Title = Constant.Wox;
// 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;
RegisterExitEvents();
AutoStartup();
AutoUpdates();
ParseCommandLineArgs(SingleInstance<App>.CommandLineArgs);
_mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible;
Logger.WoxInfo($"SDK Info: {ExceptionFormatter.SDKInfo()}");
Logger.WoxInfo("End Wox startup ---------------------------------------------------- ");
});
}
private void AutoStartup()
{
if (_settings.StartWoxOnSystemStartup)
{
if (!SettingWindow.StartupSet())
{
SettingWindow.SetStartup();
}
}
}
//[Conditional("RELEASE")]
private void AutoUpdates()
{
Task.Run(async () =>
{
if (_settings.AutoUpdates)
{
// check udpate every 5 hours
var timer = new System.Timers.Timer(1000 * 60 * 60 * 5);
timer.Elapsed += async (s, e) =>
{
await _updater.UpdateApp(true, _settings.UpdateToPrereleases);
};
timer.Start();
// check updates on startup
await _updater.UpdateApp(true, _settings.UpdateToPrereleases);
}
}).ContinueWith(ErrorReporting.UnhandledExceptionHandleTask, TaskContinuationOptions.OnlyOnFaulted);
}
private void RegisterExitEvents()
{
AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose();
Current.Exit += (s, e) => Dispose();
Current.SessionEnding += (s, e) => Dispose();
}
/// <summary>
/// let exception throw as normal is better for Debug
/// </summary>
[Conditional("RELEASE")]
private void RegisterDispatcherUnhandledException()
{
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
}
/// <summary>
/// let exception throw as normal is better for Debug
/// </summary>
[Conditional("RELEASE")]
private static void RegisterAppDomainExceptions()
{
AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandleMain;
}
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)
{
API.SaveAppAllSettings();
_disposed = true;
}
}
public void OnSecondAppStarted(IList<string> args)
{
ParseCommandLineArgs(args);
Current.MainWindow.Visibility = Visibility.Visible;
}
}
}