diff options
author | Marius Ungureanu <marius.ungureanu@xamarin.com> | 2015-02-14 02:55:50 +0300 |
---|---|---|
committer | Marius Ungureanu <marius.ungureanu@xamarin.com> | 2015-02-14 02:55:50 +0300 |
commit | 1d325dae13985318a8bd2c09a5ce1925f55b522f (patch) | |
tree | ae31b0b1ffc8929c1e68a3de1dde7f0470bdfbae /main | |
parent | 1c1e7faaef35695c7278294f082f004ed23f7486 (diff) |
[Toolbar] Move Popup Window logic into Controller.
Refactor PopupWindow to process Xwt keys.
Diffstat (limited to 'main')
4 files changed, 196 insertions, 107 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/IMainToolbarView.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/IMainToolbarView.cs index 43a35fa74a..6c0f967821 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/IMainToolbarView.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/IMainToolbarView.cs @@ -117,7 +117,42 @@ namespace MonoDevelop.Components.MainToolbar /// <value>The search category.</value> string SearchCategory { set; } + /// <summary> + /// Gets or sets the search text. + /// </summary> + /// <value>The search text.</value> + string SearchText { get; set; } + + /// <summary> + /// Focuses the search entry. + /// </summary> void FocusSearchBar (); + + /// <summary> + /// Occurs when the search entry contents changed. + /// </summary> + event EventHandler SearchEntryChanged; + + /// <summary> + /// Occurs when the search entry is activated. + /// </summary> + event EventHandler SearchEntryActivated; + + /// <summary> + /// Occurs when a key is pressed in the search entry. + /// </summary> + event EventHandler<Xwt.KeyEventArgs> SearchEntryKeyPressed; + + /// <summary> + /// Occurs when the search entry is resized. + /// </summary> + event EventHandler SearchEntryResized; + + /// <summary> + /// Gets the UI widget for the popup window to use as an anchor. + /// </summary> + /// <value>The popup anchor.</value> + Gtk.Widget PopupAnchor { get; } #endregion #region StatusBar diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbar.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbar.cs index 5a358c966e..a843c79af4 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbar.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbar.cs @@ -104,16 +104,6 @@ namespace MonoDevelop.Components.MainToolbar set; } - readonly PropertyWrapper<bool> searchForMembers = new PropertyWrapper<bool> ("MainToolbar.Search.IncludeMembers", true); - bool SearchForMembers { - get { - return searchForMembers; - } - set { - searchForMembers.Value = value; - } - } - public MonoDevelop.Ide.StatusBar StatusBar { get { return statusArea; @@ -193,14 +183,6 @@ namespace MonoDevelop.Components.MainToolbar return sb.ToString (); } - void DestroyPopup () - { - if (popup != null) { - popup.Destroy (); - popup = null; - } - } - public MainToolbar () { executionTargetsChanged = DispatchService.GuiDispatch (new EventHandler (HandleExecutionTargetsChanged)); @@ -301,41 +283,11 @@ namespace MonoDevelop.Components.MainToolbar matchEntry.Entry.ModifyFont (Pango.FontDescription.FromString ("Sans 9")); // TODO: VV: "Segoe UI 9" matchEntry.RoundedShape = true; matchEntry.Entry.Changed += HandleSearchEntryChanged; - matchEntry.Activated += (sender, e) => { - var pattern = SearchPopupSearchPattern.ParsePattern (matchEntry.Entry.Text); - if (pattern.Pattern == null && pattern.LineNumber > 0) { - DestroyPopup (); - var doc = IdeApp.Workbench.ActiveDocument; - if (doc != null && doc.Editor != null) { - doc.Select (); - doc.Editor.Caret.Location = new Mono.TextEditor.DocumentLocation (pattern.LineNumber, pattern.Column > 0 ? pattern.Column : 1); - doc.Editor.CenterToCaret (); - doc.Editor.Parent.StartCaretPulseAnimation (); - } - return; - } - if (popup != null) - popup.OpenFile (); - }; - matchEntry.Entry.KeyPressEvent += (o, args) => { - if (args.Event.Key == Gdk.Key.Escape) { - var doc = IdeApp.Workbench.ActiveDocument; - if (doc != null) { - DestroyPopup (); - doc.Select (); - } - return; - } - if (popup != null) { - args.RetVal = popup.ProcessKey (args.Event.Key, args.Event.State); - } - }; - IdeApp.Workbench.RootWindow.WidgetEvent += delegate(object o, WidgetEventArgs args) { - if (args.Event is Gdk.EventConfigure) - PositionPopup (); - }; - SizeAllocated += delegate { - PositionPopup (); + matchEntry.Activated += HandleSearchEntryActivated; + matchEntry.Entry.KeyPressEvent += HandleSearchEntryKeyPressed; + SizeAllocated += (o, e) => { + if (SearchEntryResized != null) + SearchEntryResized (o, e); }; BuildToolbar (); @@ -503,47 +455,37 @@ namespace MonoDevelop.Components.MainToolbar BuildToolbar (); } - SearchPopupWindow popup = null; - - static readonly SearchPopupSearchPattern emptyColonPattern = SearchPopupSearchPattern.ParsePattern (":"); - void HandleSearchEntryChanged (object sender, EventArgs e) { - if (string.IsNullOrEmpty (matchEntry.Entry.Text)){ - DestroyPopup (); - return; - } - var pattern = SearchPopupSearchPattern.ParsePattern (matchEntry.Entry.Text); - if (pattern.Pattern == null && pattern.LineNumber > 0 || pattern == emptyColonPattern) { - if (popup != null) { - popup.Hide (); - } - return; - } else { - if (popup != null && !popup.Visible) - popup.Show (); - } - - if (popup == null) { - popup = new SearchPopupWindow (); - popup.SearchForMembers = SearchForMembers; - popup.Destroyed += delegate { - popup = null; - matchEntry.Entry.Text = ""; - }; - PositionPopup (); - popup.ShowAll (); - } - - popup.Update (pattern); + if (SearchEntryActivated != null) + SearchEntryChanged (sender, e); + } + void HandleSearchEntryActivated (object sender, EventArgs e) + { + if (SearchEntryActivated != null) + SearchEntryActivated (sender, e); } - void PositionPopup () + void HandleSearchEntryKeyPressed (object sender, KeyPressEventArgs e) { - if (popup == null) - return; - popup.ShowPopup (matchEntry, PopupPosition.TopRight); + if (SearchEntryKeyPressed != null) { + // TODO: Refactor this in Xwt as an extension method. + var k = (Xwt.Key)e.Event.KeyValue; + var m = Xwt.ModifierKeys.None; + if ((e.Event.State & Gdk.ModifierType.ShiftMask) != 0) + m |= Xwt.ModifierKeys.Shift; + if ((e.Event.State & Gdk.ModifierType.ControlMask) != 0) + m |= Xwt.ModifierKeys.Control; + if ((e.Event.State & Gdk.ModifierType.Mod1Mask) != 0) + m |= Xwt.ModifierKeys.Alt; + // TODO: Backport this one. + if ((e.Event.State & Gdk.ModifierType.Mod2Mask) != 0) + m |= Xwt.ModifierKeys.Command; + var kargs = new Xwt.KeyEventArgs (k, m, false, (long)e.Event.Time); + SearchEntryKeyPressed (sender, kargs); + e.RetVal = kargs.Handled; + } } string GetActiveConfiguration () @@ -961,6 +903,20 @@ namespace MonoDevelop.Components.MainToolbar { matchEntry.Entry.GrabFocus (); } + + public event EventHandler SearchEntryChanged; + public event EventHandler SearchEntryActivated; + public event EventHandler<Xwt.KeyEventArgs> SearchEntryKeyPressed; + public event EventHandler SearchEntryResized; + + public Widget PopupAnchor { + get { return matchEntry; } + } + + public string SearchText { + get { return matchEntry.Entry.Text; } + set { matchEntry.Entry.Text = value; } + } #endregion } } diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbarController.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbarController.cs index a2e00ec22d..f0a23e7605 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbarController.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/MainToolbarController.cs @@ -27,6 +27,7 @@ using System; using MonoDevelop.Ide; using MonoDevelop.Components.Commands; using MonoDevelop.Core; +using Gtk; namespace MonoDevelop.Components.MainToolbar { @@ -41,6 +42,12 @@ namespace MonoDevelop.Components.MainToolbar get { return ToolbarView.StatusBar; } } + readonly PropertyWrapper<bool> searchForMembers = new PropertyWrapper<bool> ("MainToolbar.Search.IncludeMembers", true); + bool SearchForMembers { + get { return searchForMembers; } + set { searchForMembers.Value = value; } + } + public MainToolbarController (IMainToolbarView toolbarView) { ToolbarView = toolbarView; @@ -57,10 +64,102 @@ namespace MonoDevelop.Components.MainToolbar item.Activated += (o, e) => SetSearchCategory (item.Category); ToolbarView.SearchMenuItems = items; + // Register Search Entry handlers. + ToolbarView.SearchEntryChanged += HandleSearchEntryChanged; + ToolbarView.SearchEntryActivated += HandleSearchEntryActivated; + ToolbarView.SearchEntryKeyPressed += HandleSearchEntryKeyPressed; + ToolbarView.SearchEntryResized += (o, e) => PositionPopup (); + + IdeApp.Workbench.RootWindow.WidgetEvent += delegate(object o, WidgetEventArgs args) { + if (args.Event is Gdk.EventConfigure) + PositionPopup (); + }; + // Register this controller as a commandbar. IdeApp.CommandService.RegisterCommandBar (this); } + SearchPopupWindow popup = null; + static readonly SearchPopupSearchPattern emptyColonPattern = SearchPopupSearchPattern.ParsePattern (":"); + void PositionPopup () + { + if (popup == null) + return; + popup.ShowPopup (ToolbarView.PopupAnchor, PopupPosition.TopRight); + } + + void DestroyPopup () + { + if (popup != null) { + popup.Destroy (); + popup = null; + } + } + + void HandleSearchEntryChanged (object sender, EventArgs e) + { + if (string.IsNullOrEmpty (ToolbarView.SearchText)){ + DestroyPopup (); + return; + } + var pattern = SearchPopupSearchPattern.ParsePattern (ToolbarView.SearchText); + if (pattern.Pattern == null && pattern.LineNumber > 0 || pattern == emptyColonPattern) { + if (popup != null) { + popup.Hide (); + } + return; + } else { + if (popup != null && !popup.Visible) + popup.Show (); + } + + if (popup == null) { + popup = new SearchPopupWindow (); + popup.SearchForMembers = SearchForMembers; + popup.Destroyed += delegate { + popup = null; + ToolbarView.SearchText = ""; + }; + PositionPopup (); + popup.ShowAll (); + } + + popup.Update (pattern); + } + + void HandleSearchEntryActivated (object sender, EventArgs e) + { + var pattern = SearchPopupSearchPattern.ParsePattern (ToolbarView.SearchText); + if (pattern.Pattern == null && pattern.LineNumber > 0) { + DestroyPopup (); + var doc = IdeApp.Workbench.ActiveDocument; + if (doc != null && doc.Editor != null) { + doc.Select (); + doc.Editor.Caret.Location = new Mono.TextEditor.DocumentLocation (pattern.LineNumber, pattern.Column > 0 ? pattern.Column : 1); + doc.Editor.CenterToCaret (); + doc.Editor.Parent.StartCaretPulseAnimation (); + } + return; + } + if (popup != null) + popup.OpenFile (); + } + + void HandleSearchEntryKeyPressed (object sender, Xwt.KeyEventArgs e) + { + if (e.Key == Xwt.Key.Escape) { + var doc = IdeApp.Workbench.ActiveDocument; + if (doc != null) { + DestroyPopup (); + doc.Select (); + } + return; + } + if (popup != null) { + e.Handled = popup.ProcessKey (e.Key, e.Modifiers); + } + } + public void FocusSearchBar () { ToolbarView.FocusSearchBar (); diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/SearchPopupWindow.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/SearchPopupWindow.cs index f94462fbe6..7bc3520365 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/SearchPopupWindow.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Components.MainToolbar/SearchPopupWindow.cs @@ -696,39 +696,38 @@ namespace MonoDevelop.Components.MainToolbar QueueDraw (); } - internal bool ProcessKey (Gdk.Key key, Gdk.ModifierType state) + internal bool ProcessKey (Xwt.Key key, Xwt.ModifierKeys state) { switch (key) { - case Gdk.Key.Up: - if (state.HasFlag (Gdk.ModifierType.Mod2Mask)) - goto case Gdk.Key.Page_Up; - if (state.HasFlag (Gdk.ModifierType.ControlMask)) - goto case Gdk.Key.Home; + case Xwt.Key.Up: + if (state.HasFlag (Xwt.ModifierKeys.Command)) + goto case Xwt.Key.PageUp; + if (state.HasFlag (Xwt.ModifierKeys.Control)) + goto case Xwt.Key.Home; SelectItemUp (); return true; - case Gdk.Key.Down: - if (state.HasFlag (Gdk.ModifierType.Mod2Mask)) - goto case Gdk.Key.Page_Down; - if (state.HasFlag (Gdk.ModifierType.ControlMask)) - goto case Gdk.Key.End; + case Xwt.Key.Down: + if (state.HasFlag (Xwt.ModifierKeys.Command)) + goto case Xwt.Key.PageDown; + if (state.HasFlag (Xwt.ModifierKeys.Control)) + goto case Xwt.Key.End; SelectItemDown (); return true; - case Gdk.Key.KP_Page_Down: - case Gdk.Key.Page_Down: + case (Xwt.Key)Gdk.Key.KP_Page_Down: + case Xwt.Key.PageDown: SelectNextCategory (); return true; - case Gdk.Key.KP_Page_Up: - case Gdk.Key.Page_Up: + case (Xwt.Key)Gdk.Key.KP_Page_Up: + case Xwt.Key.PageUp: SelectPrevCategory (); return true; - case Gdk.Key.Home: + case Xwt.Key.Home: SelectFirstCategory (); return true; - case Gdk.Key.End: + case Xwt.Key.End: SelectLastCatgory (); return true; - - case Gdk.Key.Return: + case Xwt.Key.Return: OnItemActivated (EventArgs.Empty); return true; } |