TileView.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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 <see cref="Tiles"/>.
  9. /// </summary>
  10. public class TileView : View {
  11. TileView parentTileView;
  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. /// <summary>
  21. /// A single <see cref="View"/> presented in a <see cref="TileView"/>. To create
  22. /// new instances use <see cref="TileView.RebuildForTileCount(int)"/>
  23. /// or <see cref="TileView.InsertTile(int)"/>.
  24. /// </summary>
  25. public class Tile {
  26. /// <summary>
  27. /// The <see cref="View"/> that is showing in this <see cref="TileView"/>.
  28. /// You should add new child views to this member if you want multiple
  29. /// <see cref="View"/> within the <see cref="Tile"/>.
  30. /// </summary>
  31. public View View { get; internal set; }
  32. /// <summary>
  33. /// Gets or Sets the minimum size you to allow when splitter resizing along
  34. /// parent <see cref="TileView.Orientation"/> direction.
  35. /// </summary>
  36. public int MinSize { get; set; }
  37. /// <summary>
  38. /// The text that should be displayed above the <see cref="View"/>. This will
  39. /// either appear as content above <see cref="View"/> or superimposed over the
  40. /// the parent <see cref="TileView.IntegratedBorder"/> (if it has one).
  41. /// </summary>
  42. public string Title { get; set; }
  43. /// <summary>
  44. /// Creates a new instance of the <see cref="Tile"/> class.
  45. /// </summary>
  46. internal Tile ()
  47. {
  48. View = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
  49. Title = string.Empty;
  50. MinSize = 0;
  51. }
  52. }
  53. List<Tile> tiles;
  54. private List<Pos> splitterDistances;
  55. private List<TileViewLineView> splitterLines;
  56. /// <summary>
  57. /// The sub sections hosted by the view
  58. /// </summary>
  59. public IReadOnlyCollection<Tile> Tiles => tiles.AsReadOnly ();
  60. /// <summary>
  61. /// The splitter locations. Note that there will be N-1 splitters where
  62. /// N is the number of <see cref="Tiles"/>.
  63. /// </summary>
  64. public IReadOnlyCollection<Pos> SplitterDistances => splitterDistances.AsReadOnly ();
  65. private Orientation orientation = Orientation.Vertical;
  66. /// <summary>
  67. /// Creates a new instance of the <see cref="TileView"/> class with
  68. /// 2 tiles (i.e. left and right).
  69. /// </summary>
  70. public TileView () : this (2)
  71. {
  72. }
  73. /// <summary>
  74. /// Creates a new instance of the <see cref="TileView"/> class with
  75. /// <paramref name="tiles"/> number of tiles.
  76. /// </summary>
  77. /// <param name="tiles"></param>
  78. public TileView (int tiles)
  79. {
  80. CanFocus = true;
  81. RebuildForTileCount (tiles);
  82. }
  83. /// <summary>
  84. /// Invoked when any of the <see cref="SplitterDistances"/> is changed.
  85. /// </summary>
  86. public event SplitterEventHandler SplitterMoved;
  87. /// <summary>
  88. /// Raises the <see cref="SplitterMoved"/> event
  89. /// </summary>
  90. protected virtual void OnSplitterMoved (int idx)
  91. {
  92. SplitterMoved?.Invoke (this, new SplitterEventArgs (this, idx, splitterDistances [idx]));
  93. }
  94. /// <summary>
  95. /// Scraps all <see cref="Tiles"/> and creates <paramref name="count"/> new tiles
  96. /// in orientation <see cref="Orientation"/>
  97. /// </summary>
  98. /// <param name="count"></param>
  99. public void RebuildForTileCount (int count)
  100. {
  101. tiles = new List<Tile> ();
  102. // TODO: keep these if growing
  103. splitterDistances = new List<Pos> ();
  104. splitterLines = new List<TileViewLineView> ();
  105. RemoveAll ();
  106. tiles.Clear ();
  107. splitterDistances.Clear ();
  108. if (count == 0) {
  109. return;
  110. }
  111. for (int i = 0; i < count; i++) {
  112. if (i > 0) {
  113. var currentPos = Pos.Percent ((100 / count) * i);
  114. splitterDistances.Add (currentPos);
  115. var line = new TileViewLineView (this, i - 1);
  116. Add (line);
  117. splitterLines.Add (line);
  118. }
  119. var tile = new Tile ();
  120. tiles.Add (tile);
  121. Add (tile.View);
  122. }
  123. LayoutSubviews ();
  124. }
  125. /// <summary>
  126. /// Adds a new <see cref="Tile"/> to the collection at <paramref name="idx"/>.
  127. /// This will also add another splitter line
  128. /// </summary>
  129. /// <param name="idx"></param>
  130. /// <exception cref="NotImplementedException"></exception>
  131. public Tile InsertTile (int idx)
  132. {
  133. var oldTiles = Tiles.ToArray ();
  134. RebuildForTileCount (oldTiles.Length + 1);
  135. Tile toReturn = null;
  136. for(int i=0;i<tiles.Count;i++) {
  137. if(i != idx) {
  138. var oldTile = oldTiles [i > idx ? i - 1 : i];
  139. // remove the new empty View
  140. Remove (tiles [i].View);
  141. // restore old Tile and View
  142. tiles [i] = oldTile;
  143. Add (tiles [i].View);
  144. }
  145. else
  146. {
  147. toReturn = tiles[i];
  148. }
  149. }
  150. SetNeedsDisplay ();
  151. LayoutSubviews ();
  152. return toReturn;
  153. }
  154. /// <summary>
  155. /// Removes a <see cref="Tiles"/> at the provided <paramref name="idx"/> from
  156. /// the view. Returns the removed tile or null if already empty.
  157. /// </summary>
  158. /// <param name="idx"></param>
  159. /// <returns></returns>
  160. public Tile RemoveTile (int idx)
  161. {
  162. var oldTiles = Tiles.ToArray ();
  163. if (idx < 0 || idx >= oldTiles.Length) {
  164. return null;
  165. }
  166. var removed = Tiles.ElementAt (idx);
  167. RebuildForTileCount (oldTiles.Length - 1);
  168. for (int i = 0; i < tiles.Count; i++) {
  169. int oldIdx = i >= idx ? i + 1: i;
  170. var oldTile = oldTiles [oldIdx];
  171. // remove the new empty View
  172. Remove (tiles [i].View);
  173. // restore old Tile and View
  174. tiles [i] = oldTile;
  175. Add (tiles [i].View);
  176. }
  177. SetNeedsDisplay ();
  178. LayoutSubviews ();
  179. return removed;
  180. }
  181. ///<summary>
  182. /// Returns the index of the first <see cref="Tile"/> in
  183. /// <see cref="Tiles"/> which contains <paramref name="view"/>.
  184. ///</summary>
  185. public int IndexOf(View view)
  186. {
  187. // TODO: Could be recursive (i.e. search nested Subviews)
  188. return tiles.IndexOf((t)=>t.View == view || t.View.Subviews.Contains(view));
  189. }
  190. /// <summary>
  191. /// Orientation of the dividing line (Horizontal or Vertical).
  192. /// </summary>
  193. public Orientation Orientation {
  194. get { return orientation; }
  195. set {
  196. orientation = value;
  197. LayoutSubviews ();
  198. }
  199. }
  200. /// <inheritdoc/>
  201. public override void LayoutSubviews ()
  202. {
  203. var contentArea = Bounds;
  204. if (HasBorder ()) {
  205. // TODO: Bound with Max/Min
  206. contentArea = new Rect (
  207. contentArea.X + 1,
  208. contentArea.Y + 1,
  209. Math.Max (0, contentArea.Width - 2),
  210. Math.Max (0, contentArea.Height - 2));
  211. }
  212. Setup (contentArea);
  213. base.LayoutSubviews ();
  214. }
  215. /// <summary>
  216. /// <para>Distance Horizontally or Vertically to the splitter line when
  217. /// neither view is collapsed.
  218. /// </para>
  219. /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
  220. /// are supported for this property.</para>
  221. /// </summary>
  222. public void SetSplitterPos (int idx, Pos value)
  223. {
  224. if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
  225. throw new ArgumentException ($"Only Percent and Absolute values are supported. Passed value was {value.GetType ().Name}");
  226. }
  227. splitterDistances [idx] = value;
  228. GetRootTileView ().LayoutSubviews ();
  229. OnSplitterMoved (idx);
  230. }
  231. /// <inheritdoc/>
  232. public override bool OnEnter (View view)
  233. {
  234. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  235. return base.OnEnter (view);
  236. }
  237. /// <inheritdoc/>
  238. public override void Redraw (Rect bounds)
  239. {
  240. Driver.SetAttribute (ColorScheme.Normal);
  241. Clear ();
  242. base.Redraw (bounds);
  243. var lc = new LineCanvas ();
  244. var allLines = GetAllLineViewsRecursively (this);
  245. var allTitlesToRender = GetAllTitlesToRenderRecursively(this);
  246. if (IsRootTileView ()) {
  247. if (HasBorder ()) {
  248. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, IntegratedBorder);
  249. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, IntegratedBorder);
  250. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, IntegratedBorder);
  251. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
  252. }
  253. foreach (var line in allLines.Where (l => l.Visible)) {
  254. bool isRoot = splitterLines.Contains (line);
  255. line.ViewToScreen (0, 0, out var x1, out var y1);
  256. var origin = ScreenToView (x1, y1);
  257. var length = line.Orientation == Orientation.Horizontal ?
  258. line.Frame.Width - 1 :
  259. line.Frame.Height - 1;
  260. if (!isRoot) {
  261. if (line.Orientation == Orientation.Horizontal) {
  262. origin.X -= 1;
  263. } else {
  264. origin.Y -= 1;
  265. }
  266. length += 2;
  267. }
  268. lc.AddLine (origin, length, line.Orientation, IntegratedBorder);
  269. }
  270. }
  271. Driver.SetAttribute (ColorScheme.Normal);
  272. lc.Draw (this, bounds);
  273. // Redraw the lines so that focus/drag symbol renders
  274. foreach (var line in allLines) {
  275. line.DrawSplitterSymbol ();
  276. }
  277. // Draw Titles over Border
  278. foreach(var titleToRender in allTitlesToRender)
  279. {
  280. var renderAt = titleToRender.GetLocalCoordinateForTitle(this);
  281. if(renderAt.Y < 0)
  282. {
  283. // If we have no border then root level tiles
  284. // have nowhere to render their titles.
  285. continue;
  286. }
  287. // TODO: Render with focus color if focused
  288. var title = titleToRender.Tile.Title;
  289. for(int i=0;i<title.Length;i++)
  290. {
  291. AddRune(renderAt.X+i,renderAt.Y,title[i]);
  292. }
  293. }
  294. }
  295. /// <summary>
  296. /// Converts <see cref="Tile.View"/> of <see cref="Tiles"/> element <paramref name="idx"/>
  297. /// from a regular <see cref="View"/> to a new nested <see cref="TileView"/> with <paramref name="panels"/>
  298. /// number of panels. Returns false if the element already contains a nested view.
  299. /// </summary>
  300. /// <remarks>After successful splitting, the <paramref name="result"/> <see cref="TileView"/>
  301. /// will contain the previous (replaced) <see cref="Tile.View"/> at element 0.</remarks>
  302. /// <param name="idx">The element of <see cref="Tiles"/> that is to be subdivided.</param>
  303. /// <param name="panels">The number of panels that the <see cref="Tile"/> should be split into</param>
  304. /// <param name="result">The new nested <see cref="TileView"/>.</param>
  305. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  306. /// <see cref="TileView"/>. <see langword="false"/> if it was already a nested
  307. /// <see cref="TileView"/></returns>
  308. public bool TrySplitTile(int idx, int panels, out TileView result)
  309. {
  310. // when splitting a view into 2 sub views we will need to migrate
  311. // the title too
  312. var tile = tiles [idx];
  313. // TODO: migrate the title too right?
  314. var title = tile.Title;
  315. View toMove = tile.View;
  316. if (toMove is TileView existing) {
  317. result = existing;
  318. return false;
  319. }
  320. var newContainer = new TileView(panels) {
  321. Width = Dim.Fill (),
  322. Height = Dim.Fill (),
  323. parentTileView = this,
  324. };
  325. // Take everything out of the View we are moving
  326. var childViews = toMove.Subviews.ToArray();
  327. toMove.RemoveAll ();
  328. // Remove the view itself and replace it with the new TileView
  329. Remove (toMove);
  330. Add (newContainer);
  331. tile.View = newContainer;
  332. var newTileView1 = newContainer.tiles [0].View;
  333. // Add the original content into the first view of the new container
  334. foreach (var childView in childViews) {
  335. newTileView1.Add (childView);
  336. }
  337. result = newContainer;
  338. return true;
  339. }
  340. private List<TileViewLineView> GetAllLineViewsRecursively (View v)
  341. {
  342. var lines = new List<TileViewLineView> ();
  343. foreach (var sub in v.Subviews) {
  344. if (sub is TileViewLineView s) {
  345. if (s.Parent.GetRootTileView () == this) {
  346. lines.Add (s);
  347. }
  348. } else {
  349. lines.AddRange (GetAllLineViewsRecursively (sub));
  350. }
  351. }
  352. return lines;
  353. }
  354. private List<TileTitleToRender> GetAllTitlesToRenderRecursively (TileView v, int depth = 0)
  355. {
  356. var titles = new List<TileTitleToRender> ();
  357. foreach (var sub in v.Tiles) {
  358. // Don't render titles for invisible stuff!
  359. if(!sub.View.Visible)
  360. {
  361. continue;
  362. }
  363. if(sub.View is TileView subTileView)
  364. {
  365. // Panels with sub split tiles in them can never
  366. // have their Titles rendered. Instead we dive in
  367. // and pull up their children as titles
  368. titles.AddRange (GetAllTitlesToRenderRecursively (subTileView,depth+1));
  369. }
  370. else
  371. {
  372. if(sub.Title.Length > 0)
  373. {
  374. titles.Add(new TileTitleToRender(sub,depth));
  375. }
  376. }
  377. }
  378. return titles;
  379. }
  380. /// <summary>
  381. /// <para>
  382. /// <see langword="true"/> if <see cref="TileView"/> is nested within a parent <see cref="TileView"/>
  383. /// e.g. via the <see cref="TrySplitTile"/>. <see langword="false"/> if it is a root level <see cref="TileView"/>.
  384. /// </para>
  385. /// </summary>
  386. /// <remarks>Note that manually adding one <see cref="TileView"/> to another will not result in a parent/child
  387. /// relationship and both will still be considered 'root' containers. Always use
  388. /// <see cref="TrySplitTile(int, int, out TileView)"/> if you want to subdivide a <see cref="TileView"/>.</remarks>
  389. /// <returns></returns>
  390. public bool IsRootTileView ()
  391. {
  392. // TODO: don't want to layout subviews since the parent recursively lays them all out
  393. return parentTileView == null;
  394. }
  395. /// <summary>
  396. /// Returns the immediate parent <see cref="TileView"/> of this. Note that in case
  397. /// of deep nesting this might not be the root <see cref="TileView"/>. Returns null
  398. /// if this instance is not a nested child (created with
  399. /// <see cref="TrySplitTile(int, int, out TileView)"/>)
  400. /// </summary>
  401. /// <remarks>
  402. /// Use <see cref="IsRootTileView"/> to determine if the returned value is the root.
  403. /// </remarks>
  404. /// <returns></returns>
  405. public TileView GetParentTileView ()
  406. {
  407. return this.parentTileView;
  408. }
  409. private TileView GetRootTileView ()
  410. {
  411. TileView root = this;
  412. while (root.parentTileView != null) {
  413. root = root.parentTileView;
  414. }
  415. return root;
  416. }
  417. private void Setup (Rect bounds)
  418. {
  419. if (bounds.IsEmpty) {
  420. return;
  421. }
  422. RespectMinimumTileSizes ();
  423. for (int i = 0; i < splitterLines.Count; i++) {
  424. var line = splitterLines[i];
  425. line.Orientation = Orientation;
  426. line.Width = orientation == Orientation.Vertical
  427. ? 1 : Dim.Fill ();
  428. line.Height = orientation == Orientation.Vertical
  429. ? Dim.Fill () : 1;
  430. line.LineRune = orientation == Orientation.Vertical ?
  431. Driver.VLine : Driver.HLine;
  432. if (orientation == Orientation.Vertical) {
  433. line.X = splitterDistances [i];
  434. line.Y = 0;
  435. }
  436. else {
  437. line.Y = splitterDistances [i];
  438. line.X = 0;
  439. }
  440. }
  441. for (int i = 0; i < tiles.Count; i++) {
  442. var tile = tiles [i];
  443. // TODO: Deal with lines being Visibility false
  444. if (Orientation == Orientation.Vertical) {
  445. tile.View.X = i == 0 ? bounds.X : Pos.Right (splitterLines [i - 1]);
  446. tile.View.Y = bounds.Y;
  447. tile.View.Height = bounds.Height;
  448. tile.View.Width = GetTileWidthOrHeight(i, Bounds.Width);
  449. } else {
  450. tile.View.X = bounds.X;
  451. tile.View.Y = i == 0 ? 0 : Pos.Bottom (splitterLines [i - 1]);
  452. tile.View.Width = bounds.Width;
  453. tile.View.Height = GetTileWidthOrHeight(i, Bounds.Height);
  454. }
  455. }
  456. }
  457. private Dim GetTileWidthOrHeight (int i, int space)
  458. {
  459. // last tile
  460. if(i + 1 >= tiles.Count)
  461. {
  462. return Dim.Fill (HasBorder () ? 1 : 0);
  463. }
  464. var nextSplitter = splitterDistances [i].Anchor (space);
  465. var lastSplitter = i >= 1 ? splitterDistances [i-1].Anchor (space) : 0;
  466. var distance = nextSplitter - lastSplitter;
  467. if(i>0) {
  468. return distance - 1;
  469. }
  470. return distance - (HasBorder() ? 1 : 0);
  471. }
  472. private void RespectMinimumTileSizes ()
  473. {
  474. // if we are not yet initialized then we don't know
  475. // how big we are and therefore cannot sensibly calculate
  476. // how big the views will be with a given SplitterDistance
  477. if (!IsInitialized) {
  478. return;
  479. }
  480. // how much space is there?
  481. var availableSpace = Orientation == Orientation.Horizontal
  482. ? this.Bounds.Height
  483. : this.Bounds.Width;
  484. var fullSpace = availableSpace;
  485. var lastSplitterLocation = 0;
  486. for(int i=0;i< splitterDistances.Count; i++) {
  487. var splitterLocation = splitterDistances [i].Anchor(fullSpace);
  488. var availableLeft = splitterLocation - lastSplitterLocation;
  489. // Border steals space
  490. availableLeft -= HasBorder () && i == 0 ? 1 : 0;
  491. var availableRight = fullSpace - splitterLocation;
  492. // Border steals space
  493. availableRight -= HasBorder () && i == 0 ? 1 : 0;
  494. // Splitter line steals space
  495. availableRight--;
  496. // TODO: Test 3+ panel max/mins because this calculation is probably wrong
  497. var requiredLeft = tiles [i].MinSize;
  498. var requiredRight = tiles [i+1].MinSize;
  499. if (availableLeft < requiredLeft) {
  500. // There is not enough space for panel on left
  501. var insteadTake = requiredLeft + (HasBorder() ? 1 :0);
  502. // Don't take more than the available space in view
  503. insteadTake = Math.Max(0,Math.Min (fullSpace, insteadTake));
  504. splitterDistances [i] = insteadTake;
  505. splitterLocation = insteadTake;
  506. }
  507. else if (availableRight < requiredRight) {
  508. // There is not enough space for panel on right
  509. var insteadTake = fullSpace - (requiredRight + (HasBorder()?1:0));
  510. // leave 1 space for the splitter
  511. insteadTake --;
  512. insteadTake = Math.Max (0, Math.Min (fullSpace, insteadTake));
  513. splitterDistances [i] = insteadTake;
  514. splitterLocation = insteadTake;
  515. }
  516. availableSpace -= splitterLocation;
  517. lastSplitterLocation = splitterLocation;
  518. }
  519. }
  520. private class TileTitleToRender
  521. {
  522. public Tile Tile {get;}
  523. public int Depth {get;}
  524. public TileTitleToRender(Tile tile, int depth)
  525. {
  526. Tile = tile;
  527. Depth = depth;
  528. }
  529. /// <summary>
  530. /// Translates the <see cref="Tile"/> title location from its local
  531. /// coordinate space <paramref name="intoCoordinateSpace"/>.
  532. /// </summary>
  533. public Point GetLocalCoordinateForTitle(TileView intoCoordinateSpace)
  534. {
  535. Tile.View.ViewToScreen(0,0, out var screenCol, out var screenRow);
  536. screenRow--;
  537. return intoCoordinateSpace.ScreenToView(screenCol,screenRow);
  538. }
  539. }
  540. private class TileViewLineView : LineView {
  541. public TileView Parent { get; private set; }
  542. public int Idx { get; }
  543. Point? dragPosition;
  544. Pos dragOrignalPos;
  545. public Point? moveRuneRenderLocation;
  546. public TileViewLineView (TileView parent, int idx)
  547. {
  548. CanFocus = true;
  549. TabStop = true;
  550. this.Parent = parent;
  551. Idx = idx;
  552. base.AddCommand (Command.Right, () => {
  553. return MoveSplitter (1, 0);
  554. });
  555. base.AddCommand (Command.Left, () => {
  556. return MoveSplitter (-1, 0);
  557. });
  558. base.AddCommand (Command.LineUp, () => {
  559. return MoveSplitter (0, -1);
  560. });
  561. base.AddCommand (Command.LineDown, () => {
  562. return MoveSplitter (0, 1);
  563. });
  564. AddKeyBinding (Key.CursorRight, Command.Right);
  565. AddKeyBinding (Key.CursorLeft, Command.Left);
  566. AddKeyBinding (Key.CursorUp, Command.LineUp);
  567. AddKeyBinding (Key.CursorDown, Command.LineDown);
  568. }
  569. public override bool ProcessKey (KeyEvent kb)
  570. {
  571. if (!CanFocus || !HasFocus) {
  572. return base.ProcessKey (kb);
  573. }
  574. var result = InvokeKeybindings (kb);
  575. if (result != null)
  576. return (bool)result;
  577. return base.ProcessKey (kb);
  578. }
  579. public override void PositionCursor ()
  580. {
  581. base.PositionCursor ();
  582. var location = moveRuneRenderLocation ??
  583. new Point (Bounds.Width / 2, Bounds.Height / 2);
  584. Move (location.X, location.Y);
  585. }
  586. public override bool OnEnter (View view)
  587. {
  588. Driver.SetCursorVisibility (CursorVisibility.Default);
  589. PositionCursor ();
  590. return base.OnEnter (view);
  591. }
  592. public override void Redraw (Rect bounds)
  593. {
  594. base.Redraw (bounds);
  595. DrawSplitterSymbol ();
  596. }
  597. public void DrawSplitterSymbol ()
  598. {
  599. if (CanFocus && HasFocus) {
  600. var location = moveRuneRenderLocation ??
  601. new Point (Bounds.Width / 2, Bounds.Height / 2);
  602. AddRune (location.X, location.Y, Driver.Diamond);
  603. }
  604. }
  605. public override bool MouseEvent (MouseEvent mouseEvent)
  606. {
  607. if (!CanFocus) {
  608. return true;
  609. }
  610. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  611. // Start a Drag
  612. SetFocus ();
  613. Application.EnsuresTopOnFront ();
  614. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  615. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  616. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  617. Application.GrabMouse (this);
  618. if (Orientation == Orientation.Horizontal) {
  619. } else {
  620. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  621. }
  622. }
  623. return true;
  624. } else if (
  625. dragPosition.HasValue &&
  626. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  627. // Continue Drag
  628. // how far has user dragged from original location?
  629. if (Orientation == Orientation.Horizontal) {
  630. int dy = mouseEvent.Y - dragPosition.Value.Y;
  631. Parent.splitterDistances [Idx] = Offset (Y, dy);
  632. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  633. } else {
  634. int dx = mouseEvent.X - dragPosition.Value.X;
  635. Parent.splitterDistances [Idx] = Offset (X, dx);
  636. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  637. }
  638. Parent.SetNeedsDisplay ();
  639. return true;
  640. }
  641. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  642. // End Drag
  643. Application.UngrabMouse ();
  644. Driver.UncookMouse ();
  645. FinalisePosition (
  646. dragOrignalPos,
  647. Orientation == Orientation.Horizontal ? Y : X);
  648. dragPosition = null;
  649. moveRuneRenderLocation = null;
  650. }
  651. return false;
  652. }
  653. private bool MoveSplitter (int distanceX, int distanceY)
  654. {
  655. if (Orientation == Orientation.Vertical) {
  656. // Cannot move in this direction
  657. if (distanceX == 0) {
  658. return false;
  659. }
  660. var oldX = X;
  661. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  662. return true;
  663. } else {
  664. // Cannot move in this direction
  665. if (distanceY == 0) {
  666. return false;
  667. }
  668. var oldY = Y;
  669. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  670. return true;
  671. }
  672. }
  673. private Pos Offset (Pos pos, int delta)
  674. {
  675. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  676. Parent.Bounds.Height : Parent.Bounds.Width);
  677. return posAbsolute + delta;
  678. }
  679. /// <summary>
  680. /// <para>
  681. /// Moves <see cref="Parent"/> <see cref="TileView.SplitterDistances"/> to
  682. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  683. /// (absolute / relative) that <paramref name="oldValue"/> had.
  684. /// </para>
  685. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  686. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  687. /// </summary>
  688. /// <param name="oldValue"></param>
  689. /// <param name="newValue"></param>
  690. private void FinalisePosition (Pos oldValue, Pos newValue)
  691. {
  692. if (oldValue is Pos.PosFactor) {
  693. if (Orientation == Orientation.Horizontal) {
  694. Parent.SetSplitterPos(Idx, ConvertToPosFactor (newValue, Parent.Bounds.Height));
  695. } else {
  696. Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Width));
  697. }
  698. } else {
  699. Parent.SetSplitterPos (Idx, newValue);
  700. }
  701. }
  702. /// <summary>
  703. /// <para>
  704. /// Determines the absolute position of <paramref name="p"/> and
  705. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  706. /// </para>
  707. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  708. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  709. /// </summary>
  710. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  711. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  712. /// <returns></returns>
  713. private Pos ConvertToPosFactor (Pos p, int parentLength)
  714. {
  715. // calculate position in the 'middle' of the cell at p distance along parentLength
  716. float position = p.Anchor (parentLength) + 0.5f;
  717. return new Pos.PosFactor (position / parentLength);
  718. }
  719. }
  720. private bool HasBorder ()
  721. {
  722. return IntegratedBorder != BorderStyle.None;
  723. }
  724. private bool HasAnyTitles ()
  725. {
  726. return tiles.Any (t => t.Title.Length > 0);
  727. }
  728. private class ChildSplitterLine {
  729. readonly TileViewLineView currentLine;
  730. internal ChildSplitterLine (TileViewLineView currentLine)
  731. {
  732. this.currentLine = currentLine;
  733. }
  734. internal void DrawTitles ()
  735. {
  736. //TODO: Implement this
  737. /*if(currentLine.Orientation == Orientation.Horizontal)
  738. {
  739. var screenRect = currentLine.ViewToScreen (
  740. new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
  741. Driver.DrawWindowTitle (screenRect, currentLine.Parent.View2Title, 0, 0, 0, 0);
  742. }*/
  743. }
  744. }
  745. }
  746. /// <summary>
  747. /// Provides data for <see cref="TileView"/> events.
  748. /// </summary>
  749. public class SplitterEventArgs : EventArgs {
  750. /// <summary>
  751. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  752. /// </summary>
  753. /// <param name="tileView"><see cref="TileView"/> in which splitter is being moved.</param>
  754. /// <param name="idx">Index of the splitter being moved in <see cref="TileView.SplitterDistances"/>.</param>
  755. /// <param name="splitterDistance">The new <see cref="Pos"/> of the splitter line.</param>
  756. public SplitterEventArgs (TileView tileView, int idx, Pos splitterDistance)
  757. {
  758. SplitterDistance = splitterDistance;
  759. TileView = tileView;
  760. Idx = idx;
  761. }
  762. /// <summary>
  763. /// New position of the splitter line (see <see cref="TileView.SplitterDistances"/>).
  764. /// </summary>
  765. public Pos SplitterDistance { get; }
  766. /// <summary>
  767. /// Container (sender) of the event.
  768. /// </summary>
  769. public TileView TileView { get; }
  770. /// <summary>
  771. /// Gets the index of the splitter that is being moved. This can be
  772. /// used to index <see cref="TileView.SplitterDistances"/>
  773. /// </summary>
  774. public int Idx { get; }
  775. }
  776. /// <summary>
  777. /// Represents a method that will handle splitter events.
  778. /// </summary>
  779. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  780. }