Shortcut.cs 25 KB

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