diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index fde54913c..efe7b25ba 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1026,10 +1026,14 @@ namespace Terminal.Gui { /// in a visually sensible place. public virtual void PositionCursor () { - if (focused != null) + if (!CanBeVisible (this)) { + return; + } + + if (focused != null) { focused.PositionCursor (); - else { - if (CanFocus && HasFocus) { + } else { + if (CanFocus && HasFocus && Visible) { Move (textFormatter.HotKeyPos == -1 ? 0 : textFormatter.HotKeyPos, 0); } else { Move (frame.X, frame.Y); @@ -1211,6 +1215,10 @@ namespace Terminal.Gui { /// public virtual void Redraw (Rect bounds) { + if (!CanBeVisible (this)) { + return; + } + var clipRect = new Rect (Point.Empty, frame.Size); if (ColorScheme != null) @@ -1238,7 +1246,9 @@ namespace Terminal.Gui { // Draw the subview // Use the view's bounds (view-relative; Location will always be (0,0) because - view.Redraw (view.Bounds); + if (view.Visible) { + view.Redraw (view.Bounds); + } } view.NeedDisplay = Rect.Empty; view.childNeedsDisplay = false; @@ -1282,7 +1292,7 @@ namespace Terminal.Gui { if (view == null) return; //Console.WriteLine ($"Request to focus {view}"); - if (!view.CanFocus) + if (!view.CanFocus || !view.Visible) return; if (focused?.hasFocus == true && focused == view) return; @@ -1312,6 +1322,10 @@ namespace Terminal.Gui { /// public void SetFocus () { + if (!CanBeVisible (this)) { + return; + } + SuperView?.SetFocus (this); } @@ -1442,13 +1456,17 @@ namespace Terminal.Gui { /// public void FocusFirst () { + if (!CanBeVisible (this)) { + return; + } + if (tabIndexes == null) { SuperView?.SetFocus (this); return; } foreach (var view in tabIndexes) { - if (view.CanFocus && view.tabStop) { + if (view.CanFocus && view.tabStop && view.Visible) { SetFocus (view); return; } @@ -1460,6 +1478,10 @@ namespace Terminal.Gui { /// public void FocusLast () { + if (!CanBeVisible (this)) { + return; + } + if (tabIndexes == null) { SuperView?.SetFocus (this); return; @@ -1469,7 +1491,7 @@ namespace Terminal.Gui { i--; View v = tabIndexes [i]; - if (v.CanFocus && v.tabStop) { + if (v.CanFocus && v.tabStop && v.Visible) { SetFocus (v); return; } @@ -1482,6 +1504,10 @@ namespace Terminal.Gui { /// true, if previous was focused, false otherwise. public bool FocusPrev () { + if (!CanBeVisible (this)) { + return false; + } + FocusDirection = Direction.Backward; if (tabIndexes == null || tabIndexes.Count == 0) return false; @@ -1501,10 +1527,10 @@ namespace Terminal.Gui { focused_idx = i; continue; } - if (w.CanFocus && focused_idx != -1 && w.tabStop) { + if (w.CanFocus && focused_idx != -1 && w.tabStop && w.Visible) { focused.SetHasFocus (false, w); - if (w != null && w.CanFocus && w.tabStop) + if (w != null && w.CanFocus && w.tabStop && w.Visible) w.FocusLast (); SetFocus (w); @@ -1524,6 +1550,10 @@ namespace Terminal.Gui { /// true, if next was focused, false otherwise. public bool FocusNext () { + if (!CanBeVisible (this)) { + return false; + } + FocusDirection = Direction.Forward; if (tabIndexes == null || tabIndexes.Count == 0) return false; @@ -1543,10 +1573,10 @@ namespace Terminal.Gui { focused_idx = i; continue; } - if (w.CanFocus && focused_idx != -1 && w.tabStop) { + if (w.CanFocus && focused_idx != -1 && w.tabStop && w.Visible) { focused.SetHasFocus (false, w); - if (w != null && w.CanFocus && w.tabStop) + if (w != null && w.CanFocus && w.tabStop && w.Visible) w.FocusFirst (); SetFocus (w); @@ -1841,6 +1871,10 @@ namespace Terminal.Gui { /// public override bool OnMouseEnter (MouseEvent mouseEvent) { + if (!CanBeVisible (this)) { + return false; + } + MouseEventArgs args = new MouseEventArgs (mouseEvent); MouseEnter?.Invoke (args); if (args.Handled) @@ -1854,6 +1888,10 @@ namespace Terminal.Gui { /// public override bool OnMouseLeave (MouseEvent mouseEvent) { + if (!CanBeVisible (this)) { + return false; + } + MouseEventArgs args = new MouseEventArgs (mouseEvent); MouseLeave?.Invoke (args); if (args.Handled) @@ -1871,6 +1909,10 @@ namespace Terminal.Gui { /// true, if the event was handled, false otherwise. public virtual bool OnMouseEvent (MouseEvent mouseEvent) { + if (!CanBeVisible (this)) { + return false; + } + MouseEventArgs args = new MouseEventArgs (mouseEvent); MouseClick?.Invoke (args); if (args.Handled) @@ -1934,5 +1976,24 @@ namespace Terminal.Gui { } } } + + /// + /// Gets or sets the view visibility. + /// + public bool Visible { get; set; } = true; + + bool CanBeVisible (View view) + { + if (!view.Visible) { + return false; + } + for (var c = view.SuperView; c != null; c = c.SuperView) { + if (!c.Visible) { + return false; + } + } + + return true; + } } }