diff --git a/Wox.Plugin.SystemPlugins/Folder/FolderPlugin.cs b/Wox.Plugin.SystemPlugins/Folder/FolderPlugin.cs index edb8dd7a0c..00f94f06a6 100644 --- a/Wox.Plugin.SystemPlugins/Folder/FolderPlugin.cs +++ b/Wox.Plugin.SystemPlugins/Folder/FolderPlugin.cs @@ -4,165 +4,208 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Windows.Forms; -using Wox.Infrastructure; using Wox.Infrastructure.Storage.UserSettings; +using Control = System.Windows.Controls.Control; -namespace Wox.Plugin.SystemPlugins.Folder { +namespace Wox.Plugin.SystemPlugins.Folder +{ + public class FolderPlugin : BaseSystemPlugin, ISettingProvider + { + #region Properties - public class FolderPlugin : BaseSystemPlugin, ISettingProvider { + private static List driverNames; + private PluginInitContext context; - #region Properties - - private PluginInitContext context; - private static List driverNames = null; - private static Dictionary parentDirectories = new Dictionary(); - public override string Description { - get - { - return "Provide opening folder from wox directorily. You can add your favorite folders."; - } + public override string Description + { + get { return "Provide opening folder from wox directorily. You can add your favorite folders."; } } - public override string ID - { + public override string ID + { get { return "B4D3B69656E14D44865C8D818EAE47C4"; } - } + } - public override string Name { get { return "Folder"; } } - public override string IcoPath { get { return @"Images\folder.png"; } } + public override string Name + { + get { return "Folder"; } + } - #endregion Properties + public override string IcoPath + { + get { return @"Images\folder.png"; } + } - #region Misc + #endregion Properties - protected override void InitInternal(PluginInitContext context) { - this.context = context; + public Control CreateSettingPanel() + { + return new FileSystemSettings(); + } - if (UserSettingStorage.Instance.FolderLinks == null) { - UserSettingStorage.Instance.FolderLinks = new List(); - UserSettingStorage.Instance.Save(); - } - } + protected override void InitInternal(PluginInitContext context) + { + this.context = context; + this.context.API.BackKeyDownEvent += ApiBackKeyDownEvent; + InitialDriverList(); + if (UserSettingStorage.Instance.FolderLinks == null) + { + UserSettingStorage.Instance.FolderLinks = new List(); + UserSettingStorage.Instance.Save(); + } + } - public System.Windows.Controls.Control CreateSettingPanel() { - return new FileSystemSettings(); - } + private void ApiBackKeyDownEvent(object sender, WoxKeyDownEventArgs e) + { + string query = e.Query; + if (Directory.Exists(query)) + { + if (query.EndsWith("\\")) + { + query = query.Remove(query.Length - 1); + } - #endregion Misc + if (query.Contains("\\")) + { + int index = query.LastIndexOf("\\"); + query = query.Remove(index) + "\\"; + } - protected override List QueryInternal(Query query) { - var results = new List(); - var input = query.RawQuery.ToLower(); - var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower(); - var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x => x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase)); - var currentPath = link == null ? input : link.Path + input.Remove(0, inputName.Length); - InitialDriverList(); + context.API.ChangeQuery(query); + } + } - foreach (var item in UserSettingStorage.Instance.FolderLinks.Where(x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase))) { - results.Add(new Result(item.Nickname, "Images/folder.png") { - Action = (c) => { - context.ChangeQuery(item.Nickname); - return false; - } - }); - } + protected override List QueryInternal(Query query) + { + string input = query.RawQuery.ToLower(); - if (link == null && !driverNames.Any(input.StartsWith)) - return results; + List userFolderLinks = UserSettingStorage.Instance.FolderLinks.Where( + x => x.Nickname.StartsWith(input, StringComparison.OrdinalIgnoreCase)).ToList(); + List results = + userFolderLinks.Select( + item => new Result(item.Nickname, "Images/folder.png", "Ctrl + Enter to open the directory") + { + Action = c => + { + if (c.SpecialKeyState.CtrlPressed) + { + try + { + Process.Start(item.Path); + return true; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Could not start " + item.Path); + return false; + } + } + context.API.ChangeQuery(item.Path); + return false; + } + }).ToList(); - QueryInternal_Directory_Exists(currentPath, input, results); + if (!driverNames.Any(input.StartsWith)) + return results; - return results; - } + if (!input.EndsWith("\\")) + { + //"c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive" + input = input + "\\"; + } + results.AddRange(QueryInternal_Directory_Exists(input)); - private void InitialDriverList() { - if (driverNames == null) { - driverNames = new List(); - var allDrives = DriveInfo.GetDrives(); - foreach (var driver in allDrives) { - driverNames.Add(driver.Name.ToLower().TrimEnd('\\')); - } - } - } + return results; + } - private void QueryInternal_Directory_Exists(string currentPath, string input, List results) { - string path = Directory.Exists(currentPath) ? new DirectoryInfo(currentPath).FullName : Path.GetDirectoryName(input); - if (!System.IO.Directory.Exists(path)) return; + private void InitialDriverList() + { + if (driverNames == null) + { + driverNames = new List(); + DriveInfo[] allDrives = DriveInfo.GetDrives(); + foreach (DriveInfo driver in allDrives) + { + driverNames.Add(driver.Name.ToLower().TrimEnd('\\')); + } + } + } - results.Add(new Result("Open this directory", "Images/folder.png") { - Score = 100000, - Action = (c) => { - if (Directory.Exists(currentPath)) { - Process.Start(currentPath); - } - else if (currentPath.Contains("\\")) { - var index = currentPath.LastIndexOf("\\"); - Process.Start(currentPath.Remove(index) + "\\"); - } + private List QueryInternal_Directory_Exists(string rawQuery) + { + var results = new List(); + if (!Directory.Exists(rawQuery)) return results; - return true; - } - }); + results.Add(new Result("Open current directory", "Images/folder.png") + { + Score = 10000, + Action = c => + { + Process.Start(rawQuery); + return true; + } + }); + //Add children directories + DirectoryInfo[] dirs = new DirectoryInfo(rawQuery).GetDirectories(); + foreach (DirectoryInfo dir in dirs) + { + if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; - //if (System.IO.Directory.Exists(input)) { - var dirs = new DirectoryInfo(path).GetDirectories(); + DirectoryInfo dirCopy = dir; + var result = new Result(dir.Name, "Images/folder.png", "Ctrl + Enter to open the directory") + { + Action = c => + { + if (c.SpecialKeyState.CtrlPressed) + { + try + { + Process.Start(dirCopy.FullName); + return true; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Could not start " + dirCopy.FullName); + return false; + } + } + context.API.ChangeQuery(dirCopy.FullName + "\\"); + return false; + } + }; - var parentDirKey = input.TrimEnd('\\', '/'); - if (!parentDirectories.ContainsKey(parentDirKey)) parentDirectories.Add(parentDirKey, dirs); + results.Add(result); + } - var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower()); - foreach (var dir in dirs) { //.Where(x => (x.Attributes & FileAttributes.Hidden) != 0)) { - if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; + //Add children files + FileInfo[] files = new DirectoryInfo(rawQuery).GetFiles(); + foreach (FileInfo file in files) + { + if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; - var result = new Result(dir.Name, "Images/folder.png") { - Action = (c) => { - //context.ChangeQuery(dir.FullName); - context.ChangeQuery(input + dir.Name + "\\"); - return false; - } - }; + string filePath = file.FullName; + var result = new Result(Path.GetFileName(filePath), "Images/file.png") + { + Action = c => + { + try + { + Process.Start(filePath); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Could not start " + filePath); + } - if (Path.GetFileName(currentPath).ToLower() != "") { - var matchResult = fuzzy.Evaluate(dir.Name); - result.Score = matchResult.Score; - if (!matchResult.Success) continue; - } + return true; + } + }; - results.Add(result); - } - //} + results.Add(result); + } - var Folder = Path.GetDirectoryName(currentPath); - if (Folder != null) { - - //var fuzzy = FuzzyMatcher.Create(Path.GetFileName(currentPath).ToLower()); - foreach (var dir in new DirectoryInfo(Folder).GetFiles()) { //.Where(x => (x.Attributes & FileAttributes.Hidden) != 0)) { - if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; - - var dirPath = dir.FullName; - Result result = new Result(Path.GetFileNameWithoutExtension(dirPath), dirPath) { - Action = (c) => { - try { - Process.Start(dirPath); - } - catch (Exception ex) { - MessageBox.Show(ex.Message, "Could not start " + dir.Name); - } - - return true; - } - }; - - if (Path.GetFileName(currentPath) != "") { - var matchResult = fuzzy.Evaluate(dir.Name); - result.Score = matchResult.Score; - if (!matchResult.Success) continue; - } - - results.Add(result); - } - } - } - } + return results; + } + } } \ No newline at end of file diff --git a/Wox.Plugin.SystemPlugins/UrlPlugin.cs b/Wox.Plugin.SystemPlugins/UrlPlugin.cs index 313db7112c..455c49f182 100644 --- a/Wox.Plugin.SystemPlugins/UrlPlugin.cs +++ b/Wox.Plugin.SystemPlugins/UrlPlugin.cs @@ -1,29 +1,47 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; +using System.Security.Policy; +using System.Text.RegularExpressions; +using System.Windows; namespace Wox.Plugin.SystemPlugins { public class UrlPlugin : BaseSystemPlugin { + const string pattern = @"^(http|https|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$"; + Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); + protected override List QueryInternal(Query query) { var raw = query.RawQuery; - Uri uri; - if (Uri.TryCreate(raw, UriKind.Absolute, out uri)) + if (reg.IsMatch(raw)) { return new List { new Result { Title = raw, - SubTitle = "Open the typed URL...", - IcoPath = "Images/url1.png", + SubTitle = "Open " + raw, + IcoPath = "Images/url.png", Score = 8, Action = _ => { - Process.Start(uri.AbsoluteUri); - return true; + if (!raw.ToLower().StartsWith("http")) + { + raw = "http://" + raw; + } + try + { + Process.Start(raw); + return true; + } + catch(Exception ex) + { + MessageBox.Show(ex.Message, "Could not open " + raw); + return false; + } } } }; @@ -38,7 +56,7 @@ namespace Wox.Plugin.SystemPlugins public override string Name { get { return "URL handler"; } } public override string Description { get { return "Provide Opening the typed URL from Wox."; } } - public override string IcoPath { get { return "Images/url2.png"; } } + public override string IcoPath { get { return "Images/url.png"; } } protected override void InitInternal(PluginInitContext context) { diff --git a/Wox.Plugin/EventHandler.cs b/Wox.Plugin/EventHandler.cs new file mode 100644 index 0000000000..0fd8461deb --- /dev/null +++ b/Wox.Plugin/EventHandler.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Input; + +namespace Wox.Plugin +{ + public delegate void WoxKeyDownEventHandler(object sender, WoxKeyDownEventArgs e); + + public class WoxKeyDownEventArgs + { + public string Query { get; set; } + public KeyEventArgs keyEventArgs { get; set; } + } +} diff --git a/Wox.Plugin/IPublicAPI.cs b/Wox.Plugin/IPublicAPI.cs index 3ec1558f37..85e501b651 100644 --- a/Wox.Plugin/IPublicAPI.cs +++ b/Wox.Plugin/IPublicAPI.cs @@ -32,5 +32,7 @@ namespace Wox.Plugin void ReloadPlugins(); List GetAllPlugins(); + + event WoxKeyDownEventHandler BackKeyDownEvent; } } diff --git a/Wox.Plugin/Wox.Plugin.csproj b/Wox.Plugin/Wox.Plugin.csproj index dc69528ecb..59bd3041bb 100644 --- a/Wox.Plugin/Wox.Plugin.csproj +++ b/Wox.Plugin/Wox.Plugin.csproj @@ -45,6 +45,7 @@ + diff --git a/Wox/Images/file.png b/Wox/Images/file.png new file mode 100644 index 0000000000..2913d69623 Binary files /dev/null and b/Wox/Images/file.png differ diff --git a/Wox/Images/url1.png b/Wox/Images/url.png similarity index 100% rename from Wox/Images/url1.png rename to Wox/Images/url.png diff --git a/Wox/Images/url2.png b/Wox/Images/url2.png deleted file mode 100644 index 44883fd1c2..0000000000 Binary files a/Wox/Images/url2.png and /dev/null differ diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index d7838c267f..077dd6bc11 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -133,6 +133,8 @@ namespace Wox return Plugins.AllPlugins; } + public event WoxKeyDownEventHandler BackKeyDownEvent; + public void PushResults(Query query, PluginMetadata plugin, List results) { results.ForEach(o => @@ -456,31 +458,6 @@ namespace Wox private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e) { //when alt is pressed, the real key should be e.SystemKey - - Action Shift_GoBack = () => - { - if (e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift)) - { - if (tbQuery.Text.EndsWith("\\")) - { - tbQuery.Text = tbQuery.Text.Remove(tbQuery.Text.Length - 1); - } - - if (tbQuery.Text.Contains("\\")) - { - var index = tbQuery.Text.LastIndexOf("\\"); - tbQuery.Text = tbQuery.Text.Remove(index) + "\\"; - } - else - { - tbQuery.Text = ""; - return; - } - } - - tbQuery.CaretIndex = int.MaxValue; - }; - Key key = (e.Key == Key.System ? e.SystemKey : e.Key); switch (key) { @@ -518,17 +495,23 @@ namespace Wox break; case Key.Enter: - Shift_GoBack(); AcceptSelect(resultCtrl.AcceptSelect()); e.Handled = true; break; + case Key.Back: + if (BackKeyDownEvent != null) + { + BackKeyDownEvent(tbQuery,new WoxKeyDownEventArgs() + { + Query = tbQuery.Text, + keyEventArgs = e + }); + } + break; + case Key.Tab: - Shift_GoBack(); AcceptSelect(resultCtrl.AcceptSelect()); - if (!tbQuery.Text.EndsWith("\\")) tbQuery.Text += "\\"; - AcceptSelect(resultCtrl.AcceptSelect()); - tbQuery.CaretIndex = int.MaxValue; e.Handled = true; break; } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 8f2dbdeb5f..ca4ac05fa9 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -377,8 +377,7 @@ - - +