TileView.cs 28 KB

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