Shortcut.cs 24 KB

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