TileView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. /// TODO: Might be able to make Border virtual and override here
  13. /// To make this more API friendly
  14. /// <summary>
  15. /// Use this field instead of Border to create an integrated
  16. /// Border in which lines connect with subviews and splitters
  17. /// seamlessly
  18. /// </summary>
  19. public BorderStyle IntegratedBorder { get; set; }
  20. public class Tile {
  21. public View View { get; internal set; }
  22. public int MinSize { get; set; }
  23. public string Title { get; set; }
  24. public Tile ()
  25. {
  26. View = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
  27. Title = string.Empty;
  28. MinSize = 0;
  29. }
  30. }
  31. List<Tile> tiles;
  32. private List<Pos> splitterDistances;
  33. private List<TileViewLineView> splitterLines;
  34. /// <summary>
  35. /// The sub sections hosted by the view
  36. /// </summary>
  37. public IReadOnlyCollection<Tile> Tiles => tiles.AsReadOnly ();
  38. /// <summary>
  39. /// The splitter locations. Note that there will be N-1 splitters where
  40. /// N is the number of <see cref="Tiles"/>.
  41. /// </summary>
  42. public IReadOnlyCollection<Pos> SplitterDistances => splitterDistances.AsReadOnly ();
  43. private Orientation orientation = Orientation.Vertical;
  44. /// <summary>
  45. /// Creates a new instance of the TileView class.
  46. /// </summary>
  47. public TileView () : this (2)
  48. {
  49. }
  50. public TileView (int tiles)
  51. {
  52. CanFocus = true;
  53. RebuildForTileCount (tiles);
  54. }
  55. /// <summary>
  56. /// Invoked when the <see cref="SplitterDistance"/> is changed
  57. /// </summary>
  58. public event SplitterEventHandler SplitterMoved;
  59. /// <summary>
  60. /// Raises the <see cref="SplitterMoved"/> event
  61. /// </summary>
  62. protected virtual void OnSplitterMoved (int idx)
  63. {
  64. SplitterMoved?.Invoke (this, new SplitterEventArgs (this, idx, splitterDistances [idx]));
  65. }
  66. /// <summary>
  67. /// Scraps all <see cref="Tiles"/> and creates <paramref name="count"/> new tiles
  68. /// in orientation <see cref="Orientation"/>
  69. /// </summary>
  70. /// <param name="count"></param>
  71. public void RebuildForTileCount (int count)
  72. {
  73. tiles = new List<Tile> ();
  74. // TODO: keep these if growing
  75. splitterDistances = new List<Pos> ();
  76. splitterLines = new List<TileViewLineView> ();
  77. RemoveAll ();
  78. tiles.Clear ();
  79. splitterDistances.Clear ();
  80. if (count == 0) {
  81. return;
  82. }
  83. for (int i = 0; i < count; i++) {
  84. if (i > 0) {
  85. var currentPos = Pos.Percent ((100 / count) * i);
  86. splitterDistances.Add (currentPos);
  87. var line = new TileViewLineView (this, i - 1);
  88. Add (line);
  89. splitterLines.Add (line);
  90. }
  91. var tile = new Tile ();
  92. tiles.Add (tile);
  93. Add (tile.View);
  94. }
  95. LayoutSubviews ();
  96. }
  97. /// <summary>
  98. /// Adds a new <see cref="Tile"/> to the collection at <paramref name="idx"/>.
  99. /// This will also add another splitter line
  100. /// </summary>
  101. /// <param name="idx"></param>
  102. /// <exception cref="NotImplementedException"></exception>
  103. public Tile InsertTile (int idx)
  104. {
  105. var oldTiles = Tiles.ToArray ();
  106. RebuildForTileCount (oldTiles.Length + 1);
  107. Tile toReturn = null;
  108. for(int i=0;i<tiles.Count;i++) {
  109. if(i != idx) {
  110. var oldTile = oldTiles [i > idx ? i - 1 : i];
  111. // remove the new empty View
  112. Remove (tiles [i].View);
  113. // restore old Tile and View
  114. tiles [i] = oldTile;
  115. Add (tiles [i].View);
  116. }
  117. else
  118. {
  119. toReturn = tiles[i];
  120. }
  121. }
  122. SetNeedsDisplay ();
  123. LayoutSubviews ();
  124. return toReturn;
  125. }
  126. public void RemoveTile (int idx)
  127. {
  128. var oldTiles = Tiles.ToArray ();
  129. if (idx < 0 || idx >= oldTiles.Length) {
  130. return;
  131. }
  132. RebuildForTileCount (oldTiles.Length - 1);
  133. for (int i = 0; i < tiles.Count; i++) {
  134. int oldIdx = i >= idx ? i + 1: i;
  135. var oldTile = oldTiles [oldIdx];
  136. // remove the new empty View
  137. Remove (tiles [i].View);
  138. // restore old Tile and View
  139. tiles [i] = oldTile;
  140. Add (tiles [i].View);
  141. }
  142. SetNeedsDisplay ();
  143. LayoutSubviews ();
  144. }
  145. ///<summary>
  146. /// Returns the index of the first <see cref="Tile"/> in
  147. /// <see cref="Tiles"/> which contains <paramref name="view"/>.
  148. ///</summary>
  149. public int IndexOf(View view)
  150. {
  151. // TODO: Could be recursive (i.e. search nested Subviews)
  152. return tiles.IndexOf((t)=>t.View == view || t.View.Subviews.Contains(view));
  153. }
  154. /// <summary>
  155. /// Orientation of the dividing line (Horizontal or Vertical).
  156. /// </summary>
  157. public Orientation Orientation {
  158. get { return orientation; }
  159. set {
  160. orientation = value;
  161. LayoutSubviews ();
  162. }
  163. }
  164. public override void LayoutSubviews ()
  165. {
  166. var contentArea = Bounds;
  167. if (HasBorder ()) {
  168. // TODO: Bound with Max/Min
  169. contentArea = new Rect (
  170. contentArea.X + 1,
  171. contentArea.Y + 1,
  172. Math.Max (0, contentArea.Width - 2),
  173. Math.Max (0, contentArea.Height - 2));
  174. } else if (HasAnyTitles () && IsRootTileView ()) {
  175. // TODO: Bound with Max/Min
  176. contentArea = new Rect (
  177. contentArea.X,
  178. contentArea.Y + 1,
  179. contentArea.Width,
  180. Math.Max (0, contentArea.Height - 1));
  181. }
  182. Setup (contentArea);
  183. base.LayoutSubviews ();
  184. }
  185. /// <summary>
  186. /// <para>Distance Horizontally or Vertically to the splitter line when
  187. /// neither view is collapsed.
  188. /// </para>
  189. /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
  190. /// are supported for this property.</para>
  191. /// </summary>
  192. public void SetSplitterPos (int idx, Pos value)
  193. {
  194. if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
  195. throw new ArgumentException ($"Only Percent and Absolute values are supported. Passed value was {value.GetType ().Name}");
  196. }
  197. splitterDistances [idx] = value;
  198. GetRootTileView ().LayoutSubviews ();
  199. OnSplitterMoved (idx);
  200. }
  201. /// <inheritdoc/>
  202. public override bool OnEnter (View view)
  203. {
  204. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  205. return base.OnEnter (view);
  206. }
  207. /// <inheritdoc/>
  208. public override void Redraw (Rect bounds)
  209. {
  210. var childTitles = new List<ChildSplitterLine> ();
  211. Driver.SetAttribute (ColorScheme.Normal);
  212. Clear ();
  213. base.Redraw (bounds);
  214. var lc = new LineCanvas ();
  215. var allLines = GetAllChildTileViewLineViewRecursively (this);
  216. if (IsRootTileView ()) {
  217. if (HasBorder ()) {
  218. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, IntegratedBorder);
  219. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, IntegratedBorder);
  220. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, IntegratedBorder);
  221. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
  222. }
  223. foreach (var line in allLines.Where (l => l.Visible)) {
  224. bool isRoot = splitterLines.Contains (line);
  225. line.ViewToScreen (0, 0, out var x1, out var y1);
  226. var origin = ScreenToView (x1, y1);
  227. var length = line.Orientation == Orientation.Horizontal ?
  228. line.Frame.Width - 1 :
  229. line.Frame.Height - 1;
  230. if (!isRoot) {
  231. if (line.Orientation == Orientation.Horizontal) {
  232. origin.X -= 1;
  233. } else {
  234. origin.Y -= 1;
  235. }
  236. length += 2;
  237. childTitles.Add (
  238. new ChildSplitterLine (line));
  239. }
  240. lc.AddLine (origin, length, line.Orientation, IntegratedBorder);
  241. }
  242. }
  243. Driver.SetAttribute (ColorScheme.Normal);
  244. lc.Draw (this, bounds);
  245. // Redraw the lines so that focus/drag symbol renders
  246. foreach (var line in allLines) {
  247. line.DrawSplitterSymbol ();
  248. }
  249. foreach (var child in childTitles) {
  250. child.DrawTitles ();
  251. }
  252. // Draw Titles over Border
  253. for (int i = 0; i < tiles.Count; i++) {
  254. var tile = tiles [i];
  255. if (tile.View.Visible && tile.Title.Length > 0) {
  256. var screen = i == 0 ?
  257. ViewToScreen (new Rect (0, 0, bounds.Width, 1)) :
  258. ViewToScreen (splitterLines [i - 1].Frame);
  259. Driver.SetAttribute (tile.View.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  260. Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, tile.View.Frame.Width, 0), tile.Title, 0, 0, 0, 0);
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// Converts <see cref="View1"/> from a regular <see cref="View"/>
  266. /// container to a new nested <see cref="TileView"/>. If <see cref="View1"/>
  267. /// is already a <see cref="TileView"/> then returns false.
  268. /// </summary>
  269. /// <remarks>After successful splitting, the returned container's <see cref="View1"/>
  270. /// will contain the original content and <see cref="View1Title"/> (if any) while
  271. /// <see cref="View2"/> will be empty and available for adding to.
  272. /// for adding to.</remarks>
  273. /// <param name="result">The new <see cref="TileView"/> now showing in
  274. /// <see cref="View1"/> or the existing one if it was already been converted before.</param>
  275. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  276. /// <see cref="TileView"/>. <see langword="false"/> if it was already a nested
  277. /// <see cref="TileView"/></returns>
  278. public bool TryTileView(int idx, int panels, out TileView result)
  279. {
  280. // when splitting a view into 2 sub views we will need to migrate
  281. // the title too
  282. var tile = tiles [idx];
  283. var title = tile.Title;
  284. View toMove = tile.View;
  285. if (toMove is TileView existing) {
  286. result = existing;
  287. return false;
  288. }
  289. var newContainer = new TileView(panels) {
  290. Width = Dim.Fill (),
  291. Height = Dim.Fill (),
  292. parentTileView = this,
  293. };
  294. // Take everything out of the View we are moving
  295. var childViews = toMove.Subviews.ToArray();
  296. toMove.RemoveAll ();
  297. // Remove the view itself and replace it with the new TileView
  298. Remove (toMove);
  299. Add (newContainer);
  300. tile.View = newContainer;
  301. var newTileView1 = newContainer.tiles [0].View;
  302. // Add the original content into the first view of the new container
  303. foreach (var childView in childViews) {
  304. newTileView1.Add (childView);
  305. }
  306. result = newContainer;
  307. return true;
  308. }
  309. private List<TileViewLineView> GetAllChildTileViewLineViewRecursively (View v)
  310. {
  311. var lines = new List<TileViewLineView> ();
  312. foreach (var sub in v.Subviews) {
  313. if (sub is TileViewLineView s) {
  314. if (s.Parent.GetRootTileView () == this) {
  315. lines.Add (s);
  316. }
  317. } else {
  318. lines.AddRange (GetAllChildTileViewLineViewRecursively (sub));
  319. }
  320. }
  321. return lines;
  322. }
  323. private bool IsRootTileView ()
  324. {
  325. // TODO: don't want to layout subviews since the parent recursively lays them all out
  326. return parentTileView == null;
  327. }
  328. private TileView GetRootTileView ()
  329. {
  330. TileView root = this;
  331. while (root.parentTileView != null) {
  332. root = root.parentTileView;
  333. }
  334. return root;
  335. }
  336. private void Setup (Rect bounds)
  337. {
  338. if (bounds.IsEmpty) {
  339. return;
  340. }
  341. RespectMinimumTileSizes ();
  342. for (int i = 0; i < splitterLines.Count; i++) {
  343. var line = splitterLines[i];
  344. line.Orientation = Orientation;
  345. line.Width = orientation == Orientation.Vertical
  346. ? 1 : Dim.Fill ();
  347. line.Height = orientation == Orientation.Vertical
  348. ? Dim.Fill () : 1;
  349. line.LineRune = orientation == Orientation.Vertical ?
  350. Driver.VLine : Driver.HLine;
  351. if (orientation == Orientation.Vertical) {
  352. line.X = splitterDistances [i];
  353. line.Y = 0;
  354. }
  355. else {
  356. line.Y = splitterDistances [i];
  357. line.X = 0;
  358. }
  359. }
  360. for (int i = 0; i < tiles.Count; i++) {
  361. var tile = tiles [i];
  362. // TODO: Deal with lines being Visibility false
  363. if (Orientation == Orientation.Vertical) {
  364. tile.View.X = i == 0 ? bounds.X : Pos.Right (splitterLines [i - 1]);
  365. tile.View.Y = bounds.Y;
  366. tile.View.Height = bounds.Height;
  367. tile.View.Width = GetTileWidthOrHeight(i, Bounds.Width);
  368. } else {
  369. tile.View.X = bounds.X;
  370. tile.View.Y = i == 0 ? 0 : Pos.Bottom (splitterLines [i - 1]);
  371. tile.View.Width = bounds.Width;
  372. tile.View.Height = GetTileWidthOrHeight(i, Bounds.Height);
  373. }
  374. }
  375. }
  376. private Dim GetTileWidthOrHeight (int i, int space)
  377. {
  378. // last tile
  379. if(i + 1 >= tiles.Count)
  380. {
  381. return Dim.Fill (HasBorder () ? 1 : 0);
  382. }
  383. var nextSplitter = splitterDistances [i].Anchor (space);
  384. var lastSplitter = i >= 1 ? splitterDistances [i-1].Anchor (space) : 0;
  385. var distance = nextSplitter - lastSplitter;
  386. if(i>0) {
  387. return distance - 1;
  388. }
  389. return distance - (HasBorder() ? 1 : 0);
  390. }
  391. private void RespectMinimumTileSizes ()
  392. {
  393. // if we are not yet initialized then we don't know
  394. // how big we are and therefore cannot sensibly calculate
  395. // how big the views will be with a given SplitterDistance
  396. if (!IsInitialized) {
  397. return;
  398. }
  399. // how much space is there?
  400. var availableSpace = Orientation == Orientation.Horizontal
  401. ? this.Bounds.Height
  402. : this.Bounds.Width;
  403. var fullSpace = availableSpace;
  404. var lastSplitterLocation = 0;
  405. for(int i=0;i< splitterDistances.Count; i++) {
  406. var splitterLocation = splitterDistances [i].Anchor(fullSpace);
  407. var availableLeft = splitterLocation - lastSplitterLocation;
  408. // Border steals space
  409. availableLeft -= HasBorder () && i == 0 ? 1 : 0;
  410. var availableRight = fullSpace - splitterLocation;
  411. // Border steals space
  412. availableRight -= HasBorder () && i == 0 ? 1 : 0;
  413. // Splitter line steals space
  414. availableRight--;
  415. // TODO: Test 3+ panel max/mins because this calculation is probably wrong
  416. var requiredLeft = tiles [i].MinSize;
  417. var requiredRight = tiles [i+1].MinSize;
  418. if (availableLeft < requiredLeft) {
  419. // There is not enough space for panel on left
  420. var insteadTake = requiredLeft + (HasBorder() ? 1 :0);
  421. // Don't take more than the available space in view
  422. insteadTake = Math.Max(0,Math.Min (fullSpace, insteadTake));
  423. splitterDistances [i] = insteadTake;
  424. splitterLocation = insteadTake;
  425. }
  426. else if (availableRight < requiredRight) {
  427. // There is not enough space for panel on right
  428. var insteadTake = fullSpace - (requiredRight + (HasBorder()?1:0));
  429. // leave 1 space for the splitter
  430. insteadTake --;
  431. insteadTake = Math.Max (0, Math.Min (fullSpace, insteadTake));
  432. splitterDistances [i] = insteadTake;
  433. splitterLocation = insteadTake;
  434. }
  435. availableSpace -= splitterLocation;
  436. lastSplitterLocation = splitterLocation;
  437. }
  438. }
  439. private class TileViewLineView : LineView {
  440. public TileView Parent { get; private set; }
  441. public int Idx { get; }
  442. Point? dragPosition;
  443. Pos dragOrignalPos;
  444. public Point? moveRuneRenderLocation;
  445. public TileViewLineView (TileView parent, int idx)
  446. {
  447. CanFocus = true;
  448. TabStop = true;
  449. this.Parent = parent;
  450. Idx = idx;
  451. base.AddCommand (Command.Right, () => {
  452. return MoveSplitter (1, 0);
  453. });
  454. base.AddCommand (Command.Left, () => {
  455. return MoveSplitter (-1, 0);
  456. });
  457. base.AddCommand (Command.LineUp, () => {
  458. return MoveSplitter (0, -1);
  459. });
  460. base.AddCommand (Command.LineDown, () => {
  461. return MoveSplitter (0, 1);
  462. });
  463. AddKeyBinding (Key.CursorRight, Command.Right);
  464. AddKeyBinding (Key.CursorLeft, Command.Left);
  465. AddKeyBinding (Key.CursorUp, Command.LineUp);
  466. AddKeyBinding (Key.CursorDown, Command.LineDown);
  467. }
  468. public override bool ProcessKey (KeyEvent kb)
  469. {
  470. if (!CanFocus || !HasFocus) {
  471. return base.ProcessKey (kb);
  472. }
  473. var result = InvokeKeybindings (kb);
  474. if (result != null)
  475. return (bool)result;
  476. return base.ProcessKey (kb);
  477. }
  478. public override void PositionCursor ()
  479. {
  480. base.PositionCursor ();
  481. var location = moveRuneRenderLocation ??
  482. new Point (Bounds.Width / 2, Bounds.Height / 2);
  483. Move (location.X, location.Y);
  484. }
  485. public override bool OnEnter (View view)
  486. {
  487. Driver.SetCursorVisibility (CursorVisibility.Default);
  488. PositionCursor ();
  489. return base.OnEnter (view);
  490. }
  491. public override void Redraw (Rect bounds)
  492. {
  493. base.Redraw (bounds);
  494. DrawSplitterSymbol ();
  495. }
  496. public void DrawSplitterSymbol ()
  497. {
  498. if (CanFocus && HasFocus) {
  499. var location = moveRuneRenderLocation ??
  500. new Point (Bounds.Width / 2, Bounds.Height / 2);
  501. AddRune (location.X, location.Y, Driver.Diamond);
  502. }
  503. }
  504. public override bool MouseEvent (MouseEvent mouseEvent)
  505. {
  506. if (!CanFocus) {
  507. return true;
  508. }
  509. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  510. // Start a Drag
  511. SetFocus ();
  512. Application.EnsuresTopOnFront ();
  513. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  514. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  515. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  516. Application.GrabMouse (this);
  517. if (Orientation == Orientation.Horizontal) {
  518. } else {
  519. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  520. }
  521. }
  522. return true;
  523. } else if (
  524. dragPosition.HasValue &&
  525. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  526. // Continue Drag
  527. // how far has user dragged from original location?
  528. if (Orientation == Orientation.Horizontal) {
  529. int dy = mouseEvent.Y - dragPosition.Value.Y;
  530. Parent.splitterDistances [Idx] = Offset (Y, dy);
  531. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  532. } else {
  533. int dx = mouseEvent.X - dragPosition.Value.X;
  534. Parent.splitterDistances [Idx] = Offset (X, dx);
  535. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  536. }
  537. Parent.SetNeedsDisplay ();
  538. return true;
  539. }
  540. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  541. // End Drag
  542. Application.UngrabMouse ();
  543. Driver.UncookMouse ();
  544. FinalisePosition (
  545. dragOrignalPos,
  546. Orientation == Orientation.Horizontal ? Y : X);
  547. dragPosition = null;
  548. moveRuneRenderLocation = null;
  549. }
  550. return false;
  551. }
  552. private bool MoveSplitter (int distanceX, int distanceY)
  553. {
  554. if (Orientation == Orientation.Vertical) {
  555. // Cannot move in this direction
  556. if (distanceX == 0) {
  557. return false;
  558. }
  559. var oldX = X;
  560. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  561. return true;
  562. } else {
  563. // Cannot move in this direction
  564. if (distanceY == 0) {
  565. return false;
  566. }
  567. var oldY = Y;
  568. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  569. return true;
  570. }
  571. }
  572. private Pos Offset (Pos pos, int delta)
  573. {
  574. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  575. Parent.Bounds.Height : Parent.Bounds.Width);
  576. return posAbsolute + delta;
  577. }
  578. /// <summary>
  579. /// <para>
  580. /// Moves <see cref="Parent"/> <see cref="TileView.SplitterDistance"/> to
  581. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  582. /// (absolute / relative) that <paramref name="oldValue"/> had.
  583. /// </para>
  584. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  585. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  586. /// </summary>
  587. /// <param name="oldValue"></param>
  588. /// <param name="newValue"></param>
  589. private void FinalisePosition (Pos oldValue, Pos newValue)
  590. {
  591. if (oldValue is Pos.PosFactor) {
  592. if (Orientation == Orientation.Horizontal) {
  593. Parent.SetSplitterPos(Idx, ConvertToPosFactor (newValue, Parent.Bounds.Height));
  594. } else {
  595. Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Width));
  596. }
  597. } else {
  598. Parent.SetSplitterPos (Idx, newValue);
  599. }
  600. }
  601. /// <summary>
  602. /// <para>
  603. /// Determines the absolute position of <paramref name="p"/> and
  604. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  605. /// </para>
  606. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  607. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  608. /// </summary>
  609. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  610. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  611. /// <returns></returns>
  612. private Pos ConvertToPosFactor (Pos p, int parentLength)
  613. {
  614. // calculate position in the 'middle' of the cell at p distance along parentLength
  615. float position = p.Anchor (parentLength) + 0.5f;
  616. return new Pos.PosFactor (position / parentLength);
  617. }
  618. }
  619. private bool HasBorder ()
  620. {
  621. return IntegratedBorder != BorderStyle.None;
  622. }
  623. private bool HasAnyTitles ()
  624. {
  625. return tiles.Any (t => t.Title.Length > 0);
  626. }
  627. private class ChildSplitterLine {
  628. readonly TileViewLineView currentLine;
  629. internal ChildSplitterLine (TileViewLineView currentLine)
  630. {
  631. this.currentLine = currentLine;
  632. }
  633. internal void DrawTitles ()
  634. {
  635. //TODO: Implement this
  636. /*if(currentLine.Orientation == Orientation.Horizontal)
  637. {
  638. var screenRect = currentLine.ViewToScreen (
  639. new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
  640. Driver.DrawWindowTitle (screenRect, currentLine.Parent.View2Title, 0, 0, 0, 0);
  641. }*/
  642. }
  643. }
  644. }
  645. /// <summary>
  646. /// Provides data for <see cref="TileView"/> events.
  647. /// </summary>
  648. public class SplitterEventArgs : EventArgs {
  649. /// <summary>
  650. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  651. /// </summary>
  652. /// <param name="tileView"></param>
  653. /// <param name="splitterDistance"></param>
  654. public SplitterEventArgs (TileView tileView, int idx, Pos splitterDistance)
  655. {
  656. SplitterDistance = splitterDistance;
  657. TileView = tileView;
  658. Idx = idx;
  659. }
  660. /// <summary>
  661. /// New position of the <see cref="TileView.SplitterDistance"/>
  662. /// </summary>
  663. public Pos SplitterDistance { get; }
  664. /// <summary>
  665. /// Container (sender) of the event.
  666. /// </summary>
  667. public TileView TileView { get; }
  668. /// <summary>
  669. /// The splitter that is being moved (use when <see cref="TileView"/>
  670. /// has more than 2 panels).
  671. /// </summary>
  672. public int Idx { get; }
  673. }
  674. /// <summary>
  675. /// Represents a method that will handle splitter events.
  676. /// </summary>
  677. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  678. }