View.Drawing.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View // Drawing APIs
  5. {
  6. /// <summary>
  7. /// Draws the view if it needs to be drawn.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDisplay"/>,
  12. /// <see cref="SubViewNeedsDisplay"/>,
  13. /// or <see cref="NeedsLayout"/> set.
  14. /// </para>
  15. /// <para>
  16. /// // TODO: Add docs for the drawing process.
  17. /// </para>
  18. /// </remarks>
  19. public void Draw ()
  20. {
  21. if (!CanBeVisible (this) || (!NeedsDisplay && !SubViewNeedsDisplay))
  22. {
  23. return;
  24. }
  25. DoDrawAdornments ();
  26. // Set the color scheme for the view after adornments have been drawn
  27. if (ColorScheme is { })
  28. {
  29. Driver?.SetAttribute (GetNormalColor ());
  30. }
  31. // By default, we clip to the viewport preventing drawing outside the viewport
  32. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  33. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  34. Rectangle prevClip = SetClip ();
  35. DoClearViewport (Viewport);
  36. DoDrawText (Viewport);
  37. DoDrawContent (Viewport);
  38. DoDrawSubviews (Viewport);
  39. // Restore the clip before rendering the line canvas and adornment subviews
  40. // because they may draw outside the viewport.
  41. if (Driver is { })
  42. {
  43. Driver.Clip = prevClip;
  44. }
  45. DoRenderLineCanvas ();
  46. DoDrawAdornmentSubViews ();
  47. ClearNeedsDisplay ();
  48. // We're done
  49. DoDrawComplete (Viewport);
  50. }
  51. #region DrawAdornments
  52. private void DoDrawAdornmentSubViews ()
  53. {
  54. // This causes the Adornment's subviews to be REDRAWN
  55. // TODO: Figure out how to make this more efficient
  56. if (Margin?.Subviews is { })
  57. {
  58. foreach (View subview in Margin.Subviews)
  59. {
  60. subview.SetNeedsDisplay ();
  61. }
  62. Margin?.DoDrawSubviews (Margin.Viewport);
  63. }
  64. if (Border?.Subviews is { })
  65. {
  66. foreach (View subview in Border.Subviews)
  67. {
  68. subview.SetNeedsDisplay ();
  69. }
  70. Border?.DoDrawSubviews (Border.Viewport);
  71. }
  72. if (Padding?.Subviews is { })
  73. {
  74. foreach (View subview in Padding.Subviews)
  75. {
  76. subview.SetNeedsDisplay ();
  77. }
  78. Padding?.DoDrawSubviews (Padding.Viewport);
  79. }
  80. }
  81. private void DoDrawAdornments ()
  82. {
  83. if (OnDrawingAdornments ())
  84. {
  85. return;
  86. }
  87. // TODO: add event.
  88. DrawAdornments ();
  89. }
  90. /// <summary>
  91. /// Causes each of the View's Adornments to be drawn. This includes the <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/>.
  92. /// </summary>
  93. public void DrawAdornments ()
  94. {
  95. // Each of these renders lines to either this View's LineCanvas
  96. // Those lines will be finally rendered in OnRenderLineCanvas
  97. Margin?.Draw ();
  98. Border?.Draw ();
  99. Padding?.Draw ();
  100. }
  101. /// <summary>
  102. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  103. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  104. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  105. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  106. /// </summary>
  107. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  108. protected virtual bool OnDrawingAdornments () { return false; }
  109. #endregion DrawAdornments
  110. #region ClearViewport
  111. private void DoClearViewport (Rectangle viewport)
  112. {
  113. Debug.Assert (viewport == Viewport);
  114. if (OnClearingViewport (Viewport))
  115. {
  116. return;
  117. }
  118. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  119. ClearingViewport?.Invoke (this, dev);
  120. if (dev.Cancel)
  121. {
  122. return;
  123. }
  124. ClearViewport ();
  125. }
  126. /// <summary>
  127. /// Called when the <see cref="Viewport"/> is to be cleared.
  128. /// </summary>
  129. /// <param name="viewport"></param>
  130. /// <returns><see langword="true"/> to stop further clearing.</returns>
  131. protected virtual bool OnClearingViewport (Rectangle viewport) { return false; }
  132. /// <summary>Event invoked when the content area of the View is to be drawn.</summary>
  133. /// <remarks>
  134. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  135. /// <para>
  136. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  137. /// <see cref="View"/> .
  138. /// </para>
  139. /// </remarks>
  140. public event EventHandler<DrawEventArgs>? ClearingViewport;
  141. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  142. /// <remarks>
  143. /// <para>
  144. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  145. /// the portion of the content
  146. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  147. /// a
  148. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  149. /// enabled) and want
  150. /// the area outside the content to be visually distinct.
  151. /// </para>
  152. /// </remarks>
  153. public void ClearViewport ()
  154. {
  155. if (Driver is null)
  156. {
  157. return;
  158. }
  159. // Get screen-relative coords
  160. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  161. Rectangle prevClip = Driver.Clip;
  162. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  163. {
  164. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  165. toClear = Rectangle.Intersect (toClear, visibleContent);
  166. }
  167. Attribute prev = Driver.SetAttribute (GetNormalColor ());
  168. Driver.FillRect (toClear);
  169. Driver.SetAttribute (prev);
  170. Driver.Clip = prevClip;
  171. SetNeedsDisplay ();
  172. }
  173. #endregion ClearViewport
  174. #region DrawText
  175. private void DoDrawText (Rectangle viewport)
  176. {
  177. Debug.Assert (viewport == Viewport);
  178. if (OnDrawingText (Viewport))
  179. {
  180. return;
  181. }
  182. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  183. DrawingText?.Invoke (this, dev);
  184. if (dev.Cancel)
  185. {
  186. return;
  187. }
  188. DrawText ();
  189. }
  190. /// <summary>
  191. /// Called when the <see cref="Text"/> of the View is to be drawn.
  192. /// </summary>
  193. /// <param name="viewport"></param>
  194. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  195. protected virtual bool OnDrawingText (Rectangle viewport) { return false; }
  196. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  197. /// <returns>
  198. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  199. /// <see cref="Text"/>.
  200. /// </returns>
  201. public event EventHandler<DrawEventArgs>? DrawingText;
  202. /// <summary>
  203. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  204. /// </summary>
  205. public void DrawText ()
  206. {
  207. if (!string.IsNullOrEmpty (TextFormatter.Text))
  208. {
  209. TextFormatter.NeedsFormat = true;
  210. }
  211. // This should NOT clear
  212. // TODO: If the output is not in the Viewport, do nothing
  213. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  214. TextFormatter?.Draw (
  215. drawRect,
  216. HasFocus ? GetFocusColor () : GetNormalColor (),
  217. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  218. Rectangle.Empty
  219. );
  220. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  221. SetSubViewNeedsDisplay ();
  222. }
  223. #endregion DrawText
  224. #region DrawContent
  225. private void DoDrawContent (Rectangle viewport)
  226. {
  227. Debug.Assert (viewport == Viewport);
  228. if (OnDrawingContent (Viewport))
  229. {
  230. return;
  231. }
  232. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  233. DrawingContent?.Invoke (this, dev);
  234. if (dev.Cancel)
  235. { }
  236. // Do nothing.
  237. }
  238. /// <summary>
  239. /// Called when the View's content is to be drawn. The default implementation does nothing.
  240. /// </summary>
  241. /// <remarks>
  242. /// </remarks>
  243. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  244. protected virtual bool OnDrawingContent (Rectangle viewport) { return false; }
  245. /// <summary>Raised when the View's content is to be drawn.</summary>
  246. /// <remarks>
  247. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  248. /// <para>
  249. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  250. /// <see cref="View"/> .
  251. /// </para>
  252. /// </remarks>
  253. public event EventHandler<DrawEventArgs>? DrawingContent;
  254. #endregion DrawContent
  255. #region DrawSubviews
  256. private void DoDrawSubviews (Rectangle viewport)
  257. {
  258. Debug.Assert (viewport == Viewport);
  259. if (OnDrawingSubviews (Viewport))
  260. {
  261. return;
  262. }
  263. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  264. DrawingSubviews?.Invoke (this, dev);
  265. if (dev.Cancel)
  266. {
  267. return;
  268. }
  269. DrawSubviews ();
  270. }
  271. /// <summary>
  272. /// Called when the <see cref="Subviews"/> are to be drawn.
  273. /// </summary>
  274. /// <param name="viewport"></param>
  275. /// <returns><see langword="true"/> to stop further drawing of <see cref="Subviews"/>.</returns>
  276. protected virtual bool OnDrawingSubviews (Rectangle viewport) { return false; }
  277. /// <summary>Raised when the <see cref="Subviews"/> are to be drawn.</summary>
  278. /// <remarks>
  279. /// </remarks>
  280. /// <returns>
  281. /// Set <see cref="DrawEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  282. /// <see cref="Subviews"/>.
  283. /// </returns>
  284. public event EventHandler<DrawEventArgs>? DrawingSubviews;
  285. /// <summary>
  286. /// Draws the <see cref="Subviews"/>.
  287. /// </summary>
  288. public void DrawSubviews ()
  289. {
  290. if (_subviews is null || !SubViewNeedsDisplay)
  291. {
  292. return;
  293. }
  294. IEnumerable<View> subviewsNeedingDraw = _subviews.Where (view => view.Visible && (view.NeedsDisplay || view.SubViewNeedsDisplay));
  295. foreach (View view in subviewsNeedingDraw)
  296. {
  297. view.Draw ();
  298. }
  299. }
  300. #endregion DrawSubviews
  301. #region DrawLineCanvas
  302. private void DoRenderLineCanvas ()
  303. {
  304. if (OnRenderingLineCanvas ())
  305. {
  306. return;
  307. }
  308. // TODO: Add event
  309. RenderLineCanvas ();
  310. }
  311. /// <summary>
  312. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  313. /// </summary>
  314. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  315. protected virtual bool OnRenderingLineCanvas () { return false; }
  316. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  317. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  318. public LineCanvas LineCanvas { get; } = new ();
  319. /// <summary>
  320. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  321. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  322. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will be
  323. /// called to render the borders.
  324. /// </summary>
  325. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  326. /// <summary>
  327. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  328. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  329. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  330. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  331. /// </summary>
  332. public void RenderLineCanvas ()
  333. {
  334. // TODO: This is super confusing and needs to be refactored.
  335. if (Driver is null)
  336. {
  337. return;
  338. }
  339. // If we have a SuperView, it'll render our frames.
  340. if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != Rectangle.Empty)
  341. {
  342. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  343. {
  344. // Get the entire map
  345. if (p.Value is { })
  346. {
  347. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  348. Driver.Move (p.Key.X, p.Key.Y);
  349. // TODO: #2616 - Support combining sequences that don't normalize
  350. Driver.AddRune (p.Value.Value.Rune);
  351. }
  352. }
  353. LineCanvas.Clear ();
  354. }
  355. if (Subviews.Any (s => s.SuperViewRendersLineCanvas))
  356. {
  357. foreach (View subview in Subviews.Where (s => s.SuperViewRendersLineCanvas))
  358. {
  359. // Combine the LineCanvas'
  360. LineCanvas.Merge (subview.LineCanvas);
  361. subview.LineCanvas.Clear ();
  362. }
  363. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  364. {
  365. // Get the entire map
  366. if (p.Value is { })
  367. {
  368. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  369. Driver.Move (p.Key.X, p.Key.Y);
  370. // TODO: #2616 - Support combining sequences that don't normalize
  371. Driver.AddRune (p.Value.Value.Rune);
  372. }
  373. }
  374. LineCanvas.Clear ();
  375. }
  376. }
  377. #endregion DrawLineCanvas
  378. #region DrawComplete
  379. private void DoDrawComplete (Rectangle viewport)
  380. {
  381. Debug.Assert (viewport == Viewport);
  382. if (OnDrawComplete (Viewport))
  383. {
  384. return;
  385. }
  386. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  387. DrawComplete?.Invoke (this, dev);
  388. if (dev.Cancel)
  389. { }
  390. // Default implementation does nothing.
  391. }
  392. /// <summary>
  393. /// Called when the View is completed drawing.
  394. /// </summary>
  395. /// <param name="viewport"></param>
  396. protected virtual bool OnDrawComplete (Rectangle viewport) { return false; }
  397. /// <summary>Raised when the View is completed drawing.</summary>
  398. /// <remarks>
  399. /// </remarks>
  400. public event EventHandler<DrawEventArgs>? DrawComplete;
  401. #endregion DrawComplete
  402. #region NeedsDisplay
  403. // TODO: Make _needsDisplayRect nullable instead of relying on Empty
  404. // TODO: If null, it means ?
  405. // TODO: If Empty, it means no need to redraw
  406. // TODO: If not Empty, it means the region that needs to be redrawn
  407. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  408. internal Rectangle _needsDisplayRect = Rectangle.Empty;
  409. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  410. /// <remarks>
  411. /// <para>
  412. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  413. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  414. /// </para>
  415. /// <para>
  416. /// Setting has no effect on <see cref="NeedsLayout"/>.
  417. /// </para>
  418. /// </remarks>
  419. public bool NeedsDisplay
  420. {
  421. // TODO: Figure out if we can decouple NeedsDisplay from NeedsLayout. This is a temporary fix.
  422. get => _needsDisplayRect != Rectangle.Empty || NeedsLayout;
  423. set
  424. {
  425. if (value)
  426. {
  427. SetNeedsDisplay ();
  428. }
  429. else
  430. {
  431. ClearNeedsDisplay ();
  432. }
  433. }
  434. }
  435. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  436. public bool SubViewNeedsDisplay { get; private set; }
  437. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  438. /// <remarks>
  439. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  440. /// does nothing.
  441. /// </remarks>
  442. public void SetNeedsDisplay ()
  443. {
  444. Rectangle viewport = Viewport;
  445. if (_needsDisplayRect != Rectangle.Empty && viewport.IsEmpty)
  446. {
  447. // This handles the case where the view has not been initialized yet
  448. return;
  449. }
  450. SetNeedsDisplay (viewport);
  451. }
  452. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  453. /// <remarks>
  454. /// <para>
  455. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  456. /// </para>
  457. /// <para>
  458. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  459. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  460. /// </para>
  461. /// </remarks>
  462. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  463. public void SetNeedsDisplay (Rectangle viewPortRelativeRegion)
  464. {
  465. if (_needsDisplayRect.IsEmpty)
  466. {
  467. _needsDisplayRect = viewPortRelativeRegion;
  468. }
  469. else
  470. {
  471. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  472. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  473. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  474. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  475. _needsDisplayRect = new (x, y, w, h);
  476. }
  477. Margin?.SetNeedsDisplay ();
  478. Border?.SetNeedsDisplay ();
  479. Padding?.SetNeedsDisplay ();
  480. SuperView?.SetSubViewNeedsDisplay ();
  481. if (this is Adornment adornment)
  482. {
  483. adornment.Parent?.SetSubViewNeedsDisplay ();
  484. }
  485. foreach (View subview in Subviews)
  486. {
  487. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  488. {
  489. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  490. subviewRegion.X -= subview.Frame.X;
  491. subviewRegion.Y -= subview.Frame.Y;
  492. subview.SetNeedsDisplay (subviewRegion);
  493. }
  494. }
  495. }
  496. /// <summary>Sets <see cref="SubViewNeedsDisplay"/> to <see langword="true"/> for this View and all Superviews.</summary>
  497. public void SetSubViewNeedsDisplay ()
  498. {
  499. SubViewNeedsDisplay = true;
  500. if (this is Adornment adornment)
  501. {
  502. adornment.Parent?.SetSubViewNeedsDisplay ();
  503. }
  504. if (SuperView is { SubViewNeedsDisplay: false })
  505. {
  506. SuperView.SetSubViewNeedsDisplay ();
  507. }
  508. }
  509. /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
  510. protected void ClearNeedsDisplay ()
  511. {
  512. _needsDisplayRect = Rectangle.Empty;
  513. SubViewNeedsDisplay = false;
  514. Margin?.ClearNeedsDisplay ();
  515. Border?.ClearNeedsDisplay ();
  516. Padding?.ClearNeedsDisplay ();
  517. foreach (View subview in Subviews)
  518. {
  519. subview.ClearNeedsDisplay ();
  520. }
  521. }
  522. #endregion NeedsDisplay
  523. }