SplitView.cs 22 KB

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