View.Drawing.cs 32 KB

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