SplitView.cs 22 KB

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