ScrollViewTests.cs 37 KB

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