TileView.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Terminal.Gui.Graphs;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// A <see cref="View"/> consisting of a moveable bar that divides
  9. /// the display area into resizeable <see cref="Tiles"/>.
  10. /// </summary>
  11. public class TileView : View {
  12. TileView parentTileView;
  13. /// <summary>
  14. /// A single <see cref="ContentView"/> presented in a <see cref="TileView"/>. To create
  15. /// new instances use <see cref="TileView.RebuildForTileCount(int)"/>
  16. /// or <see cref="TileView.InsertTile(int)"/>.
  17. /// </summary>
  18. public class Tile {
  19. /// <summary>
  20. /// The <see cref="ContentView"/> that is contained in this <see cref="TileView"/>.
  21. /// Add new child views to this member for multiple
  22. /// <see cref="ContentView"/>s within the <see cref="Tile"/>.
  23. /// </summary>
  24. public View ContentView { get; internal set; }
  25. /// <summary>
  26. /// Gets or Sets the minimum size you to allow when splitter resizing along
  27. /// parent <see cref="TileView.Orientation"/> direction.
  28. /// </summary>
  29. public int MinSize { get; set; }
  30. /// <summary>
  31. /// The text that should be displayed above the <see cref="ContentView"/>. This
  32. /// will appear over the splitter line or border (above the view client area).
  33. /// </summary>
  34. /// <remarks>
  35. /// Title are not rendered for root level tiles
  36. /// <see cref="Border.BorderStyle"/> is <see cref="BorderStyle.None"/>.
  37. ///</remarks>
  38. public string Title {
  39. get => _title;
  40. set {
  41. if (!OnTitleChanging (_title, value)) {
  42. var old = _title;
  43. _title = value;
  44. OnTitleChanged (old, _title);
  45. return;
  46. }
  47. _title = value;
  48. }
  49. }
  50. private string _title = string.Empty;
  51. /// <summary>
  52. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  53. /// </summary>
  54. public class TitleEventArgs : EventArgs {
  55. /// <summary>
  56. /// The new Window Title.
  57. /// </summary>
  58. public ustring NewTitle { get; set; }
  59. /// <summary>
  60. /// The old Window Title.
  61. /// </summary>
  62. public ustring OldTitle { get; set; }
  63. /// <summary>
  64. /// Flag which allows cancelling the Title change.
  65. /// </summary>
  66. public bool Cancel { get; set; }
  67. /// <summary>
  68. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  69. /// </summary>
  70. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  71. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  72. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  73. {
  74. OldTitle = oldTitle;
  75. NewTitle = newTitle;
  76. }
  77. }
  78. /// <summary>
  79. /// Called before the <see cref="Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  80. /// </summary>
  81. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  82. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  83. /// <returns><c>true</c> if an event handler cancelled the Title change.</returns>
  84. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  85. {
  86. var args = new TitleEventArgs (oldTitle, newTitle);
  87. TitleChanging?.Invoke (args);
  88. return args.Cancel;
  89. }
  90. /// <summary>
  91. /// Event fired when the <see cref="Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  92. /// <c>true</c> to cancel the Title change.
  93. /// </summary>
  94. public event Action<TitleEventArgs> TitleChanging;
  95. /// <summary>
  96. /// Called when the <see cref="Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  97. /// </summary>
  98. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  99. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  100. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  101. {
  102. var args = new TitleEventArgs (oldTitle, newTitle);
  103. TitleChanged?.Invoke (args);
  104. }
  105. /// <summary>
  106. /// Event fired after the <see cref="Title"/> has been changed.
  107. /// </summary>
  108. public event Action<TitleEventArgs> TitleChanged;
  109. /// <summary>
  110. /// Creates a new instance of the <see cref="Tile"/> class.
  111. /// </summary>
  112. public Tile ()
  113. {
  114. ContentView = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
  115. #if DEBUG_IDISPOSABLE
  116. ContentView.Data = "Tile.ContentView";
  117. #endif
  118. Title = string.Empty;
  119. MinSize = 0;
  120. }
  121. }
  122. List<Tile> tiles;
  123. private List<Pos> splitterDistances;
  124. private List<TileViewLineView> splitterLines;
  125. /// <summary>
  126. /// The sub sections hosted by the view
  127. /// </summary>
  128. public IReadOnlyCollection<Tile> Tiles => tiles.AsReadOnly ();
  129. /// <summary>
  130. /// The splitter locations. Note that there will be N-1 splitters where
  131. /// N is the number of <see cref="Tiles"/>.
  132. /// </summary>
  133. public IReadOnlyCollection<Pos> SplitterDistances => splitterDistances.AsReadOnly ();
  134. private Orientation orientation = Orientation.Vertical;
  135. /// <summary>
  136. /// Creates a new instance of the <see cref="TileView"/> class with
  137. /// 2 tiles (i.e. left and right).
  138. /// </summary>
  139. public TileView () : this (2)
  140. {
  141. }
  142. /// <summary>
  143. /// Creates a new instance of the <see cref="TileView"/> class with
  144. /// <paramref name="tiles"/> number of tiles.
  145. /// </summary>
  146. /// <param name="tiles"></param>
  147. public TileView (int tiles)
  148. {
  149. CanFocus = true;
  150. RebuildForTileCount (tiles);
  151. IgnoreBorderPropertyOnRedraw = true;
  152. Border = new Border () {
  153. BorderStyle = BorderStyle.None
  154. };
  155. }
  156. /// <summary>
  157. /// Invoked when any of the <see cref="SplitterDistances"/> is changed.
  158. /// </summary>
  159. public event SplitterEventHandler SplitterMoved;
  160. /// <summary>
  161. /// Raises the <see cref="SplitterMoved"/> event
  162. /// </summary>
  163. protected virtual void OnSplitterMoved (int idx)
  164. {
  165. SplitterMoved?.Invoke (this, new SplitterEventArgs (this, idx, splitterDistances [idx]));
  166. }
  167. /// <summary>
  168. /// Scraps all <see cref="Tiles"/> and creates <paramref name="count"/> new tiles
  169. /// in orientation <see cref="Orientation"/>
  170. /// </summary>
  171. /// <param name="count"></param>
  172. public void RebuildForTileCount (int count)
  173. {
  174. tiles = new List<Tile> ();
  175. splitterDistances = new List<Pos> ();
  176. if (splitterLines != null) {
  177. foreach (var sl in splitterLines) {
  178. sl.Dispose ();
  179. }
  180. }
  181. splitterLines = new List<TileViewLineView> ();
  182. RemoveAll ();
  183. foreach (var tile in tiles) {
  184. tile.ContentView.Dispose ();
  185. tile.ContentView = null;
  186. }
  187. tiles.Clear ();
  188. splitterDistances.Clear ();
  189. if (count == 0) {
  190. return;
  191. }
  192. for (int i = 0; i < count; i++) {
  193. if (i > 0) {
  194. var currentPos = Pos.Percent ((100 / count) * i);
  195. splitterDistances.Add (currentPos);
  196. var line = new TileViewLineView (this, i - 1);
  197. Add (line);
  198. splitterLines.Add (line);
  199. }
  200. var tile = new Tile ();
  201. tiles.Add (tile);
  202. Add (tile.ContentView);
  203. tile.TitleChanged += (e) => SetNeedsDisplay ();
  204. }
  205. LayoutSubviews ();
  206. }
  207. /// <summary>
  208. /// Adds a new <see cref="Tile"/> to the collection at <paramref name="idx"/>.
  209. /// This will also add another splitter line
  210. /// </summary>
  211. /// <param name="idx"></param>
  212. public Tile InsertTile (int idx)
  213. {
  214. var oldTiles = Tiles.ToArray ();
  215. RebuildForTileCount (oldTiles.Length + 1);
  216. Tile toReturn = null;
  217. for (int i = 0; i < tiles.Count; i++) {
  218. if (i != idx) {
  219. var oldTile = oldTiles [i > idx ? i - 1 : i];
  220. // remove the new empty View
  221. Remove (tiles [i].ContentView);
  222. tiles [i].ContentView.Dispose ();
  223. tiles [i].ContentView = null;
  224. // restore old Tile and View
  225. tiles [i] = oldTile;
  226. Add (tiles [i].ContentView);
  227. } else {
  228. toReturn = tiles [i];
  229. }
  230. }
  231. SetNeedsDisplay ();
  232. LayoutSubviews ();
  233. return toReturn;
  234. }
  235. /// <summary>
  236. /// Removes a <see cref="Tiles"/> at the provided <paramref name="idx"/> from
  237. /// the view. Returns the removed tile or null if already empty.
  238. /// </summary>
  239. /// <param name="idx"></param>
  240. /// <returns></returns>
  241. public Tile RemoveTile (int idx)
  242. {
  243. var oldTiles = Tiles.ToArray ();
  244. if (idx < 0 || idx >= oldTiles.Length) {
  245. return null;
  246. }
  247. var removed = Tiles.ElementAt (idx);
  248. RebuildForTileCount (oldTiles.Length - 1);
  249. for (int i = 0; i < tiles.Count; i++) {
  250. int oldIdx = i >= idx ? i + 1 : i;
  251. var oldTile = oldTiles [oldIdx];
  252. // remove the new empty View
  253. Remove (tiles [i].ContentView);
  254. tiles [i].ContentView.Dispose ();
  255. tiles [i].ContentView = null;
  256. // restore old Tile and View
  257. tiles [i] = oldTile;
  258. Add (tiles [i].ContentView);
  259. }
  260. SetNeedsDisplay ();
  261. LayoutSubviews ();
  262. return removed;
  263. }
  264. ///<summary>
  265. /// Returns the index of the first <see cref="Tile"/> in
  266. /// <see cref="Tiles"/> which contains <paramref name="toFind"/>.
  267. ///</summary>
  268. public int IndexOf (View toFind, bool recursive = false)
  269. {
  270. for (int i = 0; i < tiles.Count; i++) {
  271. var v = tiles [i].ContentView;
  272. if (v == toFind) {
  273. return i;
  274. }
  275. if (v.Subviews.Contains (toFind)) {
  276. return i;
  277. }
  278. if (recursive) {
  279. if (RecursiveContains (v.Subviews, toFind)) {
  280. return i;
  281. }
  282. }
  283. }
  284. return -1;
  285. }
  286. private bool RecursiveContains (IEnumerable<View> haystack, View needle)
  287. {
  288. foreach (var v in haystack) {
  289. if (v == needle) {
  290. return true;
  291. }
  292. if (RecursiveContains (v.Subviews, needle)) {
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. /// <summary>
  299. /// Orientation of the dividing line (Horizontal or Vertical).
  300. /// </summary>
  301. public Orientation Orientation {
  302. get { return orientation; }
  303. set {
  304. orientation = value;
  305. LayoutSubviews ();
  306. }
  307. }
  308. /// <inheritdoc/>
  309. public override void LayoutSubviews ()
  310. {
  311. var contentArea = Bounds;
  312. if (HasBorder ()) {
  313. contentArea = new Rect (
  314. contentArea.X + 1,
  315. contentArea.Y + 1,
  316. Math.Max (0, contentArea.Width - 2),
  317. Math.Max (0, contentArea.Height - 2));
  318. }
  319. Setup (contentArea);
  320. base.LayoutSubviews ();
  321. }
  322. /// <summary>
  323. /// <para>Attempts to update the <see cref="splitterDistances"/> of line at <paramref name="idx"/>
  324. /// to the new <paramref name="value"/>. Returns false if the new position is not allowed because of
  325. /// <see cref="Tile.MinSize"/>, location of other splitters etc.
  326. /// </para>
  327. /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
  328. /// are supported for this property.</para>
  329. /// </summary>
  330. public bool SetSplitterPos (int idx, Pos value)
  331. {
  332. if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
  333. throw new ArgumentException ($"Only Percent and Absolute values are supported. Passed value was {value.GetType ().Name}");
  334. }
  335. var fullSpace = orientation == Orientation.Vertical ? Bounds.Width : Bounds.Height;
  336. if (fullSpace != 0 && !IsValidNewSplitterPos (idx, value, fullSpace)) {
  337. return false;
  338. }
  339. splitterDistances [idx] = value;
  340. GetRootTileView ().LayoutSubviews ();
  341. OnSplitterMoved (idx);
  342. return true;
  343. }
  344. /// <inheritdoc/>
  345. public override bool OnEnter (View view)
  346. {
  347. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  348. if (!Tiles.Where (t => t.ContentView.HasFocus).Any ()) {
  349. Tiles.FirstOrDefault ()?.ContentView.SetFocus ();
  350. }
  351. return base.OnEnter (view);
  352. }
  353. /// <inheritdoc/>
  354. public override void Redraw (Rect bounds)
  355. {
  356. Driver.SetAttribute (ColorScheme.Normal);
  357. Clear ();
  358. base.Redraw (bounds);
  359. var lc = new LineCanvas ();
  360. var allLines = GetAllLineViewsRecursively (this);
  361. var allTitlesToRender = GetAllTitlesToRenderRecursively (this);
  362. if (IsRootTileView ()) {
  363. if (HasBorder ()) {
  364. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, Border.BorderStyle);
  365. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, Border.BorderStyle);
  366. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, Border.BorderStyle);
  367. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, Border.BorderStyle);
  368. }
  369. foreach (var line in allLines) {
  370. bool isRoot = splitterLines.Contains (line);
  371. line.ViewToScreen (0, 0, out var x1, out var y1);
  372. var origin = ScreenToView (x1, y1);
  373. var length = line.Orientation == Orientation.Horizontal ?
  374. line.Frame.Width - 1 :
  375. line.Frame.Height - 1;
  376. if (!isRoot) {
  377. if (line.Orientation == Orientation.Horizontal) {
  378. origin.X -= 1;
  379. } else {
  380. origin.Y -= 1;
  381. }
  382. length += 2;
  383. }
  384. lc.AddLine (origin, length, line.Orientation, Border.BorderStyle);
  385. }
  386. }
  387. Driver.SetAttribute (ColorScheme.Normal);
  388. lc.Draw (this, bounds);
  389. // Redraw the lines so that focus/drag symbol renders
  390. foreach (var line in allLines) {
  391. line.DrawSplitterSymbol ();
  392. }
  393. // Draw Titles over Border
  394. foreach (var titleToRender in allTitlesToRender) {
  395. var renderAt = titleToRender.GetLocalCoordinateForTitle (this);
  396. if (renderAt.Y < 0) {
  397. // If we have no border then root level tiles
  398. // have nowhere to render their titles.
  399. continue;
  400. }
  401. // TODO: Render with focus color if focused
  402. var title = titleToRender.GetTrimmedTitle ();
  403. for (int i = 0; i < title.Length; i++) {
  404. AddRune (renderAt.X + i, renderAt.Y, title [i]);
  405. }
  406. }
  407. }
  408. /// <summary>
  409. /// Converts of <see cref="Tiles"/> element <paramref name="idx"/>
  410. /// from a regular <see cref="View"/> to a new nested <see cref="TileView"/>
  411. /// the specified <paramref name="numberOfPanels"/>.
  412. /// Returns false if the element already contains a nested view.
  413. /// </summary>
  414. /// <remarks>After successful splitting, the old contents will be moved to the
  415. /// <paramref name="result"/> <see cref="TileView"/>'s first tile.</remarks>
  416. /// <param name="idx">The element of <see cref="Tiles"/> that is to be subdivided.</param>
  417. /// <param name="numberOfPanels">The number of panels that the <see cref="Tile"/> should be split into</param>
  418. /// <param name="result">The new nested <see cref="TileView"/>.</param>
  419. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  420. /// <see cref="TileView"/>. <see langword="false"/> if it was already a nested
  421. /// <see cref="TileView"/></returns>
  422. public bool TrySplitTile (int idx, int numberOfPanels, out TileView result)
  423. {
  424. // when splitting a view into 2 sub views we will need to migrate
  425. // the title too
  426. var tile = tiles [idx];
  427. var title = tile.Title;
  428. View toMove = tile.ContentView;
  429. if (toMove is TileView existing) {
  430. result = existing;
  431. return false;
  432. }
  433. var newContainer = new TileView (numberOfPanels) {
  434. Width = Dim.Fill (),
  435. Height = Dim.Fill (),
  436. parentTileView = this,
  437. };
  438. // Take everything out of the View we are moving
  439. var childViews = toMove.Subviews.ToArray ();
  440. toMove.RemoveAll ();
  441. // Remove the view itself and replace it with the new TileView
  442. Remove (toMove);
  443. toMove.Dispose ();
  444. toMove = null;
  445. Add (newContainer);
  446. tile.ContentView = newContainer;
  447. var newTileView1 = newContainer.tiles [0].ContentView;
  448. // Add the original content into the first view of the new container
  449. foreach (var childView in childViews) {
  450. newTileView1.Add (childView);
  451. }
  452. // Move the title across too
  453. newContainer.tiles [0].Title = title;
  454. tile.Title = string.Empty;
  455. result = newContainer;
  456. return true;
  457. }
  458. private bool IsValidNewSplitterPos (int idx, Pos value, int fullSpace)
  459. {
  460. int newSize = value.Anchor (fullSpace);
  461. bool isGettingBigger = newSize > splitterDistances [idx].Anchor (fullSpace);
  462. int lastSplitterOrBorder = HasBorder () ? 1 : 0;
  463. int nextSplitterOrBorder = HasBorder () ? fullSpace - 1 : fullSpace;
  464. // Cannot move off screen right
  465. if (newSize >= fullSpace - (HasBorder () ? 1 : 0)) {
  466. if (isGettingBigger) {
  467. return false;
  468. }
  469. }
  470. // Cannot move off screen left
  471. if (newSize < (HasBorder () ? 1 : 0)) {
  472. if (!isGettingBigger) {
  473. return false;
  474. }
  475. }
  476. // Do not allow splitter to move left of the one before
  477. if (idx > 0) {
  478. int posLeft = splitterDistances [idx - 1].Anchor (fullSpace);
  479. if (newSize <= posLeft) {
  480. return false;
  481. }
  482. lastSplitterOrBorder = posLeft;
  483. }
  484. // Do not allow splitter to move right of the one after
  485. if (idx + 1 < splitterDistances.Count) {
  486. int posRight = splitterDistances [idx + 1].Anchor (fullSpace);
  487. if (newSize >= posRight) {
  488. return false;
  489. }
  490. nextSplitterOrBorder = posRight;
  491. }
  492. if (isGettingBigger) {
  493. var spaceForNext = nextSplitterOrBorder - newSize;
  494. // space required for the last line itself
  495. if (idx > 0) {
  496. spaceForNext--;
  497. }
  498. // don't grow if it would take us below min size of right panel
  499. if (spaceForNext < tiles [idx + 1].MinSize) {
  500. return false;
  501. }
  502. } else {
  503. var spaceForLast = newSize - lastSplitterOrBorder;
  504. // space required for the line itself
  505. if (idx > 0) {
  506. spaceForLast--;
  507. }
  508. // don't shrink if it would take us below min size of left panel
  509. if (spaceForLast < tiles [idx].MinSize) {
  510. return false;
  511. }
  512. }
  513. return true;
  514. }
  515. private List<TileViewLineView> GetAllLineViewsRecursively (View v)
  516. {
  517. var lines = new List<TileViewLineView> ();
  518. foreach (var sub in v.Subviews) {
  519. if (sub is TileViewLineView s) {
  520. if (s.Visible && s.Parent.GetRootTileView () == this) {
  521. lines.Add (s);
  522. }
  523. } else {
  524. if (sub.Visible) {
  525. lines.AddRange (GetAllLineViewsRecursively (sub));
  526. }
  527. }
  528. }
  529. return lines;
  530. }
  531. private List<TileTitleToRender> GetAllTitlesToRenderRecursively (TileView v, int depth = 0)
  532. {
  533. var titles = new List<TileTitleToRender> ();
  534. foreach (var sub in v.Tiles) {
  535. // Don't render titles for invisible stuff!
  536. if (!sub.ContentView.Visible) {
  537. continue;
  538. }
  539. if (sub.ContentView is TileView subTileView) {
  540. // Panels with sub split tiles in them can never
  541. // have their Titles rendered. Instead we dive in
  542. // and pull up their children as titles
  543. titles.AddRange (GetAllTitlesToRenderRecursively (subTileView, depth + 1));
  544. } else {
  545. if (sub.Title.Length > 0) {
  546. titles.Add (new TileTitleToRender (v, sub, depth));
  547. }
  548. }
  549. }
  550. return titles;
  551. }
  552. /// <summary>
  553. /// <para>
  554. /// <see langword="true"/> if <see cref="TileView"/> is nested within a parent <see cref="TileView"/>
  555. /// e.g. via the <see cref="TrySplitTile"/>. <see langword="false"/> if it is a root level <see cref="TileView"/>.
  556. /// </para>
  557. /// </summary>
  558. /// <remarks>Note that manually adding one <see cref="TileView"/> to another will not result in a parent/child
  559. /// relationship and both will still be considered 'root' containers. Always use
  560. /// <see cref="TrySplitTile(int, int, out TileView)"/> if you want to subdivide a <see cref="TileView"/>.</remarks>
  561. /// <returns></returns>
  562. public bool IsRootTileView ()
  563. {
  564. return parentTileView == null;
  565. }
  566. /// <summary>
  567. /// Returns the immediate parent <see cref="TileView"/> of this. Note that in case
  568. /// of deep nesting this might not be the root <see cref="TileView"/>. Returns null
  569. /// if this instance is not a nested child (created with
  570. /// <see cref="TrySplitTile(int, int, out TileView)"/>)
  571. /// </summary>
  572. /// <remarks>
  573. /// Use <see cref="IsRootTileView"/> to determine if the returned value is the root.
  574. /// </remarks>
  575. /// <returns></returns>
  576. public TileView GetParentTileView ()
  577. {
  578. return this.parentTileView;
  579. }
  580. private TileView GetRootTileView ()
  581. {
  582. TileView root = this;
  583. while (root.parentTileView != null) {
  584. root = root.parentTileView;
  585. }
  586. return root;
  587. }
  588. private void Setup (Rect bounds)
  589. {
  590. if (bounds.IsEmpty || bounds.Height <= 0 || bounds.Width <= 0) {
  591. return;
  592. }
  593. for (int i = 0; i < splitterLines.Count; i++) {
  594. var line = splitterLines [i];
  595. line.Orientation = Orientation;
  596. line.Width = orientation == Orientation.Vertical
  597. ? 1 : Dim.Fill ();
  598. line.Height = orientation == Orientation.Vertical
  599. ? Dim.Fill () : 1;
  600. line.LineRune = orientation == Orientation.Vertical ?
  601. Driver.VLine : Driver.HLine;
  602. if (orientation == Orientation.Vertical) {
  603. line.X = splitterDistances [i];
  604. line.Y = 0;
  605. } else {
  606. line.Y = splitterDistances [i];
  607. line.X = 0;
  608. }
  609. }
  610. HideSplittersBasedOnTileVisibility ();
  611. var visibleTiles = tiles.Where (t => t.ContentView.Visible).ToArray ();
  612. var visibleSplitterLines = splitterLines.Where (l => l.Visible).ToArray ();
  613. for (int i = 0; i < visibleTiles.Length; i++) {
  614. var tile = visibleTiles [i];
  615. if (Orientation == Orientation.Vertical) {
  616. tile.ContentView.X = i == 0 ? bounds.X : Pos.Right (visibleSplitterLines [i - 1]);
  617. tile.ContentView.Y = bounds.Y;
  618. tile.ContentView.Height = bounds.Height;
  619. tile.ContentView.Width = GetTileWidthOrHeight (i, Bounds.Width, visibleTiles, visibleSplitterLines);
  620. } else {
  621. tile.ContentView.X = bounds.X;
  622. tile.ContentView.Y = i == 0 ? 0 : Pos.Bottom (visibleSplitterLines [i - 1]);
  623. tile.ContentView.Width = bounds.Width;
  624. tile.ContentView.Height = GetTileWidthOrHeight (i, Bounds.Height, visibleTiles, visibleSplitterLines);
  625. }
  626. }
  627. }
  628. private void HideSplittersBasedOnTileVisibility ()
  629. {
  630. if (splitterLines.Count == 0) {
  631. return;
  632. }
  633. foreach (var line in splitterLines) {
  634. line.Visible = true;
  635. }
  636. for (int i = 0; i < tiles.Count; i++) {
  637. if (!tiles [i].ContentView.Visible) {
  638. // when a tile is not visible, prefer hiding
  639. // the splitter on it's left
  640. var candidate = splitterLines [Math.Max (0, i - 1)];
  641. // unless that splitter is already hidden
  642. // e.g. when hiding panels 0 and 1 of a 3 panel
  643. // container
  644. if (candidate.Visible) {
  645. candidate.Visible = false;
  646. } else {
  647. splitterLines [Math.Min (i, splitterLines.Count - 1)].Visible = false;
  648. }
  649. }
  650. }
  651. }
  652. private Dim GetTileWidthOrHeight (int i, int space, Tile [] visibleTiles, TileViewLineView [] visibleSplitterLines)
  653. {
  654. // last tile
  655. if (i + 1 >= visibleTiles.Length) {
  656. return Dim.Fill (HasBorder () ? 1 : 0);
  657. }
  658. var nextSplitter = visibleSplitterLines [i];
  659. var nextSplitterPos = Orientation == Orientation.Vertical ?
  660. nextSplitter.X : nextSplitter.Y;
  661. var nextSplitterDistance = nextSplitterPos.Anchor (space);
  662. var lastSplitter = i >= 1 ? visibleSplitterLines [i - 1] : null;
  663. var lastSplitterPos = Orientation == Orientation.Vertical ?
  664. lastSplitter?.X : lastSplitter?.Y;
  665. var lastSplitterDistance = lastSplitterPos?.Anchor (space) ?? 0;
  666. var distance = nextSplitterDistance - lastSplitterDistance;
  667. if (i > 0) {
  668. return distance - 1;
  669. }
  670. return distance - (HasBorder () ? 1 : 0);
  671. }
  672. private class TileTitleToRender {
  673. public TileView Parent { get; }
  674. public Tile Tile { get; }
  675. public int Depth { get; }
  676. public TileTitleToRender (TileView parent, Tile tile, int depth)
  677. {
  678. Parent = parent;
  679. Tile = tile;
  680. Depth = depth;
  681. }
  682. /// <summary>
  683. /// Translates the <see cref="Tile"/> title location from its local
  684. /// coordinate space <paramref name="intoCoordinateSpace"/>.
  685. /// </summary>
  686. public Point GetLocalCoordinateForTitle (TileView intoCoordinateSpace)
  687. {
  688. Tile.ContentView.ViewToScreen (0, 0, out var screenCol, out var screenRow);
  689. screenRow--;
  690. return intoCoordinateSpace.ScreenToView (screenCol, screenRow);
  691. }
  692. internal string GetTrimmedTitle ()
  693. {
  694. Dim spaceDim = Tile.ContentView.Width;
  695. var spaceAbs = spaceDim.Anchor (Parent.Bounds.Width);
  696. var title = $" {Tile.Title} ";
  697. if (title.Length > spaceAbs) {
  698. return title.Substring (0, spaceAbs);
  699. }
  700. return title;
  701. }
  702. }
  703. private class TileViewLineView : LineView {
  704. public TileView Parent { get; private set; }
  705. public int Idx { get; }
  706. Point? dragPosition;
  707. Pos dragOrignalPos;
  708. public Point? moveRuneRenderLocation;
  709. public TileViewLineView (TileView parent, int idx)
  710. {
  711. CanFocus = true;
  712. TabStop = true;
  713. this.Parent = parent;
  714. Idx = idx;
  715. base.AddCommand (Command.Right, () => {
  716. return MoveSplitter (1, 0);
  717. });
  718. base.AddCommand (Command.Left, () => {
  719. return MoveSplitter (-1, 0);
  720. });
  721. base.AddCommand (Command.LineUp, () => {
  722. return MoveSplitter (0, -1);
  723. });
  724. base.AddCommand (Command.LineDown, () => {
  725. return MoveSplitter (0, 1);
  726. });
  727. AddKeyBinding (Key.CursorRight, Command.Right);
  728. AddKeyBinding (Key.CursorLeft, Command.Left);
  729. AddKeyBinding (Key.CursorUp, Command.LineUp);
  730. AddKeyBinding (Key.CursorDown, Command.LineDown);
  731. }
  732. public override bool ProcessKey (KeyEvent kb)
  733. {
  734. if (!CanFocus || !HasFocus) {
  735. return base.ProcessKey (kb);
  736. }
  737. var result = InvokeKeybindings (kb);
  738. if (result != null)
  739. return (bool)result;
  740. return base.ProcessKey (kb);
  741. }
  742. public override void PositionCursor ()
  743. {
  744. base.PositionCursor ();
  745. var location = moveRuneRenderLocation ??
  746. new Point (Bounds.Width / 2, Bounds.Height / 2);
  747. Move (location.X, location.Y);
  748. }
  749. public override bool OnEnter (View view)
  750. {
  751. Driver.SetCursorVisibility (CursorVisibility.Default);
  752. PositionCursor ();
  753. return base.OnEnter (view);
  754. }
  755. public override void Redraw (Rect bounds)
  756. {
  757. base.Redraw (bounds);
  758. DrawSplitterSymbol ();
  759. }
  760. public void DrawSplitterSymbol ()
  761. {
  762. if (CanFocus && HasFocus) {
  763. var location = moveRuneRenderLocation ??
  764. new Point (Bounds.Width / 2, Bounds.Height / 2);
  765. AddRune (location.X, location.Y, Driver.Diamond);
  766. }
  767. }
  768. public override bool MouseEvent (MouseEvent mouseEvent)
  769. {
  770. if (!CanFocus) {
  771. return true;
  772. }
  773. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  774. // Start a Drag
  775. SetFocus ();
  776. Application.EnsuresTopOnFront ();
  777. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  778. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  779. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  780. Application.GrabMouse (this);
  781. if (Orientation == Orientation.Horizontal) {
  782. } else {
  783. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  784. }
  785. }
  786. return true;
  787. } else if (
  788. dragPosition.HasValue &&
  789. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  790. // Continue Drag
  791. // how far has user dragged from original location?
  792. if (Orientation == Orientation.Horizontal) {
  793. int dy = mouseEvent.Y - dragPosition.Value.Y;
  794. Parent.SetSplitterPos (Idx, Offset (Y, dy));
  795. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  796. } else {
  797. int dx = mouseEvent.X - dragPosition.Value.X;
  798. Parent.SetSplitterPos (Idx, Offset (X, dx));
  799. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  800. }
  801. Parent.SetNeedsDisplay ();
  802. return true;
  803. }
  804. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  805. // End Drag
  806. Application.UngrabMouse ();
  807. Driver.UncookMouse ();
  808. FinalisePosition (
  809. dragOrignalPos,
  810. Orientation == Orientation.Horizontal ? Y : X);
  811. dragPosition = null;
  812. moveRuneRenderLocation = null;
  813. }
  814. return false;
  815. }
  816. private bool MoveSplitter (int distanceX, int distanceY)
  817. {
  818. if (Orientation == Orientation.Vertical) {
  819. // Cannot move in this direction
  820. if (distanceX == 0) {
  821. return false;
  822. }
  823. var oldX = X;
  824. return FinalisePosition (oldX, Offset (X, distanceX));
  825. } else {
  826. // Cannot move in this direction
  827. if (distanceY == 0) {
  828. return false;
  829. }
  830. var oldY = Y;
  831. return FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  832. }
  833. }
  834. private Pos Offset (Pos pos, int delta)
  835. {
  836. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  837. Parent.Bounds.Height : Parent.Bounds.Width);
  838. return posAbsolute + delta;
  839. }
  840. /// <summary>
  841. /// <para>
  842. /// Moves <see cref="Parent"/> <see cref="TileView.SplitterDistances"/> to
  843. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  844. /// (absolute / relative) that <paramref name="oldValue"/> had.
  845. /// </para>
  846. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  847. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  848. /// </summary>
  849. /// <param name="oldValue"></param>
  850. /// <param name="newValue"></param>
  851. private bool FinalisePosition (Pos oldValue, Pos newValue)
  852. {
  853. if (oldValue is Pos.PosFactor) {
  854. if (Orientation == Orientation.Horizontal) {
  855. return Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Height));
  856. } else {
  857. return Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Width));
  858. }
  859. } else {
  860. return Parent.SetSplitterPos (Idx, newValue);
  861. }
  862. }
  863. /// <summary>
  864. /// <para>
  865. /// Determines the absolute position of <paramref name="p"/> and
  866. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  867. /// </para>
  868. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  869. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  870. /// </summary>
  871. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  872. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  873. /// <returns></returns>
  874. private Pos ConvertToPosFactor (Pos p, int parentLength)
  875. {
  876. // calculate position in the 'middle' of the cell at p distance along parentLength
  877. float position = p.Anchor (parentLength) + 0.5f;
  878. return new Pos.PosFactor (position / parentLength);
  879. }
  880. }
  881. private bool HasBorder ()
  882. {
  883. return Border?.BorderStyle != BorderStyle.None;
  884. }
  885. /// <inheritdoc/>
  886. protected override void Dispose (bool disposing)
  887. {
  888. foreach (var tile in Tiles) {
  889. Remove (tile.ContentView);
  890. tile.ContentView.Dispose ();
  891. }
  892. base.Dispose (disposing);
  893. }
  894. }
  895. /// <summary>
  896. /// Provides data for <see cref="TileView"/> events.
  897. /// </summary>
  898. public class SplitterEventArgs : EventArgs {
  899. /// <summary>
  900. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  901. /// </summary>
  902. /// <param name="tileView"><see cref="TileView"/> in which splitter is being moved.</param>
  903. /// <param name="idx">Index of the splitter being moved in <see cref="TileView.SplitterDistances"/>.</param>
  904. /// <param name="splitterDistance">The new <see cref="Pos"/> of the splitter line.</param>
  905. public SplitterEventArgs (TileView tileView, int idx, Pos splitterDistance)
  906. {
  907. SplitterDistance = splitterDistance;
  908. TileView = tileView;
  909. Idx = idx;
  910. }
  911. /// <summary>
  912. /// New position of the splitter line (see <see cref="TileView.SplitterDistances"/>).
  913. /// </summary>
  914. public Pos SplitterDistance { get; }
  915. /// <summary>
  916. /// Container (sender) of the event.
  917. /// </summary>
  918. public TileView TileView { get; }
  919. /// <summary>
  920. /// Gets the index of the splitter that is being moved. This can be
  921. /// used to index <see cref="TileView.SplitterDistances"/>
  922. /// </summary>
  923. public int Idx { get; }
  924. }
  925. /// <summary>
  926. /// Represents a method that will handle splitter events.
  927. /// </summary>
  928. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  929. }