ViewDrawing.cs 18 KB

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