2
0

TileView.cs 34 KB

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