View.Drawing.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. public partial class View // Drawing APIs
  5. {
  6. /// <summary>
  7. /// Draws a set of views.
  8. /// </summary>
  9. /// <param name="views">The peer views to draw.</param>
  10. /// <param name="force">If <see langword="true"/>, <see cref="View.SetNeedsDraw()"/> will be called on each view to force it to be drawn.</param>
  11. internal static void Draw (IEnumerable<View> views, bool force)
  12. {
  13. IEnumerable<View> viewsArray = views as View [] ?? views.ToArray ();
  14. // The draw context is used to track the region drawn by each view.
  15. DrawContext context = new DrawContext ();
  16. foreach (View view in viewsArray)
  17. {
  18. if (force)
  19. {
  20. view.SetNeedsDraw ();
  21. }
  22. view.Draw (context);
  23. }
  24. // Draw the margins (those whith Shadows) last to ensure they are drawn on top of the content.
  25. Margin.DrawMargins (viewsArray);
  26. }
  27. /// <summary>
  28. /// Draws the view if it needs to be drawn.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDraw"/>,
  33. /// <see cref="SubViewNeedsDraw"/>,
  34. /// or <see cref="NeedsLayout"/> set.
  35. /// </para>
  36. /// <para>
  37. /// See the View Drawing Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.GuiV2Docs/docs/drawing.html"/>.
  38. /// </para>
  39. /// </remarks>
  40. public void Draw (DrawContext? context = null)
  41. {
  42. if (!CanBeVisible (this))
  43. {
  44. return;
  45. }
  46. Region? originalClip = GetClip ();
  47. // TODO: This can be further optimized by checking NeedsDraw below and only
  48. // TODO: clearing, drawing text, drawing content, etc. if it is true.
  49. if (NeedsDraw || SubViewNeedsDraw)
  50. {
  51. // ------------------------------------
  52. // Draw the Border and Padding.
  53. // Note Margin with a Shadow is special-cased and drawn in a separate pass to support
  54. // transparent shadows.
  55. DoDrawAdornments (originalClip);
  56. SetClip (originalClip);
  57. // ------------------------------------
  58. // Clear the Viewport
  59. // By default, we clip to the viewport preventing drawing outside the viewport
  60. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  61. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  62. // Get our Viewport in screen coordinates
  63. originalClip = AddViewportToClip ();
  64. // If no context ...
  65. context ??= new DrawContext ();
  66. // TODO: Simplify/optimize SetAttribute system.
  67. DoSetAttribute ();
  68. DoClearViewport ();
  69. // ------------------------------------
  70. // Draw the subviews first (order matters: SubViews, Text, Content)
  71. if (SubViewNeedsDraw)
  72. {
  73. DoSetAttribute ();
  74. DoDrawSubViews (context);
  75. }
  76. // ------------------------------------
  77. // Draw the text
  78. DoSetAttribute ();
  79. DoDrawText (context);
  80. // ------------------------------------
  81. // Draw the content
  82. DoSetAttribute ();
  83. DoDrawContent (context);
  84. // ------------------------------------
  85. // Draw the line canvas
  86. // Restore the clip before rendering the line canvas and adornment subviews
  87. // because they may draw outside the viewport.
  88. SetClip (originalClip);
  89. originalClip = AddFrameToClip ();
  90. DoRenderLineCanvas ();
  91. // ------------------------------------
  92. // Re-draw the border and padding subviews
  93. // HACK: This is a hack to ensure that the border and padding subviews are drawn after the line canvas.
  94. DoDrawAdornmentsSubViews ();
  95. // ------------------------------------
  96. // Advance the diagnostics draw indicator
  97. Border?.AdvanceDrawIndicator ();
  98. ClearNeedsDraw ();
  99. }
  100. // ------------------------------------
  101. // This causes the Margin to be drawn in a second pass if it has a ShadowStyle
  102. // PERFORMANCE: If there is a Margin w/ Shadow, it will be redrawn each iteration of the main loop.
  103. Margin?.CacheClip ();
  104. // ------------------------------------
  105. // Reset the clip to what it was when we started
  106. SetClip (originalClip);
  107. // ------------------------------------
  108. // We're done drawing - The Clip is reset to what it was before we started.
  109. DoDrawComplete (context);
  110. }
  111. #region DrawAdornments
  112. private void DoDrawAdornmentsSubViews ()
  113. {
  114. // NOTE: We do not support subviews of Margin?
  115. if (Border?.SubViews is { } && Border.Thickness != Thickness.Empty)
  116. {
  117. // PERFORMANCE: Get the check for DrawIndicator out of this somehow.
  118. foreach (View subview in Border.SubViews.Where (v => v.Visible || v.Id == "DrawIndicator"))
  119. {
  120. if (subview.Id != "DrawIndicator")
  121. {
  122. subview.SetNeedsDraw ();
  123. }
  124. LineCanvas.Exclude (new (subview.FrameToScreen ()));
  125. }
  126. Region? saved = Border?.AddFrameToClip ();
  127. Border?.DoDrawSubViews ();
  128. SetClip (saved);
  129. }
  130. if (Padding?.SubViews is { } && Padding.Thickness != Thickness.Empty)
  131. {
  132. foreach (View subview in Padding.SubViews)
  133. {
  134. subview.SetNeedsDraw ();
  135. }
  136. Region? saved = Padding?.AddFrameToClip ();
  137. Padding?.DoDrawSubViews ();
  138. SetClip (saved);
  139. }
  140. }
  141. private void DoDrawAdornments (Region? originalClip)
  142. {
  143. if (this is Adornment)
  144. {
  145. AddFrameToClip ();
  146. }
  147. else
  148. {
  149. // Set the clip to be just the thicknesses of the adornments
  150. // TODO: Put this union logic in a method on View?
  151. Region? clipAdornments = Margin!.Thickness.AsRegion (Margin!.FrameToScreen ());
  152. clipAdornments?.Combine (Border!.Thickness.AsRegion (Border!.FrameToScreen ()), RegionOp.Union);
  153. clipAdornments?.Combine (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ()), RegionOp.Union);
  154. clipAdornments?.Combine (originalClip, RegionOp.Intersect);
  155. SetClip (clipAdornments);
  156. }
  157. if (Margin?.NeedsLayout == true)
  158. {
  159. Margin.NeedsLayout = false;
  160. // BUGBUG: This should not use ClearFrame as that clears the insides too
  161. Margin?.ClearFrame ();
  162. Margin?.Parent?.SetSubViewNeedsDraw ();
  163. }
  164. if (SubViewNeedsDraw)
  165. {
  166. // A SubView may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen.
  167. Border?.SetNeedsDraw ();
  168. Padding?.SetNeedsDraw ();
  169. Margin?.SetNeedsDraw ();
  170. }
  171. if (OnDrawingAdornments ())
  172. {
  173. return;
  174. }
  175. // TODO: add event.
  176. DrawAdornments ();
  177. }
  178. /// <summary>
  179. /// Causes <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> to be drawn.
  180. /// </summary>
  181. /// <remarks>
  182. /// <para>
  183. /// <see cref="Margin"/> is drawn in a separate pass if <see cref="ShadowStyle"/> is set.
  184. /// </para>
  185. /// </remarks>
  186. public void DrawAdornments ()
  187. {
  188. // We do not attempt to draw Margin. It is drawn in a separate pass.
  189. // Each of these renders lines to this View's LineCanvas
  190. // Those lines will be finally rendered in OnRenderLineCanvas
  191. if (Border is { } && Border.Thickness != Thickness.Empty)
  192. {
  193. Border?.Draw ();
  194. }
  195. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  196. {
  197. Padding?.Draw ();
  198. }
  199. if (Margin is { } && Margin.Thickness != Thickness.Empty && Margin.ShadowStyle == ShadowStyle.None)
  200. {
  201. Margin?.Draw ();
  202. }
  203. }
  204. private void ClearFrame ()
  205. {
  206. if (Driver is null)
  207. {
  208. return;
  209. }
  210. // Get screen-relative coords
  211. Rectangle toClear = FrameToScreen ();
  212. Attribute prev = SetAttribute (GetNormalColor ());
  213. Driver.FillRect (toClear);
  214. SetAttribute (prev);
  215. SetNeedsDraw ();
  216. }
  217. /// <summary>
  218. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  219. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  220. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  221. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  222. /// </summary>
  223. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  224. protected virtual bool OnDrawingAdornments () { return false; }
  225. #endregion DrawAdornments
  226. #region SetAttribute
  227. private void DoSetAttribute ()
  228. {
  229. if (OnSettingAttribute ())
  230. {
  231. return;
  232. }
  233. var args = new CancelEventArgs ();
  234. SettingAttribute?.Invoke (this, args);
  235. if (args.Cancel)
  236. {
  237. return;
  238. }
  239. SetNormalAttribute ();
  240. }
  241. /// <summary>
  242. /// Called when the normal attribute for the View is to be set. This is called before the View is drawn.
  243. /// </summary>
  244. /// <returns><see langword="true"/> to stop default behavior.</returns>
  245. protected virtual bool OnSettingAttribute () { return false; }
  246. /// <summary>Raised when the normal attribute for the View is to be set. This is raised before the View is drawn.</summary>
  247. /// <returns>
  248. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop default behavior.
  249. /// </returns>
  250. public event EventHandler<CancelEventArgs>? SettingAttribute;
  251. /// <summary>
  252. /// Sets the attribute for the View. This is called before the View is drawn.
  253. /// </summary>
  254. public void SetNormalAttribute ()
  255. {
  256. if (ColorScheme is { })
  257. {
  258. SetAttribute (GetNormalColor ());
  259. }
  260. }
  261. #endregion
  262. #region ClearViewport
  263. internal void DoClearViewport ()
  264. {
  265. if (ViewportSettings.HasFlag (ViewportSettings.Transparent))
  266. {
  267. return;
  268. }
  269. if (OnClearingViewport ())
  270. {
  271. return;
  272. }
  273. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, null);
  274. ClearingViewport?.Invoke (this, dev);
  275. if (dev.Cancel)
  276. {
  277. SetNeedsDraw ();
  278. return;
  279. }
  280. ClearViewport ();
  281. OnClearedViewport ();
  282. ClearedViewport?.Invoke (this, new (Viewport, Viewport, null));
  283. }
  284. /// <summary>
  285. /// Called when the <see cref="Viewport"/> is to be cleared.
  286. /// </summary>
  287. /// <returns><see langword="true"/> to stop further clearing.</returns>
  288. protected virtual bool OnClearingViewport () { return false; }
  289. /// <summary>Event invoked when the <see cref="Viewport"/> is to be cleared.</summary>
  290. /// <remarks>
  291. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  292. /// <para>
  293. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  294. /// <see cref="View"/> .
  295. /// </para>
  296. /// </remarks>
  297. public event EventHandler<DrawEventArgs>? ClearingViewport;
  298. /// <summary>
  299. /// Called when the <see cref="Viewport"/> has been cleared
  300. /// </summary>
  301. protected virtual void OnClearedViewport () { }
  302. /// <summary>Event invoked when the <see cref="Viewport"/> has been cleared.</summary>
  303. public event EventHandler<DrawEventArgs>? ClearedViewport;
  304. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  305. /// <remarks>
  306. /// <para>
  307. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  308. /// the portion of the content
  309. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  310. /// a
  311. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  312. /// enabled) and want
  313. /// the area outside the content to be visually distinct.
  314. /// </para>
  315. /// </remarks>
  316. public void ClearViewport ()
  317. {
  318. if (Driver is null)
  319. {
  320. return;
  321. }
  322. // Get screen-relative coords
  323. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  324. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  325. {
  326. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  327. toClear = Rectangle.Intersect (toClear, visibleContent);
  328. }
  329. Attribute prev = SetAttribute (GetNormalColor ());
  330. Driver.FillRect (toClear);
  331. SetAttribute (prev);
  332. SetNeedsDraw ();
  333. }
  334. #endregion ClearViewport
  335. #region DrawText
  336. private void DoDrawText (DrawContext? context = null)
  337. {
  338. if (OnDrawingText (context))
  339. {
  340. return;
  341. }
  342. if (OnDrawingText ())
  343. {
  344. return;
  345. }
  346. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  347. DrawingText?.Invoke (this, dev);
  348. if (dev.Cancel)
  349. {
  350. return;
  351. }
  352. DrawText (context);
  353. }
  354. /// <summary>
  355. /// Called when the <see cref="Text"/> of the View is to be drawn.
  356. /// </summary>
  357. /// <param name="context">The draw context to report drawn areas to.</param>
  358. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  359. protected virtual bool OnDrawingText (DrawContext? context) { return false; }
  360. /// <summary>
  361. /// Called when the <see cref="Text"/> of the View is to be drawn.
  362. /// </summary>
  363. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  364. protected virtual bool OnDrawingText () { return false; }
  365. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  366. /// <returns>
  367. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  368. /// <see cref="Text"/>.
  369. /// </returns>
  370. public event EventHandler<DrawEventArgs>? DrawingText;
  371. /// <summary>
  372. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  373. /// </summary>
  374. /// <param name="context">The draw context to report drawn areas to.</param>
  375. public void DrawText (DrawContext? context = null)
  376. {
  377. if (!string.IsNullOrEmpty (TextFormatter.Text))
  378. {
  379. TextFormatter.NeedsFormat = true;
  380. }
  381. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  382. // Use GetDrawRegion to get precise drawn areas
  383. Region textRegion = TextFormatter.GetDrawRegion (drawRect);
  384. // Report the drawn area to the context
  385. context?.AddDrawnRegion (textRegion);
  386. if (!NeedsDraw)
  387. {
  388. return;
  389. }
  390. TextFormatter?.Draw (
  391. drawRect,
  392. HasFocus ? GetFocusColor () : GetNormalColor (),
  393. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  394. Rectangle.Empty
  395. );
  396. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  397. SetSubViewNeedsDraw ();
  398. }
  399. #endregion DrawText
  400. #region DrawContent
  401. private void DoDrawContent (DrawContext? context = null)
  402. {
  403. if (OnDrawingContent (context))
  404. {
  405. return;
  406. }
  407. // TODO: Upgrade all overrides of OnDrawingContent to use DrawContext and remove this override
  408. if (OnDrawingContent ())
  409. {
  410. return;
  411. }
  412. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  413. DrawingContent?.Invoke (this, dev);
  414. if (dev.Cancel)
  415. {
  416. return;
  417. }
  418. // No default drawing; let event handlers or overrides handle it
  419. }
  420. /// <summary>
  421. /// Called when the View's content is to be drawn. The default implementation does nothing.
  422. /// </summary>
  423. /// <param name="context">The draw context to report drawn areas to.</param>
  424. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  425. protected virtual bool OnDrawingContent (DrawContext? context) { return false; }
  426. /// <summary>
  427. /// Called when the View's content is to be drawn. The default implementation does nothing.
  428. /// </summary>
  429. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  430. protected virtual bool OnDrawingContent () { return false; }
  431. /// <summary>Raised when the View's content is to be drawn.</summary>
  432. /// <remarks>
  433. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  434. /// <para>
  435. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  436. /// <see cref="View"/> .
  437. /// </para>
  438. /// </remarks>
  439. public event EventHandler<DrawEventArgs>? DrawingContent;
  440. #endregion DrawContent
  441. #region DrawSubViews
  442. private void DoDrawSubViews (DrawContext? context = null)
  443. {
  444. if (OnDrawingSubViews (context))
  445. {
  446. return;
  447. }
  448. if (OnDrawingSubViews ())
  449. {
  450. return;
  451. }
  452. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  453. DrawingSubViews?.Invoke (this, dev);
  454. if (dev.Cancel)
  455. {
  456. return;
  457. }
  458. if (!SubViewNeedsDraw)
  459. {
  460. return;
  461. }
  462. DrawSubViews (context);
  463. }
  464. /// <summary>
  465. /// Called when the <see cref="SubViews"/> are to be drawn.
  466. /// </summary>
  467. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  468. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  469. protected virtual bool OnDrawingSubViews (DrawContext? context) { return false; }
  470. /// <summary>
  471. /// Called when the <see cref="SubViews"/> are to be drawn.
  472. /// </summary>
  473. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  474. protected virtual bool OnDrawingSubViews () { return false; }
  475. /// <summary>Raised when the <see cref="SubViews"/> are to be drawn.</summary>
  476. /// <remarks>
  477. /// </remarks>
  478. /// <returns>
  479. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  480. /// <see cref="SubViews"/>.
  481. /// </returns>
  482. public event EventHandler<DrawEventArgs>? DrawingSubViews;
  483. /// <summary>
  484. /// Draws the <see cref="SubViews"/>.
  485. /// </summary>
  486. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  487. public void DrawSubViews (DrawContext? context = null)
  488. {
  489. if (InternalSubViews.Count == 0)
  490. {
  491. return;
  492. }
  493. // Draw the subviews in reverse order to leverage clipping.
  494. foreach (View view in InternalSubViews.Where (view => view.Visible).Reverse ())
  495. {
  496. // TODO: HACK - This forcing of SetNeedsDraw with SuperViewRendersLineCanvas enables auto line join to work, but is brute force.
  497. if (view.SuperViewRendersLineCanvas || view.ViewportSettings.HasFlag (ViewportSettings.Transparent))
  498. {
  499. view.SetNeedsDraw ();
  500. }
  501. view.Draw (context);
  502. if (view.SuperViewRendersLineCanvas)
  503. {
  504. LineCanvas.Merge (view.LineCanvas);
  505. view.LineCanvas.Clear ();
  506. }
  507. }
  508. }
  509. #endregion DrawSubViews
  510. #region DrawLineCanvas
  511. private void DoRenderLineCanvas ()
  512. {
  513. if (OnRenderingLineCanvas ())
  514. {
  515. return;
  516. }
  517. // TODO: Add event
  518. RenderLineCanvas ();
  519. }
  520. /// <summary>
  521. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  522. /// </summary>
  523. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  524. protected virtual bool OnRenderingLineCanvas () { return false; }
  525. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  526. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  527. public LineCanvas LineCanvas { get; } = new ();
  528. /// <summary>
  529. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  530. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  531. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will
  532. /// be
  533. /// called to render the borders.
  534. /// </summary>
  535. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  536. /// <summary>
  537. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  538. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  539. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  540. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  541. /// </summary>
  542. public void RenderLineCanvas ()
  543. {
  544. if (Driver is null)
  545. {
  546. return;
  547. }
  548. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
  549. {
  550. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  551. {
  552. // Get the entire map
  553. if (p.Value is { })
  554. {
  555. SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  556. Driver.Move (p.Key.X, p.Key.Y);
  557. // TODO: #2616 - Support combining sequences that don't normalize
  558. Driver.AddRune (p.Value.Value.Rune);
  559. }
  560. }
  561. LineCanvas.Clear ();
  562. }
  563. }
  564. #endregion DrawLineCanvas
  565. #region DrawComplete
  566. private void DoDrawComplete (DrawContext? context)
  567. {
  568. OnDrawComplete (context);
  569. DrawComplete?.Invoke (this, new (Viewport, Viewport, context));
  570. // Now, update the clip to exclude this view (not including Margin)
  571. if (this is not Adornment)
  572. {
  573. if (ViewportSettings.HasFlag (ViewportSettings.Transparent))
  574. {
  575. // context!.DrawnRegion is the region that was drawn by this view. It may include regions outside
  576. // the Viewport. We need to clip it to the Viewport.
  577. context!.ClipDrawnRegion (ViewportToScreen (Viewport));
  578. // Exclude the drawn region from the clip
  579. ExcludeFromClip (context!.GetDrawnRegion ());
  580. // Exclude the Border and Padding from the clip
  581. ExcludeFromClip (Border?.Thickness.AsRegion (FrameToScreen ()));
  582. ExcludeFromClip (Padding?.Thickness.AsRegion (FrameToScreen ()));
  583. }
  584. else
  585. {
  586. // Exclude this view (not including Margin) from the Clip
  587. Rectangle borderFrame = FrameToScreen ();
  588. if (Border is { })
  589. {
  590. borderFrame = Border.FrameToScreen ();
  591. }
  592. // In the non-transparent (typical case), we want to exclude the entire view area (borderFrame) from the clip
  593. ExcludeFromClip (borderFrame);
  594. // Update context.DrawnRegion to include the entire view (borderFrame), but clipped to our SuperView's viewport
  595. // This enables the SuperView to know what was drawn by this view.
  596. context?.AddDrawnRectangle (borderFrame);
  597. }
  598. }
  599. // TODO: Determine if we need another event that conveys the FINAL DrawContext
  600. }
  601. /// <summary>
  602. /// Called when the View is completed drawing.
  603. /// </summary>
  604. /// <remarks>
  605. /// The <paramref name="context"/> parameter provides the drawn region of the View.
  606. /// </remarks>
  607. protected virtual void OnDrawComplete (DrawContext? context) { }
  608. /// <summary>Raised when the View is completed drawing.</summary>
  609. /// <remarks>
  610. /// </remarks>
  611. public event EventHandler<DrawEventArgs>? DrawComplete;
  612. #endregion DrawComplete
  613. #region NeedsDraw
  614. // TODO: Change NeedsDraw to use a Region instead of Rectangle
  615. // TODO: Make _needsDrawRect nullable instead of relying on Empty
  616. // TODO: If null, it means ?
  617. // TODO: If Empty, it means no need to redraw
  618. // TODO: If not Empty, it means the region that needs to be redrawn
  619. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  620. internal Rectangle _needsDrawRect = Rectangle.Empty;
  621. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  622. /// <remarks>
  623. /// <para>
  624. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  625. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  626. /// </para>
  627. /// <para>
  628. /// Setting has no effect on <see cref="NeedsLayout"/>.
  629. /// </para>
  630. /// </remarks>
  631. public bool NeedsDraw
  632. {
  633. // TODO: Figure out if we can decouple NeedsDraw from NeedsLayout.
  634. get => Visible && (_needsDrawRect != Rectangle.Empty || NeedsLayout);
  635. set
  636. {
  637. if (value)
  638. {
  639. SetNeedsDraw ();
  640. }
  641. else
  642. {
  643. ClearNeedsDraw ();
  644. }
  645. }
  646. }
  647. /// <summary>Gets whether any SubViews need to be redrawn.</summary>
  648. public bool SubViewNeedsDraw { get; private set; }
  649. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  650. /// <remarks>
  651. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  652. /// does nothing.
  653. /// </remarks>
  654. public void SetNeedsDraw ()
  655. {
  656. Rectangle viewport = Viewport;
  657. if (!Visible || (_needsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  658. {
  659. // This handles the case where the view has not been initialized yet
  660. return;
  661. }
  662. SetNeedsDraw (viewport);
  663. }
  664. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  665. /// <remarks>
  666. /// <para>
  667. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  668. /// </para>
  669. /// <para>
  670. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  671. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  672. /// </para>
  673. /// </remarks>
  674. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  675. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  676. {
  677. if (!Visible)
  678. {
  679. return;
  680. }
  681. if (_needsDrawRect.IsEmpty)
  682. {
  683. _needsDrawRect = viewPortRelativeRegion;
  684. }
  685. else
  686. {
  687. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  688. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  689. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  690. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  691. _needsDrawRect = new (x, y, w, h);
  692. }
  693. // Do not set on Margin - it will be drawn in a separate pass.
  694. if (Border is { } && Border.Thickness != Thickness.Empty)
  695. {
  696. Border?.SetNeedsDraw ();
  697. }
  698. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  699. {
  700. Padding?.SetNeedsDraw ();
  701. }
  702. SuperView?.SetSubViewNeedsDraw ();
  703. if (this is Adornment adornment)
  704. {
  705. adornment.Parent?.SetSubViewNeedsDraw ();
  706. }
  707. // There was multiple enumeration error here, so calling ToArray - probably a stop gap
  708. foreach (View subview in SubViews.ToArray ())
  709. {
  710. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  711. {
  712. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  713. subviewRegion.X -= subview.Frame.X;
  714. subviewRegion.Y -= subview.Frame.Y;
  715. subview.SetNeedsDraw (subviewRegion);
  716. }
  717. }
  718. }
  719. /// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  720. public void SetSubViewNeedsDraw ()
  721. {
  722. if (!Visible)
  723. {
  724. return;
  725. }
  726. SubViewNeedsDraw = true;
  727. if (this is Adornment adornment)
  728. {
  729. adornment.Parent?.SetSubViewNeedsDraw ();
  730. }
  731. if (SuperView is { SubViewNeedsDraw: false })
  732. {
  733. SuperView.SetSubViewNeedsDraw ();
  734. }
  735. }
  736. /// <summary>Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/>.</summary>
  737. protected void ClearNeedsDraw ()
  738. {
  739. _needsDrawRect = Rectangle.Empty;
  740. SubViewNeedsDraw = false;
  741. if (Margin is { } && Margin.Thickness != Thickness.Empty)
  742. {
  743. Margin?.ClearNeedsDraw ();
  744. }
  745. if (Border is { } && Border.Thickness != Thickness.Empty)
  746. {
  747. Border?.ClearNeedsDraw ();
  748. }
  749. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  750. {
  751. Padding?.ClearNeedsDraw ();
  752. }
  753. foreach (View subview in SubViews)
  754. {
  755. subview.ClearNeedsDraw ();
  756. }
  757. if (SuperView is { })
  758. {
  759. SuperView.SubViewNeedsDraw = false;
  760. }
  761. // This ensures LineCanvas' get redrawn
  762. if (!SuperViewRendersLineCanvas)
  763. {
  764. LineCanvas.Clear ();
  765. }
  766. }
  767. #endregion NeedsDraw
  768. }