ViewDrawing.cs 21 KB

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