DrawTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. #nullable enable
  2. using System.Text;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewTests;
  6. [Trait ("Category", "Output")]
  7. public class DrawTests (ITestOutputHelper output)
  8. {
  9. [Fact]
  10. [AutoInitShutdown]
  11. [Trait ("Category", "Unicode")]
  12. public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
  13. {
  14. const string us = "\U0000f900";
  15. var r = (Rune)0xf900;
  16. Assert.Equal ("豈", us);
  17. Assert.Equal ("豈", r.ToString ());
  18. Assert.Equal (us, r.ToString ());
  19. Assert.Equal (2, us.GetColumns ());
  20. Assert.Equal (2, r.GetColumns ());
  21. var win = new Window { Title = us };
  22. var view = new View { Text = r.ToString (), Height = Dim.Fill (), Width = Dim.Fill () };
  23. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  24. win.Add (view, tf);
  25. Toplevel top = new ();
  26. top.Add (win);
  27. Application.Begin (top);
  28. AutoInitShutdownAttribute.FakeResize(new Size(10, 4)) ;
  29. const string expectedOutput = """
  30. ┌┤豈├────┐
  31. │豈 │
  32. │豈 │
  33. └────────┘
  34. """;
  35. DriverAssert.AssertDriverContentsWithFrameAre (expectedOutput, output);
  36. DriverAssert.AssertDriverContentsAre (expectedOutput, output);
  37. // This test has nothing to do with color - removing as it is not relevant and fragile
  38. top.Dispose ();
  39. }
  40. [Fact]
  41. [AutoInitShutdown]
  42. [Trait ("Category", "Output")]
  43. public void Colors_On_TextAlignment_Right_And_Bottom ()
  44. {
  45. var viewRight = new View
  46. {
  47. Text = "Test",
  48. Width = 6,
  49. Height = 1,
  50. TextAlignment = Alignment.End,
  51. };
  52. var viewBottom = new View
  53. {
  54. Text = "Test",
  55. TextDirection = TextDirection.TopBottom_LeftRight,
  56. Y = 1,
  57. Width = 1,
  58. Height = 6,
  59. VerticalTextAlignment = Alignment.End,
  60. };
  61. Toplevel top = new ();
  62. top.Add (viewRight, viewBottom);
  63. var rs = Application.Begin (top);
  64. AutoInitShutdownAttribute.FakeResize(new Size(7, 7));
  65. AutoInitShutdownAttribute.RunIteration ();
  66. DriverAssert.AssertDriverContentsWithFrameAre (
  67. """
  68. Test
  69. T
  70. e
  71. s
  72. t
  73. """,
  74. output
  75. );
  76. DriverAssert.AssertDriverAttributesAre (
  77. """
  78. 000000
  79. 0
  80. 0
  81. 0
  82. 0
  83. 0
  84. 0
  85. """,
  86. output,
  87. Application.Driver,
  88. SchemeManager.GetSchemes () ["Base"]!.Normal
  89. );
  90. top.Dispose ();
  91. }
  92. [Fact]
  93. [SetupFakeDriver]
  94. public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
  95. {
  96. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  97. Assert.True (view.NeedsLayout);
  98. Assert.True (view.NeedsDraw);
  99. view.Layout ();
  100. Assert.Equal (new (0, 0, 2, 2), view.Frame);
  101. Assert.Equal (Rectangle.Empty, view.Viewport);
  102. Assert.True (view.NeedsDraw);
  103. view.Draw ();
  104. DriverAssert.AssertDriverContentsWithFrameAre (
  105. """
  106. ┌┐
  107. └┘
  108. """,
  109. output
  110. );
  111. }
  112. [Fact]
  113. [SetupFakeDriver]
  114. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Bottom ()
  115. {
  116. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  117. view.Border!.Thickness = new (1, 1, 1, 0);
  118. view.BeginInit ();
  119. view.EndInit ();
  120. view.SetRelativeLayout (Application.Screen.Size);
  121. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  122. Assert.Equal (Rectangle.Empty, view.Viewport);
  123. view.Draw ();
  124. DriverAssert.AssertDriverContentsWithFrameAre ("──", output);
  125. }
  126. [Fact]
  127. [SetupFakeDriver]
  128. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Left ()
  129. {
  130. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  131. view.Border!.Thickness = new (0, 1, 1, 1);
  132. view.BeginInit ();
  133. view.EndInit ();
  134. view.SetRelativeLayout (Application.Screen.Size);
  135. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  136. Assert.Equal (Rectangle.Empty, view.Viewport);
  137. view.Draw ();
  138. DriverAssert.AssertDriverContentsWithFrameAre (
  139. """
  140. """,
  141. output
  142. );
  143. }
  144. [Fact]
  145. [SetupFakeDriver]
  146. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Right ()
  147. {
  148. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  149. view.Border!.Thickness = new (1, 1, 0, 1);
  150. view.BeginInit ();
  151. view.EndInit ();
  152. view.SetRelativeLayout (Application.Screen.Size);
  153. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  154. Assert.Equal (Rectangle.Empty, view.Viewport);
  155. view.Draw ();
  156. DriverAssert.AssertDriverContentsWithFrameAre (
  157. """
  158. """,
  159. output
  160. );
  161. }
  162. [Fact]
  163. [SetupFakeDriver]
  164. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Top ()
  165. {
  166. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  167. view.Border!.Thickness = new (1, 0, 1, 1);
  168. view.BeginInit ();
  169. view.EndInit ();
  170. view.SetRelativeLayout (Application.Screen.Size);
  171. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  172. Assert.Equal (Rectangle.Empty, view.Viewport);
  173. view.Draw ();
  174. DriverAssert.AssertDriverContentsWithFrameAre (
  175. "││",
  176. output
  177. );
  178. }
  179. [Fact]
  180. [AutoInitShutdown]
  181. public void Draw_Negative_Viewport_Horizontal_With_New_Lines ()
  182. {
  183. var subView = new View
  184. {
  185. Id = "subView",
  186. X = 1,
  187. Width = 1,
  188. Height = 7,
  189. Text = """
  190. s
  191. u
  192. b
  193. V
  194. i
  195. e
  196. w
  197. """
  198. };
  199. var view = new View
  200. {
  201. Id = "view", Width = 2, Height = 20, Text = """
  202. 0
  203. 1
  204. 2
  205. 3
  206. 4
  207. 5
  208. 6
  209. 7
  210. 8
  211. 9
  212. 0
  213. 1
  214. 2
  215. 3
  216. 4
  217. 5
  218. 6
  219. 7
  220. 8
  221. 9
  222. """
  223. };
  224. view.Add (subView);
  225. var content = new View { Id = "content", Width = 20, Height = 20 };
  226. content.Add (view);
  227. var container = new View
  228. {
  229. Id = "container",
  230. X = 1,
  231. Y = 1,
  232. Width = 5,
  233. Height = 5
  234. };
  235. container.Add (content);
  236. Toplevel top = new ();
  237. top.Add (container);
  238. var rs = Application.Begin (top);
  239. top.Draw ();
  240. DriverAssert.AssertDriverContentsWithFrameAre (
  241. """
  242. 0s
  243. 1u
  244. 2b
  245. 3V
  246. 4i
  247. """,
  248. output
  249. );
  250. content.X = -1;
  251. AutoInitShutdownAttribute.RunIteration ();
  252. DriverAssert.AssertDriverContentsWithFrameAre (
  253. """
  254. s
  255. u
  256. b
  257. V
  258. i
  259. """,
  260. output
  261. );
  262. content.X = -2;
  263. AutoInitShutdownAttribute.RunIteration ();
  264. DriverAssert.AssertDriverContentsWithFrameAre (@"", output);
  265. content.X = 0;
  266. content.Y = -1;
  267. AutoInitShutdownAttribute.RunIteration ();
  268. DriverAssert.AssertDriverContentsWithFrameAre (
  269. """
  270. 1u
  271. 2b
  272. 3V
  273. 4i
  274. 5e
  275. """,
  276. output
  277. );
  278. content.Y = -6;
  279. AutoInitShutdownAttribute.RunIteration ();
  280. DriverAssert.AssertDriverContentsWithFrameAre (
  281. """
  282. 6w
  283. 7
  284. 8
  285. 9
  286. 0
  287. """,
  288. output
  289. );
  290. content.Y = -19;
  291. AutoInitShutdownAttribute.RunIteration ();
  292. DriverAssert.AssertDriverContentsWithFrameAre (
  293. """
  294. 9
  295. """,
  296. output
  297. );
  298. content.Y = -20;
  299. AutoInitShutdownAttribute.RunIteration ();
  300. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  301. content.X = -2;
  302. content.Y = 0;
  303. AutoInitShutdownAttribute.RunIteration ();
  304. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  305. top.Dispose ();
  306. }
  307. [Fact]
  308. [AutoInitShutdown]
  309. public void Draw_Negative_Viewport_Horizontal_Without_New_Lines ()
  310. {
  311. // BUGBUG: This previously assumed the default height of a View was 1.
  312. var subView = new View
  313. {
  314. Id = "subView",
  315. Y = 1,
  316. Width = 7,
  317. Height = 1,
  318. Text = "subView"
  319. };
  320. var view = new View { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  321. view.Add (subView);
  322. var content = new View { Id = "content", Width = 20, Height = 20 };
  323. content.Add (view);
  324. var container = new View
  325. {
  326. Id = "container",
  327. X = 1,
  328. Y = 1,
  329. Width = 5,
  330. Height = 5
  331. };
  332. container.Add (content);
  333. Toplevel top = new ();
  334. top.Add (container);
  335. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  336. top.SubViewsLaidOut += Top_LayoutComplete;
  337. Application.Begin (top);
  338. AutoInitShutdownAttribute.RunIteration ();
  339. DriverAssert.AssertDriverContentsWithFrameAre (
  340. """
  341. 01234
  342. subVi
  343. """,
  344. output
  345. );
  346. content.X = -1;
  347. AutoInitShutdownAttribute.RunIteration ();
  348. DriverAssert.AssertDriverContentsWithFrameAre (
  349. """
  350. 12345
  351. ubVie
  352. """,
  353. output
  354. );
  355. content.Y = -1;
  356. AutoInitShutdownAttribute.RunIteration ();
  357. DriverAssert.AssertDriverContentsWithFrameAre (
  358. """
  359. ubVie
  360. """,
  361. output
  362. );
  363. content.Y = -2;
  364. AutoInitShutdownAttribute.RunIteration ();
  365. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  366. content.X = -20;
  367. content.Y = 0;
  368. AutoInitShutdownAttribute.RunIteration ();
  369. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  370. top.Dispose ();
  371. return;
  372. void Top_LayoutComplete (object? sender, LayoutEventArgs e) { Application.Driver!.Clip = new (container.Frame); }
  373. }
  374. [Fact]
  375. [AutoInitShutdown]
  376. public void Draw_Negative_Viewport_Vertical ()
  377. {
  378. var subView = new View
  379. {
  380. Id = "subView",
  381. X = 1,
  382. Width = 1,
  383. Height = 7,
  384. Text = "subView",
  385. TextDirection = TextDirection.TopBottom_LeftRight
  386. };
  387. var view = new View
  388. {
  389. Id = "view",
  390. Width = 2,
  391. Height = 20,
  392. Text = "01234567890123456789",
  393. TextDirection = TextDirection.TopBottom_LeftRight
  394. };
  395. view.Add (subView);
  396. var content = new View { Id = "content", Width = 20, Height = 20 };
  397. content.Add (view);
  398. var container = new View
  399. {
  400. Id = "container",
  401. X = 1,
  402. Y = 1,
  403. Width = 5,
  404. Height = 5
  405. };
  406. container.Add (content);
  407. Toplevel top = new ();
  408. top.Add (container);
  409. Application.Begin (top);
  410. AutoInitShutdownAttribute.RunIteration ();
  411. DriverAssert.AssertDriverContentsWithFrameAre (
  412. """
  413. 0s
  414. 1u
  415. 2b
  416. 3V
  417. 4i
  418. """,
  419. output
  420. );
  421. content.X = -1;
  422. AutoInitShutdownAttribute.RunIteration ();
  423. DriverAssert.AssertDriverContentsWithFrameAre (
  424. """
  425. s
  426. u
  427. b
  428. V
  429. i
  430. """,
  431. output
  432. );
  433. content.X = -2;
  434. AutoInitShutdownAttribute.RunIteration ();
  435. DriverAssert.AssertDriverContentsWithFrameAre (@"", output);
  436. content.X = 0;
  437. content.Y = -1;
  438. AutoInitShutdownAttribute.RunIteration ();
  439. DriverAssert.AssertDriverContentsWithFrameAre (
  440. """
  441. 1u
  442. 2b
  443. 3V
  444. 4i
  445. 5e
  446. """,
  447. output
  448. );
  449. content.Y = -6;
  450. AutoInitShutdownAttribute.RunIteration ();
  451. DriverAssert.AssertDriverContentsWithFrameAre (
  452. """
  453. 6w
  454. 7
  455. 8
  456. 9
  457. 0
  458. """,
  459. output
  460. );
  461. content.Y = -19;
  462. AutoInitShutdownAttribute.RunIteration ();
  463. DriverAssert.AssertDriverContentsWithFrameAre (
  464. """
  465. 9
  466. """,
  467. output
  468. );
  469. content.Y = -20;
  470. AutoInitShutdownAttribute.RunIteration ();
  471. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  472. content.X = -2;
  473. content.Y = 0;
  474. AutoInitShutdownAttribute.RunIteration ();
  475. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  476. top.Dispose ();
  477. }
  478. [Theory]
  479. [SetupFakeDriver]
  480. [InlineData ("𝔽𝕆𝕆𝔹𝔸R")]
  481. [InlineData ("a𐐀b")]
  482. public void DrawHotString_NonBmp (string expected)
  483. {
  484. var view = new View { Width = 10, Height = 1 };
  485. view.DrawHotString (expected, Attribute.Default, Attribute.Default);
  486. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  487. }
  488. // TODO: The tests below that use Label should use View instead.
  489. [Fact]
  490. [AutoInitShutdown]
  491. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  492. {
  493. var us = "\U0001d539";
  494. var r = (Rune)0x1d539;
  495. Assert.Equal ("𝔹", us);
  496. Assert.Equal ("𝔹", r.ToString ());
  497. Assert.Equal (us, r.ToString ());
  498. Assert.Equal (1, us.GetColumns ());
  499. Assert.Equal (1, r.GetColumns ());
  500. var win = new Window { Title = us };
  501. var view = new Label { Text = r.ToString () };
  502. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  503. win.Add (view, tf);
  504. Toplevel top = new ();
  505. top.Add (win);
  506. Application.Begin (top);
  507. AutoInitShutdownAttribute.FakeResize(new Size(10, 4));
  508. var expected = """
  509. ┌┤𝔹├─────┐
  510. │𝔹 │
  511. │𝔹 │
  512. └────────┘
  513. """;
  514. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  515. DriverAssert.AssertDriverContentsAre (expected, output);
  516. top.Dispose ();
  517. // This test has nothing to do with color - removing as it is not relevant and fragile
  518. }
  519. [Fact]
  520. [TestRespondersDisposed]
  521. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  522. {
  523. Application.Init (new FakeDriver ());
  524. Toplevel top = new ();
  525. var view = new View { X = -2, Text = "view" };
  526. top.Add (view);
  527. Application.Iteration += (s, a) =>
  528. {
  529. Assert.Equal (-2, view.X);
  530. Application.RequestStop ();
  531. };
  532. try
  533. {
  534. Application.Run (top);
  535. }
  536. catch (IndexOutOfRangeException ex)
  537. {
  538. // After the fix this exception will not be caught.
  539. Assert.IsType<IndexOutOfRangeException> (ex);
  540. }
  541. top.Dispose ();
  542. // Shutdown must be called to safely clean up Application if Init has been called
  543. Application.Shutdown ();
  544. }
  545. [Fact]
  546. [AutoInitShutdown]
  547. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  548. {
  549. var label = new Label { Text = "At 0,0" };
  550. var view = new DerivedView
  551. {
  552. X = 2,
  553. Y = 2,
  554. Width = 30,
  555. Height = 2,
  556. Text = "A text with some long width\n and also with two lines."
  557. };
  558. Toplevel top = new ();
  559. top.Add (label, view);
  560. RunState runState = Application.Begin (top);
  561. AutoInitShutdownAttribute.RunIteration ();
  562. DriverAssert.AssertDriverContentsWithFrameAre (
  563. @"
  564. At 0,0
  565. A text with some long width
  566. and also with two lines. "
  567. ,
  568. output
  569. );
  570. view.Frame = new (3, 3, 10, 1);
  571. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  572. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  573. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  574. //Application.Refresh();
  575. top.Draw ();
  576. DriverAssert.AssertDriverContentsWithFrameAre (
  577. @"
  578. At 0,0
  579. A text wit",
  580. output
  581. );
  582. Application.End (runState);
  583. top.Dispose ();
  584. }
  585. [Fact]
  586. [AutoInitShutdown]
  587. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  588. {
  589. var label = new Label { Text = "At 0,0" };
  590. var view = new DerivedView
  591. {
  592. X = 2,
  593. Y = 2,
  594. Width = 30,
  595. Height = 2,
  596. Text = "A text with some long width\n and also with two lines."
  597. };
  598. Toplevel top = new ();
  599. top.Add (label, view);
  600. RunState runState = Application.Begin (top);
  601. top.Draw ();
  602. DriverAssert.AssertDriverContentsWithFrameAre (
  603. @"
  604. At 0,0
  605. A text with some long width
  606. and also with two lines. "
  607. ,
  608. output
  609. );
  610. view.X = 3;
  611. view.Y = 3;
  612. view.Width = 10;
  613. view.Height = 1;
  614. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  615. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  616. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  617. View.SetClipToScreen ();
  618. top.Draw ();
  619. DriverAssert.AssertDriverContentsWithFrameAre (
  620. @"
  621. At 0,0
  622. A text wit"
  623. ,
  624. output
  625. );
  626. Application.End (runState);
  627. top.Dispose ();
  628. }
  629. [Fact]
  630. [AutoInitShutdown]
  631. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  632. {
  633. var label = new Label { Text = "At 0,0" };
  634. var view = new DerivedView
  635. {
  636. X = 2,
  637. Y = 2,
  638. Width = 30,
  639. Height = 2,
  640. Text = "A text with some long width\n and also with two lines."
  641. };
  642. Toplevel top = new ();
  643. top.Add (label, view);
  644. RunState runState = Application.Begin (top);
  645. AutoInitShutdownAttribute.RunIteration ();
  646. DriverAssert.AssertDriverContentsWithFrameAre (
  647. @"
  648. At 0,0
  649. A text with some long width
  650. and also with two lines. "
  651. ,
  652. output
  653. );
  654. view.Frame = new (1, 1, 10, 1);
  655. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  656. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  657. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  658. top.Draw ();
  659. DriverAssert.AssertDriverContentsWithFrameAre (
  660. @"
  661. At 0,0
  662. A text wit"
  663. ,
  664. output
  665. );
  666. Application.End (runState);
  667. top.Dispose ();
  668. }
  669. [Fact]
  670. [AutoInitShutdown]
  671. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  672. {
  673. var label = new Label { Text = "At 0,0" };
  674. var view = new DerivedView
  675. {
  676. X = 2,
  677. Y = 2,
  678. Width = 30,
  679. Height = 2,
  680. Text = "A text with some long width\n and also with two lines."
  681. };
  682. Toplevel top = new ();
  683. top.Add (label, view);
  684. RunState runState = Application.Begin (top);
  685. top.Draw ();
  686. DriverAssert.AssertDriverContentsWithFrameAre (
  687. @"
  688. At 0,0
  689. A text with some long width
  690. and also with two lines. "
  691. ,
  692. output
  693. );
  694. view.X = 1;
  695. view.Y = 1;
  696. view.Width = 10;
  697. view.Height = 1;
  698. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  699. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  700. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  701. View.SetClipToScreen ();
  702. top.Draw ();
  703. DriverAssert.AssertDriverContentsWithFrameAre (
  704. @"
  705. At 0,0
  706. A text wit"
  707. ,
  708. output
  709. );
  710. Application.End (runState);
  711. top.Dispose ();
  712. }
  713. public class DerivedView : View
  714. {
  715. public DerivedView () { CanFocus = true; }
  716. public bool IsKeyDown { get; set; }
  717. public bool IsKeyPress { get; set; }
  718. public bool IsKeyUp { get; set; }
  719. public override string Text { get; set; } = null!;
  720. protected override bool OnDrawingContent ()
  721. {
  722. var idx = 0;
  723. // BUGBUG: v2 - this should use Viewport, not Frame
  724. for (var r = 0; r < Frame.Height; r++)
  725. {
  726. for (var c = 0; c < Frame.Width; c++)
  727. {
  728. if (idx < Text.Length)
  729. {
  730. char rune = Text [idx];
  731. if (rune != '\n')
  732. {
  733. AddRune (c, r, (Rune)Text [idx]);
  734. }
  735. idx++;
  736. if (rune == '\n')
  737. {
  738. break;
  739. }
  740. }
  741. }
  742. }
  743. ClearNeedsDraw ();
  744. return true;
  745. }
  746. protected override bool OnKeyDown (Key keyEvent)
  747. {
  748. IsKeyDown = true;
  749. return true;
  750. }
  751. public override bool OnKeyUp (Key keyEvent)
  752. {
  753. IsKeyUp = true;
  754. return true;
  755. }
  756. protected override bool OnKeyDownNotHandled (Key keyEvent)
  757. {
  758. IsKeyPress = true;
  759. return true;
  760. }
  761. }
  762. }