View.Drawing.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public partial class View // Drawing APIs
  4. {
  5. private ColorScheme? _colorScheme;
  6. /// <summary>The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s color scheme.</summary>
  7. public virtual ColorScheme? ColorScheme
  8. {
  9. get
  10. {
  11. if (_colorScheme is null)
  12. {
  13. return SuperView?.ColorScheme;
  14. }
  15. return _colorScheme;
  16. }
  17. set
  18. {
  19. if (_colorScheme != value)
  20. {
  21. _colorScheme = value;
  22. if (Border is { } && Border.LineStyle != LineStyle.None && Border.ColorScheme is { })
  23. {
  24. Border.ColorScheme = _colorScheme;
  25. }
  26. SetNeedsDisplay ();
  27. }
  28. }
  29. }
  30. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  31. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  32. public LineCanvas LineCanvas { get; } = new ();
  33. // The view-relative region that needs to be redrawn. Marked internal for unit tests.
  34. internal Rectangle _needsDisplayRect = Rectangle.Empty;
  35. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  36. public bool NeedsDisplay
  37. {
  38. get => _needsDisplayRect != Rectangle.Empty;
  39. set
  40. {
  41. if (value)
  42. {
  43. SetNeedsDisplay ();
  44. }
  45. else
  46. {
  47. ClearNeedsDisplay ();
  48. }
  49. }
  50. }
  51. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  52. public bool SubViewNeedsDisplay { get; private set; }
  53. /// <summary>
  54. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  55. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  56. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawAdornments"/> method will be
  57. /// called to render the borders.
  58. /// </summary>
  59. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  60. /// <summary>Draws the specified character in the specified viewport-relative column and row of the View.</summary>
  61. /// <para>
  62. /// If the provided coordinates are outside the visible content area, this method does nothing.
  63. /// </para>
  64. /// <remarks>
  65. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  66. /// </remarks>
  67. /// <param name="col">Column (viewport-relative).</param>
  68. /// <param name="row">Row (viewport-relative).</param>
  69. /// <param name="rune">The Rune.</param>
  70. public void AddRune (int col, int row, Rune rune)
  71. {
  72. if (Move (col, row))
  73. {
  74. Driver.AddRune (rune);
  75. }
  76. }
  77. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  78. /// <remarks>
  79. /// <para>
  80. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  81. /// the portion of the content
  82. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  83. /// a
  84. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  85. /// enabled) and want
  86. /// the area outside the content to be visually distinct.
  87. /// </para>
  88. /// </remarks>
  89. public void Clear ()
  90. {
  91. if (Driver is null)
  92. {
  93. return;
  94. }
  95. // Get screen-relative coords
  96. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  97. Rectangle prevClip = Driver.Clip;
  98. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  99. {
  100. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  101. toClear = Rectangle.Intersect (toClear, visibleContent);
  102. }
  103. Attribute prev = Driver.SetAttribute (GetNormalColor ());
  104. Driver.FillRect (toClear);
  105. Driver.SetAttribute (prev);
  106. Driver.Clip = prevClip;
  107. }
  108. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  109. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  110. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  111. public void FillRect (Rectangle rect, Color? color = null)
  112. {
  113. if (Driver is null)
  114. {
  115. return;
  116. }
  117. // Get screen-relative coords
  118. Rectangle toClear = ViewportToScreen (rect);
  119. Rectangle prevClip = Driver.Clip;
  120. Driver.Clip = Rectangle.Intersect (prevClip, ViewportToScreen (Viewport with { Location = new (0, 0) }));
  121. Attribute prev = Driver.SetAttribute (new (color ?? GetNormalColor ().Background));
  122. Driver.FillRect (toClear);
  123. Driver.SetAttribute (prev);
  124. Driver.Clip = prevClip;
  125. }
  126. /// <summary>Sets the <see cref="ConsoleDriver"/>'s clip region to <see cref="Viewport"/>.</summary>
  127. /// <remarks>
  128. /// <para>
  129. /// By default, the clip rectangle is set to the intersection of the current clip region and the
  130. /// <see cref="Viewport"/>. This ensures that drawing is constrained to the viewport, but allows
  131. /// content to be drawn beyond the viewport.
  132. /// </para>
  133. /// <para>
  134. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClipContentOnly"/> set, clipping will be
  135. /// applied to just the visible content area.
  136. /// </para>
  137. /// </remarks>
  138. /// <returns>
  139. /// The current screen-relative clip region, which can be then re-applied by setting
  140. /// <see cref="ConsoleDriver.Clip"/>.
  141. /// </returns>
  142. public Rectangle SetClip ()
  143. {
  144. if (Driver is null)
  145. {
  146. return Rectangle.Empty;
  147. }
  148. Rectangle previous = Driver.Clip;
  149. // Clamp the Clip to the entire visible area
  150. Rectangle clip = Rectangle.Intersect (ViewportToScreen (Viewport with { Location = Point.Empty }), previous);
  151. if (ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly))
  152. {
  153. // Clamp the Clip to the just content area that is within the viewport
  154. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  155. clip = Rectangle.Intersect (clip, visibleContent);
  156. }
  157. Driver.Clip = clip;
  158. return previous;
  159. }
  160. /// <summary>
  161. /// Draws the view if it needs to be drawn. Causes the following virtual methods to be called (along with their related events):
  162. /// <see cref="OnDrawContent"/>, <see cref="OnDrawContentComplete"/>.
  163. /// </summary>
  164. /// <remarks>
  165. /// <para>
  166. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDisplay"/>, <see cref="SubViewNeedsDisplay"/>,
  167. /// or <see cref="LayoutNeeded"/> set.
  168. /// </para>
  169. /// <para>
  170. /// Always use <see cref="Viewport"/> (view-relative) when calling <see cref="OnDrawContent(Rectangle)"/>, NOT
  171. /// <see cref="Frame"/> (superview-relative).
  172. /// </para>
  173. /// <para>
  174. /// Views should set the color that they want to use on entry, as otherwise this will inherit the last color that
  175. /// was set globally on the driver.
  176. /// </para>
  177. /// <para>
  178. /// Overrides of <see cref="OnDrawContent(Rectangle)"/> must ensure they do not set <c>Driver.Clip</c> to a clip
  179. /// region larger than the <ref name="Viewport"/> property, as this will cause the driver to clip the entire
  180. /// region.
  181. /// </para>
  182. /// </remarks>
  183. public void Draw ()
  184. {
  185. if (!CanBeVisible (this))
  186. {
  187. return;
  188. }
  189. // TODO: This ensures overlapped views are drawn correctly. However, this is inefficient.
  190. // TODO: The correct fix is to implement non-rectangular clip regions: https://github.com/gui-cs/Terminal.Gui/issues/3413
  191. if (Arrangement.HasFlag (ViewArrangement.Overlapped))
  192. {
  193. SetNeedsDisplay ();
  194. }
  195. if (!NeedsDisplay && !SubViewNeedsDisplay && !LayoutNeeded)
  196. {
  197. return;
  198. }
  199. OnDrawAdornments ();
  200. if (ColorScheme is { })
  201. {
  202. //Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  203. Driver?.SetAttribute (GetNormalColor ());
  204. }
  205. // By default, we clip to the viewport preventing drawing outside the viewport
  206. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  207. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  208. Rectangle prevClip = SetClip ();
  209. // Invoke DrawContentEvent
  210. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  211. DrawContent?.Invoke (this, dev);
  212. if (!dev.Cancel)
  213. {
  214. OnDrawContent (Viewport);
  215. }
  216. if (Driver is { })
  217. {
  218. Driver.Clip = prevClip;
  219. }
  220. OnRenderLineCanvas ();
  221. // TODO: This is a hack to force the border subviews to draw.
  222. if (Border?.Subviews is { })
  223. {
  224. foreach (View view in Border.Subviews)
  225. {
  226. view.SetNeedsDisplay ();
  227. view.Draw ();
  228. }
  229. }
  230. // Invoke DrawContentCompleteEvent
  231. OnDrawContentComplete (Viewport);
  232. // BUGBUG: v2 - We should be able to use View.SetClip here and not have to resort to knowing Driver details.
  233. ClearLayoutNeeded ();
  234. ClearNeedsDisplay ();
  235. }
  236. /// <summary>Event invoked when the content area of the View is to be drawn.</summary>
  237. /// <remarks>
  238. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  239. /// <para>
  240. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  241. /// <see cref="View"/> .
  242. /// </para>
  243. /// </remarks>
  244. public event EventHandler<DrawEventArgs>? DrawContent;
  245. /// <summary>Event invoked when the content area of the View is completed drawing.</summary>
  246. /// <remarks>
  247. /// <para>Will be invoked after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.</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>? DrawContentComplete;
  254. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  255. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  256. /// <param name="hotColor">Hot color.</param>
  257. /// <param name="normalColor">Normal color.</param>
  258. /// <remarks>
  259. /// <para>
  260. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  261. /// default.
  262. /// </para>
  263. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  264. /// </remarks>
  265. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  266. {
  267. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  268. Application.Driver?.SetAttribute (normalColor);
  269. foreach (Rune rune in text.EnumerateRunes ())
  270. {
  271. if (rune == new Rune (hotkeySpec.Value))
  272. {
  273. Application.Driver?.SetAttribute (hotColor);
  274. continue;
  275. }
  276. Application.Driver?.AddRune (rune);
  277. Application.Driver?.SetAttribute (normalColor);
  278. }
  279. }
  280. /// <summary>
  281. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  282. /// state.
  283. /// </summary>
  284. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  285. /// <param name="focused">
  286. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  287. /// the regular ones.
  288. /// </param>
  289. /// <param name="scheme">The color scheme to use.</param>
  290. public void DrawHotString (string text, bool focused, ColorScheme scheme)
  291. {
  292. if (focused)
  293. {
  294. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  295. }
  296. else
  297. {
  298. DrawHotString (
  299. text,
  300. Enabled ? scheme.HotNormal : scheme.Disabled,
  301. Enabled ? scheme.Normal : scheme.Disabled
  302. );
  303. }
  304. }
  305. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  306. /// <returns>
  307. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  308. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  309. /// overridden can return other values.
  310. /// </returns>
  311. public virtual Attribute GetFocusColor ()
  312. {
  313. ColorScheme? cs = ColorScheme;
  314. if (cs is null)
  315. {
  316. cs = new ();
  317. }
  318. return Enabled ? cs.Focus : cs.Disabled;
  319. }
  320. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  321. /// <returns>
  322. /// <see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  323. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  324. /// overridden can return other values.
  325. /// </returns>
  326. public virtual Attribute GetHotNormalColor ()
  327. {
  328. ColorScheme? cs = ColorScheme;
  329. if (cs is null)
  330. {
  331. cs = new ();
  332. }
  333. return Enabled ? cs.HotNormal : cs.Disabled;
  334. }
  335. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  336. /// <returns>
  337. /// <see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  338. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  339. /// overridden can return other values.
  340. /// </returns>
  341. public virtual Attribute GetNormalColor ()
  342. {
  343. ColorScheme? cs = ColorScheme;
  344. if (cs is null)
  345. {
  346. cs = new ();
  347. }
  348. return Enabled ? cs.Normal : cs.Disabled;
  349. }
  350. /// <summary>Moves the drawing cursor to the specified <see cref="Viewport"/>-relative location in the view.</summary>
  351. /// <remarks>
  352. /// <para>
  353. /// If the provided coordinates are outside the visible content area, this method does nothing.
  354. /// </para>
  355. /// <para>
  356. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  357. /// </para>
  358. /// </remarks>
  359. /// <param name="col">Column (viewport-relative).</param>
  360. /// <param name="row">Row (viewport-relative).</param>
  361. public bool Move (int col, int row)
  362. {
  363. if (Driver is null || Driver?.Rows == 0)
  364. {
  365. return false;
  366. }
  367. if (col < 0 || row < 0 || col >= Viewport.Width || row >= Viewport.Height)
  368. {
  369. return false;
  370. }
  371. Point screen = ViewportToScreen (new Point (col, row));
  372. Driver?.Move (screen.X, screen.Y);
  373. return true;
  374. }
  375. // TODO: Make this cancelable
  376. /// <summary>
  377. /// Prepares <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  378. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  379. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  380. /// </summary>
  381. /// <returns></returns>
  382. public virtual bool OnDrawAdornments ()
  383. {
  384. if (!IsInitialized)
  385. {
  386. return false;
  387. }
  388. // Each of these renders lines to either this View's LineCanvas
  389. // Those lines will be finally rendered in OnRenderLineCanvas
  390. Margin?.OnDrawContent (Margin.Viewport);
  391. Border?.OnDrawContent (Border.Viewport);
  392. Padding?.OnDrawContent (Padding.Viewport);
  393. return true;
  394. }
  395. /// <summary>
  396. /// Draws the view's content, including Subviews.
  397. /// </summary>
  398. /// <remarks>
  399. /// <para>
  400. /// The <paramref name="viewport"/> parameter is provided as a convenience; it has the same values as the
  401. /// <see cref="Viewport"/> property.
  402. /// </para>
  403. /// <para>
  404. /// The <see cref="Viewport"/> Location and Size indicate what part of the View's content, defined
  405. /// by <see cref="GetContentSize ()"/>, is visible and should be drawn. The coordinates taken by <see cref="Move"/>
  406. /// and
  407. /// <see cref="AddRune"/> are relative to <see cref="Viewport"/>, thus if <c>ViewPort.Location.Y</c> is <c>5</c>
  408. /// the 6th row of the content should be drawn using <c>MoveTo (x, 5)</c>.
  409. /// </para>
  410. /// <para>
  411. /// If <see cref="GetContentSize ()"/> is larger than <c>ViewPort.Size</c> drawing code should use
  412. /// <see cref="Viewport"/>
  413. /// to constrain drawing for better performance.
  414. /// </para>
  415. /// <para>
  416. /// The <see cref="ConsoleDriver.Clip"/> may define smaller area than <see cref="Viewport"/>; complex drawing code
  417. /// can be more
  418. /// efficient by using <see cref="ConsoleDriver.Clip"/> to constrain drawing for better performance.
  419. /// </para>
  420. /// <para>
  421. /// Overrides should loop through the subviews and call <see cref="Draw"/>.
  422. /// </para>
  423. /// </remarks>
  424. /// <param name="viewport">
  425. /// The rectangle describing the currently visible viewport into the <see cref="View"/>; has the same value as
  426. /// <see cref="Viewport"/>.
  427. /// </param>
  428. public virtual void OnDrawContent (Rectangle viewport)
  429. {
  430. if (NeedsDisplay)
  431. {
  432. if (!CanBeVisible (this))
  433. {
  434. return;
  435. }
  436. // BUGBUG: this clears way too frequently. Need to optimize this.
  437. if (SuperView is { } || Arrangement.HasFlag (ViewArrangement.Overlapped))
  438. {
  439. Clear ();
  440. }
  441. if (!string.IsNullOrEmpty (TextFormatter.Text))
  442. {
  443. if (TextFormatter is { })
  444. {
  445. TextFormatter.NeedsFormat = true;
  446. }
  447. }
  448. // This should NOT clear
  449. // TODO: If the output is not in the Viewport, do nothing
  450. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  451. TextFormatter?.Draw (
  452. drawRect,
  453. HasFocus ? GetFocusColor () : GetNormalColor (),
  454. HasFocus ? ColorScheme!.HotFocus : GetHotNormalColor (),
  455. Rectangle.Empty
  456. );
  457. SetSubViewNeedsDisplay ();
  458. }
  459. // TODO: Move drawing of subviews to a separate OnDrawSubviews virtual method
  460. // Draw subviews
  461. // TODO: Implement OnDrawSubviews (cancelable);
  462. if (_subviews is { } && SubViewNeedsDisplay)
  463. {
  464. IEnumerable<View> subviewsNeedingDraw = _subviews.Where (
  465. view => view.Visible
  466. && (view.NeedsDisplay
  467. || view.SubViewNeedsDisplay
  468. || view.LayoutNeeded
  469. || view.Arrangement.HasFlag (ViewArrangement.Overlapped)
  470. ));
  471. foreach (View view in subviewsNeedingDraw)
  472. {
  473. if (view.LayoutNeeded)
  474. {
  475. view.LayoutSubviews ();
  476. }
  477. // TODO: This ensures overlapped views are drawn correctly. However, this is inefficient.
  478. // TODO: The correct fix is to implement non-rectangular clip regions: https://github.com/gui-cs/Terminal.Gui/issues/3413
  479. if (view.Arrangement.HasFlag (ViewArrangement.Overlapped))
  480. {
  481. view.SetNeedsDisplay ();
  482. }
  483. view.Draw ();
  484. }
  485. }
  486. }
  487. /// <summary>
  488. /// Called after <see cref="OnDrawContent"/> to enable overrides.
  489. /// </summary>
  490. /// <param name="viewport">
  491. /// The viewport-relative rectangle describing the currently visible viewport into the
  492. /// <see cref="View"/>
  493. /// </param>
  494. public virtual void OnDrawContentComplete (Rectangle viewport) { DrawContentComplete?.Invoke (this, new (viewport, Rectangle.Empty)); }
  495. // TODO: Make this cancelable
  496. /// <summary>
  497. /// Renders <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  498. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  499. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  500. /// </summary>
  501. /// <returns></returns>
  502. public virtual bool OnRenderLineCanvas ()
  503. {
  504. if (!IsInitialized || Driver is null)
  505. {
  506. return false;
  507. }
  508. // If we have a SuperView, it'll render our frames.
  509. if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != 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. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.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. if (Subviews.Any (s => s.SuperViewRendersLineCanvas))
  525. {
  526. foreach (View subview in Subviews.Where (s => s.SuperViewRendersLineCanvas))
  527. {
  528. // Combine the LineCanvas'
  529. LineCanvas.Merge (subview.LineCanvas);
  530. subview.LineCanvas.Clear ();
  531. }
  532. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  533. {
  534. // Get the entire map
  535. if (p.Value is { })
  536. {
  537. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  538. Driver.Move (p.Key.X, p.Key.Y);
  539. // TODO: #2616 - Support combining sequences that don't normalize
  540. Driver.AddRune (p.Value.Value.Rune);
  541. }
  542. }
  543. LineCanvas.Clear ();
  544. }
  545. return true;
  546. }
  547. /// <summary>Sets the area of this view needing to be redrawn to <see cref="Viewport"/>.</summary>
  548. /// <remarks>
  549. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  550. /// does nothing.
  551. /// </remarks>
  552. public void SetNeedsDisplay () { SetNeedsDisplay (Viewport); }
  553. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="region"/>.</summary>
  554. /// <remarks>
  555. /// <para>
  556. /// The location of <paramref name="region"/> is relative to the View's content, bound by <c>Size.Empty</c> and
  557. /// <see cref="GetContentSize ()"/>.
  558. /// </para>
  559. /// <para>
  560. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  561. /// redrawn will be the <paramref name="region"/>.
  562. /// </para>
  563. /// </remarks>
  564. /// <param name="region">The content-relative region that needs to be redrawn.</param>
  565. public void SetNeedsDisplay (Rectangle region)
  566. {
  567. if (_needsDisplayRect.IsEmpty)
  568. {
  569. _needsDisplayRect = region;
  570. }
  571. else
  572. {
  573. int x = Math.Min (_needsDisplayRect.X, region.X);
  574. int y = Math.Min (_needsDisplayRect.Y, region.Y);
  575. int w = Math.Max (_needsDisplayRect.Width, region.Width);
  576. int h = Math.Max (_needsDisplayRect.Height, region.Height);
  577. _needsDisplayRect = new (x, y, w, h);
  578. }
  579. Margin?.SetNeedsDisplay ();
  580. Border?.SetNeedsDisplay ();
  581. Padding?.SetNeedsDisplay ();
  582. SuperView?.SetSubViewNeedsDisplay ();
  583. foreach (View subview in Subviews)
  584. {
  585. if (subview.Frame.IntersectsWith (region))
  586. {
  587. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, region);
  588. subviewRegion.X -= subview.Frame.X;
  589. subviewRegion.Y -= subview.Frame.Y;
  590. subview.SetNeedsDisplay (subviewRegion);
  591. }
  592. }
  593. }
  594. /// <summary>Sets <see cref="SubViewNeedsDisplay"/> to <see langword="true"/> for this View and all Superviews.</summary>
  595. public void SetSubViewNeedsDisplay ()
  596. {
  597. SubViewNeedsDisplay = true;
  598. if (this is Adornment adornment)
  599. {
  600. adornment.Parent?.SetSubViewNeedsDisplay ();
  601. }
  602. if (SuperView is { SubViewNeedsDisplay: false })
  603. {
  604. SuperView.SetSubViewNeedsDisplay ();
  605. }
  606. }
  607. /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
  608. protected void ClearNeedsDisplay ()
  609. {
  610. _needsDisplayRect = Rectangle.Empty;
  611. SubViewNeedsDisplay = false;
  612. Margin?.ClearNeedsDisplay ();
  613. Border?.ClearNeedsDisplay ();
  614. Padding?.ClearNeedsDisplay ();
  615. foreach (View subview in Subviews)
  616. {
  617. subview.ClearNeedsDisplay ();
  618. }
  619. }
  620. }