SplitView.cs 22 KB

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