2
0

View.Drawing.cs 31 KB

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