ViewDrawing.cs 25 KB

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