View.Drawing.cs 28 KB

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