Shortcut.cs 26 KB

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