RadioGroup.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>Displays a list of mutually-exclusive items. Each items can have its own hotkey.</summary>
  4. public class RadioGroup : View, IDesignable, IOrientation
  5. {
  6. /// <summary>
  7. /// Initializes a new instance of the <see cref="RadioGroup"/> class.
  8. /// </summary>
  9. public RadioGroup ()
  10. {
  11. CanFocus = true;
  12. Width = Dim.Auto (DimAutoStyle.Content);
  13. Height = Dim.Auto (DimAutoStyle.Content);
  14. // Select (Space key or mouse click) - The default implementation sets focus. RadioGroup does not.
  15. AddCommand (Command.Select, HandleSelectCommand);
  16. // Accept (Enter key or DoubleClick) - Raise Accept event - DO NOT advance state
  17. AddCommand (Command.Accept, HandleAcceptCommand);
  18. // Hotkey - ctx may indicate a radio item hotkey was pressed. Behavior depends on HasFocus
  19. // If HasFocus and it's this.HotKey invoke Select command - DO NOT raise Accept
  20. // If it's a radio item HotKey select that item and raise Selected event - DO NOT raise Accept
  21. // If nothing is selected, select first and raise Selected event - DO NOT raise Accept
  22. AddCommand (Command.HotKey, HandleHotKeyCommand);
  23. AddCommand (Command.Up, () => HasFocus && MoveUpLeft ());
  24. AddCommand (Command.Down, () => HasFocus && MoveDownRight ());
  25. AddCommand (Command.Start, () => HasFocus && MoveHome ());
  26. AddCommand (Command.End, () => HasFocus && MoveEnd ());
  27. // ReSharper disable once UseObjectOrCollectionInitializer
  28. _orientationHelper = new (this);
  29. _orientationHelper.Orientation = Orientation.Vertical;
  30. SetupKeyBindings ();
  31. // By default, single click is already bound to Command.Select
  32. MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
  33. SubViewLayout += RadioGroup_LayoutStarted;
  34. HighlightStyle = HighlightStyle.PressedOutside | HighlightStyle.Pressed;
  35. }
  36. private bool? HandleHotKeyCommand (ICommandContext? ctx)
  37. {
  38. // If the command did not come from a keyboard event, ignore it
  39. if (ctx is not CommandContext<KeyBinding> keyCommandContext)
  40. {
  41. return false;
  42. }
  43. var item = keyCommandContext.Binding.Data as int?;
  44. if (HasFocus)
  45. {
  46. if (item is null || HotKey == keyCommandContext.Binding.Key?.NoAlt.NoCtrl.NoShift!)
  47. {
  48. // It's this.HotKey OR Another View (Label?) forwarded the hotkey command to us - Act just like `Space` (Select)
  49. return InvokeCommand (Command.Select);
  50. }
  51. }
  52. if (item is { } && item < _radioLabels.Count)
  53. {
  54. if (item.Value == SelectedItem)
  55. {
  56. return true;
  57. }
  58. // If a RadioItem.HotKey is pressed we always set the selected item - never SetFocus
  59. bool selectedItemChanged = ChangeSelectedItem (item.Value);
  60. if (selectedItemChanged)
  61. {
  62. // Doesn't matter if it's handled
  63. RaiseSelecting (ctx);
  64. return true;
  65. }
  66. return false;
  67. }
  68. if (SelectedItem == -1 && ChangeSelectedItem (0))
  69. {
  70. if (RaiseSelecting (ctx) == true)
  71. {
  72. return true;
  73. }
  74. return false;
  75. }
  76. if (RaiseHandlingHotKey () == true)
  77. {
  78. return true;
  79. }
  80. ;
  81. // Default Command.Hotkey sets focus
  82. SetFocus ();
  83. return true;
  84. }
  85. private bool? HandleAcceptCommand (ICommandContext? ctx)
  86. {
  87. if (!DoubleClickAccepts
  88. && ctx is CommandContext<MouseBinding> mouseCommandContext
  89. && mouseCommandContext.Binding.MouseEventArgs!.Flags.HasFlag (MouseFlags.Button1DoubleClicked))
  90. {
  91. return false;
  92. }
  93. return RaiseAccepting (ctx);
  94. }
  95. private bool? HandleSelectCommand (ICommandContext? ctx)
  96. {
  97. if (ctx is CommandContext<MouseBinding> mouseCommandContext
  98. && mouseCommandContext.Binding.MouseEventArgs!.Flags.HasFlag (MouseFlags.Button1Clicked))
  99. {
  100. int viewportX = mouseCommandContext.Binding.MouseEventArgs.Position.X;
  101. int viewportY = mouseCommandContext.Binding.MouseEventArgs.Position.Y;
  102. int pos = Orientation == Orientation.Horizontal ? viewportX : viewportY;
  103. int rCount = Orientation == Orientation.Horizontal
  104. ? _horizontal!.Last ().pos + _horizontal!.Last ().length
  105. : _radioLabels.Count;
  106. if (pos < rCount)
  107. {
  108. int c = Orientation == Orientation.Horizontal
  109. ? _horizontal!.FindIndex (x => x.pos <= viewportX && x.pos + x.length - 2 >= viewportX)
  110. : viewportY;
  111. if (c > -1)
  112. {
  113. // Just like the user pressing the items' hotkey
  114. return InvokeCommand (Command.HotKey, new KeyBinding ([Command.HotKey], this, c)) == true;
  115. }
  116. }
  117. return false;
  118. }
  119. var cursorChanged = false;
  120. if (SelectedItem == Cursor)
  121. {
  122. cursorChanged = MoveDownRight ();
  123. if (!cursorChanged)
  124. {
  125. cursorChanged = MoveHome ();
  126. }
  127. }
  128. var selectedItemChanged = false;
  129. if (SelectedItem != Cursor)
  130. {
  131. selectedItemChanged = ChangeSelectedItem (Cursor);
  132. }
  133. if (cursorChanged || selectedItemChanged)
  134. {
  135. if (RaiseSelecting (ctx) == true)
  136. {
  137. return true;
  138. }
  139. }
  140. return cursorChanged || selectedItemChanged;
  141. }
  142. // TODO: Fix InvertColorsOnPress - only highlight the selected item
  143. private void SetupKeyBindings ()
  144. {
  145. // Default keybindings for this view
  146. if (Orientation == Orientation.Vertical)
  147. {
  148. KeyBindings.Remove (Key.CursorUp);
  149. KeyBindings.Add (Key.CursorUp, Command.Up);
  150. KeyBindings.Remove (Key.CursorDown);
  151. KeyBindings.Add (Key.CursorDown, Command.Down);
  152. }
  153. else
  154. {
  155. KeyBindings.Remove (Key.CursorLeft);
  156. KeyBindings.Add (Key.CursorLeft, Command.Up);
  157. KeyBindings.Remove (Key.CursorRight);
  158. KeyBindings.Add (Key.CursorRight, Command.Down);
  159. }
  160. KeyBindings.Remove (Key.Home);
  161. KeyBindings.Add (Key.Home, Command.Start);
  162. KeyBindings.Remove (Key.End);
  163. KeyBindings.Add (Key.End, Command.End);
  164. }
  165. /// <summary>
  166. /// Gets or sets whether double-clicking on a Radio Item will cause the <see cref="View.Accepting"/> event to be
  167. /// raised.
  168. /// </summary>
  169. /// <remarks>
  170. /// <para>
  171. /// If <see langword="false"/> and Accept is not handled, the Accept event on the <see cref="View.SuperView"/> will
  172. /// be raised. The default is
  173. /// <see langword="true"/>.
  174. /// </para>
  175. /// </remarks>
  176. public bool DoubleClickAccepts { get; set; } = true;
  177. private List<(int pos, int length)>? _horizontal;
  178. private int _horizontalSpace = 2;
  179. /// <summary>
  180. /// Gets or sets the horizontal space for this <see cref="RadioGroup"/> if the <see cref="Orientation"/> is
  181. /// <see cref="Orientation.Horizontal"/>
  182. /// </summary>
  183. public int HorizontalSpace
  184. {
  185. get => _horizontalSpace;
  186. set
  187. {
  188. if (_horizontalSpace != value && Orientation == Orientation.Horizontal)
  189. {
  190. _horizontalSpace = value;
  191. UpdateTextFormatterText ();
  192. SetContentSize ();
  193. }
  194. }
  195. }
  196. /// <summary>
  197. /// If <see langword="true"/> the <see cref="RadioLabels"/> will each be automatically assigned a hotkey.
  198. /// <see cref="UsedHotKeys"/> will be used to ensure unique keys are assigned. Set <see cref="UsedHotKeys"/>
  199. /// before setting <see cref="RadioLabels"/> with any hotkeys that may conflict with other Views.
  200. /// </summary>
  201. public bool AssignHotKeysToRadioLabels { get; set; }
  202. /// <summary>
  203. /// Gets the list of hotkeys already used by <see cref="RadioLabels"/> or that should not be used if
  204. /// <see cref="AssignHotKeysToRadioLabels"/>
  205. /// is enabled.
  206. /// </summary>
  207. public List<Key> UsedHotKeys { get; } = [];
  208. private readonly List<string> _radioLabels = [];
  209. /// <summary>
  210. /// The radio labels to display. A <see cref="Command.HotKey"/> key binding will be added for each label enabling the
  211. /// user to select
  212. /// and/or focus the radio label using the keyboard. See <see cref="View.HotKey"/> for details on how HotKeys work.
  213. /// </summary>
  214. /// <value>The radio labels.</value>
  215. public string [] RadioLabels
  216. {
  217. get => _radioLabels.ToArray ();
  218. set
  219. {
  220. // Remove old hot key bindings
  221. foreach (string label in _radioLabels)
  222. {
  223. if (TextFormatter.FindHotKey (label, HotKeySpecifier, out _, out Key hotKey))
  224. {
  225. AddKeyBindingsForHotKey (hotKey, Key.Empty);
  226. }
  227. }
  228. _radioLabels.Clear ();
  229. // Pick a unique hotkey for each radio label
  230. for (var labelIndex = 0; labelIndex < value.Length; labelIndex++)
  231. {
  232. string label = value [labelIndex];
  233. string? newLabel = label;
  234. if (AssignHotKeysToRadioLabels)
  235. {
  236. // Find the first char in label that is [a-z], [A-Z], or [0-9]
  237. for (var i = 0; i < label.Length; i++)
  238. {
  239. if (UsedHotKeys.Contains (new (label [i])) || !char.IsAsciiLetterOrDigit (label [i]))
  240. {
  241. continue;
  242. }
  243. if (char.IsAsciiLetterOrDigit (label [i]))
  244. {
  245. char? hotChar = label [i];
  246. newLabel = label.Insert (i, HotKeySpecifier.ToString ());
  247. UsedHotKeys.Add (new (hotChar));
  248. break;
  249. }
  250. }
  251. }
  252. _radioLabels.Add (newLabel);
  253. if (TextFormatter.FindHotKey (newLabel, HotKeySpecifier, out _, out Key hotKey))
  254. {
  255. AddKeyBindingsForHotKey (Key.Empty, hotKey, labelIndex);
  256. }
  257. }
  258. SelectedItem = 0;
  259. SetContentSize ();
  260. }
  261. }
  262. private int _selected;
  263. /// <summary>Gets or sets the selected radio label index.</summary>
  264. /// <value>The index. -1 if no item is selected.</value>
  265. public int SelectedItem
  266. {
  267. get => _selected;
  268. set => ChangeSelectedItem (value);
  269. }
  270. /// <summary>
  271. /// INTERNAL Sets the selected item.
  272. /// </summary>
  273. /// <param name="value"></param>
  274. /// <returns>
  275. /// <see langword="true"/> if the selected item changed.
  276. /// </returns>
  277. private bool ChangeSelectedItem (int value)
  278. {
  279. if (_selected == value || value > _radioLabels.Count - 1)
  280. {
  281. return false;
  282. }
  283. int savedSelected = _selected;
  284. _selected = value;
  285. Cursor = Math.Max (_selected, 0);
  286. OnSelectedItemChanged (value, SelectedItem);
  287. SelectedItemChanged?.Invoke (this, new (SelectedItem, savedSelected));
  288. SetNeedsDraw ();
  289. return true;
  290. }
  291. /// <inheritdoc/>
  292. protected override bool OnDrawingContent ()
  293. {
  294. SetAttribute (GetNormalColor ());
  295. for (var i = 0; i < _radioLabels.Count; i++)
  296. {
  297. switch (Orientation)
  298. {
  299. case Orientation.Vertical:
  300. Move (0, i);
  301. break;
  302. case Orientation.Horizontal:
  303. Move (_horizontal! [i].pos, 0);
  304. break;
  305. }
  306. string rl = _radioLabels [i];
  307. SetAttribute (GetNormalColor ());
  308. Driver?.AddStr ($"{(i == _selected ? Glyphs.Selected : Glyphs.UnSelected)} ");
  309. TextFormatter.FindHotKey (rl, HotKeySpecifier, out int hotPos, out Key hotKey);
  310. if (hotPos != -1 && hotKey != Key.Empty)
  311. {
  312. Rune [] rlRunes = rl.ToRunes ();
  313. for (var j = 0; j < rlRunes.Length; j++)
  314. {
  315. Rune rune = rlRunes [j];
  316. if (j == hotPos && i == Cursor)
  317. {
  318. SetAttribute (HasFocus ? GetHotFocusColor () : GetHotNormalColor ());
  319. }
  320. else if (j == hotPos && i != Cursor)
  321. {
  322. SetAttribute (GetHotNormalColor ());
  323. }
  324. else if (HasFocus && i == Cursor)
  325. {
  326. SetAttribute (GetFocusColor ());
  327. }
  328. if (rune == HotKeySpecifier && j + 1 < rlRunes.Length)
  329. {
  330. j++;
  331. rune = rlRunes [j];
  332. if (i == Cursor)
  333. {
  334. SetAttribute (HasFocus ? GetHotFocusColor () : GetHotNormalColor ());
  335. }
  336. else if (i != Cursor)
  337. {
  338. SetAttribute (GetHotNormalColor ());
  339. }
  340. }
  341. Application.Driver?.AddRune (rune);
  342. SetAttribute (GetNormalColor ());
  343. }
  344. }
  345. else
  346. {
  347. DrawHotString (rl, HasFocus && i == Cursor);
  348. }
  349. }
  350. return true;
  351. }
  352. #region IOrientation
  353. /// <summary>
  354. /// Gets or sets the <see cref="Orientation"/> for this <see cref="RadioGroup"/>. The default is
  355. /// <see cref="Orientation.Vertical"/>.
  356. /// </summary>
  357. public Orientation Orientation
  358. {
  359. get => _orientationHelper.Orientation;
  360. set => _orientationHelper.Orientation = value;
  361. }
  362. private readonly OrientationHelper _orientationHelper;
  363. #pragma warning disable CS0067 // The event is never used
  364. /// <inheritdoc/>
  365. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  366. /// <inheritdoc/>
  367. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  368. #pragma warning restore CS0067 // The event is never used
  369. #pragma warning restore CS0067
  370. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  371. /// <param name="newOrientation"></param>
  372. public void OnOrientationChanged (Orientation newOrientation)
  373. {
  374. SetupKeyBindings ();
  375. SetContentSize ();
  376. }
  377. #endregion IOrientation
  378. // TODO: Add a SelectedItemChanging event like CheckBox has.
  379. /// <summary>Called whenever the current selected item changes. Invokes the <see cref="SelectedItemChanged"/> event.</summary>
  380. /// <param name="selectedItem"></param>
  381. /// <param name="previousSelectedItem"></param>
  382. protected virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem) { }
  383. /// <summary>
  384. /// Gets or sets the <see cref="RadioLabels"/> index for the cursor. The cursor may or may not be the selected
  385. /// RadioItem.
  386. /// </summary>
  387. /// <remarks>
  388. /// <para>
  389. /// Maps to either the X or Y position within <see cref="View.Viewport"/> depending on <see cref="Orientation"/>.
  390. /// </para>
  391. /// </remarks>
  392. public int Cursor { get; set; }
  393. /// <inheritdoc/>
  394. public override Point? PositionCursor ()
  395. {
  396. var x = 0;
  397. var y = 0;
  398. switch (Orientation)
  399. {
  400. case Orientation.Vertical:
  401. y = Cursor;
  402. break;
  403. case Orientation.Horizontal:
  404. if (_horizontal!.Count > 0)
  405. {
  406. x = _horizontal [Cursor].pos;
  407. }
  408. break;
  409. default:
  410. return null;
  411. }
  412. Move (x, y);
  413. return null; // Don't show the cursor
  414. }
  415. /// <summary>Raised when the selected radio label has changed.</summary>
  416. public event EventHandler<SelectedItemChangedArgs>? SelectedItemChanged;
  417. private bool MoveDownRight ()
  418. {
  419. if (Cursor + 1 < _radioLabels.Count)
  420. {
  421. Cursor++;
  422. SetNeedsDraw ();
  423. return true;
  424. }
  425. // Moving past should move focus to next view, not wrap
  426. return false;
  427. }
  428. private bool MoveEnd ()
  429. {
  430. Cursor = Math.Max (_radioLabels.Count - 1, 0);
  431. return true;
  432. }
  433. private bool MoveHome ()
  434. {
  435. if (Cursor != 0)
  436. {
  437. Cursor = 0;
  438. return true;
  439. }
  440. return false;
  441. }
  442. private bool MoveUpLeft ()
  443. {
  444. if (Cursor > 0)
  445. {
  446. Cursor--;
  447. SetNeedsDraw ();
  448. return true;
  449. }
  450. // Moving past should move focus to next view, not wrap
  451. return false;
  452. }
  453. private void RadioGroup_LayoutStarted (object? sender, EventArgs e) { SetContentSize (); }
  454. private void SetContentSize ()
  455. {
  456. switch (Orientation)
  457. {
  458. case Orientation.Vertical:
  459. var width = 0;
  460. foreach (string s in _radioLabels)
  461. {
  462. width = Math.Max (s.GetColumns () + 2, width);
  463. }
  464. SetContentSize (new (width, _radioLabels.Count));
  465. break;
  466. case Orientation.Horizontal:
  467. _horizontal = new ();
  468. var start = 0;
  469. var length = 0;
  470. for (var i = 0; i < _radioLabels.Count; i++)
  471. {
  472. start += length;
  473. length = _radioLabels [i].GetColumns () + 2 + (i < _radioLabels.Count - 1 ? _horizontalSpace : 0);
  474. _horizontal.Add ((start, length));
  475. }
  476. SetContentSize (new (_horizontal.Sum (item => item.length), 1));
  477. break;
  478. }
  479. }
  480. /// <inheritdoc/>
  481. public bool EnableForDesign ()
  482. {
  483. RadioLabels = new [] { "Option _1", "Option _2", "Option _3" };
  484. return true;
  485. }
  486. }