Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows ListView trigger the Enter and Leave events. #1508

Merged
merged 2 commits into from
Nov 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public override bool MouseEvent (MouseEvent me)
///<inheritdoc/>
public override void PositionCursor ()
{
if (HotKey == Key.Unknown) {
if (HotKey == Key.Unknown && text != "") {
for (int i = 0; i < base.Text.RuneCount; i++) {
if (base.Text [i] == text [0]) {
Move (i, 0);
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public override bool OnEnter (View view)

search.CursorPosition = search.Text.RuneCount;

return true;
return base.OnEnter (view);
}

///<inheritdoc/>
Expand All @@ -251,7 +251,7 @@ public override bool OnLeave (View view)
listview.TabStop = false;
}

return true;
return base.OnLeave (view);
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ public override bool OnEnter (View view)
if (lastSelectedItem == -1) {
EnsuresVisibilitySelectedItem ();
OnSelectedChanged ();
return true;
}

return base.OnEnter (view);
Expand All @@ -684,10 +683,9 @@ public override bool OnLeave (View view)
{
if (lastSelectedItem > -1) {
lastSelectedItem = -1;
return true;
}

return false;
return base.OnLeave (view);
}

void EnsuresVisibilitySelectedItem ()
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public override void Redraw (Rect bounds)
int startAtY = Math.Max (0, GetTabHeight (true) - 1);

DrawFrame (new Rect (0, startAtY, bounds.Width,
bounds.Height - spaceAtBottom - startAtY), 0, true);
Math.Max (bounds.Height - spaceAtBottom - startAtY, 0)), 0, true);
}

if (Tabs.Any ()) {
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/TextValidateField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public override void PositionCursor ()
// Fixed = true, is for inputs thar have fixed width, like masked ones.
// Fixed = false, is for normal input.
// When it's right-aligned and it's a normal input, the cursor behaves differently.
if (provider.Fixed == false && TextAlignment == TextAlignment.Right) {
if (provider?.Fixed == false && TextAlignment == TextAlignment.Right) {
Move (cursorPosition + left - 1, 0);
} else {
Move (cursorPosition + left, 0);
Expand Down Expand Up @@ -591,7 +591,7 @@ bool EndKeyHandler ()
public override bool ProcessKey (KeyEvent kb)
{
if (provider == null) {
return true;
return false;
}

switch (kb.Key) {
Expand Down
134 changes: 103 additions & 31 deletions UnitTests/AllViewsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,65 @@ public void AllViews_Tests_All_Constructors ()
public bool Constructors_FullTest (Type type)
{
foreach (var ctor in type.GetConstructors ()) {
if (type.IsGenericType && type.IsTypeDefinition) {
List<Type> gTypes = new List<Type> ();
var view = GetTypeInitializer (type, ctor);
if (view != null) {
Assert.True (type.FullName == view.GetType ().FullName);
}
}

foreach (var args in type.GetGenericArguments ()) {
gTypes.Add (typeof (object));
}
type = type.MakeGenericType (gTypes.ToArray ());
return true;
}

Assert.IsType (type, (View)Activator.CreateInstance (type));
private static View GetTypeInitializer (Type type, ConstructorInfo ctor)
{
View viewType = null;

} else {
ParameterInfo [] paramsInfo = ctor.GetParameters ();
Type paramType;
List<object> pTypes = new List<object> ();

if (type.IsGenericType) {
foreach (var args in type.GetGenericArguments ()) {
paramType = args.GetType ();
if (args.Name == "T") {
pTypes.Add (typeof (object));
} else {
AddArguments (paramType, pTypes);
}
}
}
if (type.IsGenericType && type.IsTypeDefinition) {
List<Type> gTypes = new List<Type> ();

foreach (var p in paramsInfo) {
paramType = p.ParameterType;
if (p.HasDefaultValue) {
pTypes.Add (p.DefaultValue);
foreach (var args in type.GetGenericArguments ()) {
gTypes.Add (typeof (object));
}
type = type.MakeGenericType (gTypes.ToArray ());

Assert.IsType (type, (View)Activator.CreateInstance (type));

} else {
ParameterInfo [] paramsInfo = ctor.GetParameters ();
Type paramType;
List<object> pTypes = new List<object> ();

if (type.IsGenericType) {
foreach (var args in type.GetGenericArguments ()) {
paramType = args.GetType ();
if (args.Name == "T") {
pTypes.Add (typeof (object));
} else {
AddArguments (paramType, pTypes);
}

}
}

if (type.IsGenericType && !type.IsTypeDefinition) {
Assert.IsType (type, (View)Activator.CreateInstance (type));
foreach (var p in paramsInfo) {
paramType = p.ParameterType;
if (p.HasDefaultValue) {
pTypes.Add (p.DefaultValue);
} else {
Assert.IsType (type, ctor.Invoke (pTypes.ToArray ()));
AddArguments (paramType, pTypes);
}

}

if (type.IsGenericType && !type.IsTypeDefinition) {
viewType = (View)Activator.CreateInstance (type);
Assert.IsType (type, viewType);
} else {
viewType = (View)ctor.Invoke (pTypes.ToArray ());
Assert.IsType (type, viewType);
}
}

return true;
return viewType;
}

private static void AddArguments (Type paramType, List<object> pTypes)
Expand Down Expand Up @@ -108,5 +122,63 @@ List<Type> GetAllViewClassesCollection ()
}
return types;
}

[Fact]
public void AllViews_Enter_Leave_Events ()
{
foreach (var type in GetAllViewClassesCollection ()) {
Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));

var top = Application.Top;
var vType = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
if (vType == null) {
Application.Shutdown ();
continue;
}
vType.X = 0;
vType.Y = 0;
vType.Width = 10;
vType.Height = 1;

var view = new View () {
X = 0,
Y = 1,
Width = 10,
Height = 1,
CanFocus = true
};
var vTypeEnter = 0;
var vTypeLeave = 0;
var viewEnter = 0;
var viewLeave = 0;

vType.Enter += _ => vTypeEnter++;
vType.Leave += _ => vTypeLeave++;
view.Enter += _ => viewEnter++;
view.Leave += _ => viewLeave++;

top.Add (vType, view);
Application.Begin (top);

if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal)) {
Application.Shutdown ();
continue;
}

if (vType is TextView) {
top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ()));
} else {
top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
}
top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));

Assert.Equal (2, vTypeEnter);
Assert.Equal (1, vTypeLeave);
Assert.Equal (1, viewEnter);
Assert.Equal (1, viewLeave);

Application.Shutdown ();
}
}
}
}