diff --git a/Plugins/Wox.Plugin.CMD/CMD.cs b/Plugins/Wox.Plugin.CMD/CMD.cs index fe6d29e3f7..19f639bb32 100644 --- a/Plugins/Wox.Plugin.CMD/CMD.cs +++ b/Plugins/Wox.Plugin.CMD/CMD.cs @@ -17,6 +17,7 @@ namespace Wox.Plugin.CMD private PluginInitContext context; private bool WinRStroked; private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator()); + private readonly CMDStorage _settings = CMDStorage.Instance; public List Query(Query query) { @@ -80,7 +81,7 @@ namespace Wox.Plugin.CMD private List GetHistoryCmds(string cmd, Result result) { - IEnumerable history = CMDStorage.Instance.CMDHistory.Where(o => o.Key.Contains(cmd)) + IEnumerable history = _settings.CMDHistory.Where(o => o.Key.Contains(cmd)) .OrderByDescending(o => o.Value) .Select(m => { @@ -126,7 +127,7 @@ namespace Wox.Plugin.CMD private List ResultsFromlHistory() { - IEnumerable history = CMDStorage.Instance.CMDHistory.OrderByDescending(o => o.Value) + IEnumerable history = _settings.CMDHistory.OrderByDescending(o => o.Value) .Select(m => new Result { Title = m.Key, @@ -143,7 +144,7 @@ namespace Wox.Plugin.CMD private void ExecuteCMD(string cmd, bool runAsAdministrator = false) { - var arguments = CMDStorage.Instance.LeaveCmdOpen ? $"/k {cmd}" : $"/c {cmd} & pause"; + var arguments = _settings.LeaveCmdOpen ? $"/k {cmd}" : $"/c {cmd} & pause"; var info = new ProcessStartInfo { UseShellExecute = true, @@ -154,7 +155,7 @@ namespace Wox.Plugin.CMD try { Process.Start(info); - CMDStorage.Instance.AddCmdHistory(cmd); + _settings.AddCmdHistory(cmd); } catch (FileNotFoundException e) { @@ -170,7 +171,7 @@ namespace Wox.Plugin.CMD bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state) { - if (CMDStorage.Instance.ReplaceWinR) + if (_settings.ReplaceWinR) { if (keyevent == (int)KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed) { @@ -196,7 +197,7 @@ namespace Wox.Plugin.CMD public Control CreateSettingPanel() { - return new CMDSetting(); + return new CMDSetting(_settings); } public string GetTranslatedPluginTitle() diff --git a/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs b/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs index 6b93c30abe..742a8c1eb6 100644 --- a/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs +++ b/Plugins/Wox.Plugin.CMD/CMDSetting.xaml.cs @@ -5,37 +5,40 @@ namespace Wox.Plugin.CMD { public partial class CMDSetting : UserControl { - public CMDSetting() + private readonly CMDStorage _settings; + + public CMDSetting(CMDStorage settings) { InitializeComponent(); + _settings = settings; } private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re) { - cbReplaceWinR.IsChecked = CMDStorage.Instance.ReplaceWinR; - cbLeaveCmdOpen.IsChecked = CMDStorage.Instance.LeaveCmdOpen; + cbReplaceWinR.IsChecked = _settings.ReplaceWinR; + cbLeaveCmdOpen.IsChecked = _settings.LeaveCmdOpen; cbLeaveCmdOpen.Checked += (o, e) => { - CMDStorage.Instance.LeaveCmdOpen = true; - CMDStorage.Instance.Save(); + _settings.LeaveCmdOpen = true; + _settings.Save(); }; cbLeaveCmdOpen.Unchecked += (o, e) => { - CMDStorage.Instance.LeaveCmdOpen = false; - CMDStorage.Instance.Save(); + _settings.LeaveCmdOpen = false; + _settings.Save(); }; cbReplaceWinR.Checked += (o, e) => { - CMDStorage.Instance.ReplaceWinR = true; - CMDStorage.Instance.Save(); + _settings.ReplaceWinR = true; + _settings.Save(); }; cbReplaceWinR.Unchecked += (o, e) => { - CMDStorage.Instance.ReplaceWinR = false; - CMDStorage.Instance.Save(); + _settings.ReplaceWinR = false; + _settings.Save(); }; } } diff --git a/Plugins/Wox.Plugin.Everything/Main.cs b/Plugins/Wox.Plugin.Everything/Main.cs index afdfe1fdcd..ae3d3baea8 100644 --- a/Plugins/Wox.Plugin.Everything/Main.cs +++ b/Plugins/Wox.Plugin.Everything/Main.cs @@ -17,6 +17,7 @@ namespace Wox.Plugin.Everything private readonly EverythingAPI _api = new EverythingAPI(); private static readonly List ImageExts = new List { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" }; private static readonly List ExecutableExts = new List { ".exe" }; + private ContextMenuStorage _settings = ContextMenuStorage.Instance; public List Query(Query query) { @@ -24,10 +25,10 @@ namespace Wox.Plugin.Everything if (!string.IsNullOrEmpty(query.Search)) { var keyword = query.Search; - if (ContextMenuStorage.Instance.MaxSearchCount <= 0) + if (_settings.MaxSearchCount <= 0) { - ContextMenuStorage.Instance.MaxSearchCount = 50; - ContextMenuStorage.Instance.Save(); + _settings.MaxSearchCount = 50; + _settings.Save(); } if (keyword == "uninstalleverything") @@ -47,7 +48,7 @@ namespace Wox.Plugin.Everything try { - var searchList = _api.Search(keyword, maxCount: ContextMenuStorage.Instance.MaxSearchCount).ToList(); + var searchList = _api.Search(keyword, maxCount: _settings.MaxSearchCount).ToList(); foreach (var s in searchList) { var path = s.FullPath; @@ -154,7 +155,7 @@ namespace Wox.Plugin.Everything public void Init(PluginInitContext context) { _context = context; - ContextMenuStorage.Instance.API = context.API; + _settings.API = context.API; LoadLibrary(Path.Combine( Path.Combine(context.CurrentPluginMetadata.PluginDirectory, (IntPtr.Size == 4) ? "x86" : "x64"), @@ -285,7 +286,7 @@ namespace Wox.Plugin.Everything List availableContextMenus = new List(); availableContextMenus.AddRange(GetDefaultContextMenu()); - availableContextMenus.AddRange(ContextMenuStorage.Instance.ContextMenus); + availableContextMenus.AddRange(_settings.ContextMenus); if (record.Type == ResultType.File) { diff --git a/Plugins/Wox.Plugin.Folder/FolderPlugin.cs b/Plugins/Wox.Plugin.Folder/FolderPlugin.cs index c57cdad1e8..3de94cede6 100644 --- a/Plugins/Wox.Plugin.Folder/FolderPlugin.cs +++ b/Plugins/Wox.Plugin.Folder/FolderPlugin.cs @@ -12,20 +12,21 @@ namespace Wox.Plugin.Folder { private static List driverNames; private PluginInitContext context; + private FolderStorage _settings = FolderStorage.Instance; public Control CreateSettingPanel() { - return new FileSystemSettings(context.API); + return new FileSystemSettings(context.API, _settings); } public void Init(PluginInitContext context) { this.context = context; InitialDriverList(); - if (FolderStorage.Instance.FolderLinks == null) + if (_settings.FolderLinks == null) { - FolderStorage.Instance.FolderLinks = new List(); - FolderStorage.Instance.Save(); + _settings.FolderLinks = new List(); + _settings.Save(); } } @@ -33,7 +34,7 @@ namespace Wox.Plugin.Folder { string input = query.Search.ToLower(); - List userFolderLinks = FolderStorage.Instance.FolderLinks.Where( + List userFolderLinks = _settings.FolderLinks.Where( x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase)).ToList(); List results = userFolderLinks.Select( diff --git a/Plugins/Wox.Plugin.Folder/FolderPluginSettings.xaml.cs b/Plugins/Wox.Plugin.Folder/FolderPluginSettings.xaml.cs index 9281c83715..aa109aa2ae 100644 --- a/Plugins/Wox.Plugin.Folder/FolderPluginSettings.xaml.cs +++ b/Plugins/Wox.Plugin.Folder/FolderPluginSettings.xaml.cs @@ -7,20 +7,21 @@ using DataFormats = System.Windows.DataFormats; using DragDropEffects = System.Windows.DragDropEffects; using DragEventArgs = System.Windows.DragEventArgs; using MessageBox = System.Windows.MessageBox; -using UserControl = System.Windows.Controls.UserControl; namespace Wox.Plugin.Folder { - public partial class FileSystemSettings : UserControl + public partial class FileSystemSettings { private IPublicAPI woxAPI; + private FolderStorage _settings; - public FileSystemSettings(IPublicAPI woxAPI) + public FileSystemSettings(IPublicAPI woxAPI, FolderStorage settings) { this.woxAPI = woxAPI; InitializeComponent(); - lbxFolders.ItemsSource = FolderStorage.Instance.FolderLinks; + _settings = settings; + lbxFolders.ItemsSource = _settings.FolderLinks; } private void btnDelete_Click(object sender, RoutedEventArgs e) @@ -32,9 +33,9 @@ namespace Wox.Plugin.Folder if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - FolderStorage.Instance.FolderLinks.Remove(selectedFolder); + _settings.FolderLinks.Remove(selectedFolder); lbxFolders.Items.Refresh(); - FolderStorage.Instance.Save(); + _settings.Save(); } } else @@ -53,10 +54,10 @@ namespace Wox.Plugin.Folder folderBrowserDialog.SelectedPath = selectedFolder.Path; if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { - var link = FolderStorage.Instance.FolderLinks.First(x => x.Path == selectedFolder.Path); + var link = _settings.FolderLinks.First(x => x.Path == selectedFolder.Path); link.Path = folderBrowserDialog.SelectedPath; - FolderStorage.Instance.Save(); + _settings.Save(); } lbxFolders.Items.Refresh(); @@ -78,13 +79,13 @@ namespace Wox.Plugin.Folder Path = folderBrowserDialog.SelectedPath }; - if (FolderStorage.Instance.FolderLinks == null) + if (_settings.FolderLinks == null) { - FolderStorage.Instance.FolderLinks = new List(); + _settings.FolderLinks = new List(); } - FolderStorage.Instance.FolderLinks.Add(newFolder); - FolderStorage.Instance.Save(); + _settings.FolderLinks.Add(newFolder); + _settings.Save(); } lbxFolders.Items.Refresh(); @@ -96,9 +97,9 @@ namespace Wox.Plugin.Folder if (files != null && files.Count() > 0) { - if (FolderStorage.Instance.FolderLinks == null) + if (_settings.FolderLinks == null) { - FolderStorage.Instance.FolderLinks = new List(); + _settings.FolderLinks = new List(); } foreach (string s in files) @@ -110,8 +111,8 @@ namespace Wox.Plugin.Folder Path = s }; - FolderStorage.Instance.FolderLinks.Add(newFolder); - FolderStorage.Instance.Save(); + _settings.FolderLinks.Add(newFolder); + _settings.Save(); } lbxFolders.Items.Refresh(); diff --git a/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs b/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs index c1dff38583..bc1c8cc11c 100644 --- a/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs +++ b/Plugins/Wox.Plugin.Program/AddProgramSource.xaml.cs @@ -1,4 +1,5 @@ -using System.Windows; +using System.Collections.Generic; +using System.Windows; using System.Windows.Forms; namespace Wox.Plugin.Program @@ -9,18 +10,23 @@ namespace Wox.Plugin.Program public partial class AddProgramSource { private ProgramSource _editing; + private ProgramStorage _settings; - public AddProgramSource() + public AddProgramSource(ProgramStorage settings) { + _settings = settings; InitializeComponent(); + Suffixes.Text = string.Join(";", settings.ProgramSuffixes); } - public AddProgramSource(ProgramSource edit) : this() + public AddProgramSource(ProgramSource edit, ProgramStorage settings) { _editing = edit; Directory.Text = _editing.Location; MaxDepth.Text = _editing.MaxDepth.ToString(); - Suffixes.Text = _editing.Suffixes; + Suffixes.Text = string.Join(";", _editing.Suffixes); + _settings = settings; + InitializeComponent(); } private void BrowseButton_Click(object sender, RoutedEventArgs e) @@ -43,11 +49,11 @@ namespace Wox.Plugin.Program if(_editing == null) { - ProgramStorage.Instance.ProgramSources.Add(new ProgramSource + _settings.ProgramSources.Add(new ProgramSource { Location = Directory.Text, MaxDepth = max, - Suffixes = Suffixes.Text, + Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator), Type = "FileSystemProgramSource", Enabled = true }); @@ -56,10 +62,10 @@ namespace Wox.Plugin.Program { _editing.Location = Directory.Text; _editing.MaxDepth = max; - _editing.Suffixes = Suffixes.Text; + _editing.Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator); } - ProgramStorage.Instance.Save(); + _settings.Save(); DialogResult = true; Close(); } diff --git a/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs b/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs index 255a61d9a2..97194fde7d 100644 --- a/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs +++ b/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs @@ -10,7 +10,7 @@ namespace Wox.Plugin.Program private static bool isIndexing; private static List watchedPath = new List(); - public static void AddWatch(string path, bool includingSubDirectory = true) + public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true) { if (watchedPath.Contains(path)) return; if (!Directory.Exists(path)) @@ -20,7 +20,7 @@ namespace Wox.Plugin.Program } watchedPath.Add(path); - foreach (string fileType in ProgramStorage.Instance.ProgramSuffixes.Split(';')) + foreach (string fileType in programSuffixes) { FileSystemWatcher watcher = new FileSystemWatcher { diff --git a/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs b/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs index 35348d836a..3faec277d9 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSetting.xaml.cs @@ -11,19 +11,21 @@ namespace Wox.Plugin.Program public partial class ProgramSetting : UserControl { private PluginInitContext context; + private ProgramStorage _settings; - public ProgramSetting(PluginInitContext context) + public ProgramSetting(PluginInitContext context, ProgramStorage settings) { this.context = context; InitializeComponent(); Loaded += Setting_Loaded; + _settings = settings; } private void Setting_Loaded(object sender, RoutedEventArgs e) { - programSourceView.ItemsSource = ProgramStorage.Instance.ProgramSources; - StartMenuEnabled.IsChecked = ProgramStorage.Instance.EnableStartMenuSource; - RegistryEnabled.IsChecked = ProgramStorage.Instance.EnableRegistrySource; + programSourceView.ItemsSource = _settings.ProgramSources; + StartMenuEnabled.IsChecked = _settings.EnableStartMenuSource; + RegistryEnabled.IsChecked = _settings.EnableRegistrySource; } private void ReIndexing() @@ -39,7 +41,7 @@ namespace Wox.Plugin.Program private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e) { - var add = new AddProgramSource(); + var add = new AddProgramSource(_settings); if(add.ShowDialog() ?? false) { ReIndexing(); @@ -55,8 +57,8 @@ namespace Wox.Plugin.Program if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - ProgramStorage.Instance.ProgramSources.Remove(selectedProgramSource); - ProgramStorage.Instance.Save(); + _settings.ProgramSources.Remove(selectedProgramSource); + _settings.Save(); ReIndexing(); } } @@ -72,7 +74,7 @@ namespace Wox.Plugin.Program ProgramSource selectedProgramSource = programSourceView.SelectedItem as ProgramSource; if (selectedProgramSource != null) { - var add = new AddProgramSource(selectedProgramSource); + var add = new AddProgramSource(selectedProgramSource, _settings); if (add.ShowDialog() ?? false) { ReIndexing(); @@ -92,7 +94,7 @@ namespace Wox.Plugin.Program private void BtnProgramSuffixes_OnClick(object sender, RoutedEventArgs e) { - ProgramSuffixes p = new ProgramSuffixes(context); + ProgramSuffixes p = new ProgramSuffixes(context, _settings); p.ShowDialog(); } @@ -118,14 +120,14 @@ namespace Wox.Plugin.Program { if (Directory.Exists(s)) { - ProgramStorage.Instance.ProgramSources.Add(new ProgramSource + _settings.ProgramSources.Add(new ProgramSource { Location = s, Type = "FileSystemProgramSource", Enabled = true }); - ProgramStorage.Instance.Save(); + _settings.Save(); ReIndexing(); } } @@ -134,15 +136,15 @@ namespace Wox.Plugin.Program private void StartMenuEnabled_Click(object sender, RoutedEventArgs e) { - ProgramStorage.Instance.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false; - ProgramStorage.Instance.Save(); + _settings.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false; + _settings.Save(); ReIndexing(); } private void RegistryEnabled_Click(object sender, RoutedEventArgs e) { - ProgramStorage.Instance.EnableRegistrySource = RegistryEnabled.IsChecked ?? false; - ProgramStorage.Instance.Save(); + _settings.EnableRegistrySource = RegistryEnabled.IsChecked ?? false; + _settings.Save(); ReIndexing(); } } diff --git a/Plugins/Wox.Plugin.Program/ProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSource.cs index bac55bf33a..9d944550b3 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSource.cs @@ -10,7 +10,8 @@ namespace Wox.Plugin.Program public string Type { get; set; } public int BonusPoints { get; set; } public bool Enabled { get; set; } - public string Suffixes { get; set; } + public string[] Suffixes { get; set; } + public const char SuffixSeperator = ';'; public int MaxDepth { get; set; } public Dictionary Meta { get; set; } diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs index df37a6dc51..2eee434c96 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/CommonStartMenuProgramSource.cs @@ -21,13 +21,13 @@ namespace Wox.Plugin.Program.ProgramSources return commonStartMenuPath.ToString(); } - public CommonStartMenuProgramSource() - : base(getPath()) + public CommonStartMenuProgramSource(string[] suffixes) + : base(getPath(), suffixes) { } public CommonStartMenuProgramSource(ProgramSource source) - : this() + : this(source.Suffixes) { BonusPoints = source.BonusPoints; } diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs index 653140158c..eb3bc6967f 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/FileSystemProgramSource.cs @@ -10,19 +10,19 @@ namespace Wox.Plugin.Program.ProgramSources [Serializable] public class FileSystemProgramSource : AbstractProgramSource { - private string baseDirectory; - private int maxDepth; - private string suffixes; + private string _baseDirectory; + private int _maxDepth; + private string[] _suffixes; - public FileSystemProgramSource(string baseDirectory, int maxDepth, string suffixes) + public FileSystemProgramSource(string baseDirectory, int maxDepth, string[] suffixes) { - this.baseDirectory = baseDirectory; - this.maxDepth = maxDepth; - this.suffixes = suffixes; + _baseDirectory = baseDirectory; + _maxDepth = maxDepth; + _suffixes = suffixes; } - public FileSystemProgramSource(string baseDirectory) - : this(baseDirectory, -1, "") {} + public FileSystemProgramSource(string baseDirectory, string[] suffixes) + : this(baseDirectory, -1, suffixes) {} public FileSystemProgramSource(ProgramSource source) : this(source.Location, source.MaxDepth, source.Suffixes) @@ -33,10 +33,10 @@ namespace Wox.Plugin.Program.ProgramSources public override List LoadPrograms() { List list = new List(); - if (Directory.Exists(baseDirectory)) + if (Directory.Exists(_baseDirectory)) { - GetAppFromDirectory(baseDirectory, list); - FileChangeWatcher.AddWatch(baseDirectory); + GetAppFromDirectory(_baseDirectory, list); + FileChangeWatcher.AddWatch(_baseDirectory, _suffixes); } return list; } @@ -48,7 +48,7 @@ namespace Wox.Plugin.Program.ProgramSources private void GetAppFromDirectory(string path, List list, int depth) { - if(maxDepth != -1 && depth > maxDepth) + if(_maxDepth != -1 && depth > _maxDepth) { return; } @@ -56,8 +56,7 @@ namespace Wox.Plugin.Program.ProgramSources { foreach (string file in Directory.GetFiles(path)) { - if (ProgramStorage.Instance.ProgramSuffixes.Split(';').Any(o => file.EndsWith("." + o)) || - suffixes.Split(';').Any(o => file.EndsWith("." + o))) + if (_suffixes.Any(o => file.EndsWith("." + o))) { Program p = CreateEntry(file); list.Add(p); @@ -78,7 +77,7 @@ namespace Wox.Plugin.Program.ProgramSources public override string ToString() { - return typeof(FileSystemProgramSource).Name + ":" + baseDirectory; + return typeof(FileSystemProgramSource).Name + ":" + _baseDirectory; } } } diff --git a/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs b/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs index 4b1ad87bf9..97acba337f 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSources/UserStartMenuProgramSource.cs @@ -7,13 +7,13 @@ namespace Wox.Plugin.Program.ProgramSources [Browsable(false)] public class UserStartMenuProgramSource : FileSystemProgramSource { - public UserStartMenuProgramSource() - : base(Environment.GetFolderPath(Environment.SpecialFolder.Programs)) + public UserStartMenuProgramSource(string[] suffixes) + : base(Environment.GetFolderPath(Environment.SpecialFolder.Programs), suffixes) { } public UserStartMenuProgramSource(ProgramSource source) - : this() + : this(source.Suffixes) { BonusPoints = source.BonusPoints; } diff --git a/Plugins/Wox.Plugin.Program/ProgramStorage.cs b/Plugins/Wox.Plugin.Program/ProgramStorage.cs index 1cefbe87d8..867064d304 100644 --- a/Plugins/Wox.Plugin.Program/ProgramStorage.cs +++ b/Plugins/Wox.Plugin.Program/ProgramStorage.cs @@ -1,17 +1,20 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel; using Newtonsoft.Json; using Wox.Infrastructure.Storage; namespace Wox.Plugin.Program { + [Serializable] public class ProgramStorage : JsonStrorage { [JsonProperty] public List ProgramSources { get; set; } + [JsonProperty] - public string ProgramSuffixes { get; set; } + public string[] ProgramSuffixes { get; set; } [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] [DefaultValue(true)] @@ -31,9 +34,9 @@ namespace Wox.Plugin.Program protected override void OnAfterLoad(ProgramStorage storage) { - if (string.IsNullOrEmpty(storage.ProgramSuffixes)) + if (storage.ProgramSuffixes == null || storage.ProgramSuffixes.Length == 0) { - storage.ProgramSuffixes = "lnk;exe;appref-ms;bat"; + storage.ProgramSuffixes = new[] {"bat", "appref-ms", "exe", "lnk"}; } } diff --git a/Plugins/Wox.Plugin.Program/ProgramSuffixes.xaml.cs b/Plugins/Wox.Plugin.Program/ProgramSuffixes.xaml.cs index 735c28bed5..77ab19d651 100644 --- a/Plugins/Wox.Plugin.Program/ProgramSuffixes.xaml.cs +++ b/Plugins/Wox.Plugin.Program/ProgramSuffixes.xaml.cs @@ -1,4 +1,5 @@ using System.Windows; +using System.Linq; namespace Wox.Plugin.Program { @@ -8,13 +9,14 @@ namespace Wox.Plugin.Program public partial class ProgramSuffixes { private PluginInitContext context; + private ProgramStorage _settings; - public ProgramSuffixes(PluginInitContext context) + public ProgramSuffixes(PluginInitContext context, ProgramStorage settings) { this.context = context; InitializeComponent(); - - tbSuffixes.Text = ProgramStorage.Instance.ProgramSuffixes; + _settings = settings; + tbSuffixes.Text = string.Join(ProgramSource.SuffixSeperator.ToString(), _settings.ProgramSuffixes); } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) @@ -26,7 +28,7 @@ namespace Wox.Plugin.Program return; } - ProgramStorage.Instance.ProgramSuffixes = tbSuffixes.Text; + _settings.ProgramSuffixes = tbSuffixes.Text.Split(ProgramSource.SuffixSeperator); string msg = context.API.GetTranslation("wox_plugin_program_update_file_suffixes"); MessageBox.Show(msg); } diff --git a/Plugins/Wox.Plugin.Program/Programs.cs b/Plugins/Wox.Plugin.Program/Programs.cs index d244bbb053..4a4fe374b2 100644 --- a/Plugins/Wox.Plugin.Program/Programs.cs +++ b/Plugins/Wox.Plugin.Program/Programs.cs @@ -27,6 +27,8 @@ namespace Wox.Plugin.Program {"AppPathsProgramSource", typeof(AppPathsProgramSource)} }; private PluginInitContext _context; + private static ProgramCacheStorage _cache = ProgramCacheStorage.Instance; + private static ProgramStorage _settings = ProgramStorage.Instance; public List Query(Query query) { @@ -63,10 +65,12 @@ namespace Wox.Plugin.Program this._context = context; Stopwatch.Debug("Preload programs", () => { - programs = ProgramCacheStorage.Instance.Programs; + programs = _cache.Programs; }); Log.Info($"Preload {programs.Count} programs from cache"); - Stopwatch.Debug("Program Index", IndexPrograms); + // happlebao todo fix this + //Stopwatch.Debug("Program Index", IndexPrograms); + IndexPrograms(); } public static void IndexPrograms() @@ -75,15 +79,18 @@ namespace Wox.Plugin.Program { List programSources = new List(); programSources.AddRange(LoadDeaultProgramSources()); - if (ProgramStorage.Instance.ProgramSources != null && - ProgramStorage.Instance.ProgramSources.Count(o => o.Enabled) > 0) + if (_settings.ProgramSources != null && + _settings.ProgramSources.Count(o => o.Enabled) > 0) { - programSources.AddRange(ProgramStorage.Instance.ProgramSources); + programSources.AddRange(_settings.ProgramSources); } sources.Clear(); foreach (var source in programSources.Where(o => o.Enabled)) { + // happlebao todo: temp hack for program suffixes + source.Suffixes = _settings.ProgramSuffixes; + Type sourceClass; if (SourceTypes.TryGetValue(source.Type, out sourceClass)) { @@ -112,8 +119,8 @@ namespace Wox.Plugin.Program programs = tempPrograms.GroupBy(x => new { x.ExecutePath, x.ExecuteName }) .Select(g => g.First()).ToList(); - ProgramCacheStorage.Instance.Programs = programs; - ProgramCacheStorage.Instance.Save(); + _cache.Programs = programs; + _cache.Save(); } } @@ -126,19 +133,19 @@ namespace Wox.Plugin.Program list.Add(new ProgramSource { BonusPoints = 0, - Enabled = ProgramStorage.Instance.EnableStartMenuSource, + Enabled = _settings.EnableStartMenuSource, Type = "CommonStartMenuProgramSource" }); list.Add(new ProgramSource { BonusPoints = 0, - Enabled = ProgramStorage.Instance.EnableStartMenuSource, + Enabled = _settings.EnableStartMenuSource, Type = "UserStartMenuProgramSource" }); list.Add(new ProgramSource { BonusPoints = -10, - Enabled = ProgramStorage.Instance.EnableRegistrySource, + Enabled = _settings.EnableRegistrySource, Type = "AppPathsProgramSource" }); return list; @@ -163,7 +170,7 @@ namespace Wox.Plugin.Program public Control CreateSettingPanel() { - return new ProgramSetting(_context); + return new ProgramSetting(_context, _settings); } #endregion diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs b/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs index 4a4c6a1f66..03b6138ef8 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs @@ -10,13 +10,14 @@ namespace Wox.Plugin.WebSearch { public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IMultipleActionKeywords { + private WebSearchStorage _settings = WebSearchStorage.Instance; public PluginInitContext Context { get; private set; } public List Query(Query query) { List results = new List(); WebSearch webSearch = - WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled); + _settings.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled); if (webSearch != null) { @@ -42,7 +43,7 @@ namespace Wox.Plugin.WebSearch }; results.Add(result); - if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword)) + if (_settings.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword)) { // todo use Task.Wait when .net upgraded results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch)); @@ -53,7 +54,7 @@ namespace Wox.Plugin.WebSearch private IEnumerable ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch) { - ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, Context); + ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(_settings.WebSearchSuggestionSource, Context); var suggestions = sugg?.GetSuggestions(keyword); if (suggestions != null) { @@ -83,7 +84,7 @@ namespace Wox.Plugin.WebSearch public Control CreateSettingPanel() { - return new WebSearchesSetting(this); + return new WebSearchesSetting(this, _settings); } #endregion diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml.cs b/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml.cs index f1fa170e32..b2f2368d39 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchSetting.xaml.cs @@ -18,18 +18,20 @@ namespace Wox.Plugin.WebSearch private WebSearch _updateWebSearch; private readonly PluginInitContext _context; private readonly WebSearchPlugin _plugin; + private WebSearchStorage _settings; - public WebSearchSetting(WebSearchesSetting settingWidow) + public WebSearchSetting(WebSearchesSetting settingWidow, WebSearchStorage settings) { _plugin = settingWidow.Plugin; _context = settingWidow.Context; _settingWindow = settingWidow; InitializeComponent(); + _settings = settings; } public void UpdateItem(WebSearch webSearch) { - _updateWebSearch = WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch); + _updateWebSearch = _settings.WebSearches.FirstOrDefault(o => o == webSearch); if (_updateWebSearch == null || string.IsNullOrEmpty(_updateWebSearch.Url)) { @@ -110,7 +112,7 @@ namespace Wox.Plugin.WebSearch MessageBox.Show(exception.Message); return; } - WebSearchStorage.Instance.WebSearches.Add(new WebSearch + _settings.WebSearches.Add(new WebSearch { ActionKeyword = newActionKeyword, Enabled = cbEnable.IsChecked ?? false, @@ -120,7 +122,7 @@ namespace Wox.Plugin.WebSearch }); } - WebSearchStorage.Instance.Save(); + _settings.Save(); _settingWindow.ReloadWebSearchView(); Close(); } diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchesSetting.xaml.cs b/Plugins/Wox.Plugin.WebSearch/WebSearchesSetting.xaml.cs index b6e21209e0..81f94145c3 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchesSetting.xaml.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchesSetting.xaml.cs @@ -10,22 +10,24 @@ namespace Wox.Plugin.WebSearch /// public partial class WebSearchesSetting : UserControl { + private WebSearchStorage _settings; public PluginInitContext Context { get; } public WebSearchPlugin Plugin { get; } - public WebSearchesSetting(WebSearchPlugin plugin) + public WebSearchesSetting(WebSearchPlugin plugin, WebSearchStorage settings) { Context = plugin.Context; Plugin = plugin; InitializeComponent(); Loaded += Setting_Loaded; + _settings = settings; } private void Setting_Loaded(object sender, RoutedEventArgs e) { - webSearchView.ItemsSource = WebSearchStorage.Instance.WebSearches; - cbEnableWebSearchSuggestion.IsChecked = WebSearchStorage.Instance.EnableWebSearchSuggestion; - comboBoxSuggestionSource.Visibility = WebSearchStorage.Instance.EnableWebSearchSuggestion + webSearchView.ItemsSource = _settings.WebSearches; + cbEnableWebSearchSuggestion.IsChecked = _settings.EnableWebSearchSuggestion; + comboBoxSuggestionSource.Visibility = _settings.EnableWebSearchSuggestion ? Visibility.Visible : Visibility.Collapsed; @@ -34,7 +36,7 @@ namespace Wox.Plugin.WebSearch new ComboBoxItem {Content = "Google"}, new ComboBoxItem {Content = "Baidu"} }; - ComboBoxItem selected = items.FirstOrDefault(o => o.Content.ToString() == WebSearchStorage.Instance.WebSearchSuggestionSource); + ComboBoxItem selected = items.FirstOrDefault(o => o.Content.ToString() == _settings.WebSearchSuggestionSource); if (selected == null) { selected = items[0]; @@ -51,7 +53,7 @@ namespace Wox.Plugin.WebSearch private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e) { - WebSearchSetting webSearch = new WebSearchSetting(this); + WebSearchSetting webSearch = new WebSearchSetting(this, _settings); webSearch.ShowDialog(); } @@ -64,7 +66,7 @@ namespace Wox.Plugin.WebSearch if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - WebSearchStorage.Instance.WebSearches.Remove(selectedWebSearch); + _settings.WebSearches.Remove(selectedWebSearch); webSearchView.Items.Refresh(); } } @@ -80,7 +82,7 @@ namespace Wox.Plugin.WebSearch WebSearch selectedWebSearch = webSearchView.SelectedItem as WebSearch; if (selectedWebSearch != null) { - WebSearchSetting webSearch = new WebSearchSetting(this); + WebSearchSetting webSearch = new WebSearchSetting(this, _settings); webSearch.UpdateItem(selectedWebSearch); webSearch.ShowDialog(); } @@ -94,23 +96,23 @@ namespace Wox.Plugin.WebSearch private void CbEnableWebSearchSuggestion_OnChecked(object sender, RoutedEventArgs e) { comboBoxSuggestionSource.Visibility = Visibility.Visible; - WebSearchStorage.Instance.EnableWebSearchSuggestion = true; - WebSearchStorage.Instance.Save(); + _settings.EnableWebSearchSuggestion = true; + _settings.Save(); } private void CbEnableWebSearchSuggestion_OnUnchecked(object sender, RoutedEventArgs e) { comboBoxSuggestionSource.Visibility = Visibility.Collapsed; - WebSearchStorage.Instance.EnableWebSearchSuggestion = false; - WebSearchStorage.Instance.Save(); + _settings.EnableWebSearchSuggestion = false; + _settings.Save(); } private void ComboBoxSuggestionSource_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) { - WebSearchStorage.Instance.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString(); - WebSearchStorage.Instance.Save(); + _settings.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString(); + _settings.Save(); } } } diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 29213be54b..578ed840cd 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -72,12 +72,14 @@ namespace Wox.Core.Plugin AllPlugins = (new CSharpPluginLoader().LoadPlugin(metadatas)). Concat(new JsonRPCPluginLoader().LoadPlugin(metadatas)); - //load plugin i18n languages - ResourceMerger.UpdatePluginLanguages(); + } public static void InitializePlugins(IPublicAPI api) { + //load plugin i18n languages + ResourceMerger.UpdatePluginLanguages(); + API = api; foreach (PluginPair pluginPair in AllPlugins) { diff --git a/Wox.Core/Resource/Internationalization.cs b/Wox.Core/Resource/Internationalization.cs index dbadcf094b..55d640e04c 100644 --- a/Wox.Core/Resource/Internationalization.cs +++ b/Wox.Core/Resource/Internationalization.cs @@ -12,6 +12,8 @@ namespace Wox.Core.Resource { public class Internationalization : Resource { + public UserSettingStorage Settings { get; set; } + public Internationalization() { DirectoryName = "Languages"; @@ -64,8 +66,8 @@ namespace Wox.Core.Resource } } - UserSettingStorage.Instance.Language = language.LanguageCode; - UserSettingStorage.Instance.Save(); + Settings.Language = language.LanguageCode; + Settings.Save(); ResourceMerger.UpdateResource(this); } @@ -137,7 +139,7 @@ namespace Wox.Core.Resource { if (!Directory.Exists(folder)) return string.Empty; - string path = Path.Combine(folder, UserSettingStorage.Instance.Language + ".xaml"); + string path = Path.Combine(folder, Settings.Language + ".xaml"); if (File.Exists(path)) { return path; diff --git a/Wox.Core/Resource/Theme.cs b/Wox.Core/Resource/Theme.cs index 3d9845ea31..3cd07e7284 100644 --- a/Wox.Core/Resource/Theme.cs +++ b/Wox.Core/Resource/Theme.cs @@ -15,6 +15,7 @@ namespace Wox.Core.Resource public class Theme : Resource { private static List themeDirectories = new List(); + public UserSettingStorage Settings { get; set; } public Theme() { @@ -53,8 +54,8 @@ namespace Wox.Core.Resource } } - UserSettingStorage.Instance.Theme = themeName; - UserSettingStorage.Instance.Save(); + Settings.Theme = themeName; + Settings.Save(); ResourceMerger.UpdateResource(this); // Exception of FindResource can't be cathed if global exception handle is set @@ -69,16 +70,16 @@ namespace Wox.Core.Resource { var dict = new ResourceDictionary { - Source = new Uri(GetThemePath(UserSettingStorage.Instance.Theme), UriKind.Absolute) + Source = new Uri(GetThemePath(Settings.Theme), UriKind.Absolute) }; Style queryBoxStyle = dict["QueryBoxStyle"] as Style; if (queryBoxStyle != null) { - queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.QueryBoxFont))); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStyle))); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontWeight))); - queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStretch))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(Settings.QueryBoxFont))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(Settings.QueryBoxFontStyle))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(Settings.QueryBoxFontWeight))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(Settings.QueryBoxFontStretch))); } Style resultItemStyle = dict["ItemTitleStyle"] as Style; @@ -87,10 +88,10 @@ namespace Wox.Core.Resource Style resultSubItemSelectedStyle = dict["ItemSubTitleSelectedStyle"] as Style; if (resultItemStyle != null && resultSubItemStyle != null && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null) { - Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultFont)); - Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultFontStyle)); - Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultFontWeight)); - Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultFontStretch)); + Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(Settings.ResultFont)); + Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(Settings.ResultFontStyle)); + Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(Settings.ResultFontWeight)); + Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(Settings.ResultFontStretch)); Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch }; Array.ForEach(new[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p))); diff --git a/Wox.Core/Resource/ThemeManager.cs b/Wox.Core/Resource/ThemeManager.cs index ef1414757c..193b0bd534 100644 --- a/Wox.Core/Resource/ThemeManager.cs +++ b/Wox.Core/Resource/ThemeManager.cs @@ -5,7 +5,7 @@ private static Theme instance; private static object syncObject = new object(); - public static Theme Theme + public static Theme Instance { get { diff --git a/Wox.Core/Updater/UpdaterManager.cs b/Wox.Core/Updater/UpdaterManager.cs index 9101332d55..3e37b764e2 100644 --- a/Wox.Core/Updater/UpdaterManager.cs +++ b/Wox.Core/Updater/UpdaterManager.cs @@ -22,6 +22,7 @@ namespace Wox.Core.Updater private const string UpdateFeedURL = "http://upgrade.getwox.com/update.xml"; //private const string UpdateFeedURL = "http://127.0.0.1:8888/update.xml"; private static SemanticVersion currentVersion; + private UserSettingStorage _settings; public event EventHandler PrepareUpdateReady; public event EventHandler UpdateError; @@ -43,6 +44,7 @@ namespace Wox.Core.Updater private UpdaterManager() { UpdateManager.Instance.UpdateSource = GetUpdateSource(); + _settings = UserSettingStorage.Instance; } public SemanticVersion CurrentVersion @@ -87,7 +89,7 @@ namespace Wox.Core.Updater try { NewRelease = JsonConvert.DeserializeObject(json); - if (IsNewerThanCurrent(NewRelease) && !UserSettingStorage.Instance.DontPromptUpdateMsg) + if (IsNewerThanCurrent(NewRelease) && !_settings.DontPromptUpdateMsg) { StartUpdate(); } @@ -146,7 +148,7 @@ namespace Wox.Core.Updater // get out of the way so the console window isn't obstructed try { - UpdateManager.Instance.ApplyUpdates(true, UserSettingStorage.Instance.EnableUpdateLog, false); + UpdateManager.Instance.ApplyUpdates(true, _settings.EnableUpdateLog, false); } catch (Exception e) { diff --git a/Wox.Core/UserSettings/HttpProxy.cs b/Wox.Core/UserSettings/HttpProxy.cs index cfe6ca40ed..ead2d86df4 100644 --- a/Wox.Core/UserSettings/HttpProxy.cs +++ b/Wox.Core/UserSettings/HttpProxy.cs @@ -5,39 +5,13 @@ namespace Wox.Core.UserSettings public class HttpProxy : IHttpProxy { private static readonly HttpProxy instance = new HttpProxy(); + public UserSettingStorage Settings { get; set; } + public static HttpProxy Instance => instance; - private HttpProxy() - { - } - - public static HttpProxy Instance - { - get { return instance; } - } - - public bool Enabled - { - get { return UserSettingStorage.Instance.ProxyEnabled; } - } - - public string Server - { - get { return UserSettingStorage.Instance.ProxyServer; } - } - - public int Port - { - get { return UserSettingStorage.Instance.ProxyPort; } - } - - public string UserName - { - get { return UserSettingStorage.Instance.ProxyUserName; } - } - - public string Password - { - get { return UserSettingStorage.Instance.ProxyPassword; } - } + public bool Enabled => Settings.ProxyEnabled; + public string Server => Settings.ProxyServer; + public int Port => Settings.ProxyPort; + public string UserName => Settings.ProxyUserName; + public string Password => Settings.ProxyPassword; } } \ No newline at end of file diff --git a/Wox/ActionKeywords.xaml.cs b/Wox/ActionKeywords.xaml.cs index aa718ffd12..f4119ac131 100644 --- a/Wox/ActionKeywords.xaml.cs +++ b/Wox/ActionKeywords.xaml.cs @@ -10,11 +10,13 @@ namespace Wox public partial class ActionKeywords : Window { private PluginPair _plugin; + private UserSettingStorage _settings; - public ActionKeywords(string pluginId) + public ActionKeywords(string pluginId, UserSettingStorage settings) { InitializeComponent(); _plugin = PluginManager.GetPluginForId(pluginId); + _settings = settings; if (_plugin == null) { MessageBox.Show(InternationalizationManager.Instance.GetTranslation("cannotFindSpecifiedPlugin")); @@ -48,7 +50,7 @@ namespace Wox return; } // update persistant data - UserSettingStorage.Instance.UpdateActionKeyword(_plugin.Metadata); + _settings.UpdateActionKeyword(_plugin.Metadata); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed")); Close(); diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index ca0602b369..7eeb44f27a 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Windows; using Wox.CommandArgs; using Wox.Core.Plugin; +using Wox.Core.Resource; using Wox.Core.UserSettings; using Wox.Helper; using Wox.Infrastructure; @@ -49,17 +50,21 @@ namespace Wox PluginManager.Initialize(); UserSettingStorage settings = UserSettingStorage.Instance; + + // happlebao temp fix for instance code logic + HttpProxy.Instance.Settings = settings; + InternationalizationManager.Instance.Settings = settings; + ThemeManager.Instance.Settings = settings; + MainViewModel mainVM = new MainViewModel(settings); - API = new PublicAPIInstance(mainVM); + API = new PublicAPIInstance(mainVM, settings); PluginManager.InitializePlugins(API); - Window = new MainWindow {DataContext = mainVM}; + Window = new MainWindow (settings, mainVM); NotifyIconManager notifyIconManager = new NotifyIconManager(API); 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(); + }); } diff --git a/Wox/CustomQueryHotkeySetting.xaml.cs b/Wox/CustomQueryHotkeySetting.xaml.cs index b28addc655..56344ea7a7 100644 --- a/Wox/CustomQueryHotkeySetting.xaml.cs +++ b/Wox/CustomQueryHotkeySetting.xaml.cs @@ -12,14 +12,16 @@ namespace Wox { public partial class CustomQueryHotkeySetting : Window { - private SettingWindow settingWidow; + private SettingWindow _settingWidow; private bool update; private CustomPluginHotkey updateCustomHotkey; + private UserSettingStorage _settings; - public CustomQueryHotkeySetting(SettingWindow settingWidow) + public CustomQueryHotkeySetting(SettingWindow settingWidow, UserSettingStorage settings) { - this.settingWidow = settingWidow; + _settingWidow = settingWidow; InitializeComponent(); + _settings = settings; } private void BtnCancel_OnClick(object sender, RoutedEventArgs e) @@ -37,9 +39,9 @@ namespace Wox return; } - if (UserSettingStorage.Instance.CustomPluginHotkeys == null) + if (_settings.CustomPluginHotkeys == null) { - UserSettingStorage.Instance.CustomPluginHotkeys = new List(); + _settings.CustomPluginHotkeys = new List(); } var pluginHotkey = new CustomPluginHotkey @@ -47,7 +49,7 @@ namespace Wox Hotkey = ctlHotkey.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text }; - UserSettingStorage.Instance.CustomPluginHotkeys.Add(pluginHotkey); + _settings.CustomPluginHotkeys.Add(pluginHotkey); SetHotkey(ctlHotkey.CurrentHotkey, delegate { @@ -76,14 +78,14 @@ namespace Wox MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed")); } - UserSettingStorage.Instance.Save(); - settingWidow.ReloadCustomPluginHotkeyView(); + _settings.Save(); + _settingWidow.ReloadCustomPluginHotkeyView(); Close(); } public void UpdateItem(CustomPluginHotkey item) { - updateCustomHotkey = UserSettingStorage.Instance.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey); + updateCustomHotkey = _settings.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey); if (updateCustomHotkey == null) { MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidPluginHotkey")); diff --git a/Wox/Helper/SingletonWindowOpener.cs b/Wox/Helper/SingletonWindowOpener.cs index f7786ea04b..e9d9a594ea 100644 --- a/Wox/Helper/SingletonWindowOpener.cs +++ b/Wox/Helper/SingletonWindowOpener.cs @@ -4,17 +4,17 @@ using System.Windows; namespace Wox.Helper { - public static class SingletonWindowOpener - { - public static T Open(params object[] args) where T : Window - { - var window = Application.Current.Windows.OfType().FirstOrDefault(x => x.GetType() == typeof(T)) - ?? (T)Activator.CreateInstance(typeof(T), args); + public static class SingletonWindowOpener + { + public static T Open(params object[] args) where T : Window + { + var window = Application.Current.Windows.OfType().FirstOrDefault(x => x.GetType() == typeof(T)) + ?? (T)Activator.CreateInstance(typeof(T), args); App.API.HideApp(); - window.Show(); - window.Focus(); - - return (T)window; - } - } + window.Show(); + window.Focus(); + + return (T)window; + } + } } \ No newline at end of file diff --git a/Wox/ImageLoader/ImageLoader.cs b/Wox/ImageLoader/ImageLoader.cs index 6ab671d00e..c5453d47b0 100644 --- a/Wox/ImageLoader/ImageLoader.cs +++ b/Wox/ImageLoader/ImageLoader.cs @@ -36,6 +36,13 @@ namespace Wox.ImageLoader ".appref-ms" }; + private static ImageCacheStroage _imageCache; + + static ImageLoader() + { + _imageCache = ImageCacheStroage.Instance; + } + private static ImageSource GetIcon(string fileName) { try @@ -56,7 +63,7 @@ namespace Wox.ImageLoader public static void PreloadImages() { //ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy - var imageList = new Dictionary(ImageCacheStroage.Instance.TopUsedImages); + var imageList = new Dictionary(_imageCache.TopUsedImages); Stopwatch.Debug($"Preload {imageList.Count} images", () => { foreach (var image in imageList) @@ -87,7 +94,7 @@ namespace Wox.ImageLoader if (addToCache) { - ImageCacheStroage.Instance.Add(path); + _imageCache.Add(path); } diff --git a/Wox/MainWindow.xaml b/Wox/MainWindow.xaml index dd83be5594..9bbb8f9968 100644 --- a/Wox/MainWindow.xaml +++ b/Wox/MainWindow.xaml @@ -37,9 +37,9 @@ PreviewDragOver="OnPreviewDragOver" AllowDrop="True" x:Name="QueryTextBox" /> - + Visibility="{Binding ProgressBarVisibility}" HorizontalAlignment="Right" Width="752"> diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 97da90276b..6cb0ea5f69 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -25,27 +25,33 @@ namespace Wox #region Private Fields private readonly Storyboard _progressBarStoryboard = new Storyboard(); + private UserSettingStorage _settings; #endregion + public MainWindow(UserSettingStorage settings, MainViewModel mainVM) + { + DataContext = mainVM; + InitializeComponent(); + _settings = settings; + } public MainWindow() { InitializeComponent(); } - private void OnClosing(object sender, CancelEventArgs e) { - UserSettingStorage.Instance.WindowLeft = Left; - UserSettingStorage.Instance.WindowTop = Top; - UserSettingStorage.Instance.Save(); + _settings.WindowLeft = Left; + _settings.WindowTop = Top; + _settings.Save(); } private void OnLoaded(object sender, RoutedEventArgs _) { CheckUpdate(); - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); - InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language); + ThemeManager.Instance.ChangeTheme(_settings.Theme); + InternationalizationManager.Instance.ChangeLanguage(_settings.Language); InitProgressbarAnimation(); WindowIntelopHelper.DisableControlBox(this); @@ -65,13 +71,13 @@ namespace Wox QueryTextBox.Focus(); Left = GetWindowsLeft(); Top = GetWindowsTop(); - UserSettingStorage.Instance.IncreaseActivateTimes(); + _settings.IncreaseActivateTimes(); } else { - UserSettingStorage.Instance.WindowLeft = Left; - UserSettingStorage.Instance.WindowTop = Top; - UserSettingStorage.Instance.Save(); + _settings.WindowLeft = Left; + _settings.WindowTop = Top; + _settings.Save(); } }; @@ -83,9 +89,9 @@ namespace Wox private double GetWindowsLeft() { - if (UserSettingStorage.Instance.RememberLastLaunchLocation) + if (_settings.RememberLastLaunchLocation) { - return UserSettingStorage.Instance.WindowLeft; + return _settings.WindowLeft; } var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); @@ -97,9 +103,9 @@ namespace Wox private double GetWindowsTop() { - if (UserSettingStorage.Instance.RememberLastLaunchLocation) + if (_settings.RememberLastLaunchLocation) { - return UserSettingStorage.Instance.WindowTop; + return _settings.WindowTop; } var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); @@ -132,15 +138,15 @@ namespace Wox private void InitProgressbarAnimation() { - var da = new DoubleAnimation(progressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); - var da1 = new DoubleAnimation(progressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); + var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); + var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)")); Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)")); _progressBarStoryboard.Children.Add(da); _progressBarStoryboard.Children.Add(da1); _progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever; - progressBar.Visibility = Visibility.Hidden; - progressBar.BeginStoryboard(_progressBarStoryboard); + ProgressBar.Visibility = Visibility.Hidden; + ProgressBar.BeginStoryboard(_progressBarStoryboard); } private void OnMouseDown(object sender, MouseButtonEventArgs e) @@ -150,7 +156,7 @@ namespace Wox private void OnDeactivated(object sender, EventArgs e) { - if (UserSettingStorage.Instance.HideWhenDeactive) + if (_settings.HideWhenDeactive) { App.API.HideApp(); } diff --git a/Wox/PublicAPIInstance.cs b/Wox/PublicAPIInstance.cs index c07bd42608..28abf3c345 100644 --- a/Wox/PublicAPIInstance.cs +++ b/Wox/PublicAPIInstance.cs @@ -22,11 +22,15 @@ namespace Wox private UserSettingStorage _settings; #region Constructor - public PublicAPIInstance(MainViewModel mainVM) + public PublicAPIInstance(MainViewModel mainVM, UserSettingStorage settings) { MainVM = mainVM; + _settings = settings; GlobalHotkey.Instance.hookedKeyboardCallback += KListener_hookedKeyboardCallback; WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); + SetHotkey(_settings.Hotkey, OnHotkey); + SetCustomPluginHotkey(); + } #endregion @@ -94,7 +98,7 @@ namespace Wox { Application.Current.Dispatcher.Invoke(() => { - SettingWindow sw = SingletonWindowOpener.Open(this); + SettingWindow sw = SingletonWindowOpener.Open(this, _settings); sw.SwitchTo(tabName); }); } @@ -206,7 +210,7 @@ namespace Wox private bool ShouldIgnoreHotkeys() { //double if to omit calling win32 function - if (UserSettingStorage.Instance.IgnoreHotkeysOnFullscreen) + if (_settings.IgnoreHotkeysOnFullscreen) if (WindowIntelopHelper.IsWindowFullscreen()) return true; @@ -215,8 +219,8 @@ namespace Wox internal void SetCustomPluginHotkey() { - if (UserSettingStorage.Instance.CustomPluginHotkeys == null) return; - foreach (CustomPluginHotkey hotkey in UserSettingStorage.Instance.CustomPluginHotkeys) + if (_settings.CustomPluginHotkeys == null) return; + foreach (CustomPluginHotkey hotkey in _settings.CustomPluginHotkeys) { CustomPluginHotkey hotkey1 = hotkey; SetHotkey(hotkey.Hotkey, delegate diff --git a/Wox/ResultListBox.xaml.cs b/Wox/ResultListBox.xaml.cs index 93f540fc76..c1a81adfc5 100644 --- a/Wox/ResultListBox.xaml.cs +++ b/Wox/ResultListBox.xaml.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Linq; using System.Runtime.Remoting.Contexts; +using System.Windows; using System.Windows.Controls; using Wox.Plugin; using Wox.ViewModel; @@ -9,10 +11,12 @@ namespace Wox [Synchronization] public partial class ResultListBox { - public void AddResults(List newResults, string resultId) + public void AddResults(List newRawResults) { var vm = DataContext as ResultsViewModel; - vm.AddResults(newResults, resultId); + var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList(); + vm.Results.Update(newResults); + vm.SelectedResult = vm.Results[0]; } diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index f222e59035..4271f1f4a0 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -32,12 +32,14 @@ namespace Wox bool settingsLoaded; private Dictionary featureControls = new Dictionary(); private bool themeTabLoaded; + private UserSettingStorage _settings; - public SettingWindow(IPublicAPI api) + public SettingWindow(IPublicAPI api, UserSettingStorage settings) { - _api = api; InitializeComponent(); - ResultListBoxPreview.DataContext = new ResultsViewModel(); + _settings = settings; + _api = api; + ResultListBoxPreview.DataContext = new ResultsViewModel(_settings, null); Loaded += Setting_Loaded; } @@ -46,70 +48,70 @@ namespace Wox #region General cbHideWhenDeactive.Checked += (o, e) => { - UserSettingStorage.Instance.HideWhenDeactive = true; - UserSettingStorage.Instance.Save(); + _settings.HideWhenDeactive = true; + _settings.Save(); }; cbHideWhenDeactive.Unchecked += (o, e) => { - UserSettingStorage.Instance.HideWhenDeactive = false; - UserSettingStorage.Instance.Save(); + _settings.HideWhenDeactive = false; + _settings.Save(); }; cbRememberLastLocation.Checked += (o, e) => { - UserSettingStorage.Instance.RememberLastLaunchLocation = true; - UserSettingStorage.Instance.Save(); + _settings.RememberLastLaunchLocation = true; + _settings.Save(); }; cbRememberLastLocation.Unchecked += (o, e) => { - UserSettingStorage.Instance.RememberLastLaunchLocation = false; - UserSettingStorage.Instance.Save(); + _settings.RememberLastLaunchLocation = false; + _settings.Save(); }; cbDontPromptUpdateMsg.Checked += (o, e) => { - UserSettingStorage.Instance.DontPromptUpdateMsg = true; - UserSettingStorage.Instance.Save(); + _settings.DontPromptUpdateMsg = true; + _settings.Save(); }; cbDontPromptUpdateMsg.Unchecked += (o, e) => { - UserSettingStorage.Instance.DontPromptUpdateMsg = false; - UserSettingStorage.Instance.Save(); + _settings.DontPromptUpdateMsg = false; + _settings.Save(); }; cbIgnoreHotkeysOnFullscreen.Checked += (o, e) => { - UserSettingStorage.Instance.IgnoreHotkeysOnFullscreen = true; - UserSettingStorage.Instance.Save(); + _settings.IgnoreHotkeysOnFullscreen = true; + _settings.Save(); }; cbIgnoreHotkeysOnFullscreen.Unchecked += (o, e) => { - UserSettingStorage.Instance.IgnoreHotkeysOnFullscreen = false; - UserSettingStorage.Instance.Save(); + _settings.IgnoreHotkeysOnFullscreen = false; + _settings.Save(); }; cbStartWithWindows.IsChecked = CheckApplicationIsStartupWithWindow(); comboMaxResultsToShow.SelectionChanged += (o, e) => { - UserSettingStorage.Instance.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem; - UserSettingStorage.Instance.Save(); + _settings.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem; + _settings.Save(); //MainWindow.pnlResult.lbResults.GetBindingExpression(MaxHeightProperty).UpdateTarget(); }; - cbHideWhenDeactive.IsChecked = UserSettingStorage.Instance.HideWhenDeactive; - cbDontPromptUpdateMsg.IsChecked = UserSettingStorage.Instance.DontPromptUpdateMsg; - cbRememberLastLocation.IsChecked = UserSettingStorage.Instance.RememberLastLaunchLocation; - cbIgnoreHotkeysOnFullscreen.IsChecked = UserSettingStorage.Instance.IgnoreHotkeysOnFullscreen; + cbHideWhenDeactive.IsChecked = _settings.HideWhenDeactive; + cbDontPromptUpdateMsg.IsChecked = _settings.DontPromptUpdateMsg; + cbRememberLastLocation.IsChecked = _settings.RememberLastLaunchLocation; + cbIgnoreHotkeysOnFullscreen.IsChecked = _settings.IgnoreHotkeysOnFullscreen; LoadLanguages(); comboMaxResultsToShow.ItemsSource = Enumerable.Range(2, 16); - var maxResults = UserSettingStorage.Instance.MaxResultsToShow; + var maxResults = _settings.MaxResultsToShow; comboMaxResultsToShow.SelectedItem = maxResults == 0 ? 6 : maxResults; #endregion @@ -118,15 +120,15 @@ namespace Wox cbEnableProxy.Checked += (o, e) => EnableProxy(); cbEnableProxy.Unchecked += (o, e) => DisableProxy(); - cbEnableProxy.IsChecked = UserSettingStorage.Instance.ProxyEnabled; - tbProxyServer.Text = UserSettingStorage.Instance.ProxyServer; - if (UserSettingStorage.Instance.ProxyPort != 0) + cbEnableProxy.IsChecked = _settings.ProxyEnabled; + tbProxyServer.Text = _settings.ProxyServer; + if (_settings.ProxyPort != 0) { - tbProxyPort.Text = UserSettingStorage.Instance.ProxyPort.ToString(); + tbProxyPort.Text = _settings.ProxyPort.ToString(); } - tbProxyUserName.Text = UserSettingStorage.Instance.ProxyUserName; - tbProxyPassword.Password = UserSettingStorage.Instance.ProxyPassword; - if (UserSettingStorage.Instance.ProxyEnabled) + tbProxyUserName.Text = _settings.ProxyUserName; + tbProxyPassword.Password = _settings.ProxyPassword; + if (_settings.ProxyEnabled) { EnableProxy(); } @@ -141,7 +143,7 @@ namespace Wox tbVersion.Text = UpdaterManager.Instance.CurrentVersion.ToString(); string activateTimes = string.Format(InternationalizationManager.Instance.GetTranslation("about_activate_times"), - UserSettingStorage.Instance.ActivateTimes); + _settings.ActivateTimes); tbActivatedTimes.Text = activateTimes; #endregion @@ -200,7 +202,7 @@ namespace Wox cbLanguages.ItemsSource = InternationalizationManager.Instance.LoadAvailableLanguages(); cbLanguages.DisplayMemberPath = "Display"; cbLanguages.SelectedValuePath = "LanguageCode"; - cbLanguages.SelectedValue = UserSettingStorage.Instance.Language; + cbLanguages.SelectedValue = _settings.Language; cbLanguages.SelectionChanged += cbLanguages_SelectionChanged; } @@ -212,15 +214,15 @@ namespace Wox private void CbStartWithWindows_OnChecked(object sender, RoutedEventArgs e) { AddApplicationToStartup(); - UserSettingStorage.Instance.StartWoxOnSystemStartup = true; - UserSettingStorage.Instance.Save(); + _settings.StartWoxOnSystemStartup = true; + _settings.Save(); } private void CbStartWithWindows_OnUnchecked(object sender, RoutedEventArgs e) { RemoveApplicationFromStartup(); - UserSettingStorage.Instance.StartWoxOnSystemStartup = false; - UserSettingStorage.Instance.Save(); + _settings.StartWoxOnSystemStartup = false; + _settings.Save(); } private void AddApplicationToStartup() @@ -266,9 +268,9 @@ namespace Wox _api.HideApp(); } }); - RemoveHotkey(UserSettingStorage.Instance.Hotkey); - UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString(); - UserSettingStorage.Instance.Save(); + RemoveHotkey(_settings.Hotkey); + _settings.Hotkey = ctlHotkey.CurrentHotkey.ToString(); + _settings.Save(); } } @@ -297,8 +299,8 @@ namespace Wox private void OnHotkeyTabSelected() { ctlHotkey.HotkeyChanged += ctlHotkey_OnHotkeyChanged; - ctlHotkey.SetHotkey(UserSettingStorage.Instance.Hotkey, false); - lvCustomHotkey.ItemsSource = UserSettingStorage.Instance.CustomPluginHotkeys; + ctlHotkey.SetHotkey(_settings.Hotkey, false); + lvCustomHotkey.ItemsSource = _settings.CustomPluginHotkeys; } private void BtnDeleteCustomHotkey_OnClick(object sender, RoutedEventArgs e) @@ -313,9 +315,9 @@ namespace Wox string deleteWarning = string.Format(InternationalizationManager.Instance.GetTranslation("deleteCustomHotkeyWarning"), item.Hotkey); if (MessageBox.Show(deleteWarning, InternationalizationManager.Instance.GetTranslation("delete"), MessageBoxButton.YesNo) == MessageBoxResult.Yes) { - UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item); + _settings.CustomPluginHotkeys.Remove(item); lvCustomHotkey.Items.Refresh(); - UserSettingStorage.Instance.Save(); + _settings.Save(); RemoveHotkey(item.Hotkey); } } @@ -325,7 +327,7 @@ namespace Wox CustomPluginHotkey item = lvCustomHotkey.SelectedItem as CustomPluginHotkey; if (item != null) { - CustomQueryHotkeySetting window = new CustomQueryHotkeySetting(this); + CustomQueryHotkeySetting window = new CustomQueryHotkeySetting(this, _settings); window.UpdateItem(item); window.ShowDialog(); } @@ -337,7 +339,7 @@ namespace Wox private void BtnAddCustomeHotkey_OnClick(object sender, RoutedEventArgs e) { - new CustomQueryHotkeySetting(this).ShowDialog(); + new CustomQueryHotkeySetting(this, _settings).ShowDialog(); } public void ReloadCustomPluginHotkeyView() @@ -364,26 +366,26 @@ namespace Wox if (themeTabLoaded) return; themeTabLoaded = true; - if (!string.IsNullOrEmpty(UserSettingStorage.Instance.QueryBoxFont) && - Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.QueryBoxFont)) > 0) + if (!string.IsNullOrEmpty(_settings.QueryBoxFont) && + Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(_settings.QueryBoxFont)) > 0) { - cbQueryBoxFont.Text = UserSettingStorage.Instance.QueryBoxFont; + cbQueryBoxFont.Text = _settings.QueryBoxFont; cbQueryBoxFontFaces.SelectedItem = SyntaxSugars.CallOrRescueDefault(() => ((FontFamily)cbQueryBoxFont.SelectedItem).ConvertFromInvariantStringsOrNormal( - UserSettingStorage.Instance.QueryBoxFontStyle, - UserSettingStorage.Instance.QueryBoxFontWeight, - UserSettingStorage.Instance.QueryBoxFontStretch + _settings.QueryBoxFontStyle, + _settings.QueryBoxFontWeight, + _settings.QueryBoxFontStretch )); } - if (!string.IsNullOrEmpty(UserSettingStorage.Instance.ResultFont) && - Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultFont)) > 0) + if (!string.IsNullOrEmpty(_settings.ResultFont) && + Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(_settings.ResultFont)) > 0) { - ResultFontComboBox.Text = UserSettingStorage.Instance.ResultFont; + ResultFontComboBox.Text = _settings.ResultFont; ResultFontFacesComboBox.SelectedItem = SyntaxSugars.CallOrRescueDefault(() => ((FontFamily)ResultFontComboBox.SelectedItem).ConvertFromInvariantStringsOrNormal( - UserSettingStorage.Instance.ResultFontStyle, - UserSettingStorage.Instance.ResultFontWeight, - UserSettingStorage.Instance.ResultFontStretch + _settings.ResultFontStyle, + _settings.ResultFontWeight, + _settings.ResultFontStretch )); } @@ -437,15 +439,15 @@ namespace Wox IcoPath = "Images/app.png", PluginDirectory = Path.GetDirectoryName(Application.ExecutablePath) } - }, "test id"); + }); - foreach (string theme in ThemeManager.Theme.LoadAvailableThemes()) + foreach (string theme in ThemeManager.Instance.LoadAvailableThemes()) { string themeName = Path.GetFileNameWithoutExtension(theme); themeComboBox.Items.Add(themeName); } - themeComboBox.SelectedItem = UserSettingStorage.Instance.Theme; + themeComboBox.SelectedItem = _settings.Theme; var wallpaper = WallpaperPathRetrieval.GetWallpaperPath(); if (wallpaper != null && File.Exists(wallpaper)) @@ -470,17 +472,17 @@ namespace Wox private void ThemeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { string themeName = themeComboBox.SelectedItem.ToString(); - ThemeManager.Theme.ChangeTheme(themeName); + ThemeManager.Instance.ChangeTheme(themeName); } private void CbQueryBoxFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (!settingsLoaded) return; string queryBoxFontName = cbQueryBoxFont.SelectedItem.ToString(); - UserSettingStorage.Instance.QueryBoxFont = queryBoxFontName; + _settings.QueryBoxFont = queryBoxFontName; cbQueryBoxFontFaces.SelectedItem = ((FontFamily)cbQueryBoxFont.SelectedItem).ChooseRegularFamilyTypeface(); - UserSettingStorage.Instance.Save(); - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); + _settings.Save(); + ThemeManager.Instance.ChangeTheme(_settings.Theme); } private void CbQueryBoxFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) @@ -494,11 +496,11 @@ namespace Wox } else { - UserSettingStorage.Instance.QueryBoxFontStretch = typeface.Stretch.ToString(); - UserSettingStorage.Instance.QueryBoxFontWeight = typeface.Weight.ToString(); - UserSettingStorage.Instance.QueryBoxFontStyle = typeface.Style.ToString(); - UserSettingStorage.Instance.Save(); - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); + _settings.QueryBoxFontStretch = typeface.Stretch.ToString(); + _settings.QueryBoxFontWeight = typeface.Weight.ToString(); + _settings.QueryBoxFontStyle = typeface.Style.ToString(); + _settings.Save(); + ThemeManager.Instance.ChangeTheme(_settings.Theme); } } @@ -506,10 +508,10 @@ namespace Wox { if (!settingsLoaded) return; string resultItemFont = ResultFontComboBox.SelectedItem.ToString(); - UserSettingStorage.Instance.ResultFont = resultItemFont; + _settings.ResultFont = resultItemFont; ResultFontFacesComboBox.SelectedItem = ((FontFamily)ResultFontComboBox.SelectedItem).ChooseRegularFamilyTypeface(); - UserSettingStorage.Instance.Save(); - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); + _settings.Save(); + ThemeManager.Instance.ChangeTheme(_settings.Theme); } private void OnResultFontFacesSelectionChanged(object sender, SelectionChangedEventArgs e) @@ -523,11 +525,11 @@ namespace Wox } else { - UserSettingStorage.Instance.ResultFontStretch = typeface.Stretch.ToString(); - UserSettingStorage.Instance.ResultFontWeight = typeface.Weight.ToString(); - UserSettingStorage.Instance.ResultFontStyle = typeface.Style.ToString(); - UserSettingStorage.Instance.Save(); - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); + _settings.ResultFontStretch = typeface.Stretch.ToString(); + _settings.ResultFontWeight = typeface.Weight.ToString(); + _settings.ResultFontStyle = typeface.Style.ToString(); + _settings.Save(); + ThemeManager.Instance.ChangeTheme(_settings.Theme); } } @@ -567,7 +569,7 @@ namespace Wox pluginId = pair.Metadata.ID; pluginIcon.Source = ImageLoader.ImageLoader.Load(pair.Metadata.FullIcoPath); - var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[pluginId]; + var customizedPluginConfig = _settings.CustomizedPluginConfigs[pluginId]; cbDisablePlugin.IsChecked = customizedPluginConfig != null && customizedPluginConfig.Disabled; PluginContentPanel.Content = null; @@ -585,7 +587,7 @@ namespace Wox // update in-memory data PluginManager.UpdateActionKeywordForPlugin(pair, e.OldActionKeyword, e.NewActionKeyword); // update persistant data - UserSettingStorage.Instance.UpdateActionKeyword(pair.Metadata); + _settings.UpdateActionKeyword(pair.Metadata); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed")); }; @@ -615,11 +617,11 @@ namespace Wox id = pair.Metadata.ID; name = pair.Metadata.Name; } - var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs[id]; + var customizedPluginConfig = _settings.CustomizedPluginConfigs[id]; if (customizedPluginConfig == null) { // todo when this part will be invoked - UserSettingStorage.Instance.CustomizedPluginConfigs[id] = new CustomizedPluginConfig + _settings.CustomizedPluginConfigs[id] = new CustomizedPluginConfig { Disabled = cbDisabled.IsChecked ?? true, ID = id, @@ -631,7 +633,7 @@ namespace Wox { customizedPluginConfig.Disabled = cbDisabled.IsChecked ?? true; } - UserSettingStorage.Instance.Save(); + _settings.Save(); } private void PluginActionKeywords_OnMouseUp(object sender, MouseButtonEventArgs e) @@ -643,7 +645,7 @@ namespace Wox { //third-party plugin string id = pair.Metadata.ID; - ActionKeywords changeKeywordsWindow = new ActionKeywords(id); + ActionKeywords changeKeywordsWindow = new ActionKeywords(id, _settings); changeKeywordsWindow.ShowDialog(); PluginPair plugin = PluginManager.GetPluginForId(id); if (plugin != null) pluginActionKeywords.Text = string.Join(Query.ActionKeywordSeperater, pair.Metadata.ActionKeywords.ToArray()); @@ -716,10 +718,10 @@ namespace Wox #region Proxy private void btnSaveProxy_Click(object sender, RoutedEventArgs e) { - UserSettingStorage.Instance.ProxyEnabled = cbEnableProxy.IsChecked ?? false; + _settings.ProxyEnabled = cbEnableProxy.IsChecked ?? false; int port = 80; - if (UserSettingStorage.Instance.ProxyEnabled) + if (_settings.ProxyEnabled) { if (string.IsNullOrEmpty(tbProxyServer.Text)) { @@ -738,11 +740,11 @@ namespace Wox } } - UserSettingStorage.Instance.ProxyServer = tbProxyServer.Text; - UserSettingStorage.Instance.ProxyPort = port; - UserSettingStorage.Instance.ProxyUserName = tbProxyUserName.Text; - UserSettingStorage.Instance.ProxyPassword = tbProxyPassword.Password; - UserSettingStorage.Instance.Save(); + _settings.ProxyServer = tbProxyServer.Text; + _settings.ProxyPort = port; + _settings.ProxyUserName = tbProxyUserName.Text; + _settings.ProxyPassword = tbProxyPassword.Password; + _settings.Save(); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("saveProxySuccessfully")); } diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index 0aee6f3b05..bd44f5cd48 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -51,13 +51,19 @@ namespace Wox.ViewModel _queryText = ""; _lastQuery = new Query(); _settings = settings; + _queryHistory = QueryHistoryStorage.Instance; + _userSelectedRecord = UserSelectedRecordStorage.Instance; + _topMostRecord = TopMostRecordStorage.Instance; InitializeResultListBox(); InitializeContextMenu(); InitializeKeyCommands(); - _queryHistory = QueryHistoryStorage.Instance; - _userSelectedRecord = UserSelectedRecordStorage.Instance; - _topMostRecord = TopMostRecordStorage.Instance; + + } + + public MainViewModel() + { + } #endregion @@ -331,14 +337,14 @@ namespace Wox.ViewModel private void InitializeResultListBox() { - Results = new ResultsViewModel(); + Results = new ResultsViewModel(_settings, _topMostRecord); ResultListBoxVisibility = Visibility.Collapsed; } private void InitializeContextMenu() { - ContextMenu = new ResultsViewModel(); + ContextMenu = new ResultsViewModel(_settings, _topMostRecord); ContextMenuVisibility = Visibility.Collapsed; } diff --git a/Wox/ViewModel/ResultsViewModel.cs b/Wox/ViewModel/ResultsViewModel.cs index 57ea5b1406..c87a3110cb 100644 --- a/Wox/ViewModel/ResultsViewModel.cs +++ b/Wox/ViewModel/ResultsViewModel.cs @@ -20,12 +20,20 @@ namespace Wox.ViewModel private Thickness _margin; private readonly object _resultsUpdateLock = new object(); + private UserSettingStorage _settings; + private TopMostRecordStorage _topMostRecord; + + public ResultsViewModel(UserSettingStorage settings, TopMostRecordStorage topMostRecord) + { + _settings = settings; + _topMostRecord = topMostRecord; + } #endregion #region ViewModel Properties - public int MaxHeight => UserSettingStorage.Instance.MaxResultsToShow * 50; + public int MaxHeight => _settings.MaxResultsToShow * 50; public ResultViewModel SelectedResult { @@ -75,7 +83,7 @@ namespace Wox.ViewModel private bool IsTopMostResult(Result result) { - return TopMostRecordStorage.Instance.IsTopMost(result); + return _topMostRecord.IsTopMost(result); } private int InsertIndexOf(int newScore, IList list)