Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Terminal.Gui/Core/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,12 @@ public override void PositionCursor ()
{
if (!IsMdiContainer) {
base.PositionCursor ();
if (Focused == null) {
EnsureFocus ();
if (Focused == null) {
Driver.SetCursorVisibility (CursorVisibility.Invisible);
}
}
return;
}

Expand All @@ -920,6 +926,9 @@ public override void PositionCursor ()
}
}
base.PositionCursor ();
if (Focused == null) {
Driver.SetCursorVisibility (CursorVisibility.Invisible);
}
}

/// <summary>
Expand Down
47 changes: 35 additions & 12 deletions Terminal.Gui/Core/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@ public virtual void Add (View view)
view.tabIndex = tabIndexes.IndexOf (view);
addingView = false;
}
if (view.Enabled && !Enabled) {
view.oldEnabled = true;
view.Enabled = false;
}
SetNeedsLayout ();
SetNeedsDisplay ();
OnAdded (view);
Expand Down Expand Up @@ -1303,14 +1307,16 @@ public virtual void PositionCursor ()
return;
}

if (focused?.Visible == true && focused?.Enabled == true && focused?.Frame.Width > 0 && focused.Frame.Height > 0) {
if (focused == null && SuperView != null) {
SuperView.EnsureFocus ();
} else if (focused?.Visible == true && focused?.Enabled == true && focused?.Frame.Width > 0 && focused.Frame.Height > 0) {
focused.PositionCursor ();
} else if (focused?.Visible == true && focused?.Enabled == false) {
focused = null;
} else if (CanFocus && HasFocus && Visible && Frame.Width > 0 && Frame.Height > 0) {
Move (TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition, 0);
} else {
if (CanFocus && HasFocus && Visible && Frame.Width > 0 && Frame.Height > 0) {
Move (TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition, 0);
} else {
Move (frame.X, frame.Y);
}
Move (frame.X, frame.Y);
}
}

Expand Down Expand Up @@ -1504,13 +1510,13 @@ public virtual void Redraw (Rect bounds)
var clipRect = new Rect (Point.Empty, frame.Size);

if (ColorScheme != null) {
Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
}

if (!IgnoreBorderPropertyOnRedraw && Border != null) {
Border.DrawContent (this);
} else if (ustring.IsNullOrEmpty (TextFormatter.Text) &&
(GetType ().IsNestedPublic) && !IsOverridden (this, "Redraw") &&
(GetType ().IsNestedPublic && !IsOverridden (this, "Redraw") || GetType ().Name == "View") &&
(!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded)) {

Clear ();
Expand All @@ -1525,8 +1531,8 @@ public virtual void Redraw (Rect bounds)
if (TextFormatter != null) {
TextFormatter.NeedsFormat = true;
}
TextFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? ColorScheme.Focus : GetNormalColor (),
HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled,
TextFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? GetFocusColor () : GetNormalColor (),
HasFocus ? ColorScheme.HotFocus : GetHotNormalColor (),
containerBounds);
}

Expand Down Expand Up @@ -2617,7 +2623,13 @@ public override bool Enabled {
get => base.Enabled;
set {
if (base.Enabled != value) {
base.Enabled = value;
if (value) {
if (SuperView == null || SuperView?.Enabled == true) {
base.Enabled = value;
}
} else {
base.Enabled = value;
}
if (!value && HasFocus) {
SetHasFocus (false, this);
}
Expand Down Expand Up @@ -2684,7 +2696,7 @@ public virtual Border Border {
/// to draw the view's border. If <see langword="true"/> no border is drawn (and the view is expected to draw the border
/// itself).
/// </summary>
public virtual bool IgnoreBorderPropertyOnRedraw { get; set; } = false;
public virtual bool IgnoreBorderPropertyOnRedraw { get; set; }

/// <summary>
/// Pretty prints the View
Expand Down Expand Up @@ -3109,6 +3121,17 @@ public virtual Attribute GetNormalColor ()
return Enabled ? ColorScheme.Normal : ColorScheme.Disabled;
}

/// <summary>
/// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
/// </summary>
/// <returns><see cref="Terminal.Gui.ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/>
/// or <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>.
/// If it's overridden can return other values.</returns>
public virtual Attribute GetFocusColor ()
{
return Enabled ? ColorScheme.Focus : ColorScheme.Disabled;
}

/// <summary>
/// Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions UnitTests/TopLevels/ToplevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,5 +1011,25 @@ public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
Assert.True (isEnter);
Assert.False (isLeave);
}

[Fact, AutoInitShutdown]
public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
{
var tf = new TextField ("test") { Width = 5 };
var view = new View () { Width = 10, Height = 10 };
view.Add (tf);
Application.Top.Add (view);
Application.Begin (Application.Top);

Assert.True (tf.HasFocus);
Application.Driver.GetCursorVisibility (out CursorVisibility cursor);
Assert.Equal (CursorVisibility.Default, cursor);

view.Enabled = false;
Assert.False (tf.HasFocus);
Application.Refresh ();
Application.Driver.GetCursorVisibility (out cursor);
Assert.Equal (CursorVisibility.Invisible, cursor);
}
}
}
3 changes: 3 additions & 0 deletions UnitTests/Views/ViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NStack;
using System;
using System.Collections.Generic;
using Terminal.Gui.Graphs;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -4209,6 +4210,7 @@ public void Clear_Does_Not_Spillover_Its_Parent (bool label)
v.CanFocus = true;
Assert.False (v.HasFocus);
v.SetFocus ();
Assert.True (v.HasFocus);
Application.Refresh ();
TestHelpers.AssertDriverColorsAre (@"
111111111111111111110", attributes);
Expand Down Expand Up @@ -4491,6 +4493,7 @@ A text with some long width
A text witith two lines. ", output);
}


[Fact, AutoInitShutdown]
public void Test_Nested_Views_With_Height_Equal_To_One ()
{
Expand Down