2
0

View.Drawing.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 last to ensure they are drawn on top of the content.
  26. Margin.DrawMargins (viewsArray);
  27. // DrawMargins may have caused some views have NeedsDraw/NeedsSubViewDraw set; clear them all.
  28. foreach (View view in viewsArray)
  29. {
  30. view.ClearNeedsDraw ();
  31. }
  32. // After all peer views have been drawn and cleared, we can now clear the SuperView's SubViewNeedsDraw flag.
  33. // ClearNeedsDraw() does not clear SuperView.SubViewNeedsDraw (by design, to avoid premature clearing
  34. // when siblings still need drawing), so we must do it here after ALL peers are processed.
  35. // We only clear the flag if ALL the SuperView's subviews no longer need drawing.
  36. View? lastSuperView = null;
  37. foreach (View view in viewsArray)
  38. {
  39. if (view is not Adornment && view.SuperView is { } && view.SuperView != lastSuperView)
  40. {
  41. // Check if ANY subview of this SuperView still needs drawing
  42. bool anySubViewNeedsDrawing = view.SuperView.InternalSubViews.Any (v => v.NeedsDraw || v.SubViewNeedsDraw);
  43. if (!anySubViewNeedsDrawing)
  44. {
  45. view.SuperView.SubViewNeedsDraw = false;
  46. }
  47. lastSuperView = view.SuperView;
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Draws the view if it needs to be drawn.
  53. /// </summary>
  54. /// <remarks>
  55. /// <para>
  56. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDraw"/>,
  57. /// <see cref="SubViewNeedsDraw"/>,
  58. /// or <see cref="NeedsLayout"/> set.
  59. /// </para>
  60. /// <para>
  61. /// See the View Drawing Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.Gui/docs/drawing.html"/>.
  62. /// </para>
  63. /// </remarks>
  64. public void Draw (DrawContext? context = null)
  65. {
  66. if (!CanBeVisible (this))
  67. {
  68. return;
  69. }
  70. Region? originalClip = GetClip ();
  71. // TODO: This can be further optimized by checking NeedsDraw below and only
  72. // TODO: clearing, drawing text, drawing content, etc. if it is true.
  73. if (NeedsDraw || SubViewNeedsDraw)
  74. {
  75. // ------------------------------------
  76. // Draw the Border and Padding.
  77. // Note Margin with a Shadow is special-cased and drawn in a separate pass to support
  78. // transparent shadows.
  79. DoDrawAdornments (originalClip);
  80. SetClip (originalClip);
  81. // ------------------------------------
  82. // Clear the Viewport
  83. // By default, we clip to the viewport preventing drawing outside the viewport
  84. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  85. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  86. // Get our Viewport in screen coordinates
  87. originalClip = AddViewportToClip ();
  88. // If no context ...
  89. context ??= new ();
  90. SetAttributeForRole (Enabled ? VisualRole.Normal : VisualRole.Disabled);
  91. DoClearViewport (context);
  92. // ------------------------------------
  93. // Draw the subviews first (order matters: SubViews, Text, Content)
  94. if (SubViewNeedsDraw)
  95. {
  96. DoDrawSubViews (context);
  97. }
  98. // ------------------------------------
  99. // Draw the text
  100. SetAttributeForRole (Enabled ? VisualRole.Normal : VisualRole.Disabled);
  101. DoDrawText (context);
  102. // ------------------------------------
  103. // Draw the content
  104. DoDrawContent (context);
  105. // ------------------------------------
  106. // Draw the line canvas
  107. // Restore the clip before rendering the line canvas and adornment subviews
  108. // because they may draw outside the viewport.
  109. SetClip (originalClip);
  110. originalClip = AddFrameToClip ();
  111. DoRenderLineCanvas ();
  112. // ------------------------------------
  113. // Re-draw the border and padding subviews
  114. // HACK: This is a hack to ensure that the border and padding subviews are drawn after the line canvas.
  115. DoDrawAdornmentsSubViews ();
  116. // ------------------------------------
  117. // Advance the diagnostics draw indicator
  118. Border?.AdvanceDrawIndicator ();
  119. ClearNeedsDraw ();
  120. if (this is not Adornment && SuperView is not Adornment)
  121. {
  122. // Parent
  123. Debug.Assert (Margin!.Parent == this);
  124. Debug.Assert (Border!.Parent == this);
  125. Debug.Assert (Padding!.Parent == this);
  126. // SubViewNeedsDraw is set to false by ClearNeedsDraw.
  127. Debug.Assert (SubViewNeedsDraw == false);
  128. Debug.Assert (Margin!.SubViewNeedsDraw == false);
  129. Debug.Assert (Border!.SubViewNeedsDraw == false);
  130. Debug.Assert (Padding!.SubViewNeedsDraw == false);
  131. // NeedsDraw is set to false by ClearNeedsDraw.
  132. Debug.Assert (NeedsDraw == false);
  133. Debug.Assert (Margin!.NeedsDraw == false);
  134. Debug.Assert (Border!.NeedsDraw == false);
  135. Debug.Assert (Padding!.NeedsDraw == false);
  136. }
  137. }
  138. // ------------------------------------
  139. // This causes the Margin to be drawn in a second pass if it has a ShadowStyle
  140. Margin?.CacheClip ();
  141. // ------------------------------------
  142. // Reset the clip to what it was when we started
  143. SetClip (originalClip);
  144. // ------------------------------------
  145. // We're done drawing - The Clip is reset to what it was before we started.
  146. DoDrawComplete (context);
  147. }
  148. #region DrawAdornments
  149. private void DoDrawAdornmentsSubViews ()
  150. {
  151. // NOTE: We do not support subviews of Margin?
  152. if (Border?.SubViews is { } && Border.Thickness != Thickness.Empty && Border.NeedsDraw)
  153. {
  154. // PERFORMANCE: Get the check for DrawIndicator out of this somehow.
  155. foreach (View subview in Border.SubViews.Where (v => v.Visible || v.Id == "DrawIndicator"))
  156. {
  157. if (subview.Id != "DrawIndicator")
  158. {
  159. subview.SetNeedsDraw ();
  160. }
  161. LineCanvas.Exclude (new (subview.FrameToScreen ()));
  162. }
  163. Region? saved = Border?.AddFrameToClip ();
  164. Border?.DoDrawSubViews ();
  165. SetClip (saved);
  166. }
  167. if (Padding?.SubViews is { } && Padding.Thickness != Thickness.Empty && Padding.NeedsDraw)
  168. {
  169. foreach (View subview in Padding.SubViews)
  170. {
  171. subview.SetNeedsDraw ();
  172. }
  173. Region? saved = Padding?.AddFrameToClip ();
  174. Padding?.DoDrawSubViews ();
  175. SetClip (saved);
  176. }
  177. }
  178. internal void DoDrawAdornments (Region? originalClip)
  179. {
  180. if (this is Adornment)
  181. {
  182. AddFrameToClip ();
  183. }
  184. else
  185. {
  186. // Set the clip to be just the thicknesses of the adornments
  187. // TODO: Put this union logic in a method on View?
  188. Region? clipAdornments = Margin!.Thickness.AsRegion (Margin!.FrameToScreen ());
  189. clipAdornments.Combine (Border!.Thickness.AsRegion (Border!.FrameToScreen ()), RegionOp.Union);
  190. clipAdornments.Combine (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ()), RegionOp.Union);
  191. clipAdornments.Combine (originalClip, RegionOp.Intersect);
  192. SetClip (clipAdornments);
  193. }
  194. if (Margin?.NeedsLayout == true)
  195. {
  196. Margin.NeedsLayout = false;
  197. Margin?.Thickness.Draw (Driver, FrameToScreen ());
  198. Margin?.Parent?.SetSubViewNeedsDrawDownHierarchy ();
  199. }
  200. if (SubViewNeedsDraw)
  201. {
  202. // A SubView may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen.
  203. Border?.SetNeedsDraw ();
  204. Padding?.SetNeedsDraw ();
  205. Margin?.SetNeedsDraw ();
  206. }
  207. if (OnDrawingAdornments ())
  208. {
  209. return;
  210. }
  211. // TODO: add event.
  212. DrawAdornments ();
  213. }
  214. /// <summary>
  215. /// Causes <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> to be drawn.
  216. /// </summary>
  217. /// <remarks>
  218. /// <para>
  219. /// <see cref="Margin"/> is drawn in a separate pass if <see cref="ShadowStyle"/> is set.
  220. /// </para>
  221. /// </remarks>
  222. public void DrawAdornments ()
  223. {
  224. // We do not attempt to draw Margin. It is drawn in a separate pass.
  225. // Each of these renders lines to this View's LineCanvas
  226. // Those lines will be finally rendered in OnRenderLineCanvas
  227. if (Border is { } && Border.Thickness != Thickness.Empty)
  228. {
  229. Border?.Draw ();
  230. }
  231. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  232. {
  233. Padding?.Draw ();
  234. }
  235. if (Margin is { } && Margin.Thickness != Thickness.Empty/* && Margin.ShadowStyle == ShadowStyle.None*/)
  236. {
  237. //Margin?.Draw ();
  238. }
  239. }
  240. private void ClearFrame ()
  241. {
  242. if (Driver is null)
  243. {
  244. return;
  245. }
  246. // Get screen-relative coords
  247. Rectangle toClear = FrameToScreen ();
  248. Attribute prev = SetAttribute (GetAttributeForRole (VisualRole.Normal));
  249. Driver.FillRect (toClear);
  250. SetAttribute (prev);
  251. SetNeedsDraw ();
  252. }
  253. /// <summary>
  254. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  255. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  256. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  257. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  258. /// </summary>
  259. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  260. protected virtual bool OnDrawingAdornments () { return false; }
  261. #endregion DrawAdornments
  262. #region ClearViewport
  263. internal void DoClearViewport (DrawContext? context = null)
  264. {
  265. if (!NeedsDraw || ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent) || OnClearingViewport ())
  266. {
  267. return;
  268. }
  269. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  270. ClearingViewport?.Invoke (this, dev);
  271. if (dev.Cancel)
  272. {
  273. // BUGBUG: We should add the Viewport to context.DrawRegion here?
  274. SetNeedsDraw ();
  275. return;
  276. }
  277. if (!ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  278. {
  279. ClearViewport (context);
  280. OnClearedViewport ();
  281. ClearedViewport?.Invoke (this, new (Viewport, Viewport, null));
  282. }
  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="ViewBase.ViewportSettingsFlags.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="ViewportSettingsFlags.AllowNegativeLocation"/> is
  312. /// enabled) and want
  313. /// the area outside the content to be visually distinct.
  314. /// </para>
  315. /// </remarks>
  316. public void ClearViewport (DrawContext? context = null)
  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 (ViewportSettingsFlags.ClearContentOnly))
  325. {
  326. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  327. toClear = Rectangle.Intersect (toClear, visibleContent);
  328. }
  329. Driver.FillRect (toClear);
  330. // context.AddDrawnRectangle (toClear);
  331. SetNeedsDraw ();
  332. }
  333. #endregion ClearViewport
  334. #region DrawText
  335. private void DoDrawText (DrawContext? context = null)
  336. {
  337. if (!NeedsDraw)
  338. {
  339. return;
  340. }
  341. if (!string.IsNullOrEmpty (TextFormatter.Text))
  342. {
  343. TextFormatter.NeedsFormat = true;
  344. }
  345. if (OnDrawingText (context))
  346. {
  347. return;
  348. }
  349. // TODO: Get rid of this vf in lieu of the one above
  350. if (OnDrawingText ())
  351. {
  352. return;
  353. }
  354. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  355. DrawingText?.Invoke (this, dev);
  356. if (dev.Cancel)
  357. {
  358. return;
  359. }
  360. DrawText (context);
  361. OnDrewText ();
  362. DrewText?.Invoke (this, EventArgs.Empty);
  363. }
  364. /// <summary>
  365. /// Called when the <see cref="Text"/> of the View is to be drawn.
  366. /// </summary>
  367. /// <param name="context">The draw context to report drawn areas to.</param>
  368. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  369. protected virtual bool OnDrawingText (DrawContext? context) { return false; }
  370. /// <summary>
  371. /// Called when the <see cref="Text"/> of the View is to be drawn.
  372. /// </summary>
  373. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  374. protected virtual bool OnDrawingText () { return false; }
  375. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  376. /// <returns>
  377. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  378. /// <see cref="Text"/>.
  379. /// </returns>
  380. public event EventHandler<DrawEventArgs>? DrawingText;
  381. /// <summary>
  382. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  383. /// </summary>
  384. /// <param name="context">The draw context to report drawn areas to.</param>
  385. public void DrawText (DrawContext? context = null)
  386. {
  387. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  388. // Use GetDrawRegion to get precise drawn areas
  389. Region textRegion = TextFormatter.GetDrawRegion (drawRect);
  390. // Report the drawn area to the context
  391. context?.AddDrawnRegion (textRegion);
  392. if (Driver is { })
  393. {
  394. TextFormatter.Draw (
  395. Driver,
  396. drawRect,
  397. HasFocus ? GetAttributeForRole (VisualRole.Focus) : GetAttributeForRole (VisualRole.Normal),
  398. HasFocus ? GetAttributeForRole (VisualRole.HotFocus) : GetAttributeForRole (VisualRole.HotNormal),
  399. Rectangle.Empty);
  400. }
  401. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  402. SetSubViewNeedsDrawDownHierarchy ();
  403. }
  404. /// <summary>
  405. /// Called when the <see cref="Text"/> of the View has been drawn.
  406. /// </summary>
  407. protected virtual void OnDrewText () { }
  408. /// <summary>Raised when the <see cref="Text"/> of the View has been drawn.</summary>
  409. public event EventHandler? DrewText;
  410. #endregion DrawText
  411. #region DrawContent
  412. private void DoDrawContent (DrawContext? context = null)
  413. {
  414. if (!NeedsDraw || OnDrawingContent (context))
  415. {
  416. return;
  417. }
  418. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  419. DrawingContent?.Invoke (this, dev);
  420. if (dev.Cancel)
  421. {
  422. return;
  423. }
  424. // No default drawing; let event handlers or overrides handle it
  425. }
  426. /// <summary>
  427. /// Called when the View's content is to be drawn. The default implementation does nothing.
  428. /// </summary>
  429. /// <param name="context">The draw context to report drawn areas to.</param>
  430. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  431. /// <remarks>
  432. /// <para>
  433. /// Override this method to draw custom content for your View.
  434. /// </para>
  435. /// <para>
  436. /// <b>Transparency Support:</b> If your View has <see cref="ViewportSettings"/> with <see cref="ViewportSettingsFlags.Transparent"/>
  437. /// set, you should report the exact regions you draw to via the <paramref name="context"/> parameter. This allows
  438. /// the transparency system to exclude only the drawn areas from the clip region, letting views beneath show through
  439. /// in the areas you didn't draw.
  440. /// </para>
  441. /// <para>
  442. /// Use <see cref="DrawContext.AddDrawnRectangle"/> for simple rectangular areas, or <see cref="DrawContext.AddDrawnRegion"/>
  443. /// for complex, non-rectangular shapes. All coordinates passed to these methods must be in <b>screen-relative coordinates</b>.
  444. /// Use <see cref="View.ViewportToScreen(in Rectangle)"/> or <see cref="View.ContentToScreen(in Point)"/> to convert from
  445. /// viewport-relative or content-relative coordinates.
  446. /// </para>
  447. /// <para>
  448. /// Example of drawing custom content with transparency support:
  449. /// </para>
  450. /// <code>
  451. /// protected override bool OnDrawingContent (DrawContext? context)
  452. /// {
  453. /// base.OnDrawingContent (context);
  454. ///
  455. /// // Draw content in viewport-relative coordinates
  456. /// Rectangle rect1 = new Rectangle (5, 5, 10, 3);
  457. /// Rectangle rect2 = new Rectangle (8, 8, 4, 7);
  458. /// FillRect (rect1, Glyphs.BlackCircle);
  459. /// FillRect (rect2, Glyphs.BlackCircle);
  460. ///
  461. /// // Report drawn region in screen-relative coordinates for transparency
  462. /// if (ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  463. /// {
  464. /// Region drawnRegion = new Region (ViewportToScreen (rect1));
  465. /// drawnRegion.Union (ViewportToScreen (rect2));
  466. /// context?.AddDrawnRegion (drawnRegion);
  467. /// }
  468. ///
  469. /// return true;
  470. /// }
  471. /// </code>
  472. /// </remarks>
  473. protected virtual bool OnDrawingContent (DrawContext? context) { return false; }
  474. /// <summary>Raised when the View's content is to be drawn.</summary>
  475. /// <remarks>
  476. /// <para>
  477. /// Subscribe to this event to draw custom content for the View. Use the drawing methods available on <see cref="View"/>
  478. /// such as <see cref="View.AddRune(int, int, Rune)"/>, <see cref="View.AddStr(string)"/>, and <see cref="View.FillRect(Rectangle, Rune)"/>.
  479. /// </para>
  480. /// <para>
  481. /// The event is invoked after <see cref="ClearingViewport"/> and <see cref="Text"/> have been drawn, but before any <see cref="SubViews"/> are drawn.
  482. /// </para>
  483. /// <para>
  484. /// <b>Transparency Support:</b> If the View has <see cref="ViewportSettings"/> with <see cref="ViewportSettingsFlags.Transparent"/>
  485. /// set, use the <see cref="DrawEventArgs.DrawContext"/> to report which areas were actually drawn. This enables proper transparency
  486. /// by excluding only the drawn areas from the clip region. See <see cref="DrawContext"/> for details on reporting drawn regions.
  487. /// </para>
  488. /// <para>
  489. /// The <see cref="DrawEventArgs.NewViewport"/> property provides the view-relative rectangle describing the currently visible viewport into the View.
  490. /// </para>
  491. /// </remarks>
  492. public event EventHandler<DrawEventArgs>? DrawingContent;
  493. #endregion DrawContent
  494. #region DrawSubViews
  495. private void DoDrawSubViews (DrawContext? context = null)
  496. {
  497. if (!NeedsDraw || OnDrawingSubViews (context))
  498. {
  499. return;
  500. }
  501. // TODO: Get rid of this vf in lieu of the one above
  502. if (OnDrawingSubViews ())
  503. {
  504. return;
  505. }
  506. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  507. DrawingSubViews?.Invoke (this, dev);
  508. if (dev.Cancel)
  509. {
  510. return;
  511. }
  512. if (!SubViewNeedsDraw)
  513. {
  514. return;
  515. }
  516. DrawSubViews (context);
  517. }
  518. /// <summary>
  519. /// Called when the <see cref="SubViews"/> are to be drawn.
  520. /// </summary>
  521. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  522. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  523. protected virtual bool OnDrawingSubViews (DrawContext? context) { return false; }
  524. /// <summary>
  525. /// Called when the <see cref="SubViews"/> are to be drawn.
  526. /// </summary>
  527. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  528. protected virtual bool OnDrawingSubViews () { return false; }
  529. /// <summary>Raised when the <see cref="SubViews"/> are to be drawn.</summary>
  530. /// <remarks>
  531. /// </remarks>
  532. /// <returns>
  533. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  534. /// <see cref="SubViews"/>.
  535. /// </returns>
  536. public event EventHandler<DrawEventArgs>? DrawingSubViews;
  537. /// <summary>
  538. /// Draws the <see cref="SubViews"/>.
  539. /// </summary>
  540. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  541. public void DrawSubViews (DrawContext? context = null)
  542. {
  543. if (InternalSubViews.Count == 0)
  544. {
  545. return;
  546. }
  547. // Draw the subviews in reverse order to leverage clipping.
  548. foreach (View view in InternalSubViews.Snapshot ().Where (v => v.Visible).Reverse ())
  549. {
  550. // TODO: HACK - This forcing of SetNeedsDraw with SuperViewRendersLineCanvas enables auto line join to work, but is brute force.
  551. if (view.SuperViewRendersLineCanvas || view.ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  552. {
  553. //view.SetNeedsDraw ();
  554. }
  555. view.Draw (context);
  556. if (view.SuperViewRendersLineCanvas)
  557. {
  558. LineCanvas.Merge (view.LineCanvas);
  559. view.LineCanvas.Clear ();
  560. }
  561. }
  562. }
  563. #endregion DrawSubViews
  564. #region DrawLineCanvas
  565. private void DoRenderLineCanvas ()
  566. {
  567. if (!NeedsDraw || OnRenderingLineCanvas ())
  568. {
  569. return;
  570. }
  571. // TODO: Add event
  572. RenderLineCanvas ();
  573. }
  574. /// <summary>
  575. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  576. /// </summary>
  577. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  578. protected virtual bool OnRenderingLineCanvas () { return false; }
  579. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  580. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  581. public LineCanvas LineCanvas { get; } = new ();
  582. /// <summary>
  583. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  584. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  585. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will
  586. /// be
  587. /// called to render the borders.
  588. /// </summary>
  589. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  590. /// <summary>
  591. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  592. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  593. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  594. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  595. /// </summary>
  596. public void RenderLineCanvas ()
  597. {
  598. if (Driver is null)
  599. {
  600. return;
  601. }
  602. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
  603. {
  604. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  605. {
  606. // Get the entire map
  607. if (p.Value is { })
  608. {
  609. SetAttribute (p.Value.Value.Attribute ?? GetAttributeForRole (VisualRole.Normal));
  610. Driver.Move (p.Key.X, p.Key.Y);
  611. // TODO: #2616 - Support combining sequences that don't normalize
  612. AddStr (p.Value.Value.Grapheme);
  613. }
  614. }
  615. LineCanvas.Clear ();
  616. }
  617. }
  618. #endregion DrawLineCanvas
  619. #region DrawComplete
  620. private void DoDrawComplete (DrawContext? context)
  621. {
  622. OnDrawComplete (context);
  623. DrawComplete?.Invoke (this, new (Viewport, Viewport, context));
  624. // Now, update the clip to exclude this view (not including Margin)
  625. if (this is not Adornment)
  626. {
  627. if (ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  628. {
  629. // context!.DrawnRegion is the region that was drawn by this view. It may include regions outside
  630. // the Viewport. We need to clip it to the Viewport.
  631. context!.ClipDrawnRegion (ViewportToScreen (Viewport));
  632. // Exclude the drawn region from the clip
  633. ExcludeFromClip (context.GetDrawnRegion ());
  634. // Exclude the Border and Padding from the clip
  635. ExcludeFromClip (Border?.Thickness.AsRegion (Border.FrameToScreen ()));
  636. ExcludeFromClip (Padding?.Thickness.AsRegion (Padding.FrameToScreen ()));
  637. // QUESTION: This makes it so that no nesting of transparent views is possible, but is more correct?
  638. context = new DrawContext ();
  639. }
  640. else
  641. {
  642. // Exclude this view (not including Margin) from the Clip
  643. Rectangle borderFrame = FrameToScreen ();
  644. if (Border is { })
  645. {
  646. borderFrame = Border.FrameToScreen ();
  647. }
  648. // In the non-transparent (typical case), we want to exclude the entire view area (borderFrame) from the clip
  649. ExcludeFromClip (borderFrame);
  650. // Update context.DrawnRegion to include the entire view (borderFrame), but clipped to our SuperView's viewport
  651. // This enables the SuperView to know what was drawn by this view.
  652. context?.AddDrawnRectangle (borderFrame);
  653. }
  654. }
  655. // TODO: Determine if we need another event that conveys the FINAL DrawContext
  656. }
  657. /// <summary>
  658. /// Called when the View is completed drawing.
  659. /// </summary>
  660. /// <remarks>
  661. /// The <paramref name="context"/> parameter provides the drawn region of the View.
  662. /// </remarks>
  663. protected virtual void OnDrawComplete (DrawContext? context) { }
  664. /// <summary>Raised when the View is completed drawing.</summary>
  665. /// <remarks>
  666. /// </remarks>
  667. public event EventHandler<DrawEventArgs>? DrawComplete;
  668. #endregion DrawComplete
  669. }