ViewDrawing.cs 17 KB

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