From c7dfaac61a8f6bc179d36a0b7cb5edd8d79e583f Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sun, 26 Jan 2014 21:18:01 +0800 Subject: [PATCH] Mouse select support & Code refactoring --- WinAlfred/App.xaml.cs | 11 +++++++- WinAlfred/Commands/BaseCommand.cs | 17 ++---------- WinAlfred/Commands/Command.cs | 33 ---------------------- WinAlfred/Commands/CommandFactory.cs | 31 +++++++++++++++++++++ WinAlfred/Commands/PluginCommand.cs | 8 +----- WinAlfred/Commands/SystemCommand.cs | 5 ++-- WinAlfred/Helper/SelectedRecords.cs | 16 ++++++++++- WinAlfred/MainWindow.xaml.cs | 41 +++++++++++++++------------- WinAlfred/PluginLoader/Plugins.cs | 14 +++++----- WinAlfred/ResultItem.xaml | 2 +- WinAlfred/ResultItem.xaml.cs | 15 +++++++++- WinAlfred/WinAlfred.csproj | 2 +- 12 files changed, 106 insertions(+), 89 deletions(-) delete mode 100644 WinAlfred/Commands/Command.cs create mode 100644 WinAlfred/Commands/CommandFactory.cs diff --git a/WinAlfred/App.xaml.cs b/WinAlfred/App.xaml.cs index 0f4a78997a..b40bf39652 100644 --- a/WinAlfred/App.xaml.cs +++ b/WinAlfred/App.xaml.cs @@ -51,7 +51,16 @@ namespace WinAlfred public partial class App : Application { - private MainWindow window; + + private static MainWindow window; + + public static MainWindow Window + { + get + { + return window; + } + } protected override void OnStartup(StartupEventArgs e) { diff --git a/WinAlfred/Commands/BaseCommand.cs b/WinAlfred/Commands/BaseCommand.cs index ca0a5ebe6d..1b25c7cc82 100644 --- a/WinAlfred/Commands/BaseCommand.cs +++ b/WinAlfred/Commands/BaseCommand.cs @@ -8,24 +8,11 @@ namespace WinAlfred.Commands { public abstract class BaseCommand { - private MainWindow window; - - public void Dispatch(Query query) - { - Dispatch(query, true); - } - - public abstract void Dispatch(Query query, bool updateView); - - //TODO:Ugly, we should subscribe events here, instead of just use usercontrol as the parameter - protected BaseCommand(MainWindow window) - { - this.window = window; - } + public abstract void Dispatch(Query query, bool updateView = true); protected void UpdateResultView(List results) { - window.OnUpdateResultView(results); + App.Window.OnUpdateResultView(results); } } } diff --git a/WinAlfred/Commands/Command.cs b/WinAlfred/Commands/Command.cs deleted file mode 100644 index 2bab900136..0000000000 --- a/WinAlfred/Commands/Command.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using WinAlfred.Helper; -using WinAlfred.Plugin; - -namespace WinAlfred.Commands -{ - public class Command - { - private PluginCommand pluginCmd; - private SystemCommand systemCmd; - - public Command(MainWindow window) - { - pluginCmd = new PluginCommand(window); - systemCmd = new SystemCommand(window); - } - - public void DispatchCommand(Query query) - { - systemCmd.Dispatch(query); - pluginCmd.Dispatch(query); - } - - public void DispatchCommand(Query query,bool updateView) - { - systemCmd.Dispatch(query,updateView); - pluginCmd.Dispatch(query,updateView); - } - } -} diff --git a/WinAlfred/Commands/CommandFactory.cs b/WinAlfred/Commands/CommandFactory.cs new file mode 100644 index 0000000000..9ad2b98808 --- /dev/null +++ b/WinAlfred/Commands/CommandFactory.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using WinAlfred.Helper; +using WinAlfred.Plugin; + +namespace WinAlfred.Commands +{ + internal static class CommandFactory + { + private static PluginCommand pluginCmd; + private static SystemCommand systemCmd; + + public static void DispatchCommand(Query query, bool updateView = true) + { + //lazy init command instance. + if (pluginCmd == null) + { + pluginCmd = new PluginCommand(); + } + if (systemCmd == null) + { + systemCmd = new SystemCommand(); + } + + systemCmd.Dispatch(query,updateView); + pluginCmd.Dispatch(query,updateView); + } + } +} diff --git a/WinAlfred/Commands/PluginCommand.cs b/WinAlfred/Commands/PluginCommand.cs index 590e7b252c..482e4166cb 100644 --- a/WinAlfred/Commands/PluginCommand.cs +++ b/WinAlfred/Commands/PluginCommand.cs @@ -14,13 +14,7 @@ namespace WinAlfred.Commands private string currentPythonModulePath = string.Empty; private IntPtr GIL; - public PluginCommand(MainWindow mainWindow) - : base(mainWindow) - { - - } - - public override void Dispatch(Query q,bool updateView) + public override void Dispatch(Query q,bool updateView = true) { PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == q.ActionName); if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword)) diff --git a/WinAlfred/Commands/SystemCommand.cs b/WinAlfred/Commands/SystemCommand.cs index cec28c54d2..b62989c74b 100644 --- a/WinAlfred/Commands/SystemCommand.cs +++ b/WinAlfred/Commands/SystemCommand.cs @@ -12,13 +12,12 @@ namespace WinAlfred.Commands { private List systemPlugins; - public SystemCommand(MainWindow window) - : base(window) + public SystemCommand() { systemPlugins = Plugins.AllPlugins.Where(o => o.Metadata.PluginType == PluginType.System).ToList(); } - public override void Dispatch(Query query,bool updateView) + public override void Dispatch(Query query,bool updateView = true) { foreach (PluginPair pair in systemPlugins) { diff --git a/WinAlfred/Helper/SelectedRecords.cs b/WinAlfred/Helper/SelectedRecords.cs index 21b49bd262..48375cf25d 100644 --- a/WinAlfred/Helper/SelectedRecords.cs +++ b/WinAlfred/Helper/SelectedRecords.cs @@ -13,8 +13,22 @@ namespace WinAlfred.Helper private int hasAddedCount = 0; private Dictionary dict = new Dictionary(); private string filePath = Directory.GetCurrentDirectory() + "\\selectedRecords.dat"; + private static readonly SelectedRecords instance = new SelectedRecords(); - public void LoadSelectedRecords() + private SelectedRecords() + { + LoadSelectedRecords(); + } + + public static SelectedRecords Instance + { + get + { + return instance; + } + } + + private void LoadSelectedRecords() { if (File.Exists(filePath)) { diff --git a/WinAlfred/MainWindow.xaml.cs b/WinAlfred/MainWindow.xaml.cs index d4815006cc..9add6e24b5 100644 --- a/WinAlfred/MainWindow.xaml.cs +++ b/WinAlfred/MainWindow.xaml.cs @@ -16,6 +16,7 @@ using WinAlfred.PluginLoader; using Application = System.Windows.Application; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using MessageBox = System.Windows.MessageBox; +using UserControl = System.Windows.Controls.UserControl; namespace WinAlfred { @@ -23,10 +24,8 @@ namespace WinAlfred { private KeyboardHook hook = new KeyboardHook(); private NotifyIcon notifyIcon; - private Command cmdDispatcher; Storyboard progressBarStoryboard = new Storyboard(); private bool queryHasReturn = false; - SelectedRecords selectedRecords = new SelectedRecords(); private KeyboardListener keyboardListener = new KeyboardListener(); private bool WinRStroked = false; @@ -68,7 +67,7 @@ namespace WinAlfred double oldLeft = Left; Left = 20000; ShowWinAlfred(); - cmdDispatcher.DispatchCommand(new Query("qq"), false); + CommandFactory.DispatchCommand(new Query("qq"), false); HideWinAlfred(); Left = oldLeft; } @@ -132,7 +131,7 @@ namespace WinAlfred if (resultCtrl.Dirty) resultCtrl.Clear(); }, TimeSpan.FromMilliseconds(30), null); var q = new Query(tbQuery.Text); - cmdDispatcher.DispatchCommand(q); + CommandFactory.DispatchCommand(q); queryHasReturn = false; if (Plugins.HitThirdpartyKeyword(q)) { @@ -169,7 +168,7 @@ namespace WinAlfred Activate(); Focus(); tbQuery.Focus(); - if(selectAll) tbQuery.SelectAll(); + if (selectAll) tbQuery.SelectAll(); } public void ParseArgs(string[] args) @@ -179,7 +178,7 @@ namespace WinAlfred switch (args[0]) { case "reloadWorkflows": - Plugins.Init(this); + Plugins.Init(); break; case "query": @@ -218,12 +217,11 @@ namespace WinAlfred Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2; Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 3; - Plugins.Init(this); - cmdDispatcher = new Command(this); + Plugins.Init(); InitialTray(); - selectedRecords.LoadSelectedRecords(); SetAutoStart(true); WakeupApp(); + //var engine = new Jurassic.ScriptEngine(); //MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString()); keyboardListener.hookedKeyboardCallback += KListener_hookedKeyboardCallback; @@ -279,20 +277,25 @@ namespace WinAlfred break; case Key.Enter: - Result result = resultCtrl.AcceptSelect(); - if (result != null) - { - selectedRecords.AddSelect(result); - if (!result.DontHideWinAlfredAfterSelect) - { - HideWinAlfred(); - } - } + AcceptSelect(); e.Handled = true; break; } } + private void AcceptSelect() + { + Result result = resultCtrl.AcceptSelect(); + if (result != null) + { + SelectedRecords.Instance.AddSelect(result); + if (!result.DontHideWinAlfredAfterSelect) + { + HideWinAlfred(); + } + } + } + public void OnUpdateResultView(List list) { queryHasReturn = true; @@ -302,7 +305,7 @@ namespace WinAlfred //todo:this should be opened to users, it's their choise to use it or not in thier workflows list.ForEach(o => { - if (o.AutoAjustScore) o.Score += selectedRecords.GetSelectedCount(o); + if (o.AutoAjustScore) o.Score += SelectedRecords.Instance.GetSelectedCount(o); }); resultCtrl.Dispatcher.Invoke(new Action(() => { diff --git a/WinAlfred/PluginLoader/Plugins.cs b/WinAlfred/PluginLoader/Plugins.cs index 62d9a6dd4f..cfeb565727 100644 --- a/WinAlfred/PluginLoader/Plugins.cs +++ b/WinAlfred/PluginLoader/Plugins.cs @@ -12,7 +12,7 @@ namespace WinAlfred.PluginLoader { private static List plugins = new List(); - public static void Init(MainWindow window) + public static void Init() { plugins.Clear(); BasePluginLoader.ParsePluginsConfig(); @@ -30,12 +30,12 @@ namespace WinAlfred.PluginLoader { Plugins = plugins, PluginMetadata = metadata, - ChangeQuery = s => window.ChangeQuery(s), - CloseApp = window.CloseApp, - HideApp = window.HideApp, - ShowApp = () => window.ShowApp(), - ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath), - OpenSettingDialog = ()=> window.OpenSettingDialog() + ChangeQuery = s => App.Window.ChangeQuery(s), + CloseApp = App.Window.CloseApp, + HideApp = App.Window.HideApp, + ShowApp = () => App.Window.ShowApp(), + ShowMsg = (title, subTitle, iconPath) => App.Window.ShowMsg(title, subTitle, iconPath), + OpenSettingDialog = () => App.Window.OpenSettingDialog() })); } } diff --git a/WinAlfred/ResultItem.xaml b/WinAlfred/ResultItem.xaml index 7d35b7a250..7e685c9bc8 100644 --- a/WinAlfred/ResultItem.xaml +++ b/WinAlfred/ResultItem.xaml @@ -8,7 +8,7 @@ x:Name="resultItemControl" Style="{DynamicResource ItemStyle}" Height="50"> - + diff --git a/WinAlfred/ResultItem.xaml.cs b/WinAlfred/ResultItem.xaml.cs index c2b22f18e6..3de43b4b75 100644 --- a/WinAlfred/ResultItem.xaml.cs +++ b/WinAlfred/ResultItem.xaml.cs @@ -4,9 +4,11 @@ using System.Drawing; using System.IO; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using WinAlfred.Annotations; +using WinAlfred.Helper; using WinAlfred.Plugin; using Brush = System.Windows.Media.Brush; @@ -14,6 +16,7 @@ namespace WinAlfred { public partial class ResultItem : UserControl, INotifyPropertyChanged { + private bool selected; public Result Result { get; private set; } @@ -47,7 +50,6 @@ namespace WinAlfred public ResultItem(Result result) { - InitializeComponent(); Result = result; @@ -74,6 +76,17 @@ namespace WinAlfred imgIco.Source = new BitmapImage(new Uri(path)); } } + + AddHandler(MouseLeftButtonUpEvent, new RoutedEventHandler((o, e) => + { + Result.Action(); + SelectedRecords.Instance.AddSelect(result); + if (!result.DontHideWinAlfredAfterSelect) + { + App.Window.HideApp(); + } + e.Handled = true; + })); } private static ImageSource GetIcon(string fileName) diff --git a/WinAlfred/WinAlfred.csproj b/WinAlfred/WinAlfred.csproj index a468c8b708..132ef47025 100644 --- a/WinAlfred/WinAlfred.csproj +++ b/WinAlfred/WinAlfred.csproj @@ -111,7 +111,7 @@ Designer - +