Fixes #3072, #3148. Dialog and MessageBox key binding do not handle Enter correctly (#3151)

* Moved Command.Accept/Enter key binding from Toplevel to Dialog and added unit tests

* Dialog binds Esc to Command.QuitTopLevel

* Better Dialog key binding tests

* Better MessageBox key binding tests

* List of changes:
1. Removed explicit type casting for `Application.QuitKey` in `KeyBindings.Add` method calls. This indicates that `Application.QuitKey` is already of type `KeyCode`.
2. Removed explicit type casting for `Application.AlternateForwardKey` in `KeyBindings.Add` method calls. This suggests that `Application.AlternateForwardKey` is already of type `KeyCode`.
3. Removed explicit type casting for `Application.AlternateBackwardKey` in `KeyBindings.Add` method calls. This implies that `Application.AlternateBackwardKey` is already of type `KeyCode`.

References to the code changes:
1. `KeyBindings.Add((KeyCode)Application.QuitKey, QuitApplication);` changed to `KeyBindings.Add(Application.QuitKey, QuitApplication);`
2. `KeyBindings.Add((KeyCode)Application.AlternateForwardKey, MoveForward);` changed to `KeyBindings.Add(Application.AlternateForwardKey, MoveForward);`
3. `KeyBindings.Add((KeyCode)Application.AlternateBackwardKey, MoveBackward);` changed to `KeyBindings.Add(Application.AlternateBackwardKey, MoveBackward);`

* The `Command.Accept` command now directly calls the `OnClicked` method and returns `true`, effectively incorporating the behavior of the now-removed `AcceptKey` method.

* Moved Command.Accept/Enter key binding from Toplevel to Window and added unit tests
This commit is contained in:
Tig
2024-01-10 13:22:18 -07:00
committed by GitHub
parent 0aff2f1c87
commit 707dbda71b
8 changed files with 1836 additions and 1546 deletions

View File

@@ -120,12 +120,12 @@ public class Button : View {
OnResizeNeeded ();
// Override default behavior of View
// Command.Default sets focus
// Override default behavior of View - Command.Default sets focus
AddCommand (Command.Accept, () => { OnClicked (); return true; });
KeyBindings.Add (Key.Space, Command.Default, Command.Accept);
KeyBindings.Add (Key.Enter, Command.Default, Command.Accept);
}
/// <summary>
/// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
/// </summary>
@@ -166,14 +166,6 @@ public class Button : View {
}
}
bool AcceptKey ()
{
//if (!HasFocus) {
// SetFocus ();
//}
OnClicked ();
return true;
}
/// <summary>
/// Virtual method to invoke the <see cref="Clicked"/> event.

View File

