SplitView.cs 22 KB

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