2
0

ScrollViewTests.cs 37 KB

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