2
0

View.Drawing.cs 32 KB

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