TileView.cs 23 KB

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