View.Drawing.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View // Drawing APIs
  5. {
  6. #region Drawing Primitives
  7. /// <summary>Moves the drawing cursor to the specified <see cref="Viewport"/>-relative location in the view.</summary>
  8. /// <remarks>
  9. /// <para>
  10. /// If the provided coordinates are outside the visible content area, this method does nothing.
  11. /// </para>
  12. /// <para>
  13. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  14. /// </para>
  15. /// </remarks>
  16. /// <param name="col">Column (viewport-relative).</param>
  17. /// <param name="row">Row (viewport-relative).</param>
  18. public bool Move (int col, int row)
  19. {
  20. if (Driver is null || Driver?.Rows == 0)
  21. {
  22. return false;
  23. }
  24. if (col < 0 || row < 0 || col >= Viewport.Width || row >= Viewport.Height)
  25. {
  26. return false;
  27. }
  28. Point screen = ViewportToScreen (new Point (col, row));
  29. Driver?.Move (screen.X, screen.Y);
  30. return true;
  31. }
  32. /// <summary>Draws the specified character in the specified viewport-relative column and row of the View.</summary>
  33. /// <para>
  34. /// If the provided coordinates are outside the visible content area, this method does nothing.
  35. /// </para>
  36. /// <remarks>
  37. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  38. /// </remarks>
  39. /// <param name="col">Column (viewport-relative).</param>
  40. /// <param name="row">Row (viewport-relative).</param>
  41. /// <param name="rune">The Rune.</param>
  42. public void AddRune (int col, int row, Rune rune)
  43. {
  44. if (Move (col, row))
  45. {
  46. Driver?.AddRune (rune);
  47. }
  48. }
  49. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  50. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  51. /// <param name="hotColor">Hot color.</param>
  52. /// <param name="normalColor">Normal color.</param>
  53. /// <remarks>
  54. /// <para>
  55. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  56. /// default.
  57. /// </para>
  58. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  59. /// </remarks>
  60. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  61. {
  62. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  63. Application.Driver?.SetAttribute (normalColor);
  64. foreach (Rune rune in text.EnumerateRunes ())
  65. {
  66. if (rune == new Rune (hotkeySpec.Value))
  67. {
  68. Application.Driver?.SetAttribute (hotColor);
  69. continue;
  70. }
  71. Application.Driver?.AddRune (rune);
  72. Application.Driver?.SetAttribute (normalColor);
  73. }
  74. }
  75. /// <summary>
  76. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  77. /// state.
  78. /// </summary>
  79. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  80. /// <param name="focused">
  81. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  82. /// the regular ones.
  83. /// </param>
  84. public void DrawHotString (string text, bool focused)
  85. {
  86. if (focused)
  87. {
  88. DrawHotString (text, GetHotFocusColor (), GetFocusColor ());
  89. }
  90. else
  91. {
  92. DrawHotString (
  93. text,
  94. Enabled ? GetHotNormalColor () : ColorScheme!.Disabled,
  95. Enabled ? GetNormalColor () : ColorScheme!.Disabled
  96. );
  97. }
  98. }
  99. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  100. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  101. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  102. public void FillRect (Rectangle rect, Color? color = null)
  103. {
  104. if (Driver is null)
  105. {
  106. return;
  107. }
  108. // Get screen-relative coords
  109. Rectangle toClear = ViewportToScreen (rect);
  110. Rectangle prevClip = Driver.Clip;
  111. Driver.Clip = Rectangle.Intersect (prevClip, ViewportToScreen (Viewport with { Location = new (0, 0) }));
  112. Attribute prev = Driver.SetAttribute (new (color ?? GetNormalColor ().Background));
  113. Driver.FillRect (toClear);
  114. Driver.SetAttribute (prev);
  115. Driver.Clip = prevClip;
  116. }
  117. #endregion Drawing Primitives
  118. #region Clipping
  119. /// <summary>Sets the <see cref="ConsoleDriver"/>'s clip region to <see cref="Viewport"/>.</summary>
  120. /// <remarks>
  121. /// <para>
  122. /// By default, the clip rectangle is set to the intersection of the current clip region and the
  123. /// <see cref="Viewport"/>. This ensures that drawing is constrained to the viewport, but allows
  124. /// content to be drawn beyond the viewport.
  125. /// </para>
  126. /// <para>
  127. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClipContentOnly"/> set, clipping will be
  128. /// applied to just the visible content area.
  129. /// </para>
  130. /// </remarks>
  131. /// <returns>
  132. /// The current screen-relative clip region, which can be then re-applied by setting
  133. /// <see cref="ConsoleDriver.Clip"/>.
  134. /// </returns>
  135. public Rectangle SetClip ()
  136. {
  137. if (Driver is null)
  138. {
  139. return Rectangle.Empty;
  140. }
  141. Rectangle previous = Driver.Clip;
  142. // Clamp the Clip to the entire visible area
  143. Rectangle clip = Rectangle.Intersect (ViewportToScreen (Viewport with { Location = Point.Empty }), previous);
  144. if (ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly))
  145. {
  146. // Clamp the Clip to the just content area that is within the viewport
  147. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  148. clip = Rectangle.Intersect (clip, visibleContent);
  149. }
  150. Driver.Clip = clip;
  151. return previous;
  152. }
  153. #endregion Clipping
  154. #region Drawing Engine
  155. /// <summary>
  156. /// Draws the view if it needs to be drawn.
  157. /// </summary>
  158. /// <remarks>
  159. /// <para>
  160. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDisplay"/>,
  161. /// <see cref="SubViewNeedsDisplay"/>,
  162. /// or <see cref="NeedsLayout"/> set.
  163. /// </para>
  164. /// <para>
  165. /// // TODO: Add docs for the drawing process.
  166. /// </para>
  167. /// </remarks>
  168. public void Draw ()
  169. {
  170. if (!CanBeVisible (this))
  171. {
  172. return;
  173. }
  174. if (!NeedsDisplay && !SubViewNeedsDisplay)
  175. {
  176. return;
  177. }
  178. DoDrawAdornments ();
  179. if (ColorScheme is { })
  180. {
  181. Driver?.SetAttribute (GetNormalColor ());
  182. }
  183. // By default, we clip to the viewport preventing drawing outside the viewport
  184. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  185. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  186. Rectangle prevClip = SetClip ();
  187. // Clear Viewport
  188. DoClearViewport (Viewport);
  189. // Draw Text
  190. DoDrawText (Viewport);
  191. // Draw Content
  192. DoDrawContent (Viewport);
  193. // Draw Subviews
  194. DoDrawSubviews (Viewport);
  195. if (Driver is { })
  196. {
  197. Driver.Clip = prevClip;
  198. }
  199. DoRenderLineCanvas ();
  200. DoDrawAdornmentSubViews ();
  201. ClearNeedsDisplay ();
  202. // We're done
  203. DoDrawComplete (Viewport);
  204. }
  205. #region DrawAdornments
  206. private void DoDrawAdornmentSubViews ()
  207. {
  208. if (Margin?.Subviews is { })
  209. {
  210. foreach (View subview in Margin.Subviews)
  211. {
  212. subview.SetNeedsDisplay ();
  213. }
  214. Margin?.DoDrawSubviews (Margin.Viewport);
  215. }
  216. if (Border?.Subviews is { })
  217. {
  218. foreach (View subview in Border.Subviews)
  219. {
  220. subview.SetNeedsDisplay ();
  221. }
  222. Border?.DoDrawSubviews (Border.Viewport);
  223. }
  224. if (Padding?.Subviews is { })
  225. {
  226. foreach (View subview in Padding.Subviews)
  227. {
  228. subview.SetNeedsDisplay ();
  229. }
  230. Padding?.DoDrawSubviews (Padding.Viewport);
  231. }
  232. }
  233. // TODO: Make private and change menuBar and Tab to not use
  234. internal void DoDrawAdornments ()
  235. {
  236. if (OnDrawingAdornments ())
  237. {
  238. return;
  239. }
  240. // TODO: add event.
  241. // TODO: add a DrawAdornments method
  242. // Each of these renders lines to either this View's LineCanvas
  243. // Those lines will be finally rendered in OnRenderLineCanvas
  244. Margin?.Draw ();
  245. Border?.Draw ();
  246. Padding?.Draw ();
  247. }
  248. /// <summary>
  249. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  250. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  251. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  252. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  253. /// </summary>
  254. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  255. protected virtual bool OnDrawingAdornments () { return false; }
  256. #endregion DrawAdornments
  257. #region ClearViewport
  258. private void DoClearViewport (Rectangle viewport)
  259. {
  260. Debug.Assert (viewport == Viewport);
  261. if (OnClearingViewport (Viewport))
  262. {
  263. return;
  264. }
  265. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  266. ClearingViewport?.Invoke (this, dev);
  267. if (dev.Cancel)
  268. {
  269. return;
  270. }
  271. ClearViewport ();
  272. }
  273. /// <summary>
  274. /// Called when the <see cref="Viewport"/> is to be cleared.
  275. /// </summary>
  276. /// <param name="viewport"></param>
  277. /// <returns><see langword="true"/> to stop further clearing.</returns>
  278. protected virtual bool OnClearingViewport (Rectangle viewport) { return false; }
  279. /// <summary>Event invoked when the content area of the View is to be drawn.</summary>
  280. /// <remarks>
  281. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  282. /// <para>
  283. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  284. /// <see cref="View"/> .
  285. /// </para>
  286. /// </remarks>
  287. public event EventHandler<DrawEventArgs>? ClearingViewport;
  288. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  289. /// <remarks>
  290. /// <para>
  291. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  292. /// the portion of the content
  293. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  294. /// a
  295. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  296. /// enabled) and want
  297. /// the area outside the content to be visually distinct.
  298. /// </para>
  299. /// </remarks>
  300. public void ClearViewport ()
  301. {
  302. if (Driver is null)
  303. {
  304. return;
  305. }
  306. // Get screen-relative coords
  307. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  308. Rectangle prevClip = Driver.Clip;
  309. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  310. {
  311. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  312. toClear = Rectangle.Intersect (toClear, visibleContent);
  313. }
  314. Attribute prev = Driver.SetAttribute (GetNormalColor ());
  315. Driver.FillRect (toClear);
  316. Driver.SetAttribute (prev);
  317. Driver.Clip = prevClip;
  318. SetNeedsDisplay ();
  319. }
  320. #endregion ClearViewport
  321. #region DrawText
  322. private void DoDrawText (Rectangle viewport)
  323. {
  324. Debug.Assert (viewport == Viewport);
  325. if (OnDrawingText (Viewport))
  326. {
  327. return;
  328. }
  329. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  330. DrawingText?.Invoke (this, dev);
  331. if (dev.Cancel)
  332. {
  333. return;
  334. }
  335. DrawText ();
  336. }
  337. /// <summary>
  338. /// Called when the <see cref="Text"/> of the View is to be drawn.
  339. /// </summary>
  340. /// <param name="viewport"></param>
  341. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  342. protected virtual bool OnDrawingText (Rectangle viewport) { return false; }
  343. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  344. /// <returns>
  345. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  346. /// <see cref="Text"/>.
  347. /// </returns>
  348. public event EventHandler<DrawEventArgs>? DrawingText;
  349. /// <summary>
  350. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  351. /// </summary>
  352. public void DrawText ()
  353. {
  354. if (!string.IsNullOrEmpty (TextFormatter.Text))
  355. {
  356. TextFormatter.NeedsFormat = true;
  357. }
  358. // This should NOT clear
  359. // TODO: If the output is not in the Viewport, do nothing
  360. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  361. TextFormatter?.Draw (
  362. drawRect,
  363. HasFocus ? GetFocusColor () : GetNormalColor (),
  364. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  365. Rectangle.Empty
  366. );
  367. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  368. SetSubViewNeedsDisplay ();
  369. }
  370. #endregion DrawText
  371. #region DrawContent
  372. private void DoDrawContent (Rectangle viewport)
  373. {
  374. Debug.Assert (viewport == Viewport);
  375. if (OnDrawingContent (Viewport))
  376. {
  377. return;
  378. }
  379. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  380. DrawingContent?.Invoke (this, dev);
  381. if (dev.Cancel)
  382. { }
  383. // Do nothing.
  384. }
  385. /// <summary>
  386. /// Called when the View's content is to be drawn. The default implementation does nothing.
  387. /// </summary>
  388. /// <remarks>
  389. /// </remarks>
  390. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  391. protected virtual bool OnDrawingContent (Rectangle viewport) { return false; }
  392. /// <summary>Raised when the View's content is to be drawn.</summary>
  393. /// <remarks>
  394. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  395. /// <para>
  396. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  397. /// <see cref="View"/> .
  398. /// </para>
  399. /// </remarks>
  400. public event EventHandler<DrawEventArgs>? DrawingContent;
  401. #endregion DrawContent
  402. #region DrawSubviews
  403. private void DoDrawSubviews (Rectangle viewport)
  404. {
  405. Debug.Assert (viewport == Viewport);
  406. if (OnDrawingSubviews (Viewport))
  407. {
  408. return;
  409. }
  410. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  411. DrawingSubviews?.Invoke (this, dev);
  412. if (dev.Cancel)
  413. {
  414. return;
  415. }
  416. DrawSubviews ();
  417. }
  418. /// <summary>
  419. /// Called when the <see cref="Subviews"/> are to be drawn.
  420. /// </summary>
  421. /// <param name="viewport"></param>
  422. /// <returns><see langword="true"/> to stop further drawing of <see cref="Subviews"/>.</returns>
  423. protected virtual bool OnDrawingSubviews (Rectangle viewport) { return false; }
  424. /// <summary>Raised when the <see cref="Subviews"/> are to be drawn.</summary>
  425. /// <remarks>
  426. /// </remarks>
  427. /// <returns>
  428. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  429. /// <see cref="Subviews"/>.
  430. /// </returns>
  431. public event EventHandler<DrawEventArgs>? DrawingSubviews;
  432. /// <summary>
  433. /// Draws the <see cref="Subviews"/>.
  434. /// </summary>
  435. public void DrawSubviews ()
  436. {
  437. if (_subviews is null || !SubViewNeedsDisplay)
  438. {
  439. return;
  440. }
  441. IEnumerable<View> subviewsNeedingDraw = _subviews.Where (view => view.Visible && (view.NeedsDisplay || view.SubViewNeedsDisplay));
  442. foreach (View view in subviewsNeedingDraw)
  443. {
  444. view.Draw ();
  445. }
  446. }
  447. #endregion DrawSubviews
  448. #region DrawLineCanvas
  449. internal void DoRenderLineCanvas ()
  450. {
  451. if (OnRenderingLineCanvas ())
  452. {
  453. return;
  454. }
  455. // TODO: Add event
  456. RenderLineCanvas ();
  457. }
  458. /// <summary>
  459. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  460. /// </summary>
  461. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  462. protected virtual bool OnRenderingLineCanvas () { return false; }
  463. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  464. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  465. public LineCanvas LineCanvas { get; } = new ();
  466. /// <summary>
  467. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  468. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  469. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will be
  470. /// called to render the borders.
  471. /// </summary>
  472. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  473. /// <summary>
  474. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  475. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  476. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  477. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  478. /// </summary>
  479. public void RenderLineCanvas ()
  480. {
  481. // TODO: This is super confusing and needs to be refactored.
  482. if (Driver is null)
  483. {
  484. return;
  485. }
  486. // If we have a SuperView, it'll render our frames.
  487. if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != Rectangle.Empty)
  488. {
  489. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  490. {
  491. // Get the entire map
  492. if (p.Value is { })
  493. {
  494. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  495. Driver.Move (p.Key.X, p.Key.Y);
  496. // TODO: #2616 - Support combining sequences that don't normalize
  497. Driver.AddRune (p.Value.Value.Rune);
  498. }
  499. }
  500. LineCanvas.Clear ();
  501. }
  502. if (Subviews.Any (s => s.SuperViewRendersLineCanvas))
  503. {
  504. foreach (View subview in Subviews.Where (s => s.SuperViewRendersLineCanvas))
  505. {
  506. // Combine the LineCanvas'
  507. LineCanvas.Merge (subview.LineCanvas);
  508. subview.LineCanvas.Clear ();
  509. }
  510. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  511. {
  512. // Get the entire map
  513. if (p.Value is { })
  514. {
  515. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  516. Driver.Move (p.Key.X, p.Key.Y);
  517. // TODO: #2616 - Support combining sequences that don't normalize
  518. Driver.AddRune (p.Value.Value.Rune);
  519. }
  520. }
  521. LineCanvas.Clear ();
  522. }
  523. }
  524. #endregion DrawLineCanvas
  525. #region DrawComplete
  526. private void DoDrawComplete (Rectangle viewport)
  527. {
  528. Debug.Assert (viewport == Viewport);
  529. if (OnDrawComplete (Viewport))
  530. {
  531. return;
  532. }
  533. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  534. DrawComplete?.Invoke (this, dev);
  535. if (dev.Cancel)
  536. { }
  537. // Default implementation does nothing.
  538. }
  539. /// <summary>
  540. /// Called when the View is completed drawing.
  541. /// </summary>
  542. /// <param name="viewport"></param>
  543. protected virtual bool OnDrawComplete (Rectangle viewport) { return false; }
  544. /// <summary>Raised when the View is completed drawing.</summary>
  545. /// <remarks>
  546. /// </remarks>
  547. public event EventHandler<DrawEventArgs>? DrawComplete;
  548. #endregion DrawComplete
  549. #region NeedsDisplay
  550. // TODO: Make _needsDisplayRect nullable instead of relying on Empty
  551. // TODO: If null, it means ?
  552. // TODO: If Empty, it means no need to redraw
  553. // TODO: If not Empty, it means the region that needs to be redrawn
  554. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  555. internal Rectangle _needsDisplayRect = Rectangle.Empty;
  556. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  557. /// <remarks>
  558. /// <para>
  559. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  560. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  561. /// </para>
  562. /// <para>
  563. /// Setting has no effect on <see cref="NeedsLayout"/>.
  564. /// </para>
  565. /// </remarks>
  566. public bool NeedsDisplay
  567. {
  568. // TODO: Figure out if we can decouple NeedsDisplay from NeedsLayout. This is a temporary fix.
  569. get => _needsDisplayRect != Rectangle.Empty || NeedsLayout;
  570. set
  571. {
  572. if (value)
  573. {
  574. SetNeedsDisplay ();
  575. }
  576. else
  577. {
  578. ClearNeedsDisplay ();
  579. }
  580. }
  581. }
  582. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  583. public bool SubViewNeedsDisplay { get; private set; }
  584. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  585. /// <remarks>
  586. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  587. /// does nothing.
  588. /// </remarks>
  589. public void SetNeedsDisplay ()
  590. {
  591. Rectangle viewport = Viewport;
  592. if (_needsDisplayRect != Rectangle.Empty && viewport.IsEmpty)
  593. {
  594. // This handles the case where the view has not been initialized yet
  595. return;
  596. }
  597. SetNeedsDisplay (viewport);
  598. }
  599. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  600. /// <remarks>
  601. /// <para>
  602. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  603. /// </para>
  604. /// <para>
  605. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  606. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  607. /// </para>
  608. /// </remarks>
  609. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  610. public void SetNeedsDisplay (Rectangle viewPortRelativeRegion)
  611. {
  612. if (_needsDisplayRect.IsEmpty)
  613. {
  614. _needsDisplayRect = viewPortRelativeRegion;
  615. }
  616. else
  617. {
  618. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  619. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  620. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  621. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  622. _needsDisplayRect = new (x, y, w, h);
  623. }
  624. Margin?.SetNeedsDisplay ();
  625. Border?.SetNeedsDisplay ();
  626. Padding?.SetNeedsDisplay ();
  627. SuperView?.SetSubViewNeedsDisplay ();
  628. if (this is Adornment adornment)
  629. {
  630. adornment.Parent?.SetSubViewNeedsDisplay ();
  631. }
  632. foreach (View subview in Subviews)
  633. {
  634. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  635. {
  636. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  637. subviewRegion.X -= subview.Frame.X;
  638. subviewRegion.Y -= subview.Frame.Y;
  639. subview.SetNeedsDisplay (subviewRegion);
  640. }
  641. }
  642. }
  643. /// <summary>Sets <see cref="SubViewNeedsDisplay"/> to <see langword="true"/> for this View and all Superviews.</summary>
  644. public void SetSubViewNeedsDisplay ()
  645. {
  646. SubViewNeedsDisplay = true;
  647. if (this is Adornment adornment)
  648. {
  649. adornment.Parent?.SetSubViewNeedsDisplay ();
  650. }
  651. if (SuperView is { SubViewNeedsDisplay: false })
  652. {
  653. SuperView.SetSubViewNeedsDisplay ();
  654. }
  655. }
  656. /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
  657. protected void ClearNeedsDisplay ()
  658. {
  659. _needsDisplayRect = Rectangle.Empty;
  660. SubViewNeedsDisplay = false;
  661. Margin?.ClearNeedsDisplay ();
  662. Border?.ClearNeedsDisplay ();
  663. Padding?.ClearNeedsDisplay ();
  664. foreach (View subview in Subviews)
  665. {
  666. subview.ClearNeedsDisplay ();
  667. }
  668. }
  669. #endregion NeedsDisplay
  670. #endregion Drawing Engine
  671. }