View.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. #region API Docs
  6. /// <summary>
  7. /// View is the base class for all views on the screen and represents a visible element that can render itself and
  8. /// contains zero or more nested views, called SubViews. View provides basic functionality for layout, positioning,
  9. /// and drawing. In addition, View provides keyboard and mouse event handling.
  10. /// </summary>
  11. /// <remarks>
  12. /// <list type="table">
  13. /// <listheader>
  14. /// <term>Term</term><description>Definition</description>
  15. /// </listheader>
  16. /// <item>
  17. /// <term>SubView</term>
  18. /// <description>
  19. /// A View that is contained in another view and will be rendered as part of the containing view's
  20. /// ContentArea.
  21. /// SubViews are added to another view via the <see cref="View.Add(View)"/>` method. A View may only be a
  22. /// SubView of a single View.
  23. /// </description>
  24. /// </item>
  25. /// <item>
  26. /// <term>SuperView</term><description>The View that is a container for SubViews.</description>
  27. /// </item>
  28. /// </list>
  29. /// <para>
  30. /// Focus is a concept that is used to describe which View is currently receiving user input. Only Views that are
  31. /// <see cref="Enabled"/>, <see cref="Visible"/>, and <see cref="CanFocus"/> will receive focus.
  32. /// </para>
  33. /// <para>
  34. /// Views that are focusable should implement the <see cref="PositionCursor"/> to make sure that
  35. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  36. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  37. /// the last focused view. So views should make sure that they place the cursor
  38. /// in a visually sensible place.
  39. /// </para>
  40. /// <para>
  41. /// The View defines the base functionality for user interface elements in Terminal.Gui. Views
  42. /// can contain one or more subviews, can respond to user input and render themselves on the screen.
  43. /// </para>
  44. /// <para>
  45. /// View supports two layout styles: <see cref="LayoutStyle.Absolute"/> or <see cref="LayoutStyle.Computed"/>.
  46. /// The style is determined by the values of <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and
  47. /// <see cref="Height"/>.
  48. /// If any of these is set to non-absolute <see cref="Pos"/> or <see cref="Dim"/> object,
  49. /// then the layout style is <see cref="LayoutStyle.Computed"/>. Otherwise it is <see cref="LayoutStyle.Absolute"/>.
  50. /// </para>
  51. /// <para>
  52. /// To create a View using Absolute layout, call a constructor that takes a
  53. /// Rect parameter to specify the absolute position and size or simply set <see cref="View.Frame "/>). To create a View
  54. /// using Computed layout use a constructor that does not take a Rect parameter and set the X, Y, Width and Height
  55. /// properties on the view to non-absolute values. Both approaches use coordinates that are relative to the
  56. /// <see cref="Bounds"/> of the <see cref="SuperView"/> the View is added to.
  57. /// </para>
  58. /// <para>
  59. /// Computed layout is more flexible and supports dynamic console apps where controls adjust layout
  60. /// as the terminal resizes or other Views change size or position. The
  61. /// <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and
  62. /// <see cref="Height"/> properties are <see cref="Dim"/> and <see cref="Pos"/> objects that dynamically update the
  63. /// position of a view.
  64. /// The X and Y properties are of type <see cref="Pos"/>
  65. /// and you can use either absolute positions, percentages, or anchor
  66. /// points. The Width and Height properties are of type
  67. /// <see cref="Dim"/> and can use absolute position,
  68. /// percentages, and anchors. These are useful as they will take
  69. /// care of repositioning views when view's adornments are resized or
  70. /// if the terminal size changes.
  71. /// </para>
  72. /// <para>
  73. /// Absolute layout requires specifying coordinates and sizes of Views explicitly, and the
  74. /// View will typically stay in a fixed position and size. To change the position and size use the
  75. /// <see cref="Frame"/> property.
  76. /// </para>
  77. /// <para>
  78. /// Subviews (child views) can be added to a View by calling the <see cref="Add(View)"/> method.
  79. /// The container of a View can be accessed with the <see cref="SuperView"/> property.
  80. /// </para>
  81. /// <para>
  82. /// To flag a region of the View's <see cref="Bounds"/> to be redrawn call <see cref="SetNeedsDisplay(Rect)"/>.
  83. /// To flag the entire view for redraw call <see cref="SetNeedsDisplay()"/>.
  84. /// </para>
  85. /// <para>
  86. /// The <see cref="LayoutSubviews"/> method is invoked when the size or layout of a view has
  87. /// changed. The default processing system will keep the size and dimensions
  88. /// for views that use the <see cref="LayoutStyle.Absolute"/>, and will recompute the
  89. /// Adornments for the views that use <see cref="LayoutStyle.Computed"/>.
  90. /// </para>
  91. /// <para>
  92. /// Views have a <see cref="ColorScheme"/> property that defines the default colors that subviews
  93. /// should use for rendering. This ensures that the views fit in the context where
  94. /// they are being used, and allows for themes to be plugged in. For example, the
  95. /// default colors for windows and Toplevels uses a blue background, while it uses
  96. /// a white background for dialog boxes and a red background for errors.
  97. /// </para>
  98. /// <para>
  99. /// Subclasses should not rely on <see cref="ColorScheme"/> being
  100. /// set at construction time. If a <see cref="ColorScheme"/> is not set on a view, the view will inherit the
  101. /// value from its <see cref="SuperView"/> and the value might only be valid once a view has been
  102. /// added to a SuperView.
  103. /// </para>
  104. /// <para>
  105. /// By using <see cref="ColorScheme"/> applications will work both
  106. /// in color as well as black and white displays.
  107. /// </para>
  108. /// <para>
  109. /// Views can also opt-in to more sophisticated initialization
  110. /// by implementing overrides to <see cref="ISupportInitialize.BeginInit"/> and
  111. /// <see cref="ISupportInitialize.EndInit"/> which will be called
  112. /// when the view is added to a <see cref="SuperView"/>.
  113. /// </para>
  114. /// <para>
  115. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/>
  116. /// can be implemented, in which case the <see cref="ISupportInitialize"/>
  117. /// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
  118. /// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
  119. /// to override base class layout code optimally by doing so only on first run,
  120. /// instead of on every run.
  121. /// </para>
  122. /// <para>
  123. /// See <see href="../docs/keyboard.md">for an overview of View keyboard handling.</see>
  124. /// </para>
  125. /// ///
  126. /// </remarks>
  127. #endregion API Docs
  128. public partial class View : Responder, ISupportInitializeNotification {
  129. bool _oldEnabled;
  130. string _title = string.Empty;
  131. /// <summary>
  132. /// Points to the current driver in use by the view, it is a convenience property
  133. /// for simplifying the development of new views.
  134. /// </summary>
  135. public static ConsoleDriver Driver => Application.Driver;
  136. /// <summary>
  137. /// Gets or sets arbitrary data for the view.
  138. /// </summary>
  139. /// <remarks>This property is not used internally.</remarks>
  140. public object Data { get; set; }
  141. /// <summary>
  142. /// Gets or sets an identifier for the view;
  143. /// </summary>
  144. /// <value>The identifier.</value>
  145. /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
  146. public string Id { get; set; } = "";
  147. /// <summary>
  148. /// The title to be displayed for this <see cref="View"/>. The title will be displayed if <see cref="Border"/>.
  149. /// <see cref="Thickness.Top"/>
  150. /// is greater than 0.
  151. /// </summary>
  152. /// <value>The title.</value>
  153. public string Title {
  154. get => _title;
  155. set {
  156. if (!OnTitleChanging (_title, value)) {
  157. var old = _title;
  158. _title = value;
  159. SetNeedsDisplay ();
  160. #if DEBUG
  161. if (_title != null && string.IsNullOrEmpty (Id)) {
  162. Id = _title;
  163. }
  164. #endif // DEBUG
  165. OnTitleChanged (old, _title);
  166. }
  167. }
  168. }
  169. /// <inheritdoc/>
  170. public override bool Enabled {
  171. get => base.Enabled;
  172. set {
  173. if (base.Enabled != value) {
  174. if (value) {
  175. if (SuperView == null || SuperView?.Enabled == true) {
  176. base.Enabled = value;
  177. }
  178. } else {
  179. base.Enabled = value;
  180. }
  181. if (!value && HasFocus) {
  182. SetHasFocus (false, this);
  183. }
  184. OnEnabledChanged ();
  185. SetNeedsDisplay ();
  186. if (_subviews != null) {
  187. foreach (var view in _subviews) {
  188. if (!value) {
  189. view._oldEnabled = view.Enabled;
  190. view.Enabled = false;
  191. } else {
  192. view.Enabled = view._oldEnabled;
  193. view._addingView = false;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Gets or sets whether a view is cleared if the <see cref="Visible"/> property is <see langword="false"/>.
  202. /// </summary>
  203. public bool ClearOnVisibleFalse { get; set; } = true;
  204. /// <inheritdoc/>
  205. /// >
  206. public override bool Visible {
  207. get => base.Visible;
  208. set {
  209. if (base.Visible != value) {
  210. base.Visible = value;
  211. if (!value) {
  212. if (HasFocus) {
  213. SetHasFocus (false, this);
  214. }
  215. if (ClearOnVisibleFalse) {
  216. Clear ();
  217. }
  218. }
  219. OnVisibleChanged ();
  220. SetNeedsDisplay ();
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be
  226. /// cancelled.
  227. /// </summary>
  228. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  229. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  230. /// <returns>`true` if an event handler canceled the Title change.</returns>
  231. public virtual bool OnTitleChanging (string oldTitle, string newTitle)
  232. {
  233. var args = new TitleEventArgs (oldTitle, newTitle);
  234. TitleChanging?.Invoke (this, args);
  235. return args.Cancel;
  236. }
  237. /// <summary>
  238. /// Event fired when the <see cref="View.Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  239. /// `true` to cancel the Title change.
  240. /// </summary>
  241. public event EventHandler<TitleEventArgs> TitleChanging;
  242. /// <summary>
  243. /// Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  244. /// </summary>
  245. /// <param name="oldTitle">The <see cref="View.Title"/> that is/has been replaced.</param>
  246. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  247. public virtual void OnTitleChanged (string oldTitle, string newTitle)
  248. {
  249. var args = new TitleEventArgs (oldTitle, newTitle);
  250. TitleChanged?.Invoke (this, args);
  251. }
  252. /// <summary>
  253. /// Event fired after the <see cref="View.Title"/> has been changed.
  254. /// </summary>
  255. public event EventHandler<TitleEventArgs> TitleChanged;
  256. /// <summary>
  257. /// Event fired when the <see cref="Enabled"/> value is being changed.
  258. /// </summary>
  259. public event EventHandler EnabledChanged;
  260. /// <inheritdoc/>
  261. public override void OnEnabledChanged () => EnabledChanged?.Invoke (this, EventArgs.Empty);
  262. /// <summary>
  263. /// Event fired when the <see cref="Visible"/> value is being changed.
  264. /// </summary>
  265. public event EventHandler VisibleChanged;
  266. /// <inheritdoc/>
  267. public override void OnVisibleChanged () => VisibleChanged?.Invoke (this, EventArgs.Empty);
  268. bool CanBeVisible (View view)
  269. {
  270. if (!view.Visible) {
  271. return false;
  272. }
  273. for (var c = view.SuperView; c != null; c = c.SuperView) {
  274. if (!c.Visible) {
  275. return false;
  276. }
  277. }
  278. return true;
  279. }
  280. /// <summary>
  281. /// Pretty prints the View
  282. /// </summary>
  283. /// <returns></returns>
  284. public override string ToString () => $"{GetType ().Name}({Id}){Frame}";
  285. /// <inheritdoc/>
  286. protected override void Dispose (bool disposing)
  287. {
  288. LineCanvas.Dispose ();
  289. Margin?.Dispose ();
  290. Margin = null;
  291. Border?.Dispose ();
  292. Border = null;
  293. Padding?.Dispose ();
  294. Padding = null;
  295. for (var i = InternalSubviews.Count - 1; i >= 0; i--) {
  296. var subview = InternalSubviews [i];
  297. Remove (subview);
  298. subview.Dispose ();
  299. }
  300. base.Dispose (disposing);
  301. Debug.Assert (InternalSubviews.Count == 0);
  302. }
  303. #region Constructors and Initialization
  304. /// <summary>
  305. /// Initializes a new instance of a <see cref="View"/> class with the absolute
  306. /// dimensions specified in the <paramref name="frame"/> parameter.
  307. /// </summary>
  308. /// <param name="frame">The region covered by this view.</param>
  309. /// <remarks>
  310. /// <para>
  311. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  312. /// control the size and location of the view.
  313. /// The <see cref="View"/> will be created using <see cref="LayoutStyle.Absolute"/>
  314. /// coordinates. The initial size (<see cref="View.Frame"/>) will be
  315. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  316. /// </para>
  317. /// <para>
  318. /// If <see cref="Height"/> is greater than one, word wrapping is provided.
  319. /// </para>
  320. /// <para>
  321. /// This constructor initialize a View with a <see cref="LayoutStyle"/> of
  322. /// <see cref="LayoutStyle.Absolute"/>.
  323. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  324. /// control the size and location of the view, changing it to <see cref="LayoutStyle.Computed"/>.
  325. /// </para>
  326. /// </remarks>
  327. public View (Rect frame) : this (frame, null) { }
  328. /// <summary>
  329. /// Initializes a new instance of <see cref="View"/>.
  330. /// </summary>
  331. /// <remarks>
  332. /// <para>
  333. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  334. /// control the size and location of the view.
  335. /// The <see cref="View"/> will be created using <see cref="LayoutStyle.Absolute"/>
  336. /// coordinates. The initial size (<see cref="View.Frame"/>) will be
  337. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  338. /// </para>
  339. /// <para>
  340. /// If <see cref="Height"/> is greater than one, word wrapping is provided.
  341. /// </para>
  342. /// <para>
  343. /// This constructor initialize a View with a <see cref="LayoutStyle"/> of
  344. /// <see cref="LayoutStyle.Absolute"/>.
  345. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  346. /// control the size and location of the view, changing it to <see cref="LayoutStyle.Computed"/>.
  347. /// </para>
  348. /// </remarks>
  349. public View () : this (string.Empty) { }
  350. /// <summary>
  351. /// Initializes a new instance of <see cref="View"/> in at the position specified with the
  352. /// dimensions specified in the <paramref name="x"/> and <paramref name="y"/> parameters.
  353. /// </summary>
  354. /// <remarks>
  355. /// <para>
  356. /// The <see cref="View"/> will be created at the given
  357. /// coordinates with the given string. The size (<see cref="View.Frame"/>) will be
  358. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  359. /// </para>
  360. /// <para>
  361. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  362. /// control the size and location of the view.
  363. /// </para>
  364. /// <para>
  365. /// If <see cref="Height"/> is greater than one, word wrapping is provided.
  366. /// </para>
  367. /// <para>
  368. /// This constructor initialize a View with a <see cref="LayoutStyle"/> of
  369. /// <see cref="LayoutStyle.Absolute"/>.
  370. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  371. /// control the size and location of the view, changing it to <see cref="LayoutStyle.Computed"/>.
  372. /// </para>
  373. /// </remarks>
  374. /// <param name="x">column to locate the View.</param>
  375. /// <param name="y">row to locate the View.</param>
  376. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  377. public View (int x, int y, string text) : this (TextFormatter.CalcRect (x, y, text), text) { }
  378. /// <summary>
  379. /// Initializes a new instance of a <see cref="View"/> class with the absolute
  380. /// dimensions specified in the <paramref name="frame"/> parameter.
  381. /// </summary>
  382. /// <remarks>
  383. /// <para>
  384. /// The <see cref="View"/> will be created at the given
  385. /// coordinates with the given string. The size (<see cref="View.Frame"/>) will be
  386. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  387. /// </para>
  388. /// <para>
  389. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  390. /// control the size and location of the view.
  391. /// </para>
  392. /// <para>
  393. /// If <see cref="Height"/> is greater than one, word wrapping is provided.
  394. /// </para>
  395. /// <para>
  396. /// This constructor initialize a View with a <see cref="LayoutStyle"/> of
  397. /// <see cref="LayoutStyle.Absolute"/>.
  398. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  399. /// control the size and location of the view, changing it to <see cref="LayoutStyle.Computed"/>.
  400. /// </para>
  401. /// </remarks>
  402. /// <param name="frame">Location.</param>
  403. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  404. public View (Rect frame, string text) => SetInitialProperties (text, frame, LayoutStyle.Absolute);
  405. /// <summary>
  406. /// Initializes a new instance of a <see cref="View"/> class with using the given text and text styling information.
  407. /// </summary>
  408. /// <remarks>
  409. /// <para>
  410. /// If <see cref="Height"/> is greater than one, word wrapping is provided.
  411. /// </para>
  412. /// <para>
  413. /// The <see cref="View"/> will be created at the given
  414. /// coordinates with the given string. The size (<see cref="View.Frame"/>) will be
  415. /// adjusted to fit the contents of <see cref="Text"/>, including newlines ('\n') for multiple lines.
  416. /// </para>
  417. /// <para>
  418. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  419. /// control the size and location of the view.
  420. /// </para>
  421. /// <para>
  422. /// If <see cref="Height"/> is greater than one, word wrapping is provided.
  423. /// </para>
  424. /// <para>
  425. /// This constructor initialize a View with a <see cref="LayoutStyle"/> of
  426. /// <see cref="LayoutStyle.Absolute"/>.
  427. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  428. /// control the size and location of the view, changing it to <see cref="LayoutStyle.Computed"/>.
  429. /// </para>
  430. /// </remarks>
  431. /// <param name="text">text to initialize the <see cref="Text"/> property with.</param>
  432. /// <param name="direction">The text direction.</param>
  433. public View (string text, TextDirection direction = TextDirection.LeftRight_TopBottom) => SetInitialProperties (text, Rect.Empty, LayoutStyle.Computed, direction);
  434. // TODO: v2 - Remove constructors with parameters
  435. /// <summary>
  436. /// Private helper to set the initial properties of the View that were provided via constructors.
  437. /// </summary>
  438. /// <param name="text"></param>
  439. /// <param name="rect"></param>
  440. /// <param name="layoutStyle"></param>
  441. /// <param name="direction"></param>
  442. void SetInitialProperties (string text,
  443. Rect rect,
  444. LayoutStyle layoutStyle = LayoutStyle.Computed,
  445. TextDirection direction = TextDirection.LeftRight_TopBottom)
  446. {
  447. TextFormatter = new TextFormatter ();
  448. TextFormatter.HotKeyChanged += TextFormatter_HotKeyChanged;
  449. TextDirection = direction;
  450. CanFocus = false;
  451. TabIndex = -1;
  452. TabStop = false;
  453. Text = text == null ? string.Empty : text;
  454. Frame = rect.IsEmpty ? TextFormatter.CalcRect (0, 0, text, direction) : rect;
  455. AddCommands ();
  456. Margin = CreateAdornment (typeof (Margin)) as Margin;
  457. Border = CreateAdornment (typeof (Border)) as Border;
  458. Padding = CreateAdornment (typeof (Padding)) as Padding;
  459. }
  460. /// <summary>
  461. /// Get or sets if the <see cref="View"/> has been initialized (via <see cref="ISupportInitialize.BeginInit"/>
  462. /// and <see cref="ISupportInitialize.EndInit"/>).
  463. /// </summary>
  464. /// <para>
  465. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification.IsInitialized"/>
  466. /// can be implemented, in which case the <see cref="ISupportInitialize"/>
  467. /// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
  468. /// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
  469. /// to override base class layout code optimally by doing so only on first run,
  470. /// instead of on every run.
  471. /// </para>
  472. public virtual bool IsInitialized { get; set; }
  473. /// <summary>
  474. /// Signals the View that initialization is starting. See <see cref="ISupportInitialize"/>.
  475. /// </summary>
  476. /// <remarks>
  477. /// <para>
  478. /// Views can opt-in to more sophisticated initialization
  479. /// by implementing overrides to <see cref="ISupportInitialize.BeginInit"/> and
  480. /// <see cref="ISupportInitialize.EndInit"/> which will be called
  481. /// when the view is added to a <see cref="SuperView"/>.
  482. /// </para>
  483. /// <para>
  484. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/>
  485. /// can be implemented too, in which case the <see cref="ISupportInitialize"/>
  486. /// methods will only be called if <see cref="ISupportInitializeNotification.IsInitialized"/>
  487. /// is <see langword="false"/>. This allows proper <see cref="View"/> inheritance hierarchies
  488. /// to override base class layout code optimally by doing so only on first run,
  489. /// instead of on every run.
  490. /// </para>
  491. /// </remarks>
  492. public virtual void BeginInit ()
  493. {
  494. if (!IsInitialized) {
  495. _oldCanFocus = CanFocus;
  496. _oldTabIndex = _tabIndex;
  497. }
  498. //throw new InvalidOperationException ("The view is already initialized.");
  499. if (_subviews?.Count > 0) {
  500. foreach (var view in _subviews) {
  501. if (!view.IsInitialized) {
  502. view.BeginInit ();
  503. }
  504. }
  505. }
  506. }
  507. /// <summary>
  508. /// Signals the View that initialization is ending. See <see cref="ISupportInitialize"/>.
  509. /// </summary>
  510. public void EndInit ()
  511. {
  512. IsInitialized = true;
  513. // These calls were moved from BeginInit as they access Bounds which is indeterminate until EndInit is called.
  514. UpdateTextDirection (TextDirection);
  515. UpdateTextFormatterText ();
  516. SetHotKey ();
  517. OnResizeNeeded ();
  518. if (_subviews != null) {
  519. foreach (var view in _subviews) {
  520. if (!view.IsInitialized) {
  521. view.EndInit ();
  522. }
  523. }
  524. }
  525. Initialized?.Invoke (this, EventArgs.Empty);
  526. }
  527. #endregion Constructors and Initialization
  528. }