TileView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 TrySplitTile(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. /// <summary>
  324. /// <para>
  325. /// <see langword="true"/> if <see cref="TileView"/> is nested within a parent <see cref="TileView"/>
  326. /// e.g. via the <see cref="TrySplitTile"/>. <see langword="false"/> if it is a root level <see cref="TileView"/>.
  327. /// </para>
  328. /// </summary>
  329. /// <remarks>Note that manually adding one <see cref="TileView"/> to another will not result in a parent/child
  330. /// relationship and both will still be considered 'root' containers. Always use
  331. /// <see cref="TrySplitTile(int, int, out TileView)"/> if you want to subdivide a <see cref="TileView"/>.</remarks>
  332. /// <returns></returns>
  333. public bool IsRootTileView ()
  334. {
  335. // TODO: don't want to layout subviews since the parent recursively lays them all out
  336. return parentTileView == null;
  337. }
  338. public TileView GetParentTileView ()
  339. {
  340. return this.parentTileView;
  341. }
  342. private TileView GetRootTileView ()
  343. {
  344. TileView root = this;
  345. while (root.parentTileView != null) {
  346. root = root.parentTileView;
  347. }
  348. return root;
  349. }
  350. private void Setup (Rect bounds)
  351. {
  352. if (bounds.IsEmpty) {
  353. return;
  354. }
  355. RespectMinimumTileSizes ();
  356. for (int i = 0; i < splitterLines.Count; i++) {
  357. var line = splitterLines[i];
  358. line.Orientation = Orientation;
  359. line.Width = orientation == Orientation.Vertical
  360. ? 1 : Dim.Fill ();
  361. line.Height = orientation == Orientation.Vertical
  362. ? Dim.Fill () : 1;
  363. line.LineRune = orientation == Orientation.Vertical ?
  364. Driver.VLine : Driver.HLine;
  365. if (orientation == Orientation.Vertical) {
  366. line.X = splitterDistances [i];
  367. line.Y = 0;
  368. }
  369. else {
  370. line.Y = splitterDistances [i];
  371. line.X = 0;
  372. }
  373. }
  374. for (int i = 0; i < tiles.Count; i++) {
  375. var tile = tiles [i];
  376. // TODO: Deal with lines being Visibility false
  377. if (Orientation == Orientation.Vertical) {
  378. tile.View.X = i == 0 ? bounds.X : Pos.Right (splitterLines [i - 1]);
  379. tile.View.Y = bounds.Y;
  380. tile.View.Height = bounds.Height;
  381. tile.View.Width = GetTileWidthOrHeight(i, Bounds.Width);
  382. } else {
  383. tile.View.X = bounds.X;
  384. tile.View.Y = i == 0 ? 0 : Pos.Bottom (splitterLines [i - 1]);
  385. tile.View.Width = bounds.Width;
  386. tile.View.Height = GetTileWidthOrHeight(i, Bounds.Height);
  387. }
  388. }
  389. }
  390. private Dim GetTileWidthOrHeight (int i, int space)
  391. {
  392. // last tile
  393. if(i + 1 >= tiles.Count)
  394. {
  395. return Dim.Fill (HasBorder () ? 1 : 0);
  396. }
  397. var nextSplitter = splitterDistances [i].Anchor (space);
  398. var lastSplitter = i >= 1 ? splitterDistances [i-1].Anchor (space) : 0;
  399. var distance = nextSplitter - lastSplitter;
  400. if(i>0) {
  401. return distance - 1;
  402. }
  403. return distance - (HasBorder() ? 1 : 0);
  404. }
  405. private void RespectMinimumTileSizes ()
  406. {
  407. // if we are not yet initialized then we don't know
  408. // how big we are and therefore cannot sensibly calculate
  409. // how big the views will be with a given SplitterDistance
  410. if (!IsInitialized) {
  411. return;
  412. }
  413. // how much space is there?
  414. var availableSpace = Orientation == Orientation.Horizontal
  415. ? this.Bounds.Height
  416. : this.Bounds.Width;
  417. var fullSpace = availableSpace;
  418. var lastSplitterLocation = 0;
  419. for(int i=0;i< splitterDistances.Count; i++) {
  420. var splitterLocation = splitterDistances [i].Anchor(fullSpace);
  421. var availableLeft = splitterLocation - lastSplitterLocation;
  422. // Border steals space
  423. availableLeft -= HasBorder () && i == 0 ? 1 : 0;
  424. var availableRight = fullSpace - splitterLocation;
  425. // Border steals space
  426. availableRight -= HasBorder () && i == 0 ? 1 : 0;
  427. // Splitter line steals space
  428. availableRight--;
  429. // TODO: Test 3+ panel max/mins because this calculation is probably wrong
  430. var requiredLeft = tiles [i].MinSize;
  431. var requiredRight = tiles [i+1].MinSize;
  432. if (availableLeft < requiredLeft) {
  433. // There is not enough space for panel on left
  434. var insteadTake = requiredLeft + (HasBorder() ? 1 :0);
  435. // Don't take more than the available space in view
  436. insteadTake = Math.Max(0,Math.Min (fullSpace, insteadTake));
  437. splitterDistances [i] = insteadTake;
  438. splitterLocation = insteadTake;
  439. }
  440. else if (availableRight < requiredRight) {
  441. // There is not enough space for panel on right
  442. var insteadTake = fullSpace - (requiredRight + (HasBorder()?1:0));
  443. // leave 1 space for the splitter
  444. insteadTake --;
  445. insteadTake = Math.Max (0, Math.Min (fullSpace, insteadTake));
  446. splitterDistances [i] = insteadTake;
  447. splitterLocation = insteadTake;
  448. }
  449. availableSpace -= splitterLocation;
  450. lastSplitterLocation = splitterLocation;
  451. }
  452. }
  453. private class TileViewLineView : LineView {
  454. public TileView Parent { get; private set; }
  455. public int Idx { get; }
  456. Point? dragPosition;
  457. Pos dragOrignalPos;
  458. public Point? moveRuneRenderLocation;
  459. public TileViewLineView (TileView parent, int idx)
  460. {
  461. CanFocus = true;
  462. TabStop = true;
  463. this.Parent = parent;
  464. Idx = idx;
  465. base.AddCommand (Command.Right, () => {
  466. return MoveSplitter (1, 0);
  467. });
  468. base.AddCommand (Command.Left, () => {
  469. return MoveSplitter (-1, 0);
  470. });
  471. base.AddCommand (Command.LineUp, () => {
  472. return MoveSplitter (0, -1);
  473. });
  474. base.AddCommand (Command.LineDown, () => {
  475. return MoveSplitter (0, 1);
  476. });
  477. AddKeyBinding (Key.CursorRight, Command.Right);
  478. AddKeyBinding (Key.CursorLeft, Command.Left);
  479. AddKeyBinding (Key.CursorUp, Command.LineUp);
  480. AddKeyBinding (Key.CursorDown, Command.LineDown);
  481. }
  482. public override bool ProcessKey (KeyEvent kb)
  483. {
  484. if (!CanFocus || !HasFocus) {
  485. return base.ProcessKey (kb);
  486. }
  487. var result = InvokeKeybindings (kb);
  488. if (result != null)
  489. return (bool)result;
  490. return base.ProcessKey (kb);
  491. }
  492. public override void PositionCursor ()
  493. {
  494. base.PositionCursor ();
  495. var location = moveRuneRenderLocation ??
  496. new Point (Bounds.Width / 2, Bounds.Height / 2);
  497. Move (location.X, location.Y);
  498. }
  499. public override bool OnEnter (View view)
  500. {
  501. Driver.SetCursorVisibility (CursorVisibility.Default);
  502. PositionCursor ();
  503. return base.OnEnter (view);
  504. }
  505. public override void Redraw (Rect bounds)
  506. {
  507. base.Redraw (bounds);
  508. DrawSplitterSymbol ();
  509. }
  510. public void DrawSplitterSymbol ()
  511. {
  512. if (CanFocus && HasFocus) {
  513. var location = moveRuneRenderLocation ??
  514. new Point (Bounds.Width / 2, Bounds.Height / 2);
  515. AddRune (location.X, location.Y, Driver.Diamond);
  516. }
  517. }
  518. public override bool MouseEvent (MouseEvent mouseEvent)
  519. {
  520. if (!CanFocus) {
  521. return true;
  522. }
  523. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  524. // Start a Drag
  525. SetFocus ();
  526. Application.EnsuresTopOnFront ();
  527. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  528. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  529. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  530. Application.GrabMouse (this);
  531. if (Orientation == Orientation.Horizontal) {
  532. } else {
  533. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  534. }
  535. }
  536. return true;
  537. } else if (
  538. dragPosition.HasValue &&
  539. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  540. // Continue Drag
  541. // how far has user dragged from original location?
  542. if (Orientation == Orientation.Horizontal) {
  543. int dy = mouseEvent.Y - dragPosition.Value.Y;
  544. Parent.splitterDistances [Idx] = Offset (Y, dy);
  545. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  546. } else {
  547. int dx = mouseEvent.X - dragPosition.Value.X;
  548. Parent.splitterDistances [Idx] = Offset (X, dx);
  549. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  550. }
  551. Parent.SetNeedsDisplay ();
  552. return true;
  553. }
  554. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  555. // End Drag
  556. Application.UngrabMouse ();
  557. Driver.UncookMouse ();
  558. FinalisePosition (
  559. dragOrignalPos,
  560. Orientation == Orientation.Horizontal ? Y : X);
  561. dragPosition = null;
  562. moveRuneRenderLocation = null;
  563. }
  564. return false;
  565. }
  566. private bool MoveSplitter (int distanceX, int distanceY)
  567. {
  568. if (Orientation == Orientation.Vertical) {
  569. // Cannot move in this direction
  570. if (distanceX == 0) {
  571. return false;
  572. }
  573. var oldX = X;
  574. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  575. return true;
  576. } else {
  577. // Cannot move in this direction
  578. if (distanceY == 0) {
  579. return false;
  580. }
  581. var oldY = Y;
  582. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  583. return true;
  584. }
  585. }
  586. private Pos Offset (Pos pos, int delta)
  587. {
  588. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  589. Parent.Bounds.Height : Parent.Bounds.Width);
  590. return posAbsolute + delta;
  591. }
  592. /// <summary>
  593. /// <para>
  594. /// Moves <see cref="Parent"/> <see cref="TileView.SplitterDistance"/> to
  595. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  596. /// (absolute / relative) that <paramref name="oldValue"/> had.
  597. /// </para>
  598. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  599. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  600. /// </summary>
  601. /// <param name="oldValue"></param>
  602. /// <param name="newValue"></param>
  603. private void FinalisePosition (Pos oldValue, Pos newValue)
  604. {
  605. if (oldValue is Pos.PosFactor) {
  606. if (Orientation == Orientation.Horizontal) {
  607. Parent.SetSplitterPos(Idx, ConvertToPosFactor (newValue, Parent.Bounds.Height));
  608. } else {
  609. Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Width));
  610. }
  611. } else {
  612. Parent.SetSplitterPos (Idx, newValue);
  613. }
  614. }
  615. /// <summary>
  616. /// <para>
  617. /// Determines the absolute position of <paramref name="p"/> and
  618. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  619. /// </para>
  620. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  621. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  622. /// </summary>
  623. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  624. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  625. /// <returns></returns>
  626. private Pos ConvertToPosFactor (Pos p, int parentLength)
  627. {
  628. // calculate position in the 'middle' of the cell at p distance along parentLength
  629. float position = p.Anchor (parentLength) + 0.5f;
  630. return new Pos.PosFactor (position / parentLength);
  631. }
  632. }
  633. private bool HasBorder ()
  634. {
  635. return IntegratedBorder != BorderStyle.None;
  636. }
  637. private bool HasAnyTitles ()
  638. {
  639. return tiles.Any (t => t.Title.Length > 0);
  640. }
  641. private class ChildSplitterLine {
  642. readonly TileViewLineView currentLine;
  643. internal ChildSplitterLine (TileViewLineView currentLine)
  644. {
  645. this.currentLine = currentLine;
  646. }
  647. internal void DrawTitles ()
  648. {
  649. //TODO: Implement this
  650. /*if(currentLine.Orientation == Orientation.Horizontal)
  651. {
  652. var screenRect = currentLine.ViewToScreen (
  653. new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
  654. Driver.DrawWindowTitle (screenRect, currentLine.Parent.View2Title, 0, 0, 0, 0);
  655. }*/
  656. }
  657. }
  658. }
  659. /// <summary>
  660. /// Provides data for <see cref="TileView"/> events.
  661. /// </summary>
  662. public class SplitterEventArgs : EventArgs {
  663. /// <summary>
  664. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  665. /// </summary>
  666. /// <param name="tileView"></param>
  667. /// <param name="splitterDistance"></param>
  668. public SplitterEventArgs (TileView tileView, int idx, Pos splitterDistance)
  669. {
  670. SplitterDistance = splitterDistance;
  671. TileView = tileView;
  672. Idx = idx;
  673. }
  674. /// <summary>
  675. /// New position of the <see cref="TileView.SplitterDistance"/>
  676. /// </summary>
  677. public Pos SplitterDistance { get; }
  678. /// <summary>
  679. /// Container (sender) of the event.
  680. /// </summary>
  681. public TileView TileView { get; }
  682. /// <summary>
  683. /// The splitter that is being moved (use when <see cref="TileView"/>
  684. /// has more than 2 panels).
  685. /// </summary>
  686. public int Idx { get; }
  687. }
  688. /// <summary>
  689. /// Represents a method that will handle splitter events.
  690. /// </summary>
  691. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  692. }