HexView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. //
  2. // HexView.cs: A hexadecimal viewer
  3. //
  4. // TODO:
  5. // - Support searching and highlighting of the search result
  6. // - Bug showing the last line
  7. //
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. namespace Terminal.Gui;
  13. /// <summary>
  14. /// An hex viewer and editor <see cref="View"/> over a <see cref="System.IO.Stream"/>
  15. /// </summary>
  16. /// <remarks>
  17. /// <para>
  18. /// <see cref="HexView"/> provides a hex editor on top of a seekable <see cref="Stream"/> with the left side showing an hex
  19. /// dump of the values in the <see cref="Stream"/> and the right side showing the contents (filtered to
  20. /// non-control sequence ASCII characters).
  21. /// </para>
  22. /// <para>
  23. /// Users can switch from one side to the other by using the tab key.
  24. /// </para>
  25. /// <para>
  26. /// To enable editing, set <see cref="AllowEdits"/> to true. When <see cref="AllowEdits"/> is true
  27. /// the user can make changes to the hexadecimal values of the <see cref="Stream"/>. Any changes are tracked
  28. /// in the <see cref="Edits"/> property (a <see cref="SortedDictionary{TKey, TValue}"/>) indicating
  29. /// the position where the changes were made and the new values. A convenience method, <see cref="ApplyEdits"/>
  30. /// will apply the edits to the <see cref="Stream"/>.
  31. /// </para>
  32. /// <para>
  33. /// Control the first byte shown by setting the <see cref="DisplayStart"/> property
  34. /// to an offset in the stream.
  35. /// </para>
  36. /// </remarks>
  37. public partial class HexView : View {
  38. SortedDictionary<long, byte> edits = new SortedDictionary<long, byte> ();
  39. Stream source;
  40. long displayStart, pos;
  41. bool firstNibble, leftSide;
  42. long position {
  43. get => pos;
  44. set {
  45. pos = value;
  46. OnPositionChanged ();
  47. }
  48. }
  49. /// <summary>
  50. /// Initializes a <see cref="HexView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  51. /// </summary>
  52. /// <param name="source">The <see cref="Stream"/> to view and edit as hex, this <see cref="Stream"/> must support seeking, or an exception will be thrown.</param>
  53. public HexView (Stream source) : base ()
  54. {
  55. Source = source;
  56. CanFocus = true;
  57. leftSide = true;
  58. firstNibble = true;
  59. // Things this view knows how to do
  60. AddCommand (Command.Left, () => MoveLeft ());
  61. AddCommand (Command.Right, () => MoveRight ());
  62. AddCommand (Command.LineDown, () => MoveDown (bytesPerLine));
  63. AddCommand (Command.LineUp, () => MoveUp (bytesPerLine));
  64. AddCommand (Command.ToggleChecked, () => ToggleSide ());
  65. AddCommand (Command.PageUp, () => MoveUp (bytesPerLine * Frame.Height));
  66. AddCommand (Command.PageDown, () => MoveDown (bytesPerLine * Frame.Height));
  67. AddCommand (Command.TopHome, () => MoveHome ());
  68. AddCommand (Command.BottomEnd, () => MoveEnd ());
  69. AddCommand (Command.StartOfLine, () => MoveStartOfLine ());
  70. AddCommand (Command.EndOfLine, () => MoveEndOfLine ());
  71. AddCommand (Command.StartOfPage, () => MoveUp (bytesPerLine * ((int)(position - displayStart) / bytesPerLine)));
  72. AddCommand (Command.EndOfPage, () => MoveDown (bytesPerLine * (Frame.Height - 1 - (int)(position - displayStart) / bytesPerLine)));
  73. // Default keybindings for this view
  74. KeyBindings.Add (KeyCode.CursorLeft, Command.Left);
  75. KeyBindings.Add (KeyCode.CursorRight, Command.Right);
  76. KeyBindings.Add (KeyCode.CursorDown, Command.LineDown);
  77. KeyBindings.Add (KeyCode.CursorUp, Command.LineUp);
  78. KeyBindings.Add (KeyCode.Enter, Command.ToggleChecked);
  79. KeyBindings.Add ('v' + KeyCode.AltMask, Command.PageUp);
  80. KeyBindings.Add (KeyCode.PageUp, Command.PageUp);
  81. KeyBindings.Add (KeyCode.V | KeyCode.CtrlMask, Command.PageDown);
  82. KeyBindings.Add (KeyCode.PageDown, Command.PageDown);
  83. KeyBindings.Add (KeyCode.Home, Command.TopHome);
  84. KeyBindings.Add (KeyCode.End, Command.BottomEnd);
  85. KeyBindings.Add (KeyCode.CursorLeft | KeyCode.CtrlMask, Command.StartOfLine);
  86. KeyBindings.Add (KeyCode.CursorRight | KeyCode.CtrlMask, Command.EndOfLine);
  87. KeyBindings.Add (KeyCode.CursorUp | KeyCode.CtrlMask, Command.StartOfPage);
  88. KeyBindings.Add (KeyCode.CursorDown | KeyCode.CtrlMask, Command.EndOfPage);
  89. LayoutComplete += HexView_LayoutComplete;
  90. }
  91. private void HexView_LayoutComplete (object sender, LayoutEventArgs e)
  92. {
  93. // Small buffers will just show the position, with the bsize field value (4 bytes)
  94. bytesPerLine = bsize;
  95. if (Bounds.Width - displayWidth > 17) {
  96. bytesPerLine = bsize * ((Bounds.Width - displayWidth) / 18);
  97. }
  98. }
  99. /// <summary>
  100. /// Initializes a <see cref="HexView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  101. /// </summary>
  102. public HexView () : this (source: new MemoryStream ()) { }
  103. /// <summary>
  104. /// Event to be invoked when an edit is made on the <see cref="Stream"/>.
  105. /// </summary>
  106. public event EventHandler<HexViewEditEventArgs> Edited;
  107. /// <summary>
  108. /// Event to be invoked when the position and cursor position changes.
  109. /// </summary>
  110. public event EventHandler<HexViewEventArgs> PositionChanged;
  111. /// <summary>
  112. /// Sets or gets the <see cref="Stream"/> the <see cref="HexView"/> is operating on; the stream must support seeking (<see cref="Stream.CanSeek"/> == true).
  113. /// </summary>
  114. /// <value>The source.</value>
  115. public Stream Source {
  116. get => source;
  117. set {
  118. if (value == null) {
  119. throw new ArgumentNullException ("source");
  120. }
  121. if (!value.CanSeek) {
  122. throw new ArgumentException ("The source stream must be seekable (CanSeek property)", "source");
  123. }
  124. source = value;
  125. if (displayStart > source.Length) {
  126. DisplayStart = 0;
  127. }
  128. if (position > source.Length) {
  129. position = 0;
  130. }
  131. SetNeedsDisplay ();
  132. }
  133. }
  134. internal void SetDisplayStart (long value)
  135. {
  136. if (value > 0 && value >= source.Length) {
  137. displayStart = source.Length - 1;
  138. } else if (value < 0) {
  139. displayStart = 0;
  140. } else {
  141. displayStart = value;
  142. }
  143. SetNeedsDisplay ();
  144. }
  145. /// <summary>
  146. /// Sets or gets the offset into the <see cref="Stream"/> that will displayed at the top of the <see cref="HexView"/>
  147. /// </summary>
  148. /// <value>The display start.</value>
  149. public long DisplayStart {
  150. get => displayStart;
  151. set {
  152. position = value;
  153. SetDisplayStart (value);
  154. }
  155. }
  156. const int displayWidth = 9;
  157. const int bsize = 4;
  158. int bpl;
  159. int bytesPerLine {
  160. get => bpl;
  161. set {
  162. bpl = value;
  163. OnPositionChanged ();
  164. }
  165. }
  166. //// BUGBUG: This should be Bounds. Or, even better use View.LayoutComplete event
  167. ///// <inheritdoc/>
  168. //public override Rect Frame {
  169. // get => base.Frame;
  170. // set {
  171. // base.Frame = value;
  172. // // Small buffers will just show the position, with the bsize field value (4 bytes)
  173. // bytesPerLine = bsize;
  174. // if (value.Width - displayWidth > 17) {
  175. // bytesPerLine = bsize * ((value.Width - displayWidth) / 18);
  176. // }
  177. // }
  178. //}
  179. //
  180. // This is used to support editing of the buffer on a peer List<>,
  181. // the offset corresponds to an offset relative to DisplayStart, and
  182. // the buffer contains the contents of a screenful of data, so the
  183. // offset is relative to the buffer.
  184. //
  185. //
  186. byte GetData (byte [] buffer, int offset, out bool edited)
  187. {
  188. long pos = DisplayStart + offset;
  189. if (edits.TryGetValue (pos, out byte v)) {
  190. edited = true;
  191. return v;
  192. }
  193. edited = false;
  194. return buffer [offset];
  195. }
  196. ///<inheritdoc/>
  197. public override void OnDrawContent (Rect contentArea)
  198. {
  199. Attribute currentAttribute;
  200. var current = ColorScheme.Focus;
  201. Driver.SetAttribute (current);
  202. Move (0, 0);
  203. // BUGBUG: Bounds!!!!
  204. var frame = Frame;
  205. int nblocks = bytesPerLine / bsize;
  206. byte [] data = new byte [nblocks * bsize * frame.Height];
  207. Source.Position = displayStart;
  208. int n = source.Read (data, 0, data.Length);
  209. var activeColor = ColorScheme.HotNormal;
  210. var trackingColor = ColorScheme.HotFocus;
  211. for (int line = 0; line < frame.Height; line++) {
  212. var lineRect = new Rect (0, line, frame.Width, 1);
  213. if (!Bounds.Contains (lineRect)) {
  214. continue;
  215. }
  216. Move (0, line);
  217. Driver.SetAttribute (ColorScheme.HotNormal);
  218. Driver.AddStr (string.Format ("{0:x8} ", displayStart + line * nblocks * bsize));
  219. currentAttribute = ColorScheme.HotNormal;
  220. SetAttribute (GetNormalColor ());
  221. for (int block = 0; block < nblocks; block++) {
  222. for (int b = 0; b < bsize; b++) {
  223. int offset = line * nblocks * bsize + block * bsize + b;
  224. byte value = GetData (data, offset, out bool edited);
  225. if (offset + displayStart == position || edited) {
  226. SetAttribute (leftSide ? activeColor : trackingColor);
  227. } else {
  228. SetAttribute (GetNormalColor ());
  229. }
  230. Driver.AddStr (offset >= n && !edited ? " " : string.Format ("{0:x2}", value));
  231. SetAttribute (GetNormalColor ());
  232. Driver.AddRune ((Rune)' ');
  233. }
  234. Driver.AddStr (block + 1 == nblocks ? " " : "| ");
  235. }
  236. for (int bitem = 0; bitem < nblocks * bsize; bitem++) {
  237. int offset = line * nblocks * bsize + bitem;
  238. byte b = GetData (data, offset, out bool edited);
  239. Rune c;
  240. if (offset >= n && !edited) {
  241. c = (Rune)' ';
  242. } else {
  243. if (b < 32) {
  244. c = (Rune)'.';
  245. } else if (b > 127) {
  246. c = (Rune)'.';
  247. } else {
  248. Rune.DecodeFromUtf8 (new ReadOnlySpan<byte> (ref b), out c, out _);
  249. }
  250. }
  251. if (offset + displayStart == position || edited) {
  252. SetAttribute (leftSide ? trackingColor : activeColor);
  253. } else {
  254. SetAttribute (GetNormalColor ());
  255. }
  256. Driver.AddRune (c);
  257. }
  258. }
  259. void SetAttribute (Attribute attribute)
  260. {
  261. if (currentAttribute != attribute) {
  262. currentAttribute = attribute;
  263. Driver.SetAttribute (attribute);
  264. }
  265. }
  266. }
  267. ///<inheritdoc/>
  268. public override void PositionCursor ()
  269. {
  270. int delta = (int)(position - displayStart);
  271. int line = delta / bytesPerLine;
  272. int item = delta % bytesPerLine;
  273. int block = item / bsize;
  274. int column = item % bsize * 3;
  275. if (leftSide) {
  276. Move (displayWidth + block * 14 + column + (firstNibble ? 0 : 1), line);
  277. } else {
  278. Move (displayWidth + bytesPerLine / bsize * 14 + item - 1, line);
  279. }
  280. }
  281. void RedisplayLine (long pos)
  282. {
  283. int delta = (int)(pos - DisplayStart);
  284. int line = delta / bytesPerLine;
  285. // BUGBUG: Bounds!
  286. SetNeedsDisplay (new Rect (0, line, Frame.Width, 1));
  287. }
  288. bool MoveEndOfLine ()
  289. {
  290. position = Math.Min (position / bytesPerLine * bytesPerLine + bytesPerLine - 1, source.Length);
  291. SetNeedsDisplay ();
  292. return true;
  293. }
  294. bool MoveStartOfLine ()
  295. {
  296. position = position / bytesPerLine * bytesPerLine;
  297. SetNeedsDisplay ();
  298. return true;
  299. }
  300. bool MoveEnd ()
  301. {
  302. position = source.Length;
  303. // BUGBUG: Bounds!
  304. if (position >= DisplayStart + bytesPerLine * Frame.Height) {
  305. SetDisplayStart (position);
  306. SetNeedsDisplay ();
  307. } else {
  308. RedisplayLine (position);
  309. }
  310. return true;
  311. }
  312. bool MoveHome ()
  313. {
  314. DisplayStart = 0;
  315. SetNeedsDisplay ();
  316. return true;
  317. }
  318. bool ToggleSide ()
  319. {
  320. leftSide = !leftSide;
  321. RedisplayLine (position);
  322. firstNibble = true;
  323. return true;
  324. }
  325. bool MoveLeft ()
  326. {
  327. RedisplayLine (position);
  328. if (leftSide) {
  329. if (!firstNibble) {
  330. firstNibble = true;
  331. return true;
  332. }
  333. firstNibble = false;
  334. }
  335. if (position == 0) {
  336. return true;
  337. }
  338. if (position - 1 < DisplayStart) {
  339. SetDisplayStart (displayStart - bytesPerLine);
  340. SetNeedsDisplay ();
  341. } else {
  342. RedisplayLine (position);
  343. }
  344. position--;
  345. return true;
  346. }
  347. bool MoveRight ()
  348. {
  349. RedisplayLine (position);
  350. if (leftSide) {
  351. if (firstNibble) {
  352. firstNibble = false;
  353. return true;
  354. } else {
  355. firstNibble = true;
  356. }
  357. }
  358. if (position < source.Length) {
  359. position++;
  360. }
  361. // BUGBUG: Bounds!
  362. if (position >= DisplayStart + bytesPerLine * Frame.Height) {
  363. SetDisplayStart (DisplayStart + bytesPerLine);
  364. SetNeedsDisplay ();
  365. } else {
  366. RedisplayLine (position);
  367. }
  368. return true;
  369. }
  370. bool MoveUp (int bytes)
  371. {
  372. RedisplayLine (position);
  373. if (position - bytes > -1) {
  374. position -= bytes;
  375. }
  376. if (position < DisplayStart) {
  377. SetDisplayStart (DisplayStart - bytes);
  378. SetNeedsDisplay ();
  379. } else {
  380. RedisplayLine (position);
  381. }
  382. return true;
  383. }
  384. bool MoveDown (int bytes)
  385. {
  386. // BUGBUG: Bounds!
  387. RedisplayLine (position);
  388. if (position + bytes < source.Length) {
  389. position += bytes;
  390. } else if (bytes == bytesPerLine * Frame.Height && source.Length >= DisplayStart + bytesPerLine * Frame.Height
  391. || bytes <= bytesPerLine * Frame.Height - bytesPerLine && source.Length <= DisplayStart + bytesPerLine * Frame.Height) {
  392. long p = position;
  393. while (p + bytesPerLine < source.Length) {
  394. p += bytesPerLine;
  395. }
  396. position = p;
  397. }
  398. if (position >= DisplayStart + bytesPerLine * Frame.Height) {
  399. SetDisplayStart (DisplayStart + bytes);
  400. SetNeedsDisplay ();
  401. } else {
  402. RedisplayLine (position);
  403. }
  404. return true;
  405. }
  406. /// <inheritdoc/>
  407. public override bool OnProcessKeyDown (Key keyEvent)
  408. {
  409. if (!AllowEdits) {
  410. return false;
  411. }
  412. // Ignore control characters and other special keys
  413. if (keyEvent.KeyCode < KeyCode.Space || keyEvent.KeyCode > KeyCode.CharMask) {
  414. return false;
  415. }
  416. if (leftSide) {
  417. int value;
  418. char k = (char)keyEvent.KeyCode;
  419. if (k >= 'A' && k <= 'F') {
  420. value = k - 'A' + 10;
  421. } else if (k >= 'a' && k <= 'f') {
  422. value = k - 'a' + 10;
  423. } else if (k >= '0' && k <= '9') {
  424. value = k - '0';
  425. } else {
  426. return false;
  427. }
  428. byte b;
  429. if (!edits.TryGetValue (position, out b)) {
  430. source.Position = position;
  431. b = (byte)source.ReadByte ();
  432. }
  433. RedisplayLine (position);
  434. if (firstNibble) {
  435. firstNibble = false;
  436. b = (byte)(b & 0xf | value << bsize);
  437. edits [position] = b;
  438. OnEdited (new HexViewEditEventArgs (position, edits [position]));
  439. } else {
  440. b = (byte)(b & 0xf0 | value);
  441. edits [position] = b;
  442. OnEdited (new HexViewEditEventArgs (position, edits [position]));
  443. MoveRight ();
  444. }
  445. return true;
  446. } else {
  447. return false;
  448. }
  449. }
  450. /// <summary>
  451. /// Method used to invoke the <see cref="Edited"/> event passing the <see cref="KeyValuePair{TKey, TValue}"/>.
  452. /// </summary>
  453. /// <param name="e">The key value pair.</param>
  454. public virtual void OnEdited (HexViewEditEventArgs e)
  455. {
  456. Edited?.Invoke (this, e);
  457. }
  458. /// <summary>
  459. /// Method used to invoke the <see cref="PositionChanged"/> event passing the <see cref="HexViewEventArgs"/> arguments.
  460. /// </summary>
  461. public virtual void OnPositionChanged ()
  462. {
  463. PositionChanged?.Invoke (this, new HexViewEventArgs (Position, CursorPosition, BytesPerLine));
  464. }
  465. /// <inheritdoc/>
  466. public override bool MouseEvent (MouseEvent me)
  467. {
  468. // BUGBUG: Test this with a border! Assumes Frame == Bounds!
  469. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  470. && !me.Flags.HasFlag (MouseFlags.WheeledDown) && !me.Flags.HasFlag (MouseFlags.WheeledUp)) {
  471. return false;
  472. }
  473. if (!HasFocus) {
  474. SetFocus ();
  475. }
  476. if (me.Flags == MouseFlags.WheeledDown) {
  477. DisplayStart = Math.Min (DisplayStart + bytesPerLine, source.Length);
  478. return true;
  479. }
  480. if (me.Flags == MouseFlags.WheeledUp) {
  481. DisplayStart = Math.Max (DisplayStart - bytesPerLine, 0);
  482. return true;
  483. }
  484. if (me.X < displayWidth) {
  485. return true;
  486. }
  487. int nblocks = bytesPerLine / bsize;
  488. int blocksSize = nblocks * 14;
  489. int blocksRightOffset = displayWidth + blocksSize - 1;
  490. if (me.X > blocksRightOffset + bytesPerLine - 1) {
  491. return true;
  492. }
  493. leftSide = me.X >= blocksRightOffset;
  494. long lineStart = me.Y * bytesPerLine + displayStart;
  495. int x = me.X - displayWidth + 1;
  496. int block = x / 14;
  497. x -= block * 2;
  498. int empty = x % 3;
  499. int item = x / 3;
  500. if (!leftSide && item > 0 && (empty == 0 || x == block * 14 + 14 - 1 - block * 2)) {
  501. return true;
  502. }
  503. firstNibble = true;
  504. if (leftSide) {
  505. position = Math.Min (lineStart + me.X - blocksRightOffset, source.Length);
  506. } else {
  507. position = Math.Min (lineStart + item, source.Length);
  508. }
  509. if (me.Flags == MouseFlags.Button1DoubleClicked) {
  510. leftSide = !leftSide;
  511. if (leftSide) {
  512. firstNibble = empty == 1;
  513. } else {
  514. firstNibble = true;
  515. }
  516. }
  517. SetNeedsDisplay ();
  518. return true;
  519. }
  520. /// <summary>
  521. /// Gets or sets whether this <see cref="HexView"/> allow editing of the <see cref="Stream"/>
  522. /// of the underlying <see cref="Stream"/>.
  523. /// </summary>
  524. /// <value><c>true</c> if allow edits; otherwise, <c>false</c>.</value>
  525. public bool AllowEdits { get; set; } = true;
  526. /// <summary>
  527. /// Gets a <see cref="SortedDictionary{TKey, TValue}"/> describing the edits done to the <see cref="HexView"/>.
  528. /// Each Key indicates an offset where an edit was made and the Value is the changed byte.
  529. /// </summary>
  530. /// <value>The edits.</value>
  531. public IReadOnlyDictionary<long, byte> Edits => edits;
  532. /// <summary>
  533. /// Gets the current character position starting at one, related to the <see cref="Stream"/>.
  534. /// </summary>
  535. public long Position => position + 1;
  536. /// <summary>
  537. /// Gets the current cursor position starting at one for both, line and column.
  538. /// </summary>
  539. public Point CursorPosition {
  540. get {
  541. int delta = (int)position;
  542. int line = delta / bytesPerLine + 1;
  543. int item = delta % bytesPerLine + 1;
  544. return new Point (item, line);
  545. }
  546. }
  547. /// <summary>
  548. /// The bytes length per line.
  549. /// </summary>
  550. public int BytesPerLine => bytesPerLine;
  551. /// <summary>
  552. /// This method applies and edits made to the <see cref="Stream"/> and resets the
  553. /// contents of the <see cref="Edits"/> property.
  554. /// </summary>
  555. /// <param name="stream">If provided also applies the changes to the passed <see cref="Stream"/></param>.
  556. public void ApplyEdits (Stream stream = null)
  557. {
  558. foreach (var kv in edits) {
  559. source.Position = kv.Key;
  560. source.WriteByte (kv.Value);
  561. source.Flush ();
  562. if (stream != null) {
  563. stream.Position = kv.Key;
  564. stream.WriteByte (kv.Value);
  565. stream.Flush ();
  566. }
  567. }
  568. edits = new SortedDictionary<long, byte> ();
  569. SetNeedsDisplay ();
  570. }
  571. /// <summary>
  572. /// This method discards the edits made to the <see cref="Stream"/> by resetting the
  573. /// contents of the <see cref="Edits"/> property.
  574. /// </summary>
  575. public void DiscardEdits ()
  576. {
  577. edits = new SortedDictionary<long, byte> ();
  578. }
  579. CursorVisibility desiredCursorVisibility = CursorVisibility.Default;
  580. /// <summary>
  581. /// Get / Set the wished cursor when the field is focused
  582. /// </summary>
  583. public CursorVisibility DesiredCursorVisibility {
  584. get => desiredCursorVisibility;
  585. set {
  586. if (desiredCursorVisibility != value && HasFocus) {
  587. Application.Driver.SetCursorVisibility (value);
  588. }
  589. desiredCursorVisibility = value;
  590. }
  591. }
  592. ///<inheritdoc/>
  593. public override bool OnEnter (View view)
  594. {
  595. Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
  596. return base.OnEnter (view);
  597. }
  598. }