From 6e3bc5e65d92000133735eecf482c28a3a93698a Mon Sep 17 00:00:00 2001 From: bao-qian Date: Wed, 1 Mar 2017 23:47:50 +0000 Subject: [PATCH] Save more --- Wox/App.xaml.cs | 31 ++++++++++++++----------------- Wox/MainWindow.xaml.cs | 2 -- Wox/PublicAPIInstance.cs | 11 +++++++++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index c285789b08..6a12586d61 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -22,6 +22,8 @@ namespace Wox private const string Unique = "Wox_Unique_Application_Mutex"; private static bool _disposed; private Settings _settings; + private MainViewModel _mainVM; + private SettingWindowViewModel _settingsVM; [STAThread] public static void Main() @@ -48,13 +50,13 @@ namespace Wox ImageLoader.Initialize(); Alphabet.Initialize(); - var settingVM = new SettingWindowViewModel(); - _settings = settingVM.Settings; + _settingsVM = new SettingWindowViewModel(); + _settings = _settingsVM.Settings; PluginManager.LoadPlugins(_settings.PluginSettings); - var mainVM = new MainViewModel(_settings); - var window = new MainWindow(_settings, mainVM); - API = new PublicAPIInstance(settingVM, mainVM); + _mainVM = new MainViewModel(_settings); + var window = new MainWindow(_settings, _mainVM); + API = new PublicAPIInstance(_settingsVM, _mainVM); PluginManager.InitializePlugins(API); Current.MainWindow = window; @@ -75,7 +77,7 @@ namespace Wox AutoStartup(); AutoUpdates(); - mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible; + _mainVM.MainWindowVisibility = _settings.HideOnStartup ? Visibility.Hidden : Visibility.Visible; Log.Info("|App.OnStartup|End Wox startup ---------------------------------------------------- "); }); } @@ -98,16 +100,14 @@ namespace Wox { // check udpate every 5 hours var timer = new Timer(1000 * 60 * 60 * 5); - timer.Elapsed += (s, e) => - { - Updater.UpdateApp(); - }; + timer.Elapsed += (s, e) => { Updater.UpdateApp(); }; timer.Start(); // check updates on startup Updater.UpdateApp(); } } + private void RegisterExitEvents() { AppDomain.CurrentDomain.ProcessExit += (s, e) => Dispose(); @@ -125,19 +125,15 @@ namespace Wox } - /// /// let exception throw as normal is better for Debug /// [Conditional("RELEASE")] private static void RegisterAppDomainExceptions() { - AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle; - AppDomain.CurrentDomain.FirstChanceException += (s, e) => - { - Log.Exception("|App.RegisterAppDomainExceptions|First Chance Exception:", e.Exception); - }; + AppDomain.CurrentDomain.FirstChanceException += + (s, e) => { Log.Exception("|App.RegisterAppDomainExceptions|First Chance Exception:", e.Exception); }; } public void Dispose() @@ -146,7 +142,8 @@ namespace Wox // 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()); + _mainVM.Save(); + _settingsVM.Save(); PluginManager.Save(); ImageLoader.Save(); diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 1f37c57af9..2b84bb890f 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -11,12 +11,10 @@ using Wox.Helper; using Wox.Infrastructure.UserSettings; using Wox.ViewModel; using Screen = System.Windows.Forms.Screen; -using ContextMenu = System.Windows.Forms.ContextMenu; using ContextMenuStrip = System.Windows.Forms.ContextMenuStrip; using DataFormats = System.Windows.DataFormats; using DragEventArgs = System.Windows.DragEventArgs; using KeyEventArgs = System.Windows.Input.KeyEventArgs; -using MenuItem = System.Windows.Forms.MenuItem; using MessageBox = System.Windows.MessageBox; using NotifyIcon = System.Windows.Forms.NotifyIcon; diff --git a/Wox/PublicAPIInstance.cs b/Wox/PublicAPIInstance.cs index 86f415727e..748c7aac65 100644 --- a/Wox/PublicAPIInstance.cs +++ b/Wox/PublicAPIInstance.cs @@ -8,7 +8,9 @@ using Squirrel; using Wox.Core.Plugin; using Wox.Core.Resource; using Wox.Helper; +using Wox.Infrastructure; using Wox.Infrastructure.Hotkey; +using Wox.Infrastructure.Image; using Wox.Plugin; using Wox.ViewModel; @@ -53,11 +55,16 @@ namespace Wox public void RestarApp() { _mainVM.MainWindowVisibility = Visibility.Hidden; + // we must manually save // UpdateManager.RestartApp() will call Environment.Exit(0) // which will cause ungraceful exit - var vm = (MainViewModel) Application.Current.MainWindow.DataContext; - vm.Save(); + _mainVM.Save(); + _settingsVM.Save(); + PluginManager.Save(); + ImageLoader.Save(); + Alphabet.Save(); + UpdateManager.RestartApp(); }