Files
Terminal.Gui/UICatalog/Scenarios/ASCIICustomButton.cs
Tig 16055c53b0 Fixes #3039. Fix View.HotKey (#3249)
* Added View.DefaultCommand etc... Started on dedicated scenario

* Fixed un-shifted hotkeys -> Fixed Key Equals. Fixed WindowsDriver passing wrong key. Etc.

* Fixed Key Bindings and HotKeys

* Fixed Key Bindings and HotKeys

* Label now correctly supports hotkey

* Disabled unix hot keys because they are annoying and get in the way

* Updated nuget. fixed warnings

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Changed TextChangingEventArgs to inherit from CancelEventArgs

* TextChangingEventArgs -> TextEventArgs

* Simplified Text events by having only on args class

* Fixed unit tests fail

* Simplified by removing TitleEventArgs

* POC of Title being primary for hotkey. Label and Button hacked to work

* POC of Title being primary for hotkey. Label and Button hacked to work - all unit tests pass

* Dropped Microsoft.NETFramework.ReferenceAssemblies

* Fixed Dialogs scenario hotkeys

* Fixed build warnings

* Fixed Border Title render bug

* Regiggering default command handling

* Regiggering default command handling

* Checkbox clean up

* Added StateEventArgs POC

* Command.Default -> Command.HotKey

* Command.Default -> Command.HotKey - fixed TableView

* Command.Default -> Command.HotKey - fixed TableView

* Updated reactive example

* Fixed Toplevel.BringOverlappedTopToFront - was reordering SubViews when it shouldn't

* WIP - broke

* Finished impl of StateEventArgs

* Deleted ToggleEventArgs.cs. Added StateEventArgs.cs

* XML doc fix

* Removed old code

* Removed commented out code

* Label.Clicked -> Label.Accept (missed this before)

* Removed Labels as Buttons scenario as it's not really  useful

* Moved SubView tests to own file

* Moved SubView tests to own file

* Simplified Text test

* Added OnAccept test

* Deleted DefaultCommand

* Modernized CheckBox

* New button test

* Cleaned up RadioGroup; added tests

* KeyCode->Key in ListView

* Added ListView unit tests

* ListView now does Accept correctly

* TreeView now does Accept correctly

* Cleaned up some TextField tests

* TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Fixed ComboBox to deal with TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Removed un-needed using statement
2024-02-22 15:11:26 -07:00

