mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Use variable instead of global static method
1. introduce variable 2. part of #389 3. refactoring program suffix in program plugin 4. 全局变量一时爽,代码重构火葬场
This commit is contained in:
@@ -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<Result> Query(Query query)
|
||||
{
|
||||
@@ -80,7 +81,7 @@ namespace Wox.Plugin.CMD
|
||||
|
||||
private List<Result> GetHistoryCmds(string cmd, Result result)
|
||||
{
|
||||
IEnumerable<Result> history = CMDStorage.Instance.CMDHistory.Where(o => o.Key.Contains(cmd))
|
||||
IEnumerable<Result> 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<Result> ResultsFromlHistory()
|
||||
{
|
||||
IEnumerable<Result> history = CMDStorage.Instance.CMDHistory.OrderByDescending(o => o.Value)
|
||||
IEnumerable<Result> 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()
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace Wox.Plugin.Everything
|
||||
private readonly EverythingAPI _api = new EverythingAPI();
|
||||
private static readonly List<string> ImageExts = new List<string> { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
|
||||
private static readonly List<string> ExecutableExts = new List<string> { ".exe" };
|
||||
private ContextMenuStorage _settings = ContextMenuStorage.Instance;
|
||||
|
||||
public List<Result> 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<ContextMenu> availableContextMenus = new List<ContextMenu>();
|
||||
availableContextMenus.AddRange(GetDefaultContextMenu());
|
||||
availableContextMenus.AddRange(ContextMenuStorage.Instance.ContextMenus);
|
||||
availableContextMenus.AddRange(_settings.ContextMenus);
|
||||
|
||||
if (record.Type == ResultType.File)
|
||||
{
|
||||
|
||||
@@ -12,20 +12,21 @@ namespace Wox.Plugin.Folder
|
||||
{
|
||||
private static List<string> 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<FolderLink>();
|
||||
FolderStorage.Instance.Save();
|
||||
_settings.FolderLinks = new List<FolderLink>();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +34,7 @@ namespace Wox.Plugin.Folder
|
||||
{
|
||||
string input = query.Search.ToLower();
|
||||
|
||||
List<FolderLink> userFolderLinks = FolderStorage.Instance.FolderLinks.Where(
|
||||
List<FolderLink> userFolderLinks = _settings.FolderLinks.Where(
|
||||
x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase)).ToList();
|
||||
List<Result> results =
|
||||
userFolderLinks.Select(
|
||||
|
||||
@@ -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<FolderLink>();
|
||||
_settings.FolderLinks = new List<FolderLink>();
|
||||
}
|
||||
|
||||
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<FolderLink>();
|
||||
_settings.FolderLinks = new List<FolderLink>();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Wox.Plugin.Program
|
||||
private static bool isIndexing;
|
||||
private static List<string> watchedPath = new List<string>();
|
||||
|
||||
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
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, string> Meta { get; set; }
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<Program> LoadPrograms()
|
||||
{
|
||||
List<Program> list = new List<Program>();
|
||||
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<Program> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<ProgramStorage>
|
||||
{
|
||||
[JsonProperty]
|
||||
public List<ProgramSource> 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"};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<Result> 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<ProgramSource> programSources = new List<ProgramSource>();
|
||||
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
|
||||
|
||||
@@ -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<Result> Query(Query query)
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
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<Result> 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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -10,22 +10,24 @@ namespace Wox.Plugin.WebSearch
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user