From ac0634c2d355e8b76a9b82aab1981f44199be53a Mon Sep 17 00:00:00 2001 From: Patrick Simpson Date: Fri, 30 Jun 2017 15:26:45 +0200 Subject: [PATCH] [KOE-125] Fixed initial focus handling for shared folders dialog. Also no longer automatically opening when user tabs away from dialog. --- .../Controls/KAbstractComboBox.cs | 3 ++- .../AcaciaZPushPlugin/Controls/KBusyHider.cs | 21 +++++++++++++++++++ .../AcaciaZPushPlugin/Controls/KComboBox.cs | 17 ++++++++++++--- .../AcaciaZPushPlugin/Controls/KUIUtil.cs | 20 ++++++++++++++++-- .../SharedFoldersDialog.Designer.cs | 3 +++ .../SharedFolders/SharedFoldersDialog.cs | 13 ++++++++++-- .../SharedFolders/SharedFoldersDialog.resx | 4 ++-- .../AcaciaZPushPlugin/Native/User32.cs | 7 +++++++ .../AcaciaZPushPlugin/UI/GABLookupControl.cs | 3 ++- 9 files changed, 80 insertions(+), 11 deletions(-) diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KAbstractComboBox.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KAbstractComboBox.cs index 7d7fd52..25581e6 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KAbstractComboBox.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KAbstractComboBox.cs @@ -108,8 +108,9 @@ namespace Acacia.Controls _edit.PreviewKeyDown += _edit_PreviewKeyDown; } - public void FocusEdit() + protected override void OnGotFocus(EventArgs e) { + // Make the edit active _edit.Select(); } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KBusyHider.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KBusyHider.cs index f23c866..9c43af1 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KBusyHider.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KBusyHider.cs @@ -33,6 +33,7 @@ namespace Acacia.Controls private KBusyIndicator _busyOverlay = null; private KBusyIndicator _completeOverlay = null; private string _busyText; + private readonly List _doneActions = new List(); public bool Busy { @@ -59,6 +60,21 @@ namespace Acacia.Controls } } + /// + /// Executes the action when no longer busy. If not busy now, the action is executed straight away. + /// + public void OnDoneBusy(Action action) + { + if (!Busy) + { + action(); + } + else + { + _doneActions.Add(action); + } + } + private void RemoveOverlay(KBusyIndicator overlay) { Controls.Remove(overlay); @@ -66,6 +82,11 @@ namespace Acacia.Controls // And enable the controls foreach (Control control in Controls) control.Enabled = true; + + // Excute any actions + foreach (Action action in _doneActions) + action(); + _doneActions.Clear(); } private KBusyIndicator CreateOverlay(string text, bool showProgress) diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KComboBox.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KComboBox.cs index 3d613d5..546e7cf 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KComboBox.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KComboBox.cs @@ -16,6 +16,13 @@ namespace Acacia.Controls { #region Drop-down list + protected enum CommitSource + { + MouseClick, + KeyTab, + KeyEnter + } + /// /// Custom list for the drop-down. Performs a few functions: /// - Prevents grabbing the focus away from the edit when clicked @@ -73,7 +80,7 @@ namespace Acacia.Controls { // Select the item under the mouse and commit SelectedIndex = IndexFromPoint(PointToClient(Cursor.Position)); - CommitSelection(); + CommitSelection(CommitSource.MouseClick); } protected override void DefWndProc(ref Message m) @@ -96,8 +103,10 @@ namespace Acacia.Controls return new Size(w, ItemHeight * Math.Min(Items.Count, _owner.MaxDropDownItems)); } - public void CommitSelection() + public CommitSource _commitSource; + public void CommitSelection(CommitSource source) { + _commitSource = source; _committedIndex = SelectedIndex; base.OnSelectedIndexChanged(new EventArgs()); } @@ -167,6 +176,8 @@ namespace Acacia.Controls OnSelectedItemChanged(); } + protected CommitSource GetCommitSource() { return _list._commitSource; } + public DisplayItem SelectedItem { get { return _selectedItem; } @@ -371,7 +382,7 @@ namespace Acacia.Controls if (DroppedDown) { if (_list.SelectedIndex >= 0) - _list.CommitSelection(); + _list.CommitSelection(e.KeyCode == Keys.Enter ? CommitSource.KeyEnter : CommitSource.KeyTab); DroppedDown = false; } e.IsInputKey = e.KeyCode == Keys.Enter; diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KUIUtil.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KUIUtil.cs index 8b980d2..54b7339 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KUIUtil.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Controls/KUIUtil.cs @@ -1,4 +1,6 @@ -/// Copyright 2016 Kopano b.v. + +using Acacia.Native; +/// Copyright 2016 Kopano b.v. /// /// This program is free software: you can redistribute it and/or modify /// it under the terms of the GNU Affero General Public License, version 3, @@ -13,7 +15,6 @@ /// along with this program.If not, see. /// /// Consult LICENSE file for details - using System; using System.Collections.Generic; using System.Drawing; @@ -137,5 +138,20 @@ namespace Acacia.Controls } #endregion + + #region Focus + + public static Control GetFocusedControl() + { + Control focusedControl = null; + // To get hold of the focused control: + IntPtr focusedHandle = User32.GetFocus(); + if (focusedHandle != IntPtr.Zero) + // Note that if the focused Control is not a .Net control, then this will return null. + focusedControl = Control.FromHandle(focusedHandle); + return focusedControl; + } + + #endregion } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.Designer.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.Designer.cs index 8834769..879face 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.Designer.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.Designer.cs @@ -70,6 +70,7 @@ this._mainBusyHider.Controls.Add(this._layoutMain); resources.ApplyResources(this._mainBusyHider, "_mainBusyHider"); this._mainBusyHider.Name = "_mainBusyHider"; + this._mainBusyHider.TabStop = true; // // _layoutMain // @@ -78,6 +79,7 @@ this._layoutMain.Controls.Add(this.kTreeFolders, 0, 1); this._layoutMain.Controls.Add(this._layoutOptions, 0, 2); this._layoutMain.Name = "_layoutMain"; + this._layoutMain.TabStop = true; // // _layoutSelectUser // @@ -86,6 +88,7 @@ this._layoutSelectUser.Controls.Add(this.gabLookup, 1, 0); this._layoutSelectUser.Controls.Add(this.buttonOpenUser, 2, 0); this._layoutSelectUser.Name = "_layoutSelectUser"; + this._layoutSelectUser.TabStop = true; // // labelSelectUser // diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.cs index 9f7e722..bfdb053 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.cs @@ -148,14 +148,23 @@ namespace Acacia.Features.SharedFolders }; FocusNode(node); } - kTreeFolders.Focus(); + SetInitialFocus(kTreeFolders); } else { - gabLookup.FocusEdit(); + SetInitialFocus(gabLookup); } } + private void SetInitialFocus(Control control) + { + // If busy, setting the focus doesn't work, as the control is enabled. Wait until done. + BusyHider.OnDoneBusy(() => + { + control.Focus(); + }); + } + private void dialogButtons_Apply(object sender, EventArgs e) { BusyText = Properties.Resources.SharedFolders_Applying_Label; diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.resx b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.resx index 0e2826d..a3eb232 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.resx +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Features/SharedFolders/SharedFoldersDialog.resx @@ -190,7 +190,7 @@ 260, 23 - 0 + 1 gabLookup @@ -286,7 +286,7 @@ 442, 277 - 0 + 1 kTreeFolders diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Native/User32.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Native/User32.cs index 9a00021..f9c42b0 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Native/User32.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/Native/User32.cs @@ -249,5 +249,12 @@ namespace Acacia.Native public static extern int GetSystemMetrics(SystemMetric nIndex); #endregion + + #region Focus + + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)] + public static extern IntPtr GetFocus(); + + #endregion } } diff --git a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/UI/GABLookupControl.cs b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/UI/GABLookupControl.cs index 7c32a6b..7ea7567 100644 --- a/src/AcaciaZPushPlugin/AcaciaZPushPlugin/UI/GABLookupControl.cs +++ b/src/AcaciaZPushPlugin/AcaciaZPushPlugin/UI/GABLookupControl.cs @@ -163,7 +163,8 @@ namespace Acacia.UI protected override void OnSelectedItemChanged() { _selectedUser = (GABUser)SelectedItem?.Item; - SelectedUserChanged?.Invoke(this, new SelectedUserEventArgs(_selectedUser, true)); + // If the tab key was used to select, the user wants to click open + SelectedUserChanged?.Invoke(this, new SelectedUserEventArgs(_selectedUser, GetCommitSource() != CommitSource.KeyTab)); } #endregion