diff --git a/Plugins/Wox.Plugin.CMD/CMD.cs b/Plugins/Wox.Plugin.CMD/CMD.cs index 5a7ccf3523..be6043d51a 100644 --- a/Plugins/Wox.Plugin.CMD/CMD.cs +++ b/Plugins/Wox.Plugin.CMD/CMD.cs @@ -13,7 +13,7 @@ using Control = System.Windows.Controls.Control; namespace Wox.Plugin.CMD { - public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IContextMenu + public class CMD : IPlugin, ISettingProvider, IPluginI18n, IContextMenu { private PluginInitContext context; private bool WinRStroked; diff --git a/Plugins/Wox.Plugin.Everything/Main.cs b/Plugins/Wox.Plugin.Everything/Main.cs index 18a82d292a..2b79949eb9 100644 --- a/Plugins/Wox.Plugin.Everything/Main.cs +++ b/Plugins/Wox.Plugin.Everything/Main.cs @@ -13,7 +13,7 @@ using Wox.Plugin.Everything.Everything; namespace Wox.Plugin.Everything { - public class Main : IPlugin, IPluginI18n, IContextMenu + public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable { private readonly EverythingAPI _api = new EverythingAPI(); private static readonly List ImageExts = new List { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" }; @@ -34,7 +34,7 @@ namespace Wox.Plugin.Everything _settings = _storage.Load(); } - ~Main() + public void Save() { _storage.Save(); } diff --git a/Plugins/Wox.Plugin.Folder/FolderPlugin.cs b/Plugins/Wox.Plugin.Folder/FolderPlugin.cs index 582495f327..0a8185d4d6 100644 --- a/Plugins/Wox.Plugin.Folder/FolderPlugin.cs +++ b/Plugins/Wox.Plugin.Folder/FolderPlugin.cs @@ -9,7 +9,7 @@ using Wox.Infrastructure.Storage; namespace Wox.Plugin.Folder { - public class FolderPlugin : IPlugin, ISettingProvider, IPluginI18n + public class FolderPlugin : IPlugin, ISettingProvider, IPluginI18n, ISavable { private static List driverNames; private PluginInitContext context; @@ -23,7 +23,7 @@ namespace Wox.Plugin.Folder _settings = _storage.Load(); } - ~FolderPlugin() + public void Save() { _storage.Save(); } diff --git a/Plugins/Wox.Plugin.Program/Programs.cs b/Plugins/Wox.Plugin.Program/Programs.cs index 5b5ca1530b..7c2cb20043 100644 --- a/Plugins/Wox.Plugin.Program/Programs.cs +++ b/Plugins/Wox.Plugin.Program/Programs.cs @@ -14,7 +14,7 @@ using Stopwatch = Wox.Infrastructure.Stopwatch; namespace Wox.Plugin.Program { - public class Programs : ISettingProvider, IPlugin, IPluginI18n, IContextMenu + public class Programs : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable { private static object lockObject = new object(); private static List _programs = new List(); @@ -42,7 +42,7 @@ namespace Wox.Plugin.Program _cache = _cacheStorage.Load(); } - ~Programs() + public void Save() { _settingsStorage.Save(); _cacheStorage.Save(); diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs b/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs index 64c69edc6a..2bb2ee877d 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs @@ -9,7 +9,7 @@ using Wox.Plugin.WebSearch.SuggestionSources; namespace Wox.Plugin.WebSearch { - public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IMultipleActionKeywords + public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IMultipleActionKeywords, ISavable { public PluginInitContext Context { get; private set; } @@ -22,7 +22,7 @@ namespace Wox.Plugin.WebSearch _settings = _storage.Load(); } - ~WebSearchPlugin() + public void Save() { _storage.Save(); } diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index a28ccd7cf3..aeed2d95c8 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -8,6 +8,7 @@ using Wox.Core.UserSettings; using Wox.Infrastructure; using Wox.Infrastructure.Exception; using Wox.Infrastructure.Logger; +using Wox.Infrastructure.Storage; using Wox.Plugin; namespace Wox.Core.Plugin @@ -50,6 +51,15 @@ namespace Wox.Core.Plugin ValidateUserDirectory(); } + public static void Save() + { + foreach (var plugin in AllPlugins) + { + var savable = plugin.Plugin as ISavable; + savable?.Save(); + } + } + public static void InitializePlugins(IPublicAPI api) { var metadatas = PluginConfig.Parse(Directories); diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index 7f9fd26f5f..f823452e2c 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -9,6 +9,7 @@ using System.Windows; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; +using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage; namespace Wox.Infrastructure.Image @@ -71,7 +72,7 @@ namespace Wox.Infrastructure.Image public static void PreloadImages() { - Stopwatch.Debug($"Preload {_cache.TopUsedImages.Count} images", () => + Stopwatch.Debug("Preload images from cache", () => { _cache.TopUsedImages.AsParallel().Where(i => !_imageSources.ContainsKey(i.Key)).ForAll(i => { @@ -87,6 +88,7 @@ namespace Wox.Infrastructure.Image } }); }); + Log.Info($"Preload {_cache.TopUsedImages.Count} images from cache"); } public static ImageSource Load(string path, bool addToCache = true) diff --git a/Wox.Infrastructure/Storage/ISavable.cs b/Wox.Infrastructure/Storage/ISavable.cs new file mode 100644 index 0000000000..67e4aa721b --- /dev/null +++ b/Wox.Infrastructure/Storage/ISavable.cs @@ -0,0 +1,11 @@ +namespace Wox.Infrastructure.Storage +{ + /// + /// Save plugin settings/cache, + /// todo should be merged into a abstract class intead of seperate interface + /// + public interface ISavable + { + void Save(); + } +} diff --git a/Wox.Infrastructure/Wox.Infrastructure.csproj b/Wox.Infrastructure/Wox.Infrastructure.csproj index c3defba823..093b682489 100644 --- a/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -78,6 +78,7 @@ + diff --git a/Wox/App.xaml b/Wox/App.xaml index 39976c70f0..d7c5a879e3 100644 --- a/Wox/App.xaml +++ b/Wox/App.xaml @@ -1,6 +1,8 @@  + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + Exit="OnExit" + SessionEnding="OnSessionEnding"> diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index c6fb67a19c..caa00246b5 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -13,11 +13,12 @@ using Stopwatch = Wox.Infrastructure.Stopwatch; namespace Wox { - public partial class App : Application, ISingleInstanceApp + public partial class App : ISingleInstanceApp { private const string Unique = "Wox_Unique_Application_Mutex"; public static MainWindow Window { get; private set; } public static PublicAPIInstance API { get; private set; } + private bool _saved; [STAThread] public static void Main() @@ -71,5 +72,29 @@ namespace Wox CommandArgsFactory.Execute(args); } } + + private void OnExit(object sender, ExitEventArgs e) + { + Save(); + } + + private void OnSessionEnding(object sender, SessionEndingCancelEventArgs e) + { + Save(); + } + + private void Save() + { + // 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 (!_saved) + { + var vm = (MainViewModel) Window.DataContext; + vm.Save(); + PluginManager.Save(); + ImageLoader.Save(); + _saved = true; + } + } } } diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index 8bbac730dc..b63707512d 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -18,7 +18,7 @@ using Wox.Storage; namespace Wox.ViewModel { - public class MainViewModel : BaseViewModel + public class MainViewModel : BaseViewModel, ISavable { #region Private Fields @@ -81,7 +81,7 @@ namespace Wox.ViewModel InitializeKeyCommands(); } - ~MainViewModel() + public void Save() { _settingsStorage.Save(); _queryHistoryStorage.Save();