ViewDrawing.cs 22 KB

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