Shortcut.cs 26 KB

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