ViewDrawing.cs 18 KB

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