PublicAPIInstance.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Windows; 9 10 using ManagedCommon; 11 using Microsoft.Toolkit.Uwp.Notifications; 12 using PowerLauncher.Helper; 13 using PowerLauncher.Plugin; 14 using PowerLauncher.ViewModel; 15 using Windows.UI.Notifications; 16 using Wox.Infrastructure; 17 using Wox.Infrastructure.Image; 18 using Wox.Plugin; 19 20 namespace Wox 21 { 22 public class PublicAPIInstance : IPublicAPI, IDisposable 23 { 24 private readonly SettingWindowViewModel _settingsVM; 25 private readonly MainViewModel _mainVM; 26 private readonly Alphabet _alphabet; 27 private readonly ThemeManager _themeManager; 28 private bool _disposed; 29 30 public event Common.UI.ThemeChangedHandler ThemeChanged; 31 32 public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, Alphabet alphabet, ThemeManager themeManager) 33 { 34 _settingsVM = settingsVM ?? throw new ArgumentNullException(nameof(settingsVM)); 35 _mainVM = mainVM ?? throw new ArgumentNullException(nameof(mainVM)); 36 _alphabet = alphabet ?? throw new ArgumentNullException(nameof(alphabet)); 37 _themeManager = themeManager ?? throw new ArgumentNullException(nameof(themeManager)); 38 _themeManager.ThemeChanged += OnThemeChanged; 39 40 ToastNotificationManagerCompat.OnActivated += args => 41 { 42 }; 43 } 44 45 public void RemoveUserSelectedItem(Result result) 46 { 47 _mainVM.RemoveUserSelectedRecord(result); 48 _mainVM.ChangeQueryText(_mainVM.QueryText, true); 49 } 50 51 public void ChangeQuery(string query, bool requery = false) 52 { 53 Application.Current.Dispatcher.Invoke(() => 54 { 55 _mainVM.ChangeQueryText(query, requery); 56 }); 57 } 58 59 public void CheckForNewUpdate() 60 { 61 // _settingsVM.UpdateApp(); 62 } 63 64 public void SaveAppAllSettings() 65 { 66 _mainVM.Save(); 67 _settingsVM.Save(); 68 PluginManager.Save(); 69 ImageLoader.Save(); 70 _alphabet.Save(); 71 } 72 73 public void ReloadAllPluginData() 74 { 75 PluginManager.ReloadData(); 76 } 77 78 public void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true) 79 { 80 Application.Current.Dispatcher.Invoke(() => 81 { 82 MessageBox.Show(subTitle, title); 83 }); 84 } 85 86 public void ShowNotification(string text, string secondaryText = null) 87 { 88 var builder = new ToastContentBuilder().AddText(text); 89 90 if (!string.IsNullOrWhiteSpace(secondaryText)) 91 { 92 builder.AddText(secondaryText); 93 } 94 95 Application.Current.Dispatcher.Invoke(() => 96 { 97 var toast = new ToastNotification(builder.GetToastContent().GetXml()); 98 ToastNotificationManagerCompat.CreateToastNotifier().Show(toast); 99 }); 100 } 101 102 public List<PluginPair> GetAllPlugins() 103 { 104 return PluginManager.AllPlugins.ToList(); 105 } 106 107 public Theme GetCurrentTheme() 108 { 109 return _themeManager.CurrentTheme; 110 } 111 112 public void Dispose() 113 { 114 Dispose(disposing: true); 115 GC.SuppressFinalize(this); 116 } 117 118 protected void OnThemeChanged(Theme oldTheme, Theme newTheme) 119 { 120 ThemeChanged?.Invoke(oldTheme, newTheme); 121 } 122 123 protected virtual void Dispose(bool disposing) 124 { 125 if (!_disposed) 126 { 127 if (disposing) 128 { 129 _themeManager.ThemeChanged -= OnThemeChanged; 130 _disposed = true; 131 } 132 } 133 } 134 } 135 }