123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- //
- // TimeField.cs: text entry for time
- //
- // Author: Jörg Preiß
- //
- // Licensed under the MIT license
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- 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 EventHandler<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, "")
- {
- SetInitialProperties (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;
- SetInitialProperties (time);
- }
- /// <summary>
- /// Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Computed"/> positioning.
- /// </summary>
- public TimeField () : this (time: TimeSpan.MinValue) { }
- void SetInitialProperties (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;
- TextChanging += TextField_TextChanging;
- // Things this view knows how to do
- AddCommand (Command.DeleteCharRight, () => { DeleteCharRight (); return true; });
- AddCommand (Command.DeleteCharLeft, () => { DeleteCharLeft (false); return true; });
- AddCommand (Command.LeftHome, () => MoveHome ());
- AddCommand (Command.Left, () => MoveLeft ());
- AddCommand (Command.RightEnd, () => MoveEnd ());
- AddCommand (Command.Right, () => MoveRight ());
- // Default keybindings for this view
- KeyBindings.Add (KeyCode.Delete, Command.DeleteCharRight);
- KeyBindings.Add (Key.D.WithCtrl, Command.DeleteCharRight);
- KeyBindings.Add (Key.Backspace, Command.DeleteCharLeft);
- KeyBindings.Add (Key.D.WithAlt, Command.DeleteCharLeft);
- KeyBindings.Add (Key.Home, Command.LeftHome);
- KeyBindings.Add (Key.A.WithCtrl, Command.LeftHome);
- KeyBindings.Add (Key.CursorLeft, Command.Left);
- KeyBindings.Add (Key.B.WithCtrl, Command.Left);
- KeyBindings.Add (Key.End, Command.RightEnd);
- KeyBindings.Add (Key.E.WithCtrl, Command.RightEnd);
- KeyBindings.Add (Key.CursorRight, Command.Right);
- KeyBindings.Add (Key.F.WithCtrl, Command.Right);
- }
- void TextField_TextChanging (object sender, TextChangingEventArgs e)
- {
- try {
- int spaces = 0;
- for (int i = 0; i < e.NewText.Length; i++) {
- if (e.NewText [i] == ' ') {
- spaces++;
- } else {
- break;
- }
- }
- spaces += _fieldLen;
- string trimedText = e.NewText [..spaces];
- spaces -= _fieldLen;
- trimedText = trimedText.Replace (new string (' ', spaces), " ");
- if (trimedText != e.NewText) {
- e.NewText = trimedText;
- }
- if (!TimeSpan.TryParseExact (e.NewText.Trim (), _format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result)) {
- e.Cancel = true;
- }
- AdjCursorPosition (CursorPosition, true);
- } catch (Exception) {
- e.Cancel = true;
- }
- }
- /// <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 = Text.EnumerateRunes ().ToList ();
- var newText = text.GetRange (0, CursorPosition);
- newText.Add (key);
- if (CursorPosition < _fieldLen)
- newText = [.. newText, .. text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))];
- return SetText (StringExtensions.ToString (newText));
- }
- bool SetText (string text)
- {
- if (string.IsNullOrEmpty (text)) {
- return false;
- }
- text = NormalizeFormat (text);
- string [] vals = text.Split (_sepChar);
- bool isValidTime = true;
- int hour = Int32.Parse (vals [0]);
- int minute = Int32.Parse (vals [1]);
- int second = _isShort ? 0 : vals.Length > 2 ? Int32.Parse (vals [2]) : 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;
- }
- string NormalizeFormat (string text, string fmt = null, string sepChar = null)
- {
- if (string.IsNullOrEmpty (fmt)) {
- fmt = _format;
- }
- fmt = fmt.Replace ("\\", "");
- if (string.IsNullOrEmpty (sepChar)) {
- sepChar = _sepChar;
- }
- if (fmt.Length != text.Length) {
- return text;
- }
- var fmtText = text.ToCharArray ();
- for (int i = 0; i < text.Length; i++) {
- var c = fmt [i];
- if (c.ToString () == sepChar && text [i].ToString () != sepChar) {
- fmtText [i] = c;
- }
- }
- return new string (fmtText);
- }
- void IncCursorPosition ()
- {
- if (CursorPosition >= _fieldLen) {
- CursorPosition = _fieldLen;
- return;
- }
- CursorPosition++;
- AdjCursorPosition (CursorPosition);
- }
- void DecCursorPosition ()
- {
- if (CursorPosition <= 1) {
- CursorPosition = 1;
- return;
- }
- CursorPosition--;
- AdjCursorPosition (CursorPosition, false);
- }
- void AdjCursorPosition (int point, bool increment = true)
- {
- var newPoint = point;
- if (point > _fieldLen) {
- newPoint = _fieldLen;
- }
- if (point < 1) {
- newPoint = 1;
- }
- if (newPoint != point) {
- CursorPosition = newPoint;
- }
- while (Text [CursorPosition] == _sepChar [0]) {
- if (increment) {
- CursorPosition++;
- } else {
- CursorPosition--;
- }
- }
- }
- ///<inheritdoc/>
- public override bool OnProcessKeyDown (Key a)
- {
- // Ignore non-numeric characters.
- if (a.KeyCode is >= (KeyCode)(int)KeyCode.D0 and <= (KeyCode)(int)KeyCode.D9) {
- if (!ReadOnly) {
- if (SetText ((Rune)a)) {
- IncCursorPosition ();
- }
- }
- return true;
- }
- return false;
- }
- bool MoveRight ()
- {
- ClearAllSelection ();
- IncCursorPosition ();
- return true;
- }
- new bool MoveEnd ()
- {
- ClearAllSelection ();
- CursorPosition = _fieldLen;
- return true;
- }
- bool MoveLeft ()
- {
- ClearAllSelection ();
- DecCursorPosition ();
- return true;
- }
- bool MoveHome ()
- {
- // Home, C-A
- ClearAllSelection ();
- CursorPosition = 1;
- return true;
- }
- /// <inheritdoc/>
- public override void DeleteCharLeft (bool useOldCursorPos = true)
- {
- if (ReadOnly) {
- return;
- }
- ClearAllSelection ();
- SetText ((Rune)'0');
- DecCursorPosition ();
- return;
- }
- /// <inheritdoc/>
- public override void DeleteCharRight ()
- {
- if (ReadOnly) {
- return;
- }
- ClearAllSelection ();
- SetText ((Rune)'0');
- return;
- }
- ///<inheritdoc/>
- public override bool MouseEvent (MouseEvent ev)
- {
- var result = base.MouseEvent (ev);
- if (result && SelectedLength == 0 && ev.Flags.HasFlag (MouseFlags.Button1Pressed)) {
- int point = ev.X;
- AdjCursorPosition (point, true);
- }
- return result;
- }
- /// <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 (this, args);
- }
- }
|