Shortcut.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. using System.ComponentModel;
  2. using System.Threading.Channels;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Displays a command, help text, and a key binding. When the key specified by <see cref="Key"/> is pressed, the command will be invoked. Useful for
  6. /// displaying a command in <see cref="Bar"/> such as a
  7. /// menu, toolbar, or status bar.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// The following user actions will invoke the <see cref="Command.Accept"/>, causing the
  12. /// <see cref="View.Accept"/> event to be fired:
  13. /// - Clicking on the <see cref="Shortcut"/>.
  14. /// - Pressing the key specified by <see cref="Key"/>.
  15. /// - Pressing the HotKey specified by <see cref="CommandView"/>.
  16. /// </para>
  17. /// <para>
  18. /// If <see cref="KeyBindingScope"/> is <see cref="KeyBindingScope.Application"/>, <see cref="Key"/> will invoked <see cref="Command.Accept"/>
  19. /// command regardless of what View has focus, enabling an application-wide keyboard shortcut.
  20. /// </para>
  21. /// <para>
  22. /// By default, a Shortcut displays the command text on the left side, the help text in the middle, and the key
  23. /// binding on the
  24. /// right side. Set <see cref="AlignmentModes"/> to <see cref="AlignmentModes.EndToStart"/> to reverse the order.
  25. /// </para>
  26. /// <para>
  27. /// The command text can be set by setting the <see cref="CommandView"/>'s Text property or by setting
  28. /// <see cref="View.Title"/>.
  29. /// </para>
  30. /// <para>
  31. /// The help text can be set by setting the <see cref="HelpText"/> property or by setting <see cref="View.Text"/>.
  32. /// </para>
  33. /// <para>
  34. /// The key text is set by setting the <see cref="Key"/> property.
  35. /// If the <see cref="Key"/> is <see cref="Key.Empty"/>, the <see cref="Key"/> text is not displayed.
  36. /// </para>
  37. /// </remarks>
  38. public class Shortcut : View
  39. {
  40. /// <summary>
  41. /// Creates a new instance of <see cref="Shortcut"/>.
  42. /// </summary>
  43. /// <remarks>
  44. /// <para>
  45. /// This is a helper API that mimics the V1 API for creating StatusItems.
  46. /// </para>
  47. /// </remarks>
  48. /// <param name="key"></param>
  49. /// <param name="commandText"></param>
  50. /// <param name="action"></param>
  51. /// <param name="helpText"></param>
  52. public Shortcut (Key key, string commandText, Action action, string helpText = null)
  53. {
  54. Id = "_shortcut";
  55. HighlightStyle = HighlightStyle.Pressed;
  56. Highlight += Shortcut_Highlight;
  57. CanFocus = true;
  58. Width = GetWidthDimAuto ();
  59. Height = Dim.Auto (DimAutoStyle.Content, 1);
  60. AddCommand (Command.HotKey, ctx => OnAccept (ctx));
  61. AddCommand (Command.Accept, ctx => OnAccept (ctx));
  62. AddCommand (Command.Select, ctx => OnSelect (ctx));
  63. KeyBindings.Add (KeyCode.Enter, Command.Accept);
  64. KeyBindings.Add (KeyCode.Space, Command.Select);
  65. TitleChanged += Shortcut_TitleChanged; // This needs to be set before CommandView is set
  66. CommandView = new ()
  67. {
  68. Width = Dim.Auto (),
  69. Height = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: 1)
  70. };
  71. HelpView.Id = "_helpView";
  72. HelpView.CanFocus = false;
  73. HelpView.Text = helpText;
  74. Add (HelpView);
  75. KeyView.Id = "_keyView";
  76. KeyView.CanFocus = false;
  77. Add (KeyView);
  78. // If the user clicks anywhere on the Shortcut, other than the CommandView, invoke the Command
  79. MouseClick += Shortcut_MouseClick;
  80. HelpView.MouseClick += Subview_MouseClick;
  81. KeyView.MouseClick += Subview_MouseClick;
  82. LayoutStarted += OnLayoutStarted;
  83. Initialized += OnInitialized;
  84. if (key is null)
  85. {
  86. key = Key.Empty;
  87. }
  88. Key = key;
  89. Title = commandText;
  90. Action = action;
  91. return;
  92. void OnInitialized (object sender, EventArgs e)
  93. {
  94. SuperViewRendersLineCanvas = true;
  95. Border.Settings &= ~BorderSettings.Title;
  96. ShowHide ();
  97. // Force Width to DimAuto to calculate natural width and then set it back
  98. Dim savedDim = Width;
  99. Width = GetWidthDimAuto ();
  100. _minimumDimAutoWidth = Frame.Width;
  101. Width = savedDim;
  102. SetCommandViewDefaultLayout ();
  103. SetHelpViewDefaultLayout ();
  104. SetKeyViewDefaultLayout ();
  105. SetColors ();
  106. }
  107. // Helper to set Width consistently
  108. Dim GetWidthDimAuto ()
  109. {
  110. // TODO: PosAlign.CalculateMinDimension is a hack. Need to figure out a better way of doing this.
  111. return Dim.Auto (
  112. DimAutoStyle.Content,
  113. Dim.Func (() => PosAlign.CalculateMinDimension (0, Subviews, Dimension.Width)),
  114. Dim.Func (() => PosAlign.CalculateMinDimension (0, Subviews, Dimension.Width)));
  115. }
  116. }
  117. /// <summary>
  118. /// Creates a new instance of <see cref="Shortcut"/>.
  119. /// </summary>
  120. public Shortcut () : this (Key.Empty, string.Empty, null) { }
  121. private Orientation _orientation = Orientation.Horizontal;
  122. /// <summary>
  123. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Shortcut"/>. The default is
  124. /// <see cref="Orientation.Horizontal"/>, which is ideal for status bar, menu bar, and tool bar items If set to
  125. /// <see cref="Orientation.Vertical"/>,
  126. /// the Shortcut will be configured for vertical layout, which is ideal for menu items.
  127. /// </summary>
  128. public Orientation Orientation
  129. {
  130. get => _orientation;
  131. set
  132. {
  133. _orientation = value;
  134. // TODO: Determine what, if anything, is opinionated about the orientation.
  135. }
  136. }
  137. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast;
  138. /// <summary>
  139. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Shortcut"/>.
  140. /// </summary>
  141. /// <remarks>
  142. /// <para>
  143. /// The default is <see cref="AlignmentModes.StartToEnd"/>. This means that the CommandView will be on the left,
  144. /// HelpView in the middle, and KeyView on the right.
  145. /// </para>
  146. /// </remarks>
  147. public AlignmentModes AlignmentModes
  148. {
  149. get => _alignmentModes;
  150. set
  151. {
  152. _alignmentModes = value;
  153. SetCommandViewDefaultLayout ();
  154. SetHelpViewDefaultLayout ();
  155. SetKeyViewDefaultLayout ();
  156. }
  157. }
  158. // When one of the subviews is "empty" we don't want to show it. So we
  159. // Use Add/Remove. We need to be careful to add them in the right order
  160. // so Pos.Align works correctly.
  161. internal void ShowHide ()
  162. {
  163. RemoveAll ();
  164. if (CommandView.Visible)
  165. {
  166. Add (CommandView);
  167. }
  168. if (HelpView.Visible && !string.IsNullOrEmpty (HelpView.Text))
  169. {
  170. Add (HelpView);
  171. }
  172. if (KeyView.Visible && Key != Key.Empty)
  173. {
  174. Add (KeyView);
  175. }
  176. }
  177. // This is used to calculate the minimum width of the Shortcut when the width is NOT Dim.Auto
  178. private int? _minimumDimAutoWidth;
  179. // When layout starts, we need to adjust the layout of the HelpView and KeyView
  180. private void OnLayoutStarted (object sender, LayoutEventArgs e)
  181. {
  182. if (Width is DimAuto widthAuto)
  183. {
  184. _minimumDimAutoWidth = Frame.Width;
  185. }
  186. else
  187. {
  188. if (string.IsNullOrEmpty (HelpView.Text))
  189. {
  190. return;
  191. }
  192. int currentWidth = Frame.Width;
  193. // If our width is smaller than the natural width then reduce width of HelpView first.
  194. // Then KeyView.
  195. // Don't ever reduce CommandView (it should spill).
  196. // When Horizontal, Key is first, then Help, then Command.
  197. // When Vertical, Command is first, then Help, then Key.
  198. // BUGBUG: This does not do what the above says.
  199. // TODO: Add Unit tests for this.
  200. if (currentWidth < _minimumDimAutoWidth)
  201. {
  202. int delta = _minimumDimAutoWidth.Value - currentWidth;
  203. int maxHelpWidth = int.Max (0, HelpView.Text.GetColumns () + Margin.Thickness.Horizontal - delta);
  204. switch (maxHelpWidth)
  205. {
  206. case 0:
  207. // Hide HelpView
  208. HelpView.Visible = false;
  209. HelpView.X = 0;
  210. break;
  211. case 1:
  212. // Scrunch it by removing margins
  213. HelpView.Margin.Thickness = new (0, 0, 0, 0);
  214. break;
  215. case 2:
  216. // Scrunch just the right margin
  217. Thickness t = GetMarginThickness ();
  218. HelpView.Margin.Thickness = new (t.Right, t.Top, t.Left - 1, t.Bottom);
  219. break;
  220. default:
  221. // Default margin
  222. HelpView.Margin.Thickness = GetMarginThickness ();
  223. break;
  224. }
  225. if (maxHelpWidth > 0)
  226. {
  227. HelpView.X = Pos.Align (Alignment.End, AlignmentModes);
  228. // Leverage Dim.Auto's max:
  229. HelpView.Width = Dim.Auto (DimAutoStyle.Text, maximumContentDim: maxHelpWidth);
  230. HelpView.Visible = true;
  231. }
  232. }
  233. else
  234. {
  235. // Reset to default
  236. //SetCommandViewDefaultLayout();
  237. SetHelpViewDefaultLayout ();
  238. //SetKeyViewDefaultLayout ();
  239. }
  240. }
  241. }
  242. private Thickness GetMarginThickness ()
  243. {
  244. if (Orientation == Orientation.Vertical)
  245. {
  246. return new (1, 0, 1, 0);
  247. }
  248. return new (1, 0, 1, 0);
  249. }
  250. private Color? _savedForeColor;
  251. private void Shortcut_Highlight (object sender, CancelEventArgs<HighlightStyle> e)
  252. {
  253. if (e.CurrentValue.HasFlag (HighlightStyle.Pressed))
  254. {
  255. if (!_savedForeColor.HasValue)
  256. {
  257. _savedForeColor = base.ColorScheme.Normal.Foreground;
  258. }
  259. var cs = new ColorScheme (base.ColorScheme)
  260. {
  261. Normal = new (ColorScheme.Normal.Foreground.GetHighlightColor (), base.ColorScheme.Normal.Background)
  262. };
  263. base.ColorScheme = cs;
  264. }
  265. if (e.CurrentValue == HighlightStyle.None && _savedForeColor.HasValue)
  266. {
  267. var cs = new ColorScheme (base.ColorScheme)
  268. {
  269. Normal = new (_savedForeColor.Value, base.ColorScheme.Normal.Background)
  270. };
  271. base.ColorScheme = cs;
  272. }
  273. SuperView?.SetNeedsDisplay ();
  274. e.Cancel = true;
  275. }
  276. private void Shortcut_MouseClick (object sender, MouseEventEventArgs e)
  277. {
  278. // When the Shortcut is clicked, we want to invoke the Command and Set focus
  279. var view = sender as View;
  280. if (!e.Handled)
  281. {
  282. // If the subview (likely CommandView) didn't handle the mouse click, invoke the command.
  283. e.Handled = InvokeCommand (Command.Accept) == true;
  284. }
  285. if (CanFocus)
  286. {
  287. SetFocus ();
  288. }
  289. }
  290. private void Subview_MouseClick (object sender, MouseEventEventArgs e)
  291. {
  292. // TODO: Remove. This does nothing.
  293. return;
  294. }
  295. #region Command
  296. private View _commandView = new ();
  297. /// <summary>
  298. /// Gets or sets the View that displays the command text and hotkey.
  299. /// </summary>
  300. /// <remarks>
  301. /// <para>
  302. /// By default, the <see cref="View.Title"/> of the <see cref="CommandView"/> is displayed as the Shortcut's
  303. /// command text.
  304. /// </para>
  305. /// <para>
  306. /// By default, the CommandView is a <see cref="View"/> with <see cref="View.CanFocus"/> set to
  307. /// <see langword="false"/>.
  308. /// </para>
  309. /// <para>
  310. /// Setting the <see cref="CommandView"/> will add it to the <see cref="Shortcut"/> and remove any existing
  311. /// <see cref="CommandView"/>.
  312. /// </para>
  313. /// </remarks>
  314. /// <example>
  315. /// <para>
  316. /// This example illustrates how to add a <see cref="Shortcut"/> to a <see cref="StatusBar"/> that toggles the
  317. /// <see cref="Application.Force16Colors"/> property.
  318. /// </para>
  319. /// <code>
  320. /// var force16ColorsShortcut = new Shortcut
  321. /// {
  322. /// Key = Key.F6,
  323. /// KeyBindingScope = KeyBindingScope.HotKey,
  324. /// CommandView = new CheckBox { Text = "Force 16 Colors" }
  325. /// };
  326. /// var cb = force16ColorsShortcut.CommandView as CheckBox;
  327. /// cb.Checked = Application.Force16Colors;
  328. ///
  329. /// cb.Toggled += (s, e) =>
  330. /// {
  331. /// var cb = s as CheckBox;
  332. /// Application.Force16Colors = cb!.Checked == true;
  333. /// Application.Refresh();
  334. /// };
  335. /// StatusBar.Add(force16ColorsShortcut);
  336. /// </code>
  337. /// </example>
  338. public View CommandView
  339. {
  340. get => _commandView;
  341. set
  342. {
  343. if (value == null)
  344. {
  345. throw new ArgumentNullException ();
  346. }
  347. if (_commandView is { })
  348. {
  349. Remove (_commandView);
  350. _commandView?.Dispose ();
  351. }
  352. _commandView = value;
  353. _commandView.Id = "_commandView";
  354. // The default behavior is for CommandView to not get focus. I
  355. // If you want it to get focus, you need to set it.
  356. _commandView.CanFocus = false;
  357. _commandView.HotKeyChanged += (s, e) =>
  358. {
  359. if (e.NewKey != Key.Empty)
  360. {
  361. // Add it
  362. AddKeyBindingsForHotKey (e.OldKey, e.NewKey);
  363. }
  364. };
  365. _commandView.HotKeySpecifier = new ('_');
  366. Title = _commandView.Text;
  367. SetCommandViewDefaultLayout ();
  368. SetHelpViewDefaultLayout ();
  369. SetKeyViewDefaultLayout ();
  370. ShowHide ();
  371. UpdateKeyBinding ();
  372. return;
  373. }
  374. }
  375. private void SetCommandViewDefaultLayout ()
  376. {
  377. CommandView.Margin.Thickness = GetMarginThickness ();
  378. CommandView.X = Pos.Align (Alignment.End, AlignmentModes);
  379. CommandView.Y = 0; //Pos.Center ();
  380. }
  381. private void Shortcut_TitleChanged (object sender, EventArgs<string> e)
  382. {
  383. // If the Title changes, update the CommandView text.
  384. // This is a helper to make it easier to set the CommandView text.
  385. // CommandView is public and replaceable, but this is a convenience.
  386. _commandView.Text = Title;
  387. }
  388. #endregion Command
  389. #region Help
  390. /// <summary>
  391. /// The subview that displays the help text for the command. Internal for unit testing.
  392. /// </summary>
  393. internal View HelpView { get; } = new ();
  394. private void SetHelpViewDefaultLayout ()
  395. {
  396. HelpView.Margin.Thickness = GetMarginThickness ();
  397. HelpView.X = Pos.Align (Alignment.End, AlignmentModes);
  398. HelpView.Y = 0; //Pos.Center ();
  399. HelpView.Width = Dim.Auto (DimAutoStyle.Text);
  400. HelpView.Height = CommandView?.Visible == true ? Dim.Height (CommandView) : 1;
  401. HelpView.Visible = true;
  402. HelpView.VerticalTextAlignment = Alignment.Center;
  403. }
  404. /// <summary>
  405. /// Gets or sets the help text displayed in the middle of the Shortcut. Identical in function to <see cref="HelpText"/>
  406. /// .
  407. /// </summary>
  408. public override string Text
  409. {
  410. get => HelpView?.Text;
  411. set
  412. {
  413. if (HelpView is {})
  414. {
  415. HelpView.Text = value;
  416. ShowHide ();
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// Gets or sets the help text displayed in the middle of the Shortcut.
  422. /// </summary>
  423. public string HelpText
  424. {
  425. get => HelpView?.Text;
  426. set
  427. {
  428. if (HelpView is {})
  429. {
  430. HelpView.Text = value;
  431. ShowHide ();
  432. }
  433. }
  434. }
  435. #endregion Help
  436. #region Key
  437. private Key _key = Key.Empty;
  438. /// <summary>
  439. /// Gets or sets the <see cref="Key"/> that will be bound to the <see cref="Command.Accept"/> command.
  440. /// </summary>
  441. public Key Key
  442. {
  443. get => _key;
  444. set
  445. {
  446. if (value == null)
  447. {
  448. throw new ArgumentNullException ();
  449. }
  450. _key = value;
  451. UpdateKeyBinding ();
  452. KeyView.Text = Key == Key.Empty ? string.Empty : $"{Key}";
  453. ShowHide ();
  454. }
  455. }
  456. private KeyBindingScope _keyBindingScope = KeyBindingScope.HotKey;
  457. /// <summary>
  458. /// Gets or sets the scope for the key binding for how <see cref="Key"/> is bound to <see cref="Command"/>.
  459. /// </summary>
  460. public KeyBindingScope KeyBindingScope
  461. {
  462. get => _keyBindingScope;
  463. set
  464. {
  465. _keyBindingScope = value;
  466. UpdateKeyBinding ();
  467. }
  468. }
  469. /// <summary>
  470. /// Gets the subview that displays the key. Internal for unit testing.
  471. /// </summary>
  472. internal View KeyView { get; } = new ();
  473. private int _minimumKeyTextSize;
  474. /// <summary>
  475. /// Gets or sets the minimum size of the key text. Useful for aligning the key text with other <see cref="Shortcut"/>s.
  476. /// </summary>
  477. public int MinimumKeyTextSize
  478. {
  479. get => _minimumKeyTextSize;
  480. set
  481. {
  482. if (value == _minimumKeyTextSize)
  483. {
  484. //return;
  485. }
  486. _minimumKeyTextSize = value;
  487. SetKeyViewDefaultLayout ();
  488. CommandView.SetNeedsLayout ();
  489. HelpView.SetNeedsLayout ();
  490. KeyView.SetNeedsLayout ();
  491. SetSubViewNeedsDisplay ();
  492. }
  493. }
  494. private int GetMinimumKeyViewSize () { return MinimumKeyTextSize; }
  495. private void SetKeyViewDefaultLayout ()
  496. {
  497. KeyView.Margin.Thickness = GetMarginThickness ();
  498. KeyView.X = Pos.Align (Alignment.End, AlignmentModes);
  499. KeyView.Y = 0; //Pos.Center ();
  500. KeyView.Width = Dim.Auto (DimAutoStyle.Text, Dim.Func (GetMinimumKeyViewSize));
  501. KeyView.Height = CommandView?.Visible == true ? Dim.Height (CommandView) : 1;
  502. KeyView.Visible = true;
  503. // Right align the text in the keyview
  504. KeyView.TextAlignment = Alignment.End;
  505. KeyView.VerticalTextAlignment = Alignment.Center;
  506. KeyView.KeyBindings.Clear ();
  507. }
  508. private void UpdateKeyBinding ()
  509. {
  510. if (Key != null)
  511. {
  512. // Disable the command view key bindings
  513. CommandView.KeyBindings.Remove (Key);
  514. CommandView.KeyBindings.Remove (CommandView.HotKey);
  515. KeyBindings.Remove (Key);
  516. KeyBindings.Add (Key, KeyBindingScope | KeyBindingScope.HotKey, Command.Accept);
  517. //KeyBindings.Add (Key, KeyBindingScope.HotKey, Command.Accept);
  518. }
  519. }
  520. #endregion Key
  521. #region Accept Handling
  522. /// <summary>
  523. /// Called when the <see cref="Command.Accept"/> command is received. This
  524. /// occurs
  525. /// - if the user clicks anywhere on the shortcut with the mouse
  526. /// - if the user presses Key
  527. /// - if the user presses the HotKey specified by CommandView
  528. /// - if HasFocus and the user presses Space or Enter (or any other key bound to Command.Accept).
  529. /// </summary>
  530. protected bool? OnAccept (CommandContext ctx)
  531. {
  532. var cancel = false;
  533. switch (ctx.KeyBinding?.Scope)
  534. {
  535. case KeyBindingScope.Application:
  536. cancel = base.OnAccept () == true;
  537. break;
  538. case KeyBindingScope.Focused:
  539. base.OnAccept ();
  540. // cancel if we're focused
  541. cancel = true;
  542. break;
  543. case KeyBindingScope.HotKey:
  544. cancel = base.OnAccept () == true;
  545. if (CanFocus)
  546. {
  547. SetFocus ();
  548. cancel = true;
  549. }
  550. break;
  551. default:
  552. // Mouse
  553. cancel = base.OnAccept () == true;
  554. break;
  555. }
  556. CommandView.InvokeCommand (Command.Accept, ctx.Key, ctx.KeyBinding);
  557. if (Action is { })
  558. {
  559. Action.Invoke ();
  560. // Assume if there's a subscriber to Action, it's handled.
  561. cancel = true;
  562. }
  563. return cancel;
  564. }
  565. /// <summary>
  566. /// Gets or sets the action to be invoked when the shortcut key is pressed or the shortcut is clicked on with the
  567. /// mouse.
  568. /// </summary>
  569. /// <remarks>
  570. /// Note, the <see cref="View.Accept"/> event is fired first, and if cancelled, the event will not be invoked.
  571. /// </remarks>
  572. [CanBeNull]
  573. public Action Action { get; set; }
  574. #endregion Accept Handling
  575. private bool? OnSelect (CommandContext ctx)
  576. {
  577. if (CommandView.GetSupportedCommands ().Contains (Command.Select))
  578. {
  579. return CommandView.InvokeCommand (Command.Select, ctx.Key, ctx.KeyBinding);
  580. }
  581. return false;
  582. }
  583. #region Focus
  584. /// <inheritdoc/>
  585. public override ColorScheme ColorScheme
  586. {
  587. get => base.ColorScheme;
  588. set
  589. {
  590. base.ColorScheme = value;
  591. SetColors ();
  592. }
  593. }
  594. /// <summary>
  595. /// </summary>
  596. internal void SetColors ()
  597. {
  598. // Border should match superview.
  599. Border.ColorScheme = SuperView?.ColorScheme;
  600. if (HasFocus)
  601. {
  602. // When we have focus, we invert the colors
  603. base.ColorScheme = new (base.ColorScheme)
  604. {
  605. Normal = base.ColorScheme.Focus,
  606. HotNormal = base.ColorScheme.HotFocus,
  607. HotFocus = base.ColorScheme.HotNormal,
  608. Focus = base.ColorScheme.Normal
  609. };
  610. }
  611. else
  612. {
  613. base.ColorScheme = SuperView?.ColorScheme ?? base.ColorScheme;
  614. }
  615. // Set KeyView's colors to show "hot"
  616. if (IsInitialized && base.ColorScheme is { })
  617. {
  618. var cs = new ColorScheme (base.ColorScheme)
  619. {
  620. Normal = base.ColorScheme.HotNormal,
  621. HotNormal = base.ColorScheme.Normal
  622. };
  623. KeyView.ColorScheme = cs;
  624. }
  625. }
  626. View _lastFocusedView;
  627. /// <inheritdoc/>
  628. public override bool OnEnter (View view)
  629. {
  630. SetColors ();
  631. _lastFocusedView = view;
  632. return base.OnEnter (view);
  633. }
  634. /// <inheritdoc/>
  635. public override bool OnLeave (View view)
  636. {
  637. SetColors ();
  638. _lastFocusedView = this;
  639. return base.OnLeave (view);
  640. }
  641. #endregion Focus
  642. /// <inheritdoc/>
  643. protected override void Dispose (bool disposing)
  644. {
  645. if (disposing)
  646. {
  647. if (CommandView?.IsAdded == false)
  648. {
  649. CommandView.Dispose ();
  650. }
  651. if (HelpView?.IsAdded == false)
  652. {
  653. HelpView.Dispose ();
  654. }
  655. if (KeyView?.IsAdded == false)
  656. {
  657. KeyView.Dispose ();
  658. }
  659. }
  660. base.Dispose (disposing);
  661. }
  662. }