SplitView.cs 21 KB

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