View.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. #region API Docs
  6. /// <summary>
  7. /// View is the base class all visible elements. View can render itself and
  8. /// contains zero or more nested views, called SubViews. View provides basic functionality for layout, arrangement, and
  9. /// drawing. In addition, View provides keyboard and mouse event handling. See the
  10. /// <see href="../docs/view.html">
  11. /// View
  12. /// Deep Dive
  13. /// </see>
  14. /// for more.
  15. /// </summary>
  16. /// <remarks>
  17. /// <list type="table">
  18. /// <listheader>
  19. /// <term>Term</term><description>Definition</description>
  20. /// </listheader>
  21. /// <item>
  22. /// <term>SubView</term>
  23. /// <description>
  24. /// A View that is contained in another view and will be rendered as part of the containing view's
  25. /// ContentArea. SubViews are added to another view via the <see cref="View.Add(View)"/>` method. A View
  26. /// may only be a SubView of a single View.
  27. /// </description>
  28. /// </item>
  29. /// <item>
  30. /// <term>SuperView</term><description>The View that is a container for SubViews.</description>
  31. /// </item>
  32. /// <item>
  33. /// <term>Input</term>
  34. /// <description>
  35. /// <para>
  36. /// Key Bindings is the preferred way of handling keyboard input in View implementations.
  37. /// The View calls
  38. /// <see cref="AddCommand(Terminal.Gui.Command,Terminal.Gui.View.CommandImplementation)"/> to declare
  39. /// it supports a particular command and then uses <see cref="KeyBindings"/>
  40. /// to indicate which key presses will invoke the command.
  41. /// </para>
  42. /// <para>
  43. /// Mouse Bindings is the preferred way of handling mouse input in View implementations. The View calls
  44. /// <see cref="AddCommand(Terminal.Gui.Command,Terminal.Gui.View.CommandImplementation)"/> to declare
  45. /// it supports a
  46. /// particular command and then uses <see cref="MouseBindings"/> to indicate which mouse events will
  47. /// invoke the command.
  48. /// </para>
  49. /// <para>
  50. /// See the
  51. /// <see href="../docs/mouse.html">
  52. /// Mouse
  53. /// Deep Dive
  54. /// </see>
  55. /// and
  56. /// <see href="../docs/keyboard.html">
  57. /// Keyboard
  58. /// Deep Dive
  59. /// </see>
  60. /// for more information.
  61. /// </para>
  62. /// </description>
  63. /// </item>
  64. /// <item>
  65. /// <term>Layout</term>
  66. /// <description>
  67. /// <para>
  68. /// Terminal.Gui provides a rich system for how View objects are laid out relative to each other. The
  69. /// layout system also defines how coordinates are specified.
  70. /// </para>
  71. /// <para>
  72. /// The <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties are
  73. /// <see cref="Dim"/> and <see cref="Pos"/> objects that dynamically update the position of a view. The
  74. /// X and Y properties are of type <see cref="Pos"/> and you can use either absolute positions,
  75. /// percentages, or anchor points. The Width and Height properties are of type <see cref="Dim"/> and
  76. /// can use absolute position, percentages, and anchors. These are useful as they will take care of
  77. /// repositioning views when view's adornments are resized or if the terminal size changes.
  78. /// </para>
  79. /// <para>
  80. /// See the
  81. /// <see href="../docs/layout.html">
  82. /// Layout
  83. /// Deep Dive
  84. /// </see>
  85. /// for more information.
  86. /// </para>
  87. /// </description>
  88. /// </item>
  89. /// <item>
  90. /// <term>Arrangement</term>
  91. /// <description>
  92. /// <para>
  93. /// Complimenting the Layout system, <see cref="Arrangement"/> controls how the user can use the mouse
  94. /// and keyboard to arrange views and enables either Tiled or Overlapped layouts.
  95. /// </para>
  96. /// <para>
  97. /// See the
  98. /// <see href="../docs/arrangement.html">
  99. /// Arrangement
  100. /// Deep Dive
  101. /// </see>
  102. /// for more information.
  103. /// </para>
  104. /// </description>
  105. /// </item>
  106. /// <item>
  107. /// <term>Drawing</term>
  108. /// <description>
  109. /// <para>
  110. /// Apps draw using the <see cref="Move"/> and <see cref="AddRune(Rune)"/> APIs. Move selects the
  111. /// column and row of the Cell and AddRune places
  112. /// the specified glyph in that cell using the <see cref="Attribute"/> that was most recently set via
  113. /// <see cref="SetAttribute"/>.
  114. /// The ConsoleDriver caches all changed Cells and efficiently outputs them to the terminal each
  115. /// iteration of the Application. In other words, Terminal.Gui uses deferred rendering.
  116. /// </para>
  117. /// <para>
  118. /// The View draw APIs all take coordinates specified in Viewport-Relative coordinates. That is,
  119. /// <c>(0,0)</c> is the top-left cell visible to the user.
  120. /// </para>
  121. /// <para>
  122. /// If a View need to redraw because something changed within it's Content Area it can call
  123. /// <see cref="SetNeedsDraw()"/>.
  124. /// </para>
  125. /// <para>
  126. /// Terminal.Gui supports the full range of Unicode/wide characters.
  127. /// This includes emoji, CJK characters, and other wide characters. For Unicode characters that require
  128. /// more than one cell,
  129. /// AddRune and the ConsoleDriver automatically manage the cells. Extension methods to Rune are
  130. /// provided to determine if a Rune is a wide character and to get the width of a Rune.
  131. /// </para>
  132. /// <para>
  133. /// The <see cref="ColorScheme"/> provides consistent colors across all views. The
  134. /// <see cref="ColorScheme"/> is inherited from the <see cref="SuperView"/>. The
  135. /// <see cref="ColorScheme"/> is used to set the <see cref="Attribute"/> for drawing.
  136. /// </para>
  137. /// The <see cref="Color"/> class represents a color. It provides automatic mapping between the legacy
  138. /// 4-bit (16-color) system and 24-bit colors. It contains properties for the red, green, and blue
  139. /// components of the color.
  140. /// The Color class also contains a static property for each of the 16 ANSI colors. Use
  141. /// <see cref="SetAttribute"/> to change the colors used when drawing.</para>
  142. /// <para>
  143. /// </para>
  144. /// <para>
  145. /// Clipping enables better performance by ensuring on regions of the terminal that need to be drawn
  146. /// actually get drawn by the ConsoleDriver. Terminal.Gui supports non-rectangular clip regions with
  147. /// <see cref="Region"/>.
  148. /// There is an <see cref="Application"/>-managed clip region. Developers cannot change this directly,
  149. /// but can use <see cref="ClipFrame"/>, <see cref="ClipViewport"/>, and <see cref="SetClip"/> to
  150. /// modify the clip region.
  151. /// </para>
  152. /// <para>
  153. /// <see cref="LineCanvas"/> provides auto join, a smart TUI drawing system that automatically selects
  154. /// the correct line/box drawing glyphs for intersections making drawing complex shapes easy.
  155. /// </para>
  156. /// <para>
  157. /// A set of static properties are provided for the common glyphs used in TUI apps. See
  158. /// <see cref="Glyphs"/>.
  159. /// </para>
  160. /// <para>
  161. /// See the
  162. /// <see href="../docs/drawing.html">
  163. /// Drawing
  164. /// Deep Dive
  165. /// </see>
  166. /// for more information.
  167. /// </para>
  168. /// </description>
  169. /// </item>
  170. /// <item>
  171. /// <term>Text</term>
  172. /// <description>
  173. /// <para>
  174. /// A rich text formatting engine is provided in <see cref="TextFormatter"/>. TextFormatter provides
  175. /// methods for formatting text with horizontal and vertical alignment, word wrapping, and hotkeys.
  176. /// </para>
  177. /// <para>
  178. /// See the
  179. /// <see href="../docs/navigation.html">
  180. /// Navigation
  181. /// Deep Dive
  182. /// </see>
  183. /// for more information.
  184. /// </para>
  185. /// </description>
  186. /// </item>
  187. /// <item>
  188. /// <term>Navigation</term>
  189. /// <description>
  190. /// <para>
  191. /// Navigation refers to the user experience for moving focus between views in the application
  192. /// view-hierarchy. Focus is a concept that is used to describe which View is currently receiving user
  193. /// input. Only
  194. /// Views that are
  195. /// <see cref="Enabled"/>, <see cref="Visible"/>, and <see cref="CanFocus"/> will receive focus. NOTE:
  196. /// <see cref="CanFocus"/> is <see langword="false"/> by default.
  197. /// </para>
  198. /// <para>
  199. /// Views that are focusable should override <see cref="PositionCursor"/> to make sure that the cursor
  200. /// is
  201. /// placed in a location that makes sense. Some terminals do not have a way of hiding the cursor, so it
  202. /// can be
  203. /// distracting to have the cursor left at the last focused view. So views should make sure that they
  204. /// place the
  205. /// cursor in a visually sensible place. The default implementation of <see cref="PositionCursor"/>
  206. /// will place the
  207. /// cursor at either the hotkey (if defined) or <c>0,0</c>.
  208. /// </para>
  209. /// <para>
  210. /// See the
  211. /// <see href="../docs/navigation.html">
  212. /// Navigation
  213. /// Deep Dive
  214. /// </see>
  215. /// for more information.
  216. /// </para>
  217. /// </description>
  218. /// </item>
  219. /// <item>
  220. /// <term>Scrolling</term>
  221. /// <description>
  222. /// <para>
  223. /// The ability to scroll content is built into View. The <see cref="Viewport"/> represents the
  224. /// scrollable "viewport" into the View's Content Area (which is defined by the return value of
  225. /// <see cref="GetContentSize"/>).
  226. /// </para>
  227. /// <para>
  228. /// Terminal.Gui also provides the ability show a visual scroll bar that responds to mouse input. This
  229. /// ability is not enabled by default given how precious TUI screen real estate is.
  230. /// Use <see cref="VerticalScrollBar"/> and <see cref="HorizontalScrollBar"/> to enable this feature.
  231. /// </para>
  232. /// <para>
  233. /// Use <see cref="ViewportSettings"/> to adjust the behavior of scrolling.
  234. /// </para>
  235. /// <para>
  236. /// See the
  237. /// <see href="../docs/scrolling.html">
  238. /// Scrolling
  239. /// Deep Dive
  240. /// </see>
  241. /// for more information.
  242. /// </para>
  243. /// </description>
  244. /// </item>
  245. /// </list>
  246. /// <para>
  247. /// Views can opt in to more sophisticated initialization by implementing overrides to
  248. /// <see cref="ISupportInitialize.BeginInit"/> and <see cref="ISupportInitialize.EndInit"/> which will be called
  249. /// when the view is added to a <see cref="SuperView"/>.
  250. /// </para>
  251. /// <para>
  252. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/> can
  253. /// be implemented, in which case the <see cref="ISupportInitialize"/> methods will only be called if
  254. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  255. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on
  256. /// first run, instead of on every run.
  257. /// </para>
  258. /// <para>See <see href="../docs/keyboard.md"> for an overview of View keyboard handling.</see></para>
  259. /// </remarks>
  260. #endregion API Docs
  261. public partial class View : IDisposable, ISupportInitializeNotification
  262. {
  263. #region Constructors and Initialization
  264. /// <summary>Gets or sets arbitrary data for the view.</summary>
  265. /// <remarks>This property is not used internally.</remarks>
  266. public object? Data { get; set; }
  267. /// <summary>Gets or sets an identifier for the view;</summary>
  268. /// <value>The identifier.</value>
  269. /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
  270. public string Id { get; set; } = "";
  271. /// <summary>
  272. /// Points to the current driver in use by the view, it is a convenience property for simplifying the development
  273. /// of new views.
  274. /// </summary>
  275. public static IConsoleDriver? Driver => Application.Driver;
  276. /// <summary>Initializes a new instance of <see cref="View"/>.</summary>
  277. /// <remarks>
  278. /// <para>
  279. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  280. /// control the size and location of the view.
  281. /// </para>
  282. /// </remarks>
  283. public View ()
  284. {
  285. #if DEBUG_IDISPOSABLE
  286. Instances.Add (this);
  287. #endif
  288. SetupAdornments ();
  289. SetupCommands ();
  290. SetupKeyboard ();
  291. SetupMouse ();
  292. SetupText ();
  293. SetupScrollBars ();
  294. }
  295. /// <summary>
  296. /// Raised once when the <see cref="View"/> is being initialized for the first time. Allows
  297. /// configurations and assignments to be performed before the <see cref="View"/> being shown.
  298. /// View implements <see cref="ISupportInitializeNotification"/> to allow for more sophisticated initialization.
  299. /// </summary>
  300. public event EventHandler? Initialized;
  301. /// <summary>
  302. /// Get or sets if the <see cref="View"/> has been initialized (via <see cref="ISupportInitialize.BeginInit"/>
  303. /// and <see cref="ISupportInitialize.EndInit"/>).
  304. /// </summary>
  305. /// <para>
  306. /// If first-run-only initialization is preferred, overrides to
  307. /// <see cref="ISupportInitializeNotification.IsInitialized"/> can be implemented, in which case the
  308. /// <see cref="ISupportInitialize"/> methods will only be called if
  309. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  310. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on first
  311. /// run, instead of on every run.
  312. /// </para>
  313. public virtual bool IsInitialized { get; set; }
  314. /// <summary>Signals the View that initialization is starting. See <see cref="ISupportInitialize"/>.</summary>
  315. /// <remarks>
  316. /// <para>
  317. /// Views can opt-in to more sophisticated initialization by implementing overrides to
  318. /// <see cref="ISupportInitialize.BeginInit"/> and <see cref="ISupportInitialize.EndInit"/> which will be called
  319. /// when the <see cref="SuperView"/> is initialized.
  320. /// </para>
  321. /// <para>
  322. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/> can
  323. /// be implemented too, in which case the <see cref="ISupportInitialize"/> methods will only be called if
  324. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  325. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on
  326. /// first run, instead of on every run.
  327. /// </para>
  328. /// </remarks>
  329. public virtual void BeginInit ()
  330. {
  331. if (IsInitialized)
  332. {
  333. throw new InvalidOperationException ("The view is already initialized.");
  334. }
  335. #if AUTO_CANFOCUS
  336. _oldCanFocus = CanFocus;
  337. _oldTabIndex = _tabIndex;
  338. #endif
  339. BeginInitAdornments ();
  340. if (_subviews?.Count > 0)
  341. {
  342. foreach (View view in _subviews)
  343. {
  344. if (!view.IsInitialized)
  345. {
  346. view.BeginInit ();
  347. }
  348. }
  349. }
  350. }
  351. // TODO: Implement logic that allows EndInit to throw if BeginInit has not been called
  352. // TODO: See EndInit_Called_Without_BeginInit_Throws test.
  353. /// <summary>Signals the View that initialization is ending. See <see cref="ISupportInitialize"/>.</summary>
  354. /// <remarks>
  355. /// <para>Initializes all Subviews and Invokes the <see cref="Initialized"/> event.</para>
  356. /// </remarks>
  357. public virtual void EndInit ()
  358. {
  359. if (IsInitialized)
  360. {
  361. throw new InvalidOperationException ("The view is already initialized.");
  362. }
  363. IsInitialized = true;
  364. EndInitAdornments ();
  365. // TODO: Move these into ViewText.cs as EndInit_Text() to consolidate.
  366. // TODO: Verify UpdateTextDirection really needs to be called here.
  367. // These calls were moved from BeginInit as they access Viewport which is indeterminate until EndInit is called.
  368. UpdateTextDirection (TextDirection);
  369. UpdateTextFormatterText ();
  370. if (_subviews is { })
  371. {
  372. foreach (View view in _subviews)
  373. {
  374. if (!view.IsInitialized)
  375. {
  376. view.EndInit ();
  377. }
  378. }
  379. }
  380. // TODO: Figure out how to move this out of here and just depend on LayoutNeeded in Mainloop
  381. Layout (); // the EventLog in AllViewsTester fails to layout correctly if this is not here (convoluted Dim.Fill(Func)).
  382. SetNeedsLayout ();
  383. Initialized?.Invoke (this, EventArgs.Empty);
  384. }
  385. #endregion Constructors and Initialization
  386. #region Visibility
  387. private bool _enabled = true;
  388. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can respond to user interaction.</summary>
  389. public bool Enabled
  390. {
  391. get => _enabled;
  392. set
  393. {
  394. if (_enabled == value)
  395. {
  396. return;
  397. }
  398. _enabled = value;
  399. if (!_enabled && HasFocus)
  400. {
  401. HasFocus = false;
  402. }
  403. if (_enabled
  404. && CanFocus
  405. && Visible
  406. && !HasFocus
  407. && SuperView is null or { HasFocus: true, Visible: true, Enabled: true, Focused: null })
  408. {
  409. SetFocus ();
  410. }
  411. OnEnabledChanged ();
  412. SetNeedsDraw ();
  413. if (Border is { })
  414. {
  415. Border.Enabled = _enabled;
  416. }
  417. if (_subviews is null)
  418. {
  419. return;
  420. }
  421. foreach (View view in _subviews)
  422. {
  423. view.Enabled = Enabled;
  424. }
  425. }
  426. }
  427. /// <summary>Raised when the <see cref="Enabled"/> value is being changed.</summary>
  428. public event EventHandler? EnabledChanged;
  429. // TODO: Change this event to match the standard TG event model.
  430. /// <summary>Invoked when the <see cref="Enabled"/> property from a view is changed.</summary>
  431. public virtual void OnEnabledChanged () { EnabledChanged?.Invoke (this, EventArgs.Empty); }
  432. private bool _visible = true;
  433. // TODO: Remove virtual once Menu/MenuBar are removed. MenuBar is the only override.
  434. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> is visible.</summary>
  435. public virtual bool Visible
  436. {
  437. get => _visible;
  438. set
  439. {
  440. if (_visible == value)
  441. {
  442. return;
  443. }
  444. if (OnVisibleChanging ())
  445. {
  446. return;
  447. }
  448. CancelEventArgs<bool> args = new (in _visible, ref value);
  449. VisibleChanging?.Invoke (this, args);
  450. if (args.Cancel)
  451. {
  452. return;
  453. }
  454. _visible = value;
  455. if (!_visible)
  456. {
  457. if (HasFocus)
  458. {
  459. HasFocus = false;
  460. }
  461. }
  462. if (_visible
  463. && CanFocus
  464. && Enabled
  465. && !HasFocus
  466. && SuperView is null or { HasFocus: true, Visible: true, Enabled: true, Focused: null })
  467. {
  468. SetFocus ();
  469. }
  470. OnVisibleChanged ();
  471. VisibleChanged?.Invoke (this, EventArgs.Empty);
  472. SetNeedsLayout ();
  473. SuperView?.SetNeedsLayout ();
  474. SetNeedsDraw ();
  475. if (SuperView is { })
  476. {
  477. SuperView?.SetNeedsDraw ();
  478. }
  479. else
  480. {
  481. Application.ClearScreenNextIteration = true;
  482. }
  483. }
  484. }
  485. /// <summary>Called when <see cref="Visible"/> is changing. Can be cancelled by returning <see langword="true"/>.</summary>
  486. protected virtual bool OnVisibleChanging () { return false; }
  487. /// <summary>
  488. /// Raised when the <see cref="Visible"/> value is being changed. Can be cancelled by setting Cancel to
  489. /// <see langword="true"/>.
  490. /// </summary>
  491. public event EventHandler<CancelEventArgs<bool>>? VisibleChanging;
  492. /// <summary>Called when <see cref="Visible"/> has changed.</summary>
  493. protected virtual void OnVisibleChanged () { }
  494. /// <summary>Raised when <see cref="Visible"/> has changed.</summary>
  495. public event EventHandler? VisibleChanged;
  496. /// <summary>
  497. /// INTERNAL Indicates whether all views up the Superview hierarchy are visible.
  498. /// </summary>
  499. /// <param name="view">The view to test.</param>
  500. /// <returns>
  501. /// <see langword="false"/> if `view.Visible` is <see langword="false"/> or any Superview is not visible,
  502. /// <see langword="true"/> otherwise.
  503. /// </returns>
  504. internal static bool CanBeVisible (View view)
  505. {
  506. if (!view.Visible)
  507. {
  508. return false;
  509. }
  510. for (View? c = view.SuperView; c != null; c = c.SuperView)
  511. {
  512. if (!c.Visible)
  513. {
  514. return false;
  515. }
  516. }
  517. return true;
  518. }
  519. #endregion Visibility
  520. #region Title
  521. private string _title = string.Empty;
  522. /// <summary>Gets the <see cref="Gui.TextFormatter"/> used to format <see cref="Title"/>.</summary>
  523. internal TextFormatter TitleTextFormatter { get; init; } = new ();
  524. /// <summary>
  525. /// The title to be displayed for this <see cref="View"/>. The title will be displayed if <see cref="Border"/>.
  526. /// <see cref="Thickness.Top"/> is greater than 0. The title can be used to set the <see cref="HotKey"/>
  527. /// for the view by prefixing character with <see cref="HotKeySpecifier"/> (e.g. <c>"T_itle"</c>).
  528. /// </summary>
  529. /// <remarks>
  530. /// <para>
  531. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable Title-based hotkey support set
  532. /// <see cref="HotKeySpecifier"/> to <c>(Rune)0xffff</c>.
  533. /// </para>
  534. /// <para>
  535. /// Only the first HotKey specifier found in <see cref="Title"/> is supported.
  536. /// </para>
  537. /// <para>
  538. /// To cause the hotkey to be rendered with <see cref="Text"/>,
  539. /// set <c>View.</c><see cref="TextFormatter.HotKeySpecifier"/> to the desired character.
  540. /// </para>
  541. /// </remarks>
  542. /// <value>The title.</value>
  543. public string Title
  544. {
  545. get
  546. {
  547. #if DEBUG_IDISPOSABLE
  548. if (WasDisposed)
  549. {
  550. throw new ObjectDisposedException (GetType ().FullName);
  551. }
  552. #endif
  553. return _title;
  554. }
  555. set
  556. {
  557. #if DEBUG_IDISPOSABLE
  558. if (WasDisposed)
  559. {
  560. throw new ObjectDisposedException (GetType ().FullName);
  561. }
  562. #endif
  563. if (value == _title)
  564. {
  565. return;
  566. }
  567. if (!OnTitleChanging (ref value))
  568. {
  569. string old = _title;
  570. _title = value;
  571. TitleTextFormatter.Text = _title;
  572. SetTitleTextFormatterSize ();
  573. SetHotKeyFromTitle ();
  574. SetNeedsDraw ();
  575. #if DEBUG
  576. if (string.IsNullOrEmpty (Id))
  577. {
  578. Id = _title;
  579. }
  580. #endif // DEBUG
  581. OnTitleChanged ();
  582. }
  583. }
  584. }
  585. private void SetTitleTextFormatterSize ()
  586. {
  587. TitleTextFormatter.ConstrainToSize = new (
  588. TextFormatter.GetWidestLineLength (TitleTextFormatter.Text)
  589. - (TitleTextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  590. ? Math.Max (HotKeySpecifier.GetColumns (), 0)
  591. : 0),
  592. 1);
  593. }
  594. // TODO: Change this event to match the standard TG event model.
  595. /// <summary>Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.</summary>
  596. protected void OnTitleChanged () { TitleChanged?.Invoke (this, new (in _title)); }
  597. /// <summary>
  598. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can
  599. /// be cancelled.
  600. /// </summary>
  601. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  602. /// <returns>`true` if an event handler canceled the Title change.</returns>
  603. protected bool OnTitleChanging (ref string newTitle)
  604. {
  605. CancelEventArgs<string> args = new (ref _title, ref newTitle);
  606. TitleChanging?.Invoke (this, args);
  607. return args.Cancel;
  608. }
  609. /// <summary>Raised after the <see cref="View.Title"/> has been changed.</summary>
  610. public event EventHandler<EventArgs<string>>? TitleChanged;
  611. /// <summary>
  612. /// Raised when the <see cref="View.Title"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to `true`
  613. /// to cancel the Title change.
  614. /// </summary>
  615. public event EventHandler<CancelEventArgs<string>>? TitleChanging;
  616. #endregion
  617. /// <summary>Pretty prints the View</summary>
  618. /// <returns></returns>
  619. public override string ToString () { return $"{GetType ().Name}({Id}){Frame}"; }
  620. private bool _disposedValue;
  621. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  622. /// <remarks>
  623. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  624. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  625. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  626. /// </remarks>
  627. /// <param name="disposing"></param>
  628. protected virtual void Dispose (bool disposing)
  629. {
  630. LineCanvas.Dispose ();
  631. DisposeMouse ();
  632. DisposeKeyboard ();
  633. DisposeAdornments ();
  634. DisposeScrollBars ();
  635. for (int i = InternalSubviews.Count - 1; i >= 0; i--)
  636. {
  637. View subview = InternalSubviews [i];
  638. Remove (subview);
  639. subview.Dispose ();
  640. }
  641. if (!_disposedValue)
  642. {
  643. if (disposing)
  644. {
  645. // TODO: dispose managed state (managed objects)
  646. }
  647. _disposedValue = true;
  648. }
  649. Debug.Assert (InternalSubviews.Count == 0);
  650. }
  651. /// <summary>
  652. /// Riased when the <see cref="View"/> is being disposed.
  653. /// </summary>
  654. public event EventHandler? Disposing;
  655. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.</summary>
  656. public void Dispose ()
  657. {
  658. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  659. Disposing?.Invoke (this, EventArgs.Empty);
  660. Dispose (true);
  661. GC.SuppressFinalize (this);
  662. #if DEBUG_IDISPOSABLE
  663. WasDisposed = true;
  664. foreach (View instance in Instances.Where (x => x.WasDisposed).ToList ())
  665. {
  666. Instances.Remove (instance);
  667. }
  668. #endif
  669. }
  670. #if DEBUG_IDISPOSABLE
  671. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  672. public bool WasDisposed { get; set; }
  673. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  674. public int DisposedCount { get; set; } = 0;
  675. /// <summary>For debug purposes</summary>
  676. public static List<View> Instances { get; set; } = [];
  677. #endif
  678. }