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
4 changes: 4 additions & 0 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public void SetInitialCursorVisibility ()

public bool GetCursorVisibility (out CursorVisibility visibility)
{
if (ScreenBuffer == IntPtr.Zero) {
visibility = CursorVisibility.Invisible;
return false;
}
if (!GetConsoleCursorInfo (ScreenBuffer, out ConsoleCursorInfo info)) {
var err = Marshal.GetLastWin32Error ();
if (err != 0) {
Expand Down
3 changes: 3 additions & 0 deletions Terminal.Gui/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ public static RunState Begin (Toplevel toplevel)
if (Top != null && toplevel != Top && !toplevels.Contains (Top)) {
Top.Dispose ();
Top = null;
} else if (Top != null && toplevel != Top && toplevels.Contains (Top)) {
Top.OnLeave (toplevel);
}
if (string.IsNullOrEmpty (toplevel.Id.ToString ())) {
var count = 1;
Expand Down Expand Up @@ -1048,6 +1050,7 @@ public static void End (RunState runState)
MdiTop.OnAllChildClosed ();
} else {
SetCurrentAsTop ();
Current.OnEnter (Current);
}
Refresh ();
}
Expand Down
12 changes: 12 additions & 0 deletions Terminal.Gui/Core/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,18 @@ public virtual bool ShowChild (Toplevel top = null)
}
return false;
}

///<inheritdoc/>
public override bool OnEnter (View view)
{
return MostFocused?.OnEnter (view) ?? base.OnEnter (view);
}

///<inheritdoc/>
public override bool OnLeave (View view)
{
return MostFocused?.OnLeave (view) ?? base.OnLeave (view);
}
}

/// <summary>
Expand Down
20 changes: 10 additions & 10 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,16 @@ public override bool OnEnter (View view)
return base.OnEnter (view);
}

///<inheritdoc/>
public override bool OnLeave (View view)
{
if (Application.MouseGrabView != null && Application.MouseGrabView == this) {
Application.UngrabMouse ();
}

return base.OnLeave (view);
}

// Returns an encoded region start..end (top 32 bits are the row, low32 the column)
void GetEncodedRegionBounds (out long start, out long end,
int? startRow = null, int? startCol = null, int? cRow = null, int? cCol = null)
Expand Down Expand Up @@ -4447,16 +4457,6 @@ void ProcessMouseClick (MouseEvent ev, out List<Rune> line)
line = r;
}

///<inheritdoc/>
public override bool OnLeave (View view)
{
if (Application.MouseGrabView != null && Application.MouseGrabView == this) {
Application.UngrabMouse ();
}

return base.OnLeave (view);
}

/// <summary>
/// Allows clearing the <see cref="HistoryText.HistoryTextItem"/> items updating the original text.
/// </summary>
Expand Down
47 changes: 42 additions & 5 deletions UnitTests/TopLevels/ToplevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ public void Mouse_Drag_On_Top_With_Superview_Null ()
((FakeDriver)Application.Driver).SetBufferSize (40, 15);
MessageBox.Query ("About", "Hello Word", "Ok");

} else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
} else if (iterations == 1) TestHelpers.AssertDriverContentsWithFrameAre (@"
File
┌ Window ──────────────────────────────┐
│ │
Expand All @@ -712,7 +712,7 @@ public void Mouse_Drag_On_Top_With_Superview_Null ()
│ │
└──────────────────────────────────────┘
CTRL-N New ", output);
else if (iterations == 2) {
else if (iterations == 2) {
Assert.Null (Application.MouseGrabView);
// Grab the mouse
ReflectionTools.InvokePrivate (
Expand Down Expand Up @@ -815,8 +815,8 @@ public void Mouse_Drag_On_Top_With_Superview_Null ()

Assert.Null (Application.MouseGrabView);

} else if (iterations == 8) Application.RequestStop ();
else if (iterations == 9) Application.RequestStop ();
} else if (iterations == 8) Application.RequestStop ();
else if (iterations == 9) Application.RequestStop ();
};

Application.Run ();
Expand Down Expand Up @@ -956,7 +956,7 @@ public void Mouse_Drag_On_Top_With_Superview_Not_Null ()

Assert.Null (Application.MouseGrabView);

} else if (iterations == 8) Application.RequestStop ();
} else if (iterations == 8) Application.RequestStop ();
};

Application.Run ();
Expand All @@ -974,5 +974,42 @@ public void EnsureVisibleBounds_With_Border_Null_Not_Throws ()
exception = Record.Exception (() => ((FakeDriver)Application.Driver).SetBufferSize (10, 0));
Assert.Null (exception);
}

[Fact, AutoInitShutdown]
public void OnEnter_OnLeave_Triggered_On_Application_Begin_End ()
{
var isEnter = false;
var isLeave = false;
var v = new View ();
v.Enter += (_) => isEnter = true;
v.Leave += (_) => isLeave = true;
var top = Application.Top;
top.Add (v);

Assert.False (v.CanFocus);
var exception = Record.Exception (() => top.OnEnter (top));
Assert.Null (exception);
exception = Record.Exception (() => top.OnLeave (top));
Assert.Null (exception);

v.CanFocus = true;
Application.Begin (top);

Assert.True (isEnter);
Assert.False (isLeave);

isEnter = false;
var d = new Dialog ();
var rs = Application.Begin (d);

Assert.False (isEnter);
Assert.True (isLeave);

isLeave = false;
Application.End (rs);

Assert.True (isEnter);
Assert.False (isLeave);
}
}
}