ViewDrawing.cs 24 KB

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