@@ -70,6 +70,8 @@ public class Dialog : Window {
Modal = true;
ButtonAlignment = DefaultButtonAlignment;
KeyBindings.Add (Key.Esc, Command.QuitToplevel);
if (buttons != null) {
foreach (var b in buttons) {
AddButton (b);
@@ -104,6 +106,7 @@ public class Dialog : Window {
//button.AutoSize = false; // BUGBUG: v2 - Hack to get around autosize not accounting for Margin?
buttons.Add (button);
Add (button);
SetNeedsDisplay ();
if (IsInitialized) {
LayoutSubviews ();
@@ -228,16 +231,4 @@ public class Dialog : Window {
break;
}
}
// BUGBUG: Why is this not handled by a key binding???
///<inheritdoc/>
public override bool OnProcessKeyDown (Key a)
{
switch (a.KeyCode) {
case KeyCode.Esc:
Application.RequestStop (this);
return true;
}
return false;
}
}

View File

@@ -223,42 +223,32 @@ public partial class Toplevel : View {
Application.Refresh ();
return true;
});
AddCommand (Command.Accept, () => {
// TODO: Perhaps all views should support the concept of being default?
// TODO: It's bad that Toplevel is tightly coupled with Button
if (Subviews.FirstOrDefault (v => v is Button && ((Button)v).IsDefault && ((Button)v).Enabled) is Button defaultBtn) {
defaultBtn.InvokeCommand (Command.Accept);
return true;
}
return false;
});
// Default keybindings for this view
KeyBindings.Add ((KeyCode)Application.QuitKey, Command.QuitToplevel);
KeyBindings.Add (Application.QuitKey, Command.QuitToplevel);
KeyBindings.Add (KeyCode.CursorRight, Command.NextView);
KeyBindings.Add (KeyCode.CursorDown, Command.NextView);
KeyBindings.Add (KeyCode.CursorLeft, Command.PreviousView);
KeyBindings.Add (KeyCode.CursorUp, Command.PreviousView);
KeyBindings.Add (Key.CursorRight, Command.NextView);
KeyBindings.Add (Key.CursorDown, Command.NextView);
KeyBindings.Add (Key.CursorLeft, Command.PreviousView);
KeyBindings.Add (Key.CursorUp, Command.PreviousView);
KeyBindings.Add (KeyCode.Tab, Command.NextView);
KeyBindings.Add (KeyCode.Tab | KeyCode.ShiftMask, Command.PreviousView);
KeyBindings.Add (KeyCode.Tab | KeyCode.CtrlMask, Command.NextViewOrTop);
KeyBindings.Add (KeyCode.Tab | KeyCode.ShiftMask | KeyCode.CtrlMask, Command.PreviousViewOrTop);
KeyBindings.Add (Key.Tab, Command.NextView);
KeyBindings.Add (Key.Tab.WithShift, Command.PreviousView);
KeyBindings.Add (Key.Tab.WithCtrl, Command.NextViewOrTop);
KeyBindings.Add (Key.Tab.WithShift.WithCtrl, Command.PreviousViewOrTop);
KeyBindings.Add (KeyCode.F5, Command.Refresh);
KeyBindings.Add ((KeyCode)Application.AlternateForwardKey, Command.NextViewOrTop); // Needed on Unix
KeyBindings.Add ((KeyCode)Application.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix
KeyBindings.Add (Key.F5, Command.Refresh);
KeyBindings.Add (Application.AlternateForwardKey, Command.NextViewOrTop); // Needed on Unix
KeyBindings.Add (Application.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix
#if UNIX_KEY_BINDINGS
KeyBindings.Add (Key.Z | Key.CtrlMask, Command.Suspend);
KeyBindings.Add (Key.L | Key.CtrlMask, Command.Refresh);// Unix
KeyBindings.Add (Key.F | Key.CtrlMask, Command.NextView);// Unix
KeyBindings.Add (Key.I | Key.CtrlMask, Command.NextView); // Unix
KeyBindings.Add (Key.B | Key.CtrlMask, Command.PreviousView);// Unix
KeyBindings.Add (Key.Z.WithCtrl, Command.Suspend);
KeyBindings.Add (Key.L.WithCtrl, Command.Refresh);// Unix
KeyBindings.Add (Key.F.WithCtrl, Command.NextView);// Unix
KeyBindings.Add (Key.I.WithCtrl, Command.NextView); // Unix
KeyBindings.Add (Key.B.WithCtrl, Command.PreviousView);// Unix
#endif
// This enables the default button to be activated by the Enter key.
KeyBindings.Add (KeyCode.Enter, Command.Accept);
}
void Application_UnGrabbingMouse (object sender, GrabMouseEventArgs e)
@@ -286,7 +276,7 @@ public partial class Toplevel : View {
/// <param name="e"></param>
public virtual void OnAlternateForwardKeyChanged (KeyChangedEventArgs e)
{
KeyBindings.Replace ((KeyCode)e.OldKey, (KeyCode)e.NewKey);
KeyBindings.Replace (e.OldKey, e.NewKey);
AlternateForwardKeyChanged?.Invoke (this, e);
}
@@ -301,7 +291,7 @@ public partial class Toplevel : View {
/// <param name="e"></param>
public virtual void OnAlternateBackwardKeyChanged (KeyChangedEventArgs e)
{
KeyBindings.Replace ((KeyCode)e.OldKey, (KeyCode)e.NewKey);
KeyBindings.Replace (e.OldKey, e.NewKey);
AlternateBackwardKeyChanged?.Invoke (this, e);
}
@@ -316,7 +306,7 @@ public partial class Toplevel : View {
/// <param name="e"></param>
public virtual void OnQuitKeyChanged (KeyChangedEventArgs e)
{
KeyBindings.Replace ((KeyCode)e.OldKey, (KeyCode)e.NewKey);
KeyBindings.Replace (e.OldKey, e.NewKey);
QuitKeyChanged?.Invoke (this, e);
}

View File

@@ -1,60 +1,68 @@
using System;
using System.Collections;
using System.Linq;
using System.Text.Json.Serialization;
using System.Text;
using Terminal.Gui;
using static Terminal.Gui.ConfigurationManager;
namespace Terminal.Gui {
namespace Terminal.Gui;
/// <summary>
/// A <see cref="Toplevel"/> <see cref="View"/> with <see cref="View.BorderStyle"/> set to
/// <see cref="LineStyle.Single"/>. Provides a container for other views.
/// </summary>
/// <remarks>
/// <para>
/// If any subview is a button and the <see cref="Button.IsDefault"/> property is set to true, the Enter key
/// will invoke the <see cref="Command.Accept"/> command on that subview.
/// </para>
/// </remarks>
public class Window : Toplevel {
/// <summary>
/// Initializes a new instance of the <see cref="Window"/> class using
/// <see cref="LayoutStyle.Computed"/> positioning.
/// </summary>
public Window () => SetInitialProperties ();
/// <summary>
/// A <see cref="Toplevel"/> <see cref="View"/> with <see cref="View.BorderStyle"/> set to <see cref="LineStyle.Single"/>.
/// Initializes a new instance of the <see cref="Window"/> class using
/// <see cref="LayoutStyle.Computed"/> positioning.
/// </summary>
public Window (Rect frame) : base (frame) => SetInitialProperties ();
// TODO: enable this
///// <summary>
///// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is <see cref="LineStyle.Single"/>.
///// </summary>
///// <remarks>
///// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>s.
///// </remarks>
/////[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
////public static ColorScheme DefaultColorScheme { get; set; } = Colors.Base;
/// <summary>
/// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is
/// <see cref="LineStyle.Single"/>.
/// </summary>
/// <remarks>
/// <para>
/// This is a helper class to simplify creating a <see cref="Toplevel"/> with a border.
/// </para>
/// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all
/// <see cref="Window"/>s.
/// </remarks>
public class Window : Toplevel {
/// <summary>
/// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
/// </summary>
public Window () : base () {
SetInitialProperties ();
}
[SerializableConfigurationProperty (Scope = typeof (ThemeScope))] [JsonConverter (typeof (JsonStringEnumConverter))]
public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
/// <summary>
/// Initializes a new instance of the <see cref="Window"/> class using <see cref="LayoutStyle.Computed"/> positioning.
/// </summary>
public Window (Rect frame) : base (frame)
{
SetInitialProperties ();
}
void SetInitialProperties ()
{
CanFocus = true;
ColorScheme = Colors.Base; // TODO: make this a theme property
BorderStyle = DefaultBorderStyle;
// TODO: enable this
///// <summary>
///// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is <see cref="LineStyle.Single"/>.
///// </summary>
///// <remarks>
///// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>s.
///// </remarks>
/////[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
////public static ColorScheme DefaultColorScheme { get; set; } = Colors.Base;
// This enables the default button to be activated by the Enter key.
AddCommand (Command.Accept, () => {
// TODO: Perhaps all views should support the concept of being default?
if (Subviews.FirstOrDefault (v => v is Button { IsDefault: true, Enabled: true }) is Button defaultBtn) {
defaultBtn.InvokeCommand (Command.Accept);
return true;
}
return false;
});
/// <summary>
/// The default <see cref="LineStyle"/> for <see cref="Window"/>'s border. The default is <see cref="LineStyle.Single"/>.
/// </summary>
/// <remarks>
/// This property can be set in a Theme to change the default <see cref="LineStyle"/> for all <see cref="Window"/>s.
/// </remarks>
[SerializableConfigurationProperty (Scope = typeof (ThemeScope)), JsonConverter (typeof (JsonStringEnumConverter))]
public static LineStyle DefaultBorderStyle { get; set; } = LineStyle.Single;
void SetInitialProperties ()
{
CanFocus = true;
ColorScheme = Colors.Base; // TODO: make this a theme property
BorderStyle = DefaultBorderStyle;
}
KeyBindings.Add (Key.Enter, Command.Accept);
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -78,39 +78,57 @@ namespace Terminal.Gui.ViewsTests {
Application.Top.Add (btn);
Application.Begin (Application.Top);
// Hot key. Both alone and with alt
Assert.Equal (KeyCode.T, btn.HotKey);
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T | KeyCode.AltMask)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
clicked = false;
// IsDefault = false
// Space and Enter should work
Assert.False (btn.IsDefault);
Assert.False (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.False (clicked);
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (clicked);
clicked = false;
// IsDefault = true
// Space and Enter should work
btn.IsDefault = true;
Assert.False (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (clicked);
clicked = false;
// Toplevel does not handle Enter, so it should get passed on to button
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.AltMask | KeyCode.T)));
Assert.True (clicked);
clicked = false;
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Enter)));
// Direct
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Enter)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Space)));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (new ((KeyCode)'T')));
Assert.True (clicked);
clicked = false;
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
// Change hotkey:
btn.Text = "Te_st";
clicked = false;
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
clicked = false;
}
[Fact]

