Files
Terminal.Gui/Terminal.Gui/Views/TimeField.cs
Thomas Nind ea7981dc59 Adds Key Binding support. Also refactors Autocomplete and Undo/Redo. (#1556)
* Refactored ProcessKey to use public methods for case logic

* Added KeyBinding class

* Refactored key binding to split key->command from command->implementation

This reduces duplication and simplifies the API

* Finishing key bindings implementation in ListView.

* Adding more unit tests to the ListView.

* Added key bindings to the Button and more features.

* Replaces Action for Func<KeyEvent, bool> on CommandImplementations.

* Allowing commands to have any number of arguments.

* Implementing key bindings on Checkbox view.

* Added test for changing HotKey in Button and made ReplaceKeyBinding protected

* Changed `CommandImplementations` to `Func<KeyEvent, bool>` to better understand current command implementations

* Implementing key bindings in ComboBox.

* Renamed Command keys and fixed ComboBox issues:

- Fixed pressing Esc in ListAndCombos scenario without selecting cause an array out of bounds error
- Changed the Esc key in ComboBox to also collapse the list selection
- Added bool return to public virtual method Expand and Collapse (this is a breaking change)

* Implementing key bindings in DateField.

* Organizing some things.

* Implementing key bindings on TimeField.

* No key bindings on FrameView.

* Added keybinding support to TreeView

* Added mouse support and more features.

* Updating NuGet packages.

* Putting text on the same line.

* Changing function command to Func<bool>.

* Added a read only Position, CursorPosition properties and events.

* Keybindings for GraphView

* Added a stream argument to ApplyEdits to only save the edits.

* Implementing key bindings on the HexView.

* Added MenuOpened event and others bug fixes.

* Fixing typo.

* Unifying constructors initializations.

* Implementing keybindings in the Menu.

* Removing unnecessary variable.

* Implementing keybindings in RadioGroup view.

* Changing Home to TopHome and End to BottomEnd.

* Implementing keybindings in the ScrollView.

* Changing the PageLeft and PageRight keybindings.

* Fixing PageLeft and RightPage.

* Removing CleanUp command.

* Key bindings for TabView

* Keybindings for TableView

* Fixed unit tests for PageDown to correctly assign input focus to the TableView

* Fixes the CalculateLeftColumn method avoiding jump two columns on forward moving.

* Fixes #1525. Gives the same backspace behavior as TextView.

* Changes kill-to-start key to work on Linux too.

* Fixes SelectedStart, SelectedText and some cleaning.

* Implementing keybindings in TextField.

* Updated command names and merged as discussed with @BDisp

- Merged LeftItem and LeftChar to Left (same for Right).
- Also renamed Kill to Cut
- Added ScrollLeft / ScrollRight (and renamed ScrollLineUp to just ScrollUp

* Renamed Command.InsertChar to ToggleOverwriteMode and added Enable/Disable

* Removed 'Mode' suffix from toggle overwrite

* Allows navigation to outside a TextView if IsMdiContainer is true.

* Implementing keybindings in Toplevel.

* Fixing null reference exception.

* Changing to keys instances events instead static.

* Transferring the events to the Toplevel.

* Implementing keybindings in TextView.

* Removing static from the QuitKeyChanged and adding unit test.

* Replacing Added with the Initialized event.

* Ignore control characters and other special keys.

* Changing InvokeKeybindings to return Nullable bool and added two more keys to the Toplevel.

* Implementing keybindings in Autocomplete. I had to derive from View.

* Added keybindings menu item to UICatalog

* Added ClearBinding

* Implementing IAutocomplete, abstract Autocomplete and derived TextViewAutocomplete.

* Implementing keybindings in the TextValidateProvider

* Add keybinding to CellActivationKey.

* Fixing some formats.

* Add ObjectActivationKey to the keybindings.

* Made it much easier to implement abstract base `Autocomplete` in other views by moving methods up out of `TextViewAutocomplete` implementation

* Allowing Autocomplete to popup inside or outside the container.

* Fixes the cursor not being showing if the text length is equal to the view width.

* A unit test to prove the 4df5897.

* Removed unused method `GetCursorPosition` from Autocomplete

* Trimmed down implementation specific methods from IAutocomplete

* Fixed xmldoc comment tag

* Format Autocomplete on multiline and fixes wrap settings.

* Adding keys from a to z to avoid the Key.Space on ToString.

* Fixes the vertical position outside the container.

* Adding more key unit tests.

* Changing comment to upper case and proving that doesn't will breaking nothing.

* Replaces Pos.Bottom to Pos.AnchorEnd.

* Fixes popup on resizing.

* Should only using the Pos.Bottom to position outside the view.

* Fixes #1584

* Fixes https://github.com/migueldeicaza/gui.cs/issues/1584#issuecomment-1027987475

* Fixes some bugs with SelectedItem.

* Command must also return a nullable bool.

* Ensures updating the ComboBox text on leaving the control.

* Only with the nullable bool was possible to make the MoveUp and the MoveDown working.

* Added logging of which scenario failed in test

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2022-02-08 10:40:40 -08:00

342 lines
8.5 KiB
C#

//
// TimeField.cs: text entry for time
//
// Author: Jörg Preiß
//
// Licensed under the MIT license
using System;
using System.Globalization;
using System.Linq;
using NStack;
namespace Terminal.Gui {
/// <summary>
/// Time editing <see cref="View"/>
/// </summary>
/// <remarks>
/// The <see cref="TimeField"/> <see cref="View"/> provides time editing functionality with mouse support.
/// </remarks>
public class TimeField : TextField {
TimeSpan time;
bool isShort;
int longFieldLen = 8;
int shortFieldLen = 5;
string sepChar;
string longFormat;
string shortFormat;
int fieldLen => isShort ? shortFieldLen : longFieldLen;
string format => isShort ? shortFormat : longFormat;
/// <summary>
/// TimeChanged event, raised when the Date has changed.
/// </summary>
/// <remarks>
/// This event is raised when the <see cref="Time"/> changes.
/// </remarks>
/// <remarks>
/// The passed <see cref="EventArgs"/> is a <see cref="DateTimeEventArgs{T}"/> containing the old value, new value, and format string.
/// </remarks>
public event Action<DateTimeEventArgs<TimeSpan>> TimeChanged;
/// <summary>
/// Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Absolute"/> positioning.
/// </summary>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="time">Initial time.</param>
/// <param name="isShort">If true, the seconds are hidden. Sets the <see cref="IsShortFormat"/> property.</param>
public TimeField (int x, int y, TimeSpan time, bool isShort = false) : base (x, y, isShort ? 7 : 10, "")
{
Initialize (time, isShort);
}
/// <summary>
/// Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Computed"/> positioning.
/// </summary>
/// <param name="time">Initial time</param>
public TimeField (TimeSpan time) : base (string.Empty)
{
Width = fieldLen + 2;
Initialize (time);
}
/// <summary>
/// Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Computed"/> positioning.
/// </summary>
public TimeField () : this (time: TimeSpan.MinValue) { }
void Initialize (TimeSpan time, bool isShort = false)
{
CultureInfo cultureInfo = CultureInfo.CurrentCulture;
sepChar = cultureInfo.DateTimeFormat.TimeSeparator;
longFormat = $" hh\\{sepChar}mm\\{sepChar}ss";
shortFormat = $" hh\\{sepChar}mm";
this.isShort = isShort;
Time = time;
CursorPosition = 1;
TextChanged += TextField_TextChanged;
// Things this view knows how to do
AddCommand (Command.DeleteCharRight, () => { DeleteCharRight (); return true; });
AddCommand (Command.DeleteCharLeft, () => { DeleteCharLeft (); return true; });
AddCommand (Command.LeftHome, () => MoveHome ());
AddCommand (Command.Left, () => MoveLeft ());
AddCommand (Command.RightEnd, () => MoveEnd ());
AddCommand (Command.Right, () => MoveRight ());
// Default keybindings for this view
AddKeyBinding (Key.DeleteChar, Command.DeleteCharRight);
AddKeyBinding (Key.D | Key.CtrlMask, Command.DeleteCharRight);
AddKeyBinding (Key.Delete, Command.DeleteCharLeft);
AddKeyBinding (Key.Backspace, Command.DeleteCharLeft);
AddKeyBinding (Key.Home, Command.LeftHome);
AddKeyBinding (Key.A | Key.CtrlMask, Command.LeftHome);
AddKeyBinding (Key.CursorLeft, Command.Left);
AddKeyBinding (Key.B | Key.CtrlMask, Command.Left);
AddKeyBinding (Key.End, Command.RightEnd);
AddKeyBinding (Key.E | Key.CtrlMask, Command.RightEnd);
AddKeyBinding (Key.CursorRight, Command.Right);
AddKeyBinding (Key.F | Key.CtrlMask, Command.Right);
}
void TextField_TextChanged (ustring e)
{
try {
if (!TimeSpan.TryParseExact (Text.ToString ().Trim (), format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result))
Text = e;
} catch (Exception) {
Text = e;
}
}
/// <summary>
/// Gets or sets the time of the <see cref="TimeField"/>.
/// </summary>
/// <remarks>
/// </remarks>
public TimeSpan Time {
get {
return time;
}
set {
if (ReadOnly)
return;
var oldTime = time;
time = value;
this.Text = " " + value.ToString (format.Trim ());
var args = new DateTimeEventArgs<TimeSpan> (oldTime, value, format);
if (oldTime != value) {
OnTimeChanged (args);
}
}
}
/// <summary>
/// Get or sets whether <see cref="TimeField"/> uses the short or long time format.
/// </summary>
public bool IsShortFormat {
get => isShort;
set {
isShort = value;
if (isShort)
Width = 7;
else
Width = 10;
var ro = ReadOnly;
if (ro)
ReadOnly = false;
SetText (Text);
ReadOnly = ro;
SetNeedsDisplay ();
}
}
/// <inheritdoc/>
public override int CursorPosition {
get => base.CursorPosition;
set {
base.CursorPosition = Math.Max (Math.Min (value, fieldLen), 1);
}
}
bool SetText (Rune key)
{
var text = TextModel.ToRunes (Text);
var newText = text.GetRange (0, CursorPosition);
newText.Add (key);
if (CursorPosition < fieldLen)
newText = newText.Concat (text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList ();
return SetText (ustring.Make (newText));
}
bool SetText (ustring text)
{
if (text.IsEmpty) {
return false;
}
ustring [] vals = text.Split (ustring.Make (sepChar));
bool isValidTime = true;
int hour = Int32.Parse (vals [0].ToString ());
int minute = Int32.Parse (vals [1].ToString ());
int second = isShort ? 0 : vals.Length > 2 ? Int32.Parse (vals [2].ToString ()) : 0;
if (hour < 0) {
isValidTime = false;
hour = 0;
vals [0] = "0";
} else if (hour > 23) {
isValidTime = false;
hour = 23;
vals [0] = "23";
}
if (minute < 0) {
isValidTime = false;
minute = 0;
vals [1] = "0";
} else if (minute > 59) {
isValidTime = false;
minute = 59;
vals [1] = "59";
}
if (second < 0) {
isValidTime = false;
second = 0;
vals [2] = "0";
} else if (second > 59) {
isValidTime = false;
second = 59;
vals [2] = "59";
}
string t = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}";
if (!TimeSpan.TryParseExact (t.Trim (), format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result) ||
!isValidTime)
return false;
Time = result;
return true;
}
void IncCursorPosition ()
{
if (CursorPosition == fieldLen)
return;
if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
CursorPosition++;
}
void DecCursorPosition ()
{
if (CursorPosition == 1)
return;
if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
CursorPosition--;
}
void AdjCursorPosition ()
{
if (Text [CursorPosition] == sepChar.ToCharArray () [0])
CursorPosition++;
}
///<inheritdoc/>
public override bool ProcessKey (KeyEvent kb)
{
var result = InvokeKeybindings (kb);
if (result != null)
return (bool)result;
// Ignore non-numeric characters.
if (kb.Key < (Key)((int)Key.D0) || kb.Key > (Key)((int)Key.D9))
return false;
if (ReadOnly)
return true;
if (SetText (TextModel.ToRunes (ustring.Make ((uint)kb.Key)).First ()))
IncCursorPosition ();
return true;
}
bool MoveRight ()
{
IncCursorPosition ();
return true;
}
bool MoveEnd ()
{
CursorPosition = fieldLen;
return true;
}
bool MoveLeft ()
{
DecCursorPosition ();
return true;
}
bool MoveHome ()
{
// Home, C-A
CursorPosition = 1;
return true;
}
/// <inheritdoc/>
public override void DeleteCharLeft (bool useOldCursorPos = true)
{
if (ReadOnly)
return;
SetText ('0');
DecCursorPosition ();
return;
}
/// <inheritdoc/>
public override void DeleteCharRight ()
{
if (ReadOnly)
return;
SetText ('0');
return;
}
///<inheritdoc/>
public override bool MouseEvent (MouseEvent ev)
{
if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
return false;
if (!HasFocus)
SetFocus ();
var point = ev.X;
if (point > fieldLen)
point = fieldLen;
if (point < 1)
point = 1;
CursorPosition = point;
AdjCursorPosition ();
return true;
}
/// <summary>
/// Event firing method that invokes the <see cref="TimeChanged"/> event.
/// </summary>
/// <param name="args">The event arguments</param>
public virtual void OnTimeChanged (DateTimeEventArgs<TimeSpan> args)
{
TimeChanged?.Invoke (args);
}
}
}