TimeField.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // TimeField.cs: text entry for time
  3. //
  4. // Author: Jörg Preiß
  5. //
  6. // Licensed under the MIT license
  7. using System;
  8. using System.Globalization;
  9. using System.Linq;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// Time editing <see cref="View"/>
  14. /// </summary>
  15. /// <remarks>
  16. /// The <see cref="TimeField"/> <see cref="View"/> provides time editing functionality with mouse support.
  17. /// </remarks>
  18. public class TimeField : TextField {
  19. bool isShort;
  20. int longFieldLen = 8;
  21. int shortFieldLen = 5;
  22. int FieldLen { get { return isShort ? shortFieldLen : longFieldLen; } }
  23. string sepChar;
  24. string longFormat;
  25. string shortFormat;
  26. string Format { get { return isShort ? shortFormat : longFormat; } }
  27. /// <summary>
  28. /// Initializes a new instance of <see cref="TimeField"/> at an absolute position and fixed size.
  29. /// </summary>
  30. /// <param name="x">The x coordinate.</param>
  31. /// <param name="y">The y coordinate.</param>
  32. /// <param name="time">Initial time contents.</param>
  33. /// <param name="isShort">If true, the seconds are hidden.</param>
  34. public TimeField (int x, int y, DateTime time, bool isShort = false) : base (x, y, isShort ? 7 : 10, "")
  35. {
  36. CultureInfo cultureInfo = CultureInfo.CurrentCulture;
  37. sepChar = cultureInfo.DateTimeFormat.TimeSeparator;
  38. longFormat = $" HH{sepChar}mm{sepChar}ss";
  39. shortFormat = $" HH{sepChar}mm";
  40. this.isShort = isShort;
  41. CursorPosition = 1;
  42. Time = time;
  43. Changed += TimeField_Changed;
  44. }
  45. void TimeField_Changed (object sender, ustring e)
  46. {
  47. if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
  48. Text = e;
  49. }
  50. /// <summary>
  51. /// Gets or sets the time of the <see cref="TimeField"/>.
  52. /// </summary>
  53. /// <remarks>
  54. /// </remarks>
  55. public DateTime Time {
  56. get {
  57. if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) return new DateTime ();
  58. return result;
  59. }
  60. set {
  61. this.Text = value.ToString (Format);
  62. }
  63. }
  64. bool SetText (Rune key)
  65. {
  66. var text = TextModel.ToRunes (Text);
  67. var newText = text.GetRange (0, CursorPosition);
  68. newText.Add (key);
  69. if (CursorPosition < FieldLen)
  70. newText = newText.Concat (text.GetRange (CursorPosition + 1, text.Count - (CursorPosition + 1))).ToList ();
  71. return SetText (ustring.Make (newText));
  72. }
  73. bool SetText (ustring text)
  74. {
  75. ustring [] vals = text.Split (ustring.Make (sepChar));
  76. bool isValidTime = true;
  77. int hour = Int32.Parse (vals [0].ToString ());
  78. int minute = Int32.Parse (vals [1].ToString ());
  79. int second = isShort ? 0 : Int32.Parse (vals [2].ToString ());
  80. if (hour < 0) {
  81. isValidTime = false;
  82. hour = 0;
  83. vals [0] = "0";
  84. } else if (hour > 23) {
  85. isValidTime = false;
  86. hour = 23;
  87. vals [0] = "23";
  88. }
  89. if (minute < 0) {
  90. isValidTime = false;
  91. minute = 0;
  92. vals [1] = "0";
  93. } else if (minute > 59) {
  94. isValidTime = false;
  95. minute = 59;
  96. vals [1] = "59";
  97. }
  98. if (second < 0) {
  99. isValidTime = false;
  100. second = 0;
  101. vals [2] = "0";
  102. } else if (second > 59) {
  103. isValidTime = false;
  104. second = 59;
  105. vals [2] = "59";
  106. }
  107. string time = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}";
  108. Text = time;
  109. if (!DateTime.TryParseExact (text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
  110. !isValidTime)
  111. return false;
  112. return true;
  113. }
  114. void IncCursorPosition ()
  115. {
  116. if (CursorPosition == FieldLen)
  117. return;
  118. if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
  119. CursorPosition++;
  120. }
  121. void DecCursorPosition ()
  122. {
  123. if (CursorPosition == 1)
  124. return;
  125. if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
  126. CursorPosition--;
  127. }
  128. void AdjCursorPosition ()
  129. {
  130. if (Text [CursorPosition] == sepChar.ToCharArray () [0])
  131. CursorPosition++;
  132. }
  133. ///<inheritdoc cref="ProcessKey(KeyEvent)"/>
  134. public override bool ProcessKey (KeyEvent kb)
  135. {
  136. switch (kb.Key) {
  137. case Key.DeleteChar:
  138. case Key.ControlD:
  139. SetText ('0');
  140. break;
  141. case Key.Delete:
  142. case Key.Backspace:
  143. SetText ('0');
  144. DecCursorPosition ();
  145. break;
  146. // Home, C-A
  147. case Key.Home:
  148. case Key.ControlA:
  149. CursorPosition = 1;
  150. break;
  151. case Key.CursorLeft:
  152. case Key.ControlB:
  153. DecCursorPosition ();
  154. break;
  155. case Key.End:
  156. case Key.ControlE: // End
  157. CursorPosition = FieldLen;
  158. break;
  159. case Key.CursorRight:
  160. case Key.ControlF:
  161. IncCursorPosition ();
  162. break;
  163. default:
  164. // Ignore non-numeric characters.
  165. if (kb.Key < (Key)((int)'0') || kb.Key > (Key)((int)'9'))
  166. return false;
  167. if (SetText (TextModel.ToRunes (ustring.Make ((uint)kb.Key)).First ()))
  168. IncCursorPosition ();
  169. return true;
  170. }
  171. return true;
  172. }
  173. ///<inheritdoc cref="MouseEvent(Gui.MouseEvent)"/>
  174. public override bool MouseEvent (MouseEvent ev)
  175. {
  176. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  177. return false;
  178. if (!HasFocus)
  179. SuperView.SetFocus (this);
  180. var point = ev.X;
  181. if (point > FieldLen)
  182. point = FieldLen;
  183. if (point < 1)
  184. point = 1;
  185. CursorPosition = point;
  186. AdjCursorPosition ();
  187. return true;
  188. }
  189. }
  190. }