View.Drawing.cs 28 KB

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