View File

@@ -1,130 +1,122 @@
using System;
using Xunit;
using Xunit;
using Xunit.Abstractions;
//using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
// Alias Console to MockConsole so we don't accidentally use Console
using Console = Terminal.Gui.FakeConsole;
using System.Text;
namespace Terminal.Gui.ViewsTests;
namespace Terminal.Gui.ViewsTests {
public class WindowTests {
readonly ITestOutputHelper output;
public class WindowTests {
readonly ITestOutputHelper _output;
public WindowTests (ITestOutputHelper output)
{
this.output = output;
}
public WindowTests (ITestOutputHelper output) => this._output = output;
[Fact]
public void New_Initializes ()
{
// Parameterless
var r = new Window ();
Assert.NotNull (r);
Assert.Equal (string.Empty, r.Title);
Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
Assert.Equal ("Window()(0,0,0,0)", r.ToString ());
Assert.True (r.CanFocus);
Assert.False (r.HasFocus);
Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
Assert.Null (r.Focused);
Assert.NotNull (r.ColorScheme);
Assert.Equal (Dim.Fill (0), r.Width);
Assert.Equal (Dim.Fill (0), r.Height);
Assert.Null (r.X);
Assert.Null (r.Y);
Assert.False (r.IsCurrentTop);
Assert.Empty (r.Id);
Assert.False (r.WantContinuousButtonPressed);
Assert.False (r.WantMousePositionReports);
Assert.Null (r.SuperView);
Assert.Null (r.MostFocused);
Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
[Fact]
public void New_Initializes ()
{
// Parameterless
var r = new Window ();
Assert.NotNull (r);
Assert.Equal (string.Empty, r.Title);
Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
Assert.Equal ("Window()(0,0,0,0)", r.ToString ());
Assert.True (r.CanFocus);
Assert.False (r.HasFocus);
Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
Assert.Null (r.Focused);
Assert.NotNull (r.ColorScheme);
Assert.Equal (Dim.Fill (), r.Width);
Assert.Equal (Dim.Fill (), r.Height);
Assert.Null (r.X);
Assert.Null (r.Y);
Assert.False (r.IsCurrentTop);
Assert.Empty (r.Id);
Assert.False (r.WantContinuousButtonPressed);
Assert.False (r.WantMousePositionReports);
Assert.Null (r.SuperView);
Assert.Null (r.MostFocused);
Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
// Empty Rect
r = new Window (Rect.Empty) { Title = "title" };
Assert.NotNull (r);
Assert.Equal ("title", r.Title);
Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
Assert.Equal ("Window(title)(0,0,0,0)", r.ToString ());
Assert.True (r.CanFocus);
Assert.False (r.HasFocus);
Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
Assert.Null (r.Focused);
Assert.NotNull (r.ColorScheme);
Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
Assert.Null (r.Height); // avoiding Dim errors.
Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
Assert.Null (r.Y); // avoiding Pos errors.
Assert.False (r.IsCurrentTop);
Assert.Equal (r.Title, r.Id);
Assert.False (r.WantContinuousButtonPressed);
Assert.False (r.WantMousePositionReports);
Assert.Null (r.SuperView);
Assert.Null (r.MostFocused);
Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
// Empty Rect
r = new Window (Rect.Empty) { Title = "title" };
Assert.NotNull (r);
Assert.Equal ("title", r.Title);
Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
Assert.Equal ("Window(title)(0,0,0,0)", r.ToString ());
Assert.True (r.CanFocus);
Assert.False (r.HasFocus);
Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
Assert.Null (r.Focused);
Assert.NotNull (r.ColorScheme);
Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
Assert.Null (r.Height); // avoiding Dim errors.
Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
Assert.Null (r.Y); // avoiding Pos errors.
Assert.False (r.IsCurrentTop);
Assert.Equal (r.Title, r.Id);
Assert.False (r.WantContinuousButtonPressed);
Assert.False (r.WantMousePositionReports);
Assert.Null (r.SuperView);
Assert.Null (r.MostFocused);
Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
// Rect with values
r = new Window (new Rect (1, 2, 3, 4)) { Title = "title" };
Assert.Equal ("title", r.Title);
Assert.NotNull (r);
Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
Assert.Equal ("Window(title)(1,2,3,4)", r.ToString ());
Assert.True (r.CanFocus);
Assert.False (r.HasFocus);
Assert.Equal (new Rect (0, 0, 1, 2), r.Bounds);
Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
Assert.Null (r.Focused);
Assert.NotNull (r.ColorScheme);
Assert.Null (r.Width);
Assert.Null (r.Height);
Assert.Null (r.X);
Assert.Null (r.Y);
Assert.False (r.IsCurrentTop);
Assert.Equal (r.Title, r.Id);
Assert.False (r.WantContinuousButtonPressed);
Assert.False (r.WantMousePositionReports);
Assert.Null (r.SuperView);
Assert.Null (r.MostFocused);
Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
r.Dispose ();
}
// Rect with values
r = new Window (new Rect (1, 2, 3, 4)) { Title = "title" };
Assert.Equal ("title", r.Title);
Assert.NotNull (r);
Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
Assert.Equal ("Window(title)(1,2,3,4)", r.ToString ());
Assert.True (r.CanFocus);
Assert.False (r.HasFocus);
Assert.Equal (new Rect (0, 0, 1, 2), r.Bounds);
Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
Assert.Null (r.Focused);
Assert.NotNull (r.ColorScheme);
Assert.Null (r.Width);
Assert.Null (r.Height);
Assert.Null (r.X);
Assert.Null (r.Y);
Assert.False (r.IsCurrentTop);
Assert.Equal (r.Title, r.Id);
Assert.False (r.WantContinuousButtonPressed);
Assert.False (r.WantMousePositionReports);
Assert.Null (r.SuperView);
Assert.Null (r.MostFocused);
Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
r.Dispose ();
}
[Fact, AutoInitShutdown]
public void MenuBar_And_StatusBar_Inside_Window ()
{
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("File", new MenuItem [] {
new MenuItem ("Open", "", null),
new MenuItem ("Quit", "", null),
}),
new MenuBarItem ("Edit", new MenuItem [] {
new MenuItem ("Copy", "", null),
})
});
[Fact] [AutoInitShutdown]
public void MenuBar_And_StatusBar_Inside_Window ()
{
var menu = new MenuBar (new MenuBarItem [] {
new ("File", new MenuItem [] {
new ("Open", "", null),
new ("Quit", "", null)
}),
new ("Edit", new MenuItem [] {
new ("Copy", "", null)
})
});
var sb = new StatusBar (new StatusItem [] {
new StatusItem (KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null),
new StatusItem (KeyCode.CtrlMask | KeyCode.O, "~^O~ Open", null),
new StatusItem (KeyCode.CtrlMask | KeyCode.C, "~^C~ Copy", null),
});
var sb = new StatusBar (new StatusItem [] {
new (KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null),
new (KeyCode.CtrlMask | KeyCode.O, "~^O~ Open", null),
new (KeyCode.CtrlMask | KeyCode.C, "~^C~ Copy", null)
});
var fv = new FrameView ("Frame View") {
Y = 1,
Width = Dim.Fill (),
Height = Dim.Fill (1)
};
var win = new Window ();
win.Add (menu, sb, fv);
var top = Application.Top;
top.Add (win);
Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (20, 10);
var fv = new FrameView ("Frame View") {
Y = 1,
Width = Dim.Fill (),
Height = Dim.Fill (1)
};
var win = new Window ();
win.Add (menu, sb, fv);
var top = Application.Top;
top.Add (win);
Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (20, 10);
TestHelpers.AssertDriverContentsWithFrameAre (@"
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌──────────────────┐
│ File Edit │
│┌┤Frame View├────┐│
@@ -134,11 +126,11 @@ namespace Terminal.Gui.ViewsTests {
││ ││
│└────────────────┘│
│ ^Q Quit │ ^O Open│
└──────────────────┘", output);
└──────────────────┘", _output);
((FakeDriver)Application.Driver).SetBufferSize (40, 20);
((FakeDriver)Application.Driver).SetBufferSize (40, 20);
TestHelpers.AssertDriverContentsWithFrameAre (@"
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌──────────────────────────────────────┐
│ File Edit │
│┌┤Frame View├────────────────────────┐│
@@ -158,11 +150,11 @@ namespace Terminal.Gui.ViewsTests {
││ ││
│└────────────────────────────────────┘│
│ ^Q Quit │ ^O Open │ ^C Copy │
└──────────────────────────────────────┘", output);
└──────────────────────────────────────┘", _output);
((FakeDriver)Application.Driver).SetBufferSize (20, 10);
((FakeDriver)Application.Driver).SetBufferSize (20, 10);
TestHelpers.AssertDriverContentsWithFrameAre (@"
TestHelpers.AssertDriverContentsWithFrameAre (@"
┌──────────────────┐
│ File Edit │
│┌┤Frame View├────┐│
@@ -172,42 +164,75 @@ namespace Terminal.Gui.ViewsTests {
││ ││
│└────────────────┘│
│ ^Q Quit │ ^O Open│
└──────────────────┘", output);
}
[Fact, AutoInitShutdown]
public void OnCanFocusChanged_Only_Must_ContentView_Forces_SetFocus_After_IsInitialized_Is_True ()
{
var win1 = new Window () { Id = "win1", Width = 10, Height = 1 };
var view1 = new View () { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
var win2 = new Window () { Id = "win2", Y = 6, Width = 10, Height = 1 };
var view2 = new View () { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
win2.Add (view2);
win1.Add (view1, win2);
Application.Begin (win1);
Assert.True (win1.HasFocus);
Assert.True (view1.HasFocus);
Assert.False (win2.HasFocus);
Assert.False (view2.HasFocus);
}
[Fact, AutoInitShutdown]
public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
{
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("Child", new MenuItem [] {
new MenuItem ("_Create Child", "", null)
})
});
var win = new Window ();
win.Add (menu);
Application.Top.Add (win);
Application.Begin (Application.Top);
var exception = Record.Exception (() => win.NewKeyDownEvent (new (KeyCode.AltMask)));
Assert.Null (exception);
}
└──────────────────┘", _output);
}
}
[Fact] [AutoInitShutdown]
public void OnCanFocusChanged_Only_Must_ContentView_Forces_SetFocus_After_IsInitialized_Is_True ()
{
var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
win2.Add (view2);
win1.Add (view1, win2);
Application.Begin (win1);
Assert.True (win1.HasFocus);
Assert.True (view1.HasFocus);
Assert.False (win2.HasFocus);
Assert.False (view2.HasFocus);
}
[Fact] [AutoInitShutdown]
public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
{
var menu = new MenuBar (new MenuBarItem [] {
new ("Child", new MenuItem [] {
new ("_Create Child", "", null)
})
});
var win = new Window ();
win.Add (menu);
Application.Top.Add (win);
Application.Begin (Application.Top);
var exception = Record.Exception (() => win.NewKeyDownEvent (new Key (KeyCode.AltMask)));
Assert.Null (exception);
}
public static TheoryData<Toplevel> ButtonContainers =>
new () {
{ new Window () },
{ new Dialog () }
};
[Theory, AutoInitShutdown]
[MemberData (nameof (ButtonContainers))]
public void With_Default_Button_Enter_Invokes_Accept_Action (Toplevel container)
{
var view = new View () { CanFocus = true };
var btnOk = new Button ("Accept") { IsDefault = true };
btnOk.Clicked += (s, e) => view.Text = "Test";
var btnCancel = new Button ("Cancel");
btnCancel.Clicked += (s, e) => view.Text = "";
container.Add (view, btnOk, btnCancel);
var rs = Application.Begin (container);
Assert.True (view.HasFocus);
Assert.Equal ("", view.Text);
Assert.True (Application.OnKeyDown (new Key (KeyCode.Enter)));
Assert.True (view.HasFocus);
Assert.Equal ("Test", view.Text);
btnOk.IsDefault = false;
btnCancel.IsDefault = true;
Assert.True (Application.OnKeyDown (new Key (KeyCode.Enter)));
Assert.True (view.HasFocus);
Assert.Equal ("", view.Text);
Application.End (rs);
}
}