View.Drawing.cs 30 KB

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