View.Drawing.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. 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. /// Always use <see cref="Viewport"/> (view-relative) when calling <see cref="OnDrawContent(Rectangle)"/>, NOT
  167. /// <see cref="Frame"/> (superview-relative).
  168. /// </para>
  169. /// <para>
  170. /// Views should set the color that they want to use on entry, as otherwise this will inherit the last color that
  171. /// was set globally on the driver.
  172. /// </para>
  173. /// <para>
  174. /// Overrides of <see cref="OnDrawContent(Rectangle)"/> must ensure they do not set <c>Driver.Clip</c> to a clip
  175. /// region larger than the <ref name="Viewport"/> property, as this will cause the driver to clip the entire
  176. /// region.
  177. /// </para>
  178. /// </remarks>
  179. public void Draw ()
  180. {
  181. OnDrawAdornments ();
  182. if (ColorScheme is { })
  183. {
  184. //Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  185. Driver?.SetAttribute (GetNormalColor ());
  186. }
  187. // By default, we clip to the viewport preventing drawing outside the viewport
  188. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  189. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  190. Rectangle prevClip = SetClip ();
  191. // Invoke DrawContentEvent
  192. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  193. DrawContent?.Invoke (this, dev);
  194. if (!dev.Cancel)
  195. {
  196. OnDrawContent (Viewport);
  197. }
  198. if (Driver is { })
  199. {
  200. Driver.Clip = prevClip;
  201. }
  202. OnRenderLineCanvas ();
  203. // TODO: This is a hack to force the border subviews to draw.
  204. if (Border?.Subviews is { })
  205. {
  206. foreach (View view in Border.Subviews)
  207. {
  208. view.SetNeedsDisplay ();
  209. view.Draw ();
  210. }
  211. }
  212. // Invoke DrawContentCompleteEvent
  213. OnDrawContentComplete (Viewport);
  214. // BUGBUG: v2 - We should be able to use View.SetClip here and not have to resort to knowing Driver details.
  215. ClearLayoutNeeded ();
  216. ClearNeedsDisplay ();
  217. }
  218. /// <summary>Event invoked when the content area of the View is to be drawn.</summary>
  219. /// <remarks>
  220. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  221. /// <para>
  222. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  223. /// <see cref="View"/> .
  224. /// </para>
  225. /// </remarks>
  226. public event EventHandler<DrawEventArgs>? DrawContent;
  227. /// <summary>Event invoked when the content area of the View is completed drawing.</summary>
  228. /// <remarks>
  229. /// <para>Will be invoked after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.</para>
  230. /// <para>
  231. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  232. /// <see cref="View"/> .
  233. /// </para>
  234. /// </remarks>
  235. public event EventHandler<DrawEventArgs>? DrawContentComplete;
  236. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  237. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  238. /// <param name="hotColor">Hot color.</param>
  239. /// <param name="normalColor">Normal color.</param>
  240. /// <remarks>
  241. /// <para>
  242. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  243. /// default.
  244. /// </para>
  245. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  246. /// </remarks>
  247. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  248. {
  249. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  250. Application.Driver?.SetAttribute (normalColor);
  251. foreach (Rune rune in text.EnumerateRunes ())
  252. {
  253. if (rune == new Rune (hotkeySpec.Value))
  254. {
  255. Application.Driver?.SetAttribute (hotColor);
  256. continue;
  257. }
  258. Application.Driver?.AddRune (rune);
  259. Application.Driver?.SetAttribute (normalColor);
  260. }
  261. }
  262. /// <summary>
  263. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  264. /// state.
  265. /// </summary>
  266. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  267. /// <param name="focused">
  268. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  269. /// the regular ones.
  270. /// </param>
  271. /// <param name="scheme">The color scheme to use.</param>
  272. public void DrawHotString (string text, bool focused, ColorScheme scheme)
  273. {
  274. if (focused)
  275. {
  276. DrawHotString (text, scheme.HotFocus, scheme.Focus);
  277. }
  278. else
  279. {
  280. DrawHotString (
  281. text,
  282. Enabled ? scheme.HotNormal : scheme.Disabled,
  283. Enabled ? scheme.Normal : scheme.Disabled
  284. );
  285. }
  286. }
  287. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  288. /// <returns>
  289. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  290. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  291. /// overridden can return other values.
  292. /// </returns>
  293. public virtual Attribute GetFocusColor ()
  294. {
  295. ColorScheme? cs = ColorScheme;
  296. if (cs is null)
  297. {
  298. cs = new ();
  299. }
  300. return Enabled ? cs.Focus : cs.Disabled;
  301. }
  302. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  303. /// <returns>
  304. /// <see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  305. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  306. /// overridden can return other values.
  307. /// </returns>
  308. public virtual Attribute GetHotNormalColor ()
  309. {
  310. ColorScheme? cs = ColorScheme;
  311. if (cs is null)
  312. {
  313. cs = new ();
  314. }
  315. return Enabled ? cs.HotNormal : cs.Disabled;
  316. }
  317. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  318. /// <returns>
  319. /// <see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  320. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  321. /// overridden can return other values.
  322. /// </returns>
  323. public virtual Attribute GetNormalColor ()
  324. {
  325. ColorScheme? cs = ColorScheme;
  326. if (cs is null)
  327. {
  328. cs = new ();
  329. }
  330. return Enabled ? cs.Normal : cs.Disabled;
  331. }
  332. /// <summary>Moves the drawing cursor to the specified <see cref="Viewport"/>-relative location in the view.</summary>
  333. /// <remarks>
  334. /// <para>
  335. /// If the provided coordinates are outside the visible content area, this method does nothing.
  336. /// </para>
  337. /// <para>
  338. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  339. /// </para>
  340. /// </remarks>
  341. /// <param name="col">Column (viewport-relative).</param>
  342. /// <param name="row">Row (viewport-relative).</param>
  343. public bool Move (int col, int row)
  344. {
  345. if (Driver is null || Driver?.Rows == 0)
  346. {
  347. return false;
  348. }
  349. if (col < 0 || row < 0 || col >= Viewport.Width || row >= Viewport.Height)
  350. {
  351. return false;
  352. }
  353. Point screen = ViewportToScreen (new Point (col, row));
  354. Driver?.Move (screen.X, screen.Y);
  355. return true;
  356. }
  357. // TODO: Make this cancelable
  358. /// <summary>
  359. /// Prepares <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  360. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  361. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  362. /// </summary>
  363. /// <returns></returns>
  364. public virtual bool OnDrawAdornments ()
  365. {
  366. if (!IsInitialized)
  367. {
  368. return false;
  369. }
  370. // Each of these renders lines to either this View's LineCanvas
  371. // Those lines will be finally rendered in OnRenderLineCanvas
  372. Margin?.OnDrawContent (Margin.Viewport);
  373. Border?.OnDrawContent (Border.Viewport);
  374. Padding?.OnDrawContent (Padding.Viewport);
  375. return true;
  376. }
  377. /// <summary>
  378. /// Draws the view's content, including Subviews.
  379. /// </summary>
  380. /// <remarks>
  381. /// <para>
  382. /// The <paramref name="viewport"/> parameter is provided as a convenience; it has the same values as the
  383. /// <see cref="Viewport"/> property.
  384. /// </para>
  385. /// <para>
  386. /// The <see cref="Viewport"/> Location and Size indicate what part of the View's content, defined
  387. /// by <see cref="GetContentSize ()"/>, is visible and should be drawn. The coordinates taken by <see cref="Move"/>
  388. /// and
  389. /// <see cref="AddRune"/> are relative to <see cref="Viewport"/>, thus if <c>ViewPort.Location.Y</c> is <c>5</c>
  390. /// the 6th row of the content should be drawn using <c>MoveTo (x, 5)</c>.
  391. /// </para>
  392. /// <para>
  393. /// If <see cref="GetContentSize ()"/> is larger than <c>ViewPort.Size</c> drawing code should use
  394. /// <see cref="Viewport"/>
  395. /// to constrain drawing for better performance.
  396. /// </para>
  397. /// <para>
  398. /// The <see cref="ConsoleDriver.Clip"/> may define smaller area than <see cref="Viewport"/>; complex drawing code
  399. /// can be more
  400. /// efficient by using <see cref="ConsoleDriver.Clip"/> to constrain drawing for better performance.
  401. /// </para>
  402. /// <para>
  403. /// Overrides should loop through the subviews and call <see cref="Draw"/>.
  404. /// </para>
  405. /// </remarks>
  406. /// <param name="viewport">
  407. /// The rectangle describing the currently visible viewport into the <see cref="View"/>; has the same value as
  408. /// <see cref="Viewport"/>.
  409. /// </param>
  410. public virtual void OnDrawContent (Rectangle viewport)
  411. {
  412. if (NeedsDisplay)
  413. {
  414. if (!CanBeVisible (this))
  415. {
  416. return;
  417. }
  418. if (SuperView is { } || this == Application.Top)
  419. {
  420. Clear ();
  421. }
  422. if (!string.IsNullOrEmpty (TextFormatter.Text))
  423. {
  424. if (TextFormatter is { })
  425. {
  426. TextFormatter.NeedsFormat = true;
  427. }
  428. }
  429. // This should NOT clear
  430. // TODO: If the output is not in the Viewport, do nothing
  431. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  432. TextFormatter?.Draw (
  433. drawRect,
  434. HasFocus ? GetFocusColor () : GetNormalColor (),
  435. HasFocus ? ColorScheme!.HotFocus : GetHotNormalColor (),
  436. Rectangle.Empty
  437. );
  438. SetSubViewNeedsDisplay ();
  439. }
  440. // TODO: Move drawing of subviews to a separate OnDrawSubviews virtual method
  441. // Draw subviews
  442. // TODO: Implement OnDrawSubviews (cancelable);
  443. if (_subviews is { } && SubViewNeedsDisplay)
  444. {
  445. IEnumerable<View> subviewsNeedingDraw = _subviews.Where (
  446. view => view.Visible
  447. && (view.NeedsDisplay
  448. || view.SubViewNeedsDisplay
  449. || view.LayoutNeeded)
  450. );
  451. foreach (View view in subviewsNeedingDraw)
  452. {
  453. if (view.LayoutNeeded)
  454. {
  455. view.LayoutSubviews ();
  456. }
  457. view.Draw ();
  458. }
  459. }
  460. }
  461. /// <summary>
  462. /// Called after <see cref="OnDrawContent"/> to enable overrides.
  463. /// </summary>
  464. /// <param name="viewport">
  465. /// The viewport-relative rectangle describing the currently visible viewport into the
  466. /// <see cref="View"/>
  467. /// </param>
  468. public virtual void OnDrawContentComplete (Rectangle viewport) { DrawContentComplete?.Invoke (this, new (viewport, Rectangle.Empty)); }
  469. // TODO: Make this cancelable
  470. /// <summary>
  471. /// Renders <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  472. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  473. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  474. /// </summary>
  475. /// <returns></returns>
  476. public virtual bool OnRenderLineCanvas ()
  477. {
  478. if (!IsInitialized || Driver is null)
  479. {
  480. return false;
  481. }
  482. // If we have a SuperView, it'll render our frames.
  483. if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != Rectangle.Empty)
  484. {
  485. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  486. {
  487. // Get the entire map
  488. if (p.Value is { })
  489. {
  490. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  491. Driver.Move (p.Key.X, p.Key.Y);
  492. // TODO: #2616 - Support combining sequences that don't normalize
  493. Driver.AddRune (p.Value.Value.Rune);
  494. }
  495. }
  496. LineCanvas.Clear ();
  497. }
  498. if (Subviews.Any (s => s.SuperViewRendersLineCanvas))
  499. {
  500. foreach (View subview in Subviews.Where (s => s.SuperViewRendersLineCanvas))
  501. {
  502. // Combine the LineCanvas'
  503. LineCanvas.Merge (subview.LineCanvas);
  504. subview.LineCanvas.Clear ();
  505. }
  506. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  507. {
  508. // Get the entire map
  509. if (p.Value is { })
  510. {
  511. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  512. Driver.Move (p.Key.X, p.Key.Y);
  513. // TODO: #2616 - Support combining sequences that don't normalize
  514. Driver.AddRune (p.Value.Value.Rune);
  515. }
  516. }
  517. LineCanvas.Clear ();
  518. }
  519. return true;
  520. }
  521. /// <summary>Sets the area of this view needing to be redrawn to <see cref="Viewport"/>.</summary>
  522. /// <remarks>
  523. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  524. /// does nothing.
  525. /// </remarks>
  526. public void SetNeedsDisplay () { SetNeedsDisplay (Viewport); }
  527. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="region"/>.</summary>
  528. /// <remarks>
  529. /// <para>
  530. /// The location of <paramref name="region"/> is relative to the View's content, bound by <c>Size.Empty</c> and
  531. /// <see cref="GetContentSize ()"/>.
  532. /// </para>
  533. /// <para>
  534. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  535. /// redrawn will be the <paramref name="region"/>.
  536. /// </para>
  537. /// </remarks>
  538. /// <param name="region">The content-relative region that needs to be redrawn.</param>
  539. public void SetNeedsDisplay (Rectangle region)
  540. {
  541. if (_needsDisplayRect.IsEmpty)
  542. {
  543. _needsDisplayRect = region;
  544. }
  545. else
  546. {
  547. int x = Math.Min (_needsDisplayRect.X, region.X);
  548. int y = Math.Min (_needsDisplayRect.Y, region.Y);
  549. int w = Math.Max (_needsDisplayRect.Width, region.Width);
  550. int h = Math.Max (_needsDisplayRect.Height, region.Height);
  551. _needsDisplayRect = new (x, y, w, h);
  552. }
  553. Margin?.SetNeedsDisplay ();
  554. Border?.SetNeedsDisplay ();
  555. Padding?.SetNeedsDisplay ();
  556. SuperView?.SetSubViewNeedsDisplay ();
  557. foreach (View subview in Subviews)
  558. {
  559. if (subview.Frame.IntersectsWith (region))
  560. {
  561. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, region);
  562. subviewRegion.X -= subview.Frame.X;
  563. subviewRegion.Y -= subview.Frame.Y;
  564. subview.SetNeedsDisplay (subviewRegion);
  565. }
  566. }
  567. }
  568. /// <summary>Sets <see cref="SubViewNeedsDisplay"/> to <see langword="true"/> for this View and all Superviews.</summary>
  569. public void SetSubViewNeedsDisplay ()
  570. {
  571. SubViewNeedsDisplay = true;
  572. if (this is Adornment adornment)
  573. {
  574. adornment.Parent?.SetSubViewNeedsDisplay ();
  575. }
  576. if (SuperView is { SubViewNeedsDisplay: false })
  577. {
  578. SuperView.SetSubViewNeedsDisplay ();
  579. }
  580. }
  581. /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
  582. protected void ClearNeedsDisplay ()
  583. {
  584. _needsDisplayRect = Rectangle.Empty;
  585. SubViewNeedsDisplay = false;
  586. Margin?.ClearNeedsDisplay ();
  587. Border?.ClearNeedsDisplay ();
  588. Padding?.ClearNeedsDisplay ();
  589. foreach (View subview in Subviews)
  590. {
  591. subview.ClearNeedsDisplay ();
  592. }
  593. }
  594. }