ScrollViewTests.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. using System.Text;
  2. using JetBrains.Annotations;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests;
  5. public class ScrollViewTests (ITestOutputHelper output)
  6. {
  7. [Fact]
  8. public void Adding_Views ()
  9. {
  10. var sv = new ScrollView { Width = 20, Height = 10 };
  11. sv.SetContentSize (new (30, 20));
  12. sv.Add (
  13. new View { Width = 10, Height = 5 },
  14. new View { X = 12, Y = 7, Width = 10, Height = 5 }
  15. );
  16. Assert.Equal (new (30, 20), sv.GetContentSize ());
  17. Assert.Equal (2, sv.Subviews [0].Subviews.Count);
  18. }
  19. [Fact]
  20. [AutoInitShutdown]
  21. public void AutoHideScrollBars_False_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  22. {
  23. var sv = new ScrollView { Width = 10, Height = 10, AutoHideScrollBars = false };
  24. sv.ShowHorizontalScrollIndicator = true;
  25. sv.ShowVerticalScrollIndicator = true;
  26. var top = new Toplevel ();
  27. top.Add (sv);
  28. Application.Begin (top);
  29. Assert.Equal (new (0, 0, 10, 10), sv.Viewport);
  30. Assert.False (sv.AutoHideScrollBars);
  31. Assert.True (sv.ShowHorizontalScrollIndicator);
  32. Assert.True (sv.ShowVerticalScrollIndicator);
  33. sv.Draw ();
  34. TestHelpers.AssertDriverContentsAre (
  35. @"
  36. ◄├─────┤►
  37. ",
  38. output
  39. );
  40. sv.ShowHorizontalScrollIndicator = false;
  41. Assert.Equal (new (0, 0, 10, 10), sv.Viewport);
  42. sv.ShowVerticalScrollIndicator = true;
  43. Assert.Equal (new (0, 0, 10, 10), sv.Viewport);
  44. Assert.False (sv.AutoHideScrollBars);
  45. Assert.False (sv.ShowHorizontalScrollIndicator);
  46. Assert.True (sv.ShowVerticalScrollIndicator);
  47. sv.Draw ();
  48. TestHelpers.AssertDriverContentsAre (
  49. @"
  50. ",
  51. output
  52. );
  53. sv.ShowHorizontalScrollIndicator = true;
  54. sv.ShowVerticalScrollIndicator = false;
  55. Assert.False (sv.AutoHideScrollBars);
  56. Assert.True (sv.ShowHorizontalScrollIndicator);
  57. Assert.False (sv.ShowVerticalScrollIndicator);
  58. sv.Draw ();
  59. TestHelpers.AssertDriverContentsAre (
  60. @"
  61. ◄├──────┤►
  62. ",
  63. output
  64. );
  65. sv.ShowHorizontalScrollIndicator = false;
  66. sv.ShowVerticalScrollIndicator = false;
  67. Assert.False (sv.AutoHideScrollBars);
  68. Assert.False (sv.ShowHorizontalScrollIndicator);
  69. Assert.False (sv.ShowVerticalScrollIndicator);
  70. sv.Draw ();
  71. TestHelpers.AssertDriverContentsAre (
  72. @"
  73. ",
  74. output
  75. );
  76. top.Dispose ();
  77. }
  78. [Fact]
  79. [AutoInitShutdown]
  80. public void AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  81. {
  82. var sv = new ScrollView { Width = 10, Height = 10 };
  83. var top = new Toplevel ();
  84. top.Add (sv);
  85. Application.Begin (top);
  86. Assert.True (sv.AutoHideScrollBars);
  87. Assert.False (sv.ShowHorizontalScrollIndicator);
  88. Assert.False (sv.ShowVerticalScrollIndicator);
  89. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  90. sv.AutoHideScrollBars = false;
  91. sv.ShowHorizontalScrollIndicator = true;
  92. sv.ShowVerticalScrollIndicator = true;
  93. sv.LayoutSubviews ();
  94. sv.Draw ();
  95. TestHelpers.AssertDriverContentsWithFrameAre (
  96. @"
  97. ◄├─────┤►
  98. ",
  99. output
  100. );
  101. top.Dispose ();
  102. }
  103. // There are still issue with the lower right corner of the scroll view
  104. [Fact]
  105. [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  106. public void Clear_Window_Inside_ScrollView ()
  107. {
  108. var topLabel = new Label { X = 15, Text = "At 15,0" };
  109. var sv = new ScrollView
  110. {
  111. X = 3,
  112. Y = 3,
  113. Width = 10,
  114. Height = 10,
  115. KeepContentAlwaysInViewport = false
  116. };
  117. sv.SetContentSize (new (23, 23));
  118. var bottomLabel = new Label { X = 15, Y = 15, Text = "At 15,15" };
  119. var top = new Toplevel ();
  120. top.Add (topLabel, sv, bottomLabel);
  121. Application.Begin (top);
  122. TestHelpers.AssertDriverContentsWithFrameAre (
  123. @"
  124. At 15,0
  125. ◄├┤░░░░░►
  126. At 15,15",
  127. output
  128. );
  129. Attribute [] attributes =
  130. {
  131. Colors.ColorSchemes ["TopLevel"].Normal,
  132. Colors.ColorSchemes ["TopLevel"].Focus,
  133. Colors.ColorSchemes ["Base"].Normal
  134. };
  135. TestHelpers.AssertDriverAttributesAre (
  136. @"
  137. 00000000000000000000000
  138. 00000000000000000000000
  139. 00000000000000000000000
  140. 00000000000010000000000
  141. 00000000000010000000000
  142. 00000000000010000000000
  143. 00000000000010000000000
  144. 00000000000010000000000
  145. 00000000000010000000000
  146. 00000000000010000000000
  147. 00000000000010000000000
  148. 00000000000010000000000
  149. 00011111111110000000000
  150. 00000000000000000000000
  151. 00000000000000000000000
  152. 00000000000000000000000",
  153. null,
  154. attributes
  155. );
  156. sv.Add (new Window { X = 3, Y = 3, Width = 20, Height = 20 });
  157. Application.Refresh ();
  158. TestHelpers.AssertDriverContentsWithFrameAre (
  159. @"
  160. At 15,0
  161. ┌─────░
  162. │ ░
  163. │ ░
  164. │ ░
  165. │ ░
  166. │ ▼
  167. ◄├┤░░░░░►
  168. At 15,15",
  169. output
  170. );
  171. TestHelpers.AssertDriverAttributesAre (
  172. @"
  173. 00000000000000000000000
  174. 00000000000000000000000
  175. 00000000000000000000000
  176. 00000000000010000000000
  177. 00000000000010000000000
  178. 00000000000010000000000
  179. 00000022222210000000000
  180. 00000022222210000000000
  181. 00000022222210000000000
  182. 00000022222210000000000
  183. 00000022222210000000000
  184. 00000022222210000000000
  185. 00011111111110000000000
  186. 00000000000000000000000
  187. 00000000000000000000000
  188. 00000000000000000000000",
  189. null,
  190. attributes
  191. );
  192. sv.ContentOffset = new (20, 20);
  193. Application.Refresh ();
  194. TestHelpers.AssertDriverContentsWithFrameAre (
  195. @"
  196. At 15,0
  197. │ ▲
  198. │ ░
  199. ──┘ ░
  200. ◄░░░░├─┤►
  201. At 15,15",
  202. output
  203. );
  204. TestHelpers.AssertDriverAttributesAre (
  205. @"
  206. 00000000000000000000000
  207. 00000000000000000000000
  208. 00000000000000000000000
  209. 00022200000010000000000
  210. 00022200000010000000000
  211. 00022200000010000000000
  212. 00000000000010000000000
  213. 00000000000010000000000
  214. 00000000000010000000000
  215. 00000000000010000000000
  216. 00000000000010000000000
  217. 00000000000010000000000
  218. 00011111111110000000000
  219. 00000000000000000000000
  220. 00000000000000000000000
  221. 00000000000000000000000",
  222. null,
  223. attributes
  224. );
  225. top.Dispose ();
  226. }
  227. [Fact]
  228. public void Constructors_Defaults ()
  229. {
  230. var sv = new ScrollView ();
  231. Assert.True (sv.CanFocus);
  232. Assert.Equal (new (0, 0, 0, 0), sv.Frame);
  233. Assert.Equal (Rectangle.Empty, sv.Frame);
  234. Assert.Equal (Point.Empty, sv.ContentOffset);
  235. Assert.Equal (Size.Empty, sv.GetContentSize ());
  236. Assert.True (sv.AutoHideScrollBars);
  237. Assert.True (sv.KeepContentAlwaysInViewport);
  238. sv = new () { X = 1, Y = 2, Width = 20, Height = 10 };
  239. Assert.True (sv.CanFocus);
  240. Assert.Equal (new (1, 2, 20, 10), sv.Frame);
  241. Assert.Equal (Point.Empty, sv.ContentOffset);
  242. Assert.Equal (sv.Viewport.Size, sv.GetContentSize ());
  243. Assert.True (sv.AutoHideScrollBars);
  244. Assert.True (sv.KeepContentAlwaysInViewport);
  245. }
  246. [Fact]
  247. [SetupFakeDriver]
  248. public void ContentBottomRightCorner_Draw ()
  249. {
  250. ((FakeDriver)Application.Driver!).SetBufferSize (30, 30);
  251. var top = new View { Width = 30, Height = 30, ColorScheme = new () { Normal = Attribute.Default } };
  252. Size size = new (20, 10);
  253. var sv = new ScrollView
  254. {
  255. X = 1,
  256. Y = 1,
  257. Width = 10,
  258. Height = 5,
  259. ColorScheme = new () { Normal = new (Color.Red, Color.Green) }
  260. };
  261. sv.SetContentSize (size);
  262. string text = null;
  263. for (var i = 0; i < size.Height; i++)
  264. {
  265. text += "*".Repeat (size.Width);
  266. if (i < size.Height)
  267. {
  268. text += '\n';
  269. }
  270. }
  271. var view = new View
  272. {
  273. ColorScheme = new () { Normal = new (Color.Blue, Color.Yellow) },
  274. Width = Dim.Auto (DimAutoStyle.Text),
  275. Height = Dim.Auto (DimAutoStyle.Text),
  276. Text = text
  277. };
  278. sv.Add (view);
  279. top.Add (sv);
  280. top.BeginInit ();
  281. top.EndInit ();
  282. top.LayoutSubviews ();
  283. top.Draw ();
  284. View contentBottomRightCorner = sv.Subviews.First (v => v is ScrollBarView.ContentBottomRightCorner);
  285. Assert.True (contentBottomRightCorner is ScrollBarView.ContentBottomRightCorner);
  286. Assert.True (contentBottomRightCorner.Visible);
  287. TestHelpers.AssertDriverContentsWithFrameAre (
  288. @"
  289. *********▲
  290. *********┬
  291. *********┴
  292. *********▼
  293. ◄├──┤░░░► ",
  294. output
  295. );
  296. Attribute [] attrs = { Attribute.Default, new (Color.Red, Color.Green), new (Color.Blue, Color.Yellow) };
  297. TestHelpers.AssertDriverAttributesAre (
  298. @"
  299. 000000000000
  300. 022222222210
  301. 022222222210
  302. 022222222210
  303. 022222222210
  304. 011111111110
  305. 000000000000",
  306. null,
  307. attrs
  308. );
  309. top.Dispose ();
  310. }
  311. [Fact]
  312. [AutoInitShutdown]
  313. public void
  314. ContentOffset_ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  315. {
  316. var sv = new ScrollView
  317. {
  318. Width = 10, Height = 10
  319. };
  320. sv.SetContentSize (new (50, 50));
  321. sv.ContentOffset = new (25, 25);
  322. var top = new Toplevel ();
  323. top.Add (sv);
  324. Application.Begin (top);
  325. Assert.Equal (new (-25, -25), sv.ContentOffset);
  326. Assert.Equal (new (50, 50), sv.GetContentSize ());
  327. Assert.True (sv.AutoHideScrollBars);
  328. Assert.True (sv.ShowHorizontalScrollIndicator);
  329. Assert.True (sv.ShowVerticalScrollIndicator);
  330. TestHelpers.AssertDriverContentsWithFrameAre (
  331. @"
  332. ◄░░░├─┤░►
  333. ",
  334. output
  335. );
  336. top.Dispose ();
  337. }
  338. [Fact]
  339. [AutoInitShutdown]
  340. public void ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
  341. {
  342. var sv = new ScrollView { Width = 10, Height = 10 };
  343. sv.SetContentSize (new (50, 50));
  344. var top = new Toplevel ();
  345. top.Add (sv);
  346. Application.Begin (top);
  347. Assert.Equal (50, sv.GetContentSize ().Width);
  348. Assert.Equal (50, sv.GetContentSize ().Height);
  349. Assert.True (sv.AutoHideScrollBars);
  350. Assert.True (sv.ShowHorizontalScrollIndicator);
  351. Assert.True (sv.ShowVerticalScrollIndicator);
  352. TestHelpers.AssertDriverContentsWithFrameAre (
  353. @"
  354. ◄├┤░░░░░►
  355. ",
  356. output
  357. );
  358. top.Dispose ();
  359. }
  360. [Fact]
  361. [AutoInitShutdown]
  362. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  363. {
  364. var rule = "0123456789";
  365. Size size = new (40, 40);
  366. var view = new View { Frame = new (Point.Empty, size) };
  367. view.Add (
  368. new Label
  369. {
  370. Width = Dim.Fill (),
  371. Height = 1,
  372. Text = rule.Repeat (size.Width / rule.Length)
  373. }
  374. );
  375. view.Add (
  376. new Label
  377. {
  378. Height = Dim.Fill (),
  379. Width = 1,
  380. Text = rule.Repeat (size.Height / rule.Length),
  381. TextDirection = TextDirection.TopBottom_LeftRight
  382. }
  383. );
  384. view.Add (new Label { X = 1, Y = 1, Text = "[ Press me! ]" });
  385. var scrollView = new ScrollView
  386. {
  387. X = 1,
  388. Y = 1,
  389. Width = 15,
  390. Height = 10,
  391. ShowHorizontalScrollIndicator = true,
  392. ShowVerticalScrollIndicator = true
  393. };
  394. scrollView.SetContentSize (size);
  395. scrollView.Add (view);
  396. var win = new Window { X = 1, Y = 1, Width = 20, Height = 14 };
  397. win.Add (scrollView);
  398. var top = new Toplevel ();
  399. top.Add (win);
  400. Application.Begin (top);
  401. var expected = @"
  402. ┌──────────────────┐
  403. │ │
  404. │ 01234567890123▲ │
  405. │ 1[ Press me! ]┬ │
  406. │ 2 │ │
  407. │ 3 ┴ │
  408. │ 4 ░ │
  409. │ 5 ░ │
  410. │ 6 ░ │
  411. │ 7 ░ │
  412. │ 8 ▼ │
  413. │ ◄├───┤░░░░░░░► │
  414. │ │
  415. └──────────────────┘
  416. "
  417. ;
  418. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  419. Assert.Equal (new (1, 1, 21, 14), pos);
  420. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  421. top.Draw ();
  422. expected = @"
  423. ┌──────────────────┐
  424. │ │
  425. │ 12345678901234▲ │
  426. │ [ Press me! ] ┬ │
  427. │ │ │
  428. │ ┴ │
  429. │ ░ │
  430. │ ░ │
  431. │ ░ │
  432. │ ░ │
  433. │ ▼ │
  434. │ ◄├───┤░░░░░░░► │
  435. │ │
  436. └──────────────────┘
  437. "
  438. ;
  439. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  440. Assert.Equal (new (1, 1, 21, 14), pos);
  441. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  442. top.Draw ();
  443. expected = @"
  444. ┌──────────────────┐
  445. │ │
  446. │ 23456789012345▲ │
  447. │ Press me! ] ┬ │
  448. │ │ │
  449. │ ┴ │
  450. │ ░ │
  451. │ ░ │
  452. │ ░ │
  453. │ ░ │
  454. │ ▼ │
  455. │ ◄├────┤░░░░░░► │
  456. │ │
  457. └──────────────────┘
  458. "
  459. ;
  460. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  461. Assert.Equal (new (1, 1, 21, 14), pos);
  462. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  463. top.Draw ();
  464. expected = @"
  465. ┌──────────────────┐
  466. │ │
  467. │ 34567890123456▲ │
  468. │ Press me! ] ┬ │
  469. │ │ │
  470. │ ┴ │
  471. │ ░ │
  472. │ ░ │
  473. │ ░ │
  474. │ ░ │
  475. │ ▼ │
  476. │ ◄├────┤░░░░░░► │
  477. │ │
  478. └──────────────────┘
  479. "
  480. ;
  481. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  482. Assert.Equal (new (1, 1, 21, 14), pos);
  483. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  484. top.Draw ();
  485. expected = @"
  486. ┌──────────────────┐
  487. │ │
  488. │ 45678901234567▲ │
  489. │ ress me! ] ┬ │
  490. │ │ │
  491. │ ┴ │
  492. │ ░ │
  493. │ ░ │
  494. │ ░ │
  495. │ ░ │
  496. │ ▼ │
  497. │ ◄░├───┤░░░░░░► │
  498. │ │
  499. └──────────────────┘
  500. "
  501. ;
  502. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  503. Assert.Equal (new (1, 1, 21, 14), pos);
  504. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  505. top.Draw ();
  506. expected = @"
  507. ┌──────────────────┐
  508. │ │
  509. │ 56789012345678▲ │
  510. │ ess me! ] ┬ │
  511. │ │ │
  512. │ ┴ │
  513. │ ░ │
  514. │ ░ │
  515. │ ░ │
  516. │ ░ │
  517. │ ▼ │
  518. │ ◄░├────┤░░░░░► │
  519. │ │
  520. └──────────────────┘
  521. "
  522. ;
  523. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  524. Assert.Equal (new (1, 1, 21, 14), pos);
  525. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  526. top.Draw ();
  527. expected = @"
  528. ┌──────────────────┐
  529. │ │
  530. │ 67890123456789▲ │
  531. │ ss me! ] ┬ │
  532. │ │ │
  533. │ ┴ │
  534. │ ░ │
  535. │ ░ │
  536. │ ░ │
  537. │ ░ │
  538. │ ▼ │
  539. │ ◄░├────┤░░░░░► │
  540. │ │
  541. └──────────────────┘
  542. "
  543. ;
  544. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  545. Assert.Equal (new (1, 1, 21, 14), pos);
  546. Assert.True (scrollView.OnKeyDown (Key.CursorRight));
  547. top.Draw ();
  548. expected = @"
  549. ┌──────────────────┐
  550. │ │
  551. │ 78901234567890▲ │
  552. │ s me! ] ┬ │
  553. │ │ │
  554. │ ┴ │
  555. │ ░ │
  556. │ ░ │
  557. │ ░ │
  558. │ ░ │
  559. │ ▼ │
  560. │ ◄░░├───┤░░░░░► │
  561. │ │
  562. └──────────────────┘
  563. ";
  564. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  565. Assert.Equal (new (1, 1, 21, 14), pos);
  566. Assert.True (scrollView.OnKeyDown (Key.End.WithCtrl));
  567. top.Draw ();
  568. expected = @"
  569. ┌──────────────────┐
  570. │ │
  571. │ 67890123456789▲ │
  572. │ ┬ │
  573. │ │ │
  574. │ ┴ │
  575. │ ░ │
  576. │ ░ │
  577. │ ░ │
  578. │ ░ │
  579. │ ▼ │
  580. │ ◄░░░░░░░├───┤► │
  581. │ │
  582. └──────────────────┘
  583. ";
  584. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  585. Assert.Equal (new (1, 1, 21, 14), pos);
  586. Assert.True (scrollView.OnKeyDown (Key.Home.WithCtrl));
  587. Assert.True (scrollView.OnKeyDown (Key.CursorDown));
  588. top.Draw ();
  589. expected = @"
  590. ┌──────────────────┐
  591. │ │
  592. │ 1[ Press me! ]▲ │
  593. │ 2 ┬ │
  594. │ 3 │ │
  595. │ 4 ┴ │
  596. │ 5 ░ │
  597. │ 6 ░ │
  598. │ 7 ░ │
  599. │ 8 ░ │
  600. │ 9 ▼ │
  601. │ ◄├───┤░░░░░░░► │
  602. │ │
  603. └──────────────────┘
  604. ";
  605. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  606. Assert.Equal (new (1, 1, 21, 14), pos);
  607. Assert.True (scrollView.OnKeyDown (Key.CursorDown));
  608. top.Draw ();
  609. expected = @"
  610. ┌──────────────────┐
  611. │ │
  612. │ 2 ▲ │
  613. │ 3 ┬ │
  614. │ 4 │ │
  615. │ 5 ┴ │
  616. │ 6 ░ │
  617. │ 7 ░ │
  618. │ 8 ░ │
  619. │ 9 ░ │
  620. │ 0 ▼ │
  621. │ ◄├───┤░░░░░░░► │
  622. │ │
  623. └──────────────────┘
  624. ";
  625. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  626. Assert.Equal (new (1, 1, 21, 14), pos);
  627. Assert.True (scrollView.OnKeyDown (Key.CursorDown));
  628. top.Draw ();
  629. expected = @"
  630. ┌──────────────────┐
  631. │ │
  632. │ 3 ▲ │
  633. │ 4 ┬ │
  634. │ 5 │ │
  635. │ 6 ┴ │
  636. │ 7 ░ │
  637. │ 8 ░ │
  638. │ 9 ░ │
  639. │ 0 ░ │
  640. │ 1 ▼ │
  641. │ ◄├───┤░░░░░░░► │
  642. │ │
  643. └──────────────────┘
  644. ";
  645. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  646. Assert.Equal (new (1, 1, 21, 14), pos);
  647. Assert.True (scrollView.OnKeyDown (Key.End));
  648. top.Draw ();
  649. expected = @"
  650. ┌──────────────────┐
  651. │ │
  652. │ 1 ▲ │
  653. │ 2 ░ │
  654. │ 3 ░ │
  655. │ 4 ░ │
  656. │ 5 ░ │
  657. │ 6 ░ │
  658. │ 7 ┬ │
  659. │ 8 ┴ │
  660. │ 9 ▼ │
  661. │ ◄├───┤░░░░░░░► │
  662. │ │
  663. └──────────────────┘
  664. ";
  665. TestHelpers.AssertDriverContentsAre (expected, output);
  666. top.Dispose ();
  667. }
  668. // There still have an issue with lower right corner of the scroll view
  669. [Fact]
  670. [AutoInitShutdown]
  671. public void Frame_And_Labels_Does_Not_Overspill_ScrollView ()
  672. {
  673. var sv = new ScrollView
  674. {
  675. X = 3,
  676. Y = 3,
  677. Width = 10,
  678. Height = 10,
  679. TabStop = TabBehavior.TabStop
  680. };
  681. sv.SetContentSize (new (50, 50));
  682. for (var i = 0; i < 8; i++)
  683. {
  684. sv.Add (new CustomButton ("█", $"Button {i}", 20, 3) { Y = i * 3 });
  685. }
  686. var top = new Toplevel ();
  687. top.Add (sv);
  688. Application.Begin (top);
  689. TestHelpers.AssertDriverContentsWithFrameAre (
  690. @"
  691. █████████▲
  692. ██████But┬
  693. █████████┴
  694. ┌────────░
  695. │ But░
  696. └────────░
  697. ┌────────░
  698. │ But░
  699. └────────▼
  700. ◄├┤░░░░░► ",
  701. output
  702. );
  703. sv.ContentOffset = new (5, 5);
  704. sv.LayoutSubviews ();
  705. Application.Refresh ();
  706. TestHelpers.AssertDriverContentsWithFrameAre (
  707. @"
  708. ─────────▲
  709. ─────────┬
  710. Button 2│
  711. ─────────┴
  712. ─────────░
  713. Button 3░
  714. ─────────░
  715. ─────────░
  716. Button 4▼
  717. ◄├─┤░░░░► ",
  718. output
  719. );
  720. top.Dispose ();
  721. }
  722. [Fact]
  723. public void KeyBindings_Command ()
  724. {
  725. var sv = new ScrollView { Width = 20, Height = 10 };
  726. sv.SetContentSize (new (40, 20));
  727. sv.Add (
  728. new View { Width = 20, Height = 5 },
  729. new View { X = 22, Y = 7, Width = 10, Height = 5 }
  730. );
  731. sv.BeginInit ();
  732. sv.EndInit ();
  733. Assert.True (sv.KeepContentAlwaysInViewport);
  734. Assert.True (sv.AutoHideScrollBars);
  735. Assert.Equal (Point.Empty, sv.ContentOffset);
  736. Assert.False (sv.OnKeyDown (Key.CursorUp));
  737. Assert.Equal (Point.Empty, sv.ContentOffset);
  738. Assert.True (sv.OnKeyDown (Key.CursorDown));
  739. Assert.Equal (new (0, -1), sv.ContentOffset);
  740. Assert.True (sv.OnKeyDown (Key.CursorUp));
  741. Assert.Equal (Point.Empty, sv.ContentOffset);
  742. Assert.False (sv.OnKeyDown (Key.PageUp));
  743. Assert.Equal (Point.Empty, sv.ContentOffset);
  744. Assert.True (sv.OnKeyDown (Key.PageDown));
  745. Point point0xMinus10 = new (0, -10);
  746. Assert.Equal (point0xMinus10, sv.ContentOffset);
  747. Assert.False (sv.OnKeyDown (Key.PageDown));
  748. Assert.Equal (point0xMinus10, sv.ContentOffset);
  749. Assert.False (sv.OnKeyDown (Key.CursorDown));
  750. Assert.Equal (point0xMinus10, sv.ContentOffset);
  751. Assert.True (sv.OnKeyDown (Key.V.WithAlt));
  752. Assert.Equal (Point.Empty, sv.ContentOffset);
  753. Assert.True (sv.OnKeyDown (Key.V.WithCtrl));
  754. Assert.Equal (point0xMinus10, sv.ContentOffset);
  755. Assert.False (sv.OnKeyDown (Key.CursorLeft));
  756. Assert.Equal (point0xMinus10, sv.ContentOffset);
  757. Assert.True (sv.OnKeyDown (Key.CursorRight));
  758. Assert.Equal (new (-1, -10), sv.ContentOffset);
  759. Assert.True (sv.OnKeyDown (Key.CursorLeft));
  760. Assert.Equal (point0xMinus10, sv.ContentOffset);
  761. Assert.False (sv.OnKeyDown (Key.PageUp.WithCtrl));
  762. Assert.Equal (point0xMinus10, sv.ContentOffset);
  763. Assert.True (sv.OnKeyDown (Key.PageDown.WithCtrl));
  764. Point pointMinus20xMinus10 = new (-20, -10);
  765. Assert.Equal (pointMinus20xMinus10, sv.ContentOffset);
  766. Assert.False (sv.OnKeyDown (Key.CursorRight));
  767. Assert.Equal (pointMinus20xMinus10, sv.ContentOffset);
  768. Assert.True (sv.OnKeyDown (Key.Home));
  769. Point pointMinus20x0 = new (-20, 0);
  770. Assert.Equal (pointMinus20x0, sv.ContentOffset);
  771. Assert.False (sv.OnKeyDown (Key.Home));
  772. Assert.Equal (pointMinus20x0, sv.ContentOffset);
  773. Assert.True (sv.OnKeyDown (Key.End));
  774. Assert.Equal (pointMinus20xMinus10, sv.ContentOffset);
  775. Assert.False (sv.OnKeyDown (Key.End));
  776. Assert.Equal (pointMinus20xMinus10, sv.ContentOffset);
  777. Assert.True (sv.OnKeyDown (Key.Home.WithCtrl));
  778. Assert.Equal (point0xMinus10, sv.ContentOffset);
  779. Assert.False (sv.OnKeyDown (Key.Home.WithCtrl));
  780. Assert.Equal (point0xMinus10, sv.ContentOffset);
  781. Assert.True (sv.OnKeyDown (Key.End.WithCtrl));
  782. Assert.Equal (pointMinus20xMinus10, sv.ContentOffset);
  783. Assert.False (sv.OnKeyDown (Key.End.WithCtrl));
  784. Assert.Equal (pointMinus20xMinus10, sv.ContentOffset);
  785. Assert.True (sv.OnKeyDown (Key.Home));
  786. Assert.Equal (pointMinus20x0, sv.ContentOffset);
  787. Assert.True (sv.OnKeyDown (Key.Home.WithCtrl));
  788. Assert.Equal (Point.Empty, sv.ContentOffset);
  789. sv.KeepContentAlwaysInViewport = false;
  790. Assert.False (sv.KeepContentAlwaysInViewport);
  791. Assert.True (sv.AutoHideScrollBars);
  792. Assert.Equal (Point.Empty, sv.ContentOffset);
  793. Assert.False (sv.OnKeyDown (Key.CursorUp));
  794. Assert.Equal (Point.Empty, sv.ContentOffset);
  795. Assert.True (sv.OnKeyDown (Key.CursorDown));
  796. Assert.Equal (new (0, -1), sv.ContentOffset);
  797. Assert.True (sv.OnKeyDown (Key.CursorUp));
  798. Assert.Equal (Point.Empty, sv.ContentOffset);
  799. Assert.False (sv.OnKeyDown (Key.PageUp));
  800. Assert.Equal (Point.Empty, sv.ContentOffset);
  801. Assert.True (sv.OnKeyDown (Key.PageDown));
  802. Assert.Equal (point0xMinus10, sv.ContentOffset);
  803. Assert.True (sv.OnKeyDown (Key.PageDown));
  804. Point point0xMinus19 = new (0, -19);
  805. Assert.Equal (point0xMinus19, sv.ContentOffset);
  806. Assert.False (sv.OnKeyDown (Key.PageDown));
  807. Assert.Equal (point0xMinus19, sv.ContentOffset);
  808. Assert.False (sv.OnKeyDown (Key.CursorDown));
  809. Assert.Equal (point0xMinus19, sv.ContentOffset);
  810. Assert.True (sv.OnKeyDown (Key.V.WithAlt));
  811. Assert.Equal (new (0, -9), sv.ContentOffset);
  812. Assert.True (sv.OnKeyDown (Key.V.WithCtrl));
  813. Assert.Equal (point0xMinus19, sv.ContentOffset);
  814. Assert.False (sv.OnKeyDown (Key.CursorLeft));
  815. Assert.Equal (point0xMinus19, sv.ContentOffset);
  816. Assert.True (sv.OnKeyDown (Key.CursorRight));
  817. Assert.Equal (new (-1, -19), sv.ContentOffset);
  818. Assert.True (sv.OnKeyDown (Key.CursorLeft));
  819. Assert.Equal (point0xMinus19, sv.ContentOffset);
  820. Assert.False (sv.OnKeyDown (Key.PageUp.WithCtrl));
  821. Assert.Equal (point0xMinus19, sv.ContentOffset);
  822. Assert.True (sv.OnKeyDown (Key.PageDown.WithCtrl));
  823. Assert.Equal (new (-20, -19), sv.ContentOffset);
  824. Assert.True (sv.OnKeyDown (Key.PageDown.WithCtrl));
  825. Point pointMinus39xMinus19 = new (-39, -19);
  826. Assert.Equal (pointMinus39xMinus19, sv.ContentOffset);
  827. Assert.False (sv.OnKeyDown (Key.PageDown.WithCtrl));
  828. Assert.Equal (pointMinus39xMinus19, sv.ContentOffset);
  829. Assert.False (sv.OnKeyDown (Key.CursorRight));
  830. Assert.Equal (pointMinus39xMinus19, sv.ContentOffset);
  831. Assert.True (sv.OnKeyDown (Key.PageUp.WithCtrl));
  832. var pointMinus19xMinus19 = new Point (-19, -19);
  833. Assert.Equal (pointMinus19xMinus19, sv.ContentOffset);
  834. Assert.True (sv.OnKeyDown (Key.Home));
  835. Assert.Equal (new (-19, 0), sv.ContentOffset);
  836. Assert.False (sv.OnKeyDown (Key.Home));
  837. Assert.Equal (new (-19, 0), sv.ContentOffset);
  838. Assert.True (sv.OnKeyDown (Key.End));
  839. Assert.Equal (pointMinus19xMinus19, sv.ContentOffset);
  840. Assert.False (sv.OnKeyDown (Key.End));
  841. Assert.Equal (pointMinus19xMinus19, sv.ContentOffset);
  842. Assert.True (sv.OnKeyDown (Key.Home.WithCtrl));
  843. Assert.Equal (point0xMinus19, sv.ContentOffset);
  844. Assert.False (sv.OnKeyDown (Key.Home.WithCtrl));
  845. Assert.Equal (point0xMinus19, sv.ContentOffset);
  846. Assert.True (sv.OnKeyDown (Key.End.WithCtrl));
  847. Assert.Equal (pointMinus39xMinus19, sv.ContentOffset);
  848. Assert.False (sv.OnKeyDown (Key.End.WithCtrl));
  849. Assert.Equal (pointMinus39xMinus19, sv.ContentOffset);
  850. }
  851. [Fact]
  852. [AutoInitShutdown]
  853. public void Remove_Added_View_Is_Allowed ()
  854. {
  855. var sv = new ScrollView { Width = 20, Height = 20 };
  856. sv.SetContentSize (new (100, 100));
  857. sv.Add (
  858. new View { Width = Dim.Fill (), Height = Dim.Fill (50), Id = "View1" },
  859. new View { Y = 51, Width = Dim.Fill (), Height = Dim.Fill (), Id = "View2" }
  860. );
  861. var top = new Toplevel ();
  862. top.Add (sv);
  863. Application.Begin (top);
  864. Assert.Equal (4, sv.Subviews.Count);
  865. Assert.Equal (2, sv.Subviews [0].Subviews.Count);
  866. sv.Remove (sv.Subviews [0].Subviews [1]);
  867. Assert.Equal (4, sv.Subviews.Count);
  868. Assert.Single (sv.Subviews [0].Subviews);
  869. Assert.Equal ("View1", sv.Subviews [0].Subviews [0].Id);
  870. top.Dispose ();
  871. }
  872. private class CustomButton : FrameView
  873. {
  874. private readonly Label labelFill;
  875. private readonly Label labelText;
  876. public CustomButton (string fill, string text, int width, int height)
  877. {
  878. Width = width;
  879. Height = height;
  880. labelFill = new () { Width = Dim.Fill (), Height = Dim.Fill (), Visible = false };
  881. labelFill.LayoutComplete += (s, e) =>
  882. {
  883. var fillText = new StringBuilder ();
  884. for (var i = 0; i < labelFill.Viewport.Height; i++)
  885. {
  886. if (i > 0)
  887. {
  888. fillText.AppendLine ("");
  889. }
  890. for (var j = 0; j < labelFill.Viewport.Width; j++)
  891. {
  892. fillText.Append (fill);
  893. }
  894. }
  895. labelFill.Text = fillText.ToString ();
  896. };
  897. labelText = new () { X = Pos.Center (), Y = Pos.Center (), Text = text };
  898. Add (labelFill, labelText);
  899. CanFocus = true;
  900. }
  901. protected override void OnHasFocusChanged (bool newHasFocus, [CanBeNull] View previousFocusedView, [CanBeNull] View focusedVew)
  902. {
  903. if (newHasFocus)
  904. {
  905. Border.LineStyle = LineStyle.None;
  906. Border.Thickness = new (0);
  907. labelFill.Visible = true;
  908. }
  909. else
  910. {
  911. Border.LineStyle = LineStyle.Single;
  912. Border.Thickness = new (1);
  913. labelFill.Visible = false;
  914. }
  915. }
  916. }
  917. }