Shortcut.cs 26 KB

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