420 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Terminal.Gui;
namespace UICatalog.Scenarios;
[ScenarioMetadata ("ASCIICustomButtonTest", "ASCIICustomButton sample")]
[ScenarioCategory ("Controls")]
public class ASCIICustomButtonTest : Scenario
{
private static bool _smallerWindow;
private MenuItem _miSmallerWindow;
private ScrollViewTestWindow _scrollViewTestWindow;
public override void Init ()
{
Application.Init ();
_scrollViewTestWindow = new ScrollViewTestWindow ();
var menu = new MenuBar
{
Menus =
[
new MenuBarItem (
"Window Size",
new []
{
_miSmallerWindow =
new MenuItem (
"Smaller Window",
"",
ChangeWindowSize
)
{
CheckType = MenuItemCheckStyle
.Checked
},
null,
new MenuItem (
"Quit",
"",
() => Application.RequestStop (),
null,
null,
(KeyCode)Application.QuitKey
)
}
)
]
};
Application.Top.Add (menu, _scrollViewTestWindow);
Application.Run ();
}
public override void Run () { }
private void ChangeWindowSize ()
{
_smallerWindow = (bool)(_miSmallerWindow.Checked = !_miSmallerWindow.Checked);
_scrollViewTestWindow.Dispose ();
Application.Top.Remove (_scrollViewTestWindow);
_scrollViewTestWindow = new ScrollViewTestWindow ();
Application.Top.Add (_scrollViewTestWindow);
}
public class ASCIICustomButton : Button
{
private FrameView _border;
private Label _fill;
public string Description => $"Description of: {Id}";
public void CustomInitialize ()
{
_border = new FrameView { Width = Width, Height = Height };
AutoSize = false;
var fillText = new StringBuilder ();
for (var i = 0; i < Bounds.Height; i++)
{
if (i > 0)
{
fillText.AppendLine ("");
}
for (var j = 0; j < Bounds.Width; j++)
{
fillText.Append ("█");
}
}
_fill = new Label { Visible = false, CanFocus = false, Text = fillText.ToString () };
var title = new Label { X = Pos.Center (), Y = Pos.Center (), Text = Text };
_border.MouseClick += This_MouseClick;
_fill.MouseClick += This_MouseClick;
title.MouseClick += This_MouseClick;
Add (_border, _fill, title);
}
public override bool OnEnter (View view)
{
_border.Visible = false;
_fill.Visible = true;
PointerEnter.Invoke (this);
view = this;
return base.OnEnter (view);
}
public override bool OnLeave (View view)
{
_border.Visible = true;
_fill.Visible = false;
if (view == null)
{
view = this;
}
return base.OnLeave (view);
}
public override bool OnMouseEvent (MouseEvent mouseEvent)
{
Debug.WriteLine ($"{mouseEvent.Flags}");
if (mouseEvent.Flags == MouseFlags.Button1Clicked)
{
if (!HasFocus && SuperView != null)
{
if (!SuperView.HasFocus)
{
SuperView.SetFocus ();
}
SetFocus ();
SetNeedsDisplay ();
}
OnAccept ();
return true;
}
return base.OnMouseEvent (mouseEvent);
}
public event Action<ASCIICustomButton> PointerEnter;
private void This_MouseClick (object sender, MouseEventEventArgs obj) { OnMouseEvent (obj.MouseEvent); }
}
public class ScrollViewTestWindow : Window
{
private const int BUTTON_HEIGHT = 3;
private const int BUTTON_WIDTH = 25;
private const int BUTTONS_ON_PAGE = 7;
private readonly List<Button> _buttons;
private readonly ScrollView _scrollView;
private ASCIICustomButton _selected;
public ScrollViewTestWindow ()
{
Title = "ScrollViewTestWindow";
Label titleLabel = null;
if (_smallerWindow)
{
Width = 80;
Height = 25;
_scrollView = new ScrollView
{
X = 3,
Y = 1,
Width = 24,
Height = BUTTONS_ON_PAGE * BUTTON_HEIGHT,
ShowVerticalScrollIndicator = true,
ShowHorizontalScrollIndicator = false
};
}
else
{
Width = Dim.Fill ();
Height = Dim.Fill ();
titleLabel = new Label { X = 0, Y = 0, Text = "DOCUMENTS" };
_scrollView = new ScrollView
{
X = 0,
Y = 1,
Width = 27,
Height = BUTTONS_ON_PAGE * BUTTON_HEIGHT,
ShowVerticalScrollIndicator = true,
ShowHorizontalScrollIndicator = false
};
}
_scrollView.KeyBindings.Clear ();
_buttons = new List<Button> ();
Button prevButton = null;
var count = 20;
for (var j = 0; j < count; j++)
{
Pos yPos = prevButton == null ? 0 : Pos.Bottom (prevButton);
var button = new ASCIICustomButton
{
AutoSize = false,
Id = j.ToString (),
Text = $"section {j}",
Y = yPos,
Width = BUTTON_WIDTH,
Height = BUTTON_HEIGHT
};
button.CustomInitialize ();
button.Accept += Button_Clicked;
button.PointerEnter += Button_PointerEnter;
button.MouseClick += Button_MouseClick;
button.KeyDown += Button_KeyPress;
_scrollView.Add (button);
_buttons.Add (button);
prevButton = button;
}
var closeButton = new ASCIICustomButton
{
AutoSize = false,
Id = "close",
Text = "Close",
Y = Pos.Bottom (prevButton),
Width = BUTTON_WIDTH,
Height = BUTTON_HEIGHT
};
closeButton.CustomInitialize ();
closeButton.Accept += Button_Clicked;
closeButton.PointerEnter += Button_PointerEnter;
closeButton.MouseClick += Button_MouseClick;
closeButton.KeyDown += Button_KeyPress;
_scrollView.Add (closeButton);
_buttons.Add (closeButton);
int pages = _buttons.Count / BUTTONS_ON_PAGE;
if (_buttons.Count % BUTTONS_ON_PAGE > 0)
{
pages++;
}
_scrollView.ContentSize = new Size (25, pages * BUTTONS_ON_PAGE * BUTTON_HEIGHT);
if (_smallerWindow)
{
Add (_scrollView);
}
else
{
Add (titleLabel, _scrollView);
}
}
private void Button_Clicked (object sender, EventArgs e)
{
MessageBox.Query ("Button clicked.", $"'{_selected.Text}' clicked!", "Ok");
if (_selected.Text == "Close")
{
Application.RequestStop ();
}
}
private void Button_KeyPress (object sender, Key obj)
{
switch (obj.KeyCode)
{
case KeyCode.End:
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
-(_scrollView.ContentSize.Height
- _scrollView.Frame.Height
+ (_scrollView.ShowHorizontalScrollIndicator ? 1 : 0))
);
obj.Handled = true;
return;
case KeyCode.Home:
_scrollView.ContentOffset = new Point (_scrollView.ContentOffset.X, 0);
obj.Handled = true;
return;
case KeyCode.PageDown:
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
Math.Max (
_scrollView.ContentOffset.Y
- _scrollView.Frame.Height,
-(_scrollView.ContentSize.Height
- _scrollView.Frame.Height
+ (_scrollView.ShowHorizontalScrollIndicator
? 1
: 0))
)
);
obj.Handled = true;
return;
case KeyCode.PageUp:
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
Math.Min (
_scrollView.ContentOffset.Y
+ _scrollView.Frame.Height,
0
)
);
obj.Handled = true;
return;
}
}
private void Button_MouseClick (object sender, MouseEventEventArgs obj)
{
if (obj.MouseEvent.Flags == MouseFlags.WheeledDown)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
_scrollView.ContentOffset.Y - BUTTON_HEIGHT
);
obj.Handled = true;
}
else if (obj.MouseEvent.Flags == MouseFlags.WheeledUp)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
Math.Min (_scrollView.ContentOffset.Y + BUTTON_HEIGHT, 0)
);
obj.Handled = true;
}
}
private void Button_PointerEnter (ASCIICustomButton obj)
{
bool? moveDown;
if (obj.Frame.Y > _selected?.Frame.Y)
{
moveDown = true;
}
else if (obj.Frame.Y < _selected?.Frame.Y)
{
moveDown = false;
}
else
{
moveDown = null;
}
int offSet = _selected != null
? obj.Frame.Y - _selected.Frame.Y + -_scrollView.ContentOffset.Y % BUTTON_HEIGHT
: 0;
_selected = obj;
if (moveDown == true && _selected.Frame.Y + _scrollView.ContentOffset.Y + BUTTON_HEIGHT >= _scrollView.Frame.Height && offSet != BUTTON_HEIGHT)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
Math.Min (
_scrollView.ContentOffset.Y - BUTTON_HEIGHT,
-(_selected.Frame.Y
- _scrollView.Frame.Height
+ BUTTON_HEIGHT)
)
);
}
else if (moveDown == true && _selected.Frame.Y + _scrollView.ContentOffset.Y >= _scrollView.Frame.Height)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
_scrollView.ContentOffset.Y - BUTTON_HEIGHT
);
}
else if (moveDown == true && _selected.Frame.Y + _scrollView.ContentOffset.Y < 0)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
-_selected.Frame.Y
);
}
else if (moveDown == false && _selected.Frame.Y < -_scrollView.ContentOffset.Y)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
Math.Max (
_scrollView.ContentOffset.Y + BUTTON_HEIGHT,
_selected.Frame.Y
)
);
}
else if (moveDown == false && _selected.Frame.Y + _scrollView.ContentOffset.Y > _scrollView.Frame.Height)
{
_scrollView.ContentOffset = new Point (
_scrollView.ContentOffset.X,
-(_selected.Frame.Y - _scrollView.Frame.Height + BUTTON_HEIGHT)
);
}
}
}
}