Shortcut.cs 25 KB

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