From bacf7cb688cd3fb3e2fab7202edf6b4f35da880a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Bj=C3=B6rkstr=C3=B6m?= Date: Wed, 14 Aug 2019 23:00:58 +0300 Subject: [PATCH] Fixes changing focus using backtab (#243) - Focus is now moved correctly when using tab and backtab --- Terminal.Gui/Core.cs | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index 7e86ae5af..d8b87dc33 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -219,8 +219,24 @@ namespace Terminal.Gui { /// /// public class View : Responder, IEnumerable { + internal enum Direction { + Forward, + Backward + } + View container = null; View focused = null; + Direction focusDirection; + + internal Direction FocusDirection { + get => SuperView?.FocusDirection ?? focusDirection; + set { + if (SuperView != null) + SuperView.FocusDirection = value; + else + focusDirection = value; + } + } /// /// Points to the current driver in use by the view, it is a convenience property @@ -900,7 +916,10 @@ namespace Terminal.Gui { public void EnsureFocus () { if (focused == null) - FocusFirst (); + if (FocusDirection == Direction.Forward) + FocusFirst (); + else + FocusLast (); } /// @@ -926,8 +945,10 @@ namespace Terminal.Gui { /// public void FocusLast () { - if (subviews == null) + if (subviews == null) { + SuperView?.SetFocus(this); return; + } for (int i = subviews.Count; i > 0;) { i--; @@ -946,12 +967,13 @@ namespace Terminal.Gui { /// true, if previous was focused, false otherwise. public bool FocusPrev () { + FocusDirection = Direction.Backward; if (subviews == null || subviews.Count == 0) return false; if (focused == null) { FocusLast (); - return true; + return focused != null; } int focused_idx = -1; for (int i = subviews.Count; i > 0;) { @@ -967,18 +989,13 @@ namespace Terminal.Gui { if (w.CanFocus && focused_idx != -1) { focused.HasFocus = false; - if (w.CanFocus) + if (w != null && w.CanFocus) w.FocusLast (); SetFocus (w); - return true; + return true; } } - if (focused_idx != -1) { - FocusLast (); - return true; - } - if (focused != null) { focused.HasFocus = false; focused = null; @@ -992,6 +1009,7 @@ namespace Terminal.Gui { /// true, if next was focused, false otherwise. public bool FocusNext () { + FocusDirection = Direction.Forward; if (subviews == null || subviews.Count == 0) return false;