ViewDrawing.cs 17 KB

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