ScrollViewTests.cs 36 KB

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