Shortcut.cs 25 KB

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