TileView.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. } else if (HasAnyTitles () && IsRootTileView ()) {
  212. // TODO: Bound with Max/Min
  213. contentArea = new Rect (
  214. contentArea.X,
  215. contentArea.Y + 1,
  216. contentArea.Width,
  217. Math.Max (0, contentArea.Height - 1));
  218. }
  219. Setup (contentArea);
  220. base.LayoutSubviews ();
  221. }
  222. /// <summary>
  223. /// <para>Distance Horizontally or Vertically to the splitter line when
  224. /// neither view is collapsed.
  225. /// </para>
  226. /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
  227. /// are supported for this property.</para>
  228. /// </summary>
  229. public void SetSplitterPos (int idx, Pos value)
  230. {
  231. if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
  232. throw new ArgumentException ($"Only Percent and Absolute values are supported. Passed value was {value.GetType ().Name}");
  233. }
  234. splitterDistances [idx] = value;
  235. GetRootTileView ().LayoutSubviews ();
  236. OnSplitterMoved (idx);
  237. }
  238. /// <inheritdoc/>
  239. public override bool OnEnter (View view)
  240. {
  241. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  242. return base.OnEnter (view);
  243. }
  244. /// <inheritdoc/>
  245. public override void Redraw (Rect bounds)
  246. {
  247. var childTitles = new List<ChildSplitterLine> ();
  248. Driver.SetAttribute (ColorScheme.Normal);
  249. Clear ();
  250. base.Redraw (bounds);
  251. var lc = new LineCanvas ();
  252. var allLines = GetAllChildTileViewLineViewRecursively (this);
  253. if (IsRootTileView ()) {
  254. if (HasBorder ()) {
  255. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, IntegratedBorder);
  256. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, IntegratedBorder);
  257. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, IntegratedBorder);
  258. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
  259. }
  260. foreach (var line in allLines.Where (l => l.Visible)) {
  261. bool isRoot = splitterLines.Contains (line);
  262. line.ViewToScreen (0, 0, out var x1, out var y1);
  263. var origin = ScreenToView (x1, y1);
  264. var length = line.Orientation == Orientation.Horizontal ?
  265. line.Frame.Width - 1 :
  266. line.Frame.Height - 1;
  267. if (!isRoot) {
  268. if (line.Orientation == Orientation.Horizontal) {
  269. origin.X -= 1;
  270. } else {
  271. origin.Y -= 1;
  272. }
  273. length += 2;
  274. childTitles.Add (
  275. new ChildSplitterLine (line));
  276. }
  277. lc.AddLine (origin, length, line.Orientation, IntegratedBorder);
  278. }
  279. }
  280. Driver.SetAttribute (ColorScheme.Normal);
  281. lc.Draw (this, bounds);
  282. // Redraw the lines so that focus/drag symbol renders
  283. foreach (var line in allLines) {
  284. line.DrawSplitterSymbol ();
  285. }
  286. foreach (var child in childTitles) {
  287. child.DrawTitles ();
  288. }
  289. // Draw Titles over Border
  290. for (int i = 0; i < tiles.Count; i++) {
  291. var tile = tiles [i];
  292. if (tile.View.Visible && tile.Title.Length > 0) {
  293. var screen = i == 0 ?
  294. ViewToScreen (new Rect (0, 0, bounds.Width, 1)) :
  295. ViewToScreen (splitterLines [i - 1].Frame);
  296. Driver.SetAttribute (tile.View.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  297. Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, tile.View.Frame.Width, 0), tile.Title, 0, 0, 0, 0);
  298. }
  299. }
  300. }
  301. /// <summary>
  302. /// Converts <see cref="Tile.View"/> of <see cref="Tiles"/> element <paramref name="idx"/>
  303. /// from a regular <see cref="View"/> to a new nested <see cref="TileView"/> with <paramref name="panels"/>
  304. /// number of panels. Returns false if the element already contains a nested view.
  305. /// </summary>
  306. /// <remarks>After successful splitting, the <paramref name="result"/> <see cref="TileView"/>
  307. /// will contain the previous (replaced) <see cref="Tile.View"/> at element 0.</remarks>
  308. /// <param name="idx">The element of <see cref="Tiles"/> that is to be subdivided.</param>
  309. /// <param name="panels">The number of panels that the <see cref="Tile"/> should be split into</param>
  310. /// <param name="result">The new nested <see cref="TileView"/>.</param>
  311. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  312. /// <see cref="TileView"/>. <see langword="false"/> if it was already a nested
  313. /// <see cref="TileView"/></returns>
  314. public bool TrySplitTile(int idx, int panels, out TileView result)
  315. {
  316. // when splitting a view into 2 sub views we will need to migrate
  317. // the title too
  318. var tile = tiles [idx];
  319. // TODO: migrate the title too right?
  320. var title = tile.Title;
  321. View toMove = tile.View;
  322. if (toMove is TileView existing) {
  323. result = existing;
  324. return false;
  325. }
  326. var newContainer = new TileView(panels) {
  327. Width = Dim.Fill (),
  328. Height = Dim.Fill (),
  329. parentTileView = this,
  330. };
  331. // Take everything out of the View we are moving
  332. var childViews = toMove.Subviews.ToArray();
  333. toMove.RemoveAll ();
  334. // Remove the view itself and replace it with the new TileView
  335. Remove (toMove);
  336. Add (newContainer);
  337. tile.View = newContainer;
  338. var newTileView1 = newContainer.tiles [0].View;
  339. // Add the original content into the first view of the new container
  340. foreach (var childView in childViews) {
  341. newTileView1.Add (childView);
  342. }
  343. result = newContainer;
  344. return true;
  345. }
  346. private List<TileViewLineView> GetAllChildTileViewLineViewRecursively (View v)
  347. {
  348. var lines = new List<TileViewLineView> ();
  349. foreach (var sub in v.Subviews) {
  350. if (sub is TileViewLineView s) {
  351. if (s.Parent.GetRootTileView () == this) {
  352. lines.Add (s);
  353. }
  354. } else {
  355. lines.AddRange (GetAllChildTileViewLineViewRecursively (sub));
  356. }
  357. }
  358. return lines;
  359. }
  360. /// <summary>
  361. /// <para>
  362. /// <see langword="true"/> if <see cref="TileView"/> is nested within a parent <see cref="TileView"/>
  363. /// e.g. via the <see cref="TrySplitTile"/>. <see langword="false"/> if it is a root level <see cref="TileView"/>.
  364. /// </para>
  365. /// </summary>
  366. /// <remarks>Note that manually adding one <see cref="TileView"/> to another will not result in a parent/child
  367. /// relationship and both will still be considered 'root' containers. Always use
  368. /// <see cref="TrySplitTile(int, int, out TileView)"/> if you want to subdivide a <see cref="TileView"/>.</remarks>
  369. /// <returns></returns>
  370. public bool IsRootTileView ()
  371. {
  372. // TODO: don't want to layout subviews since the parent recursively lays them all out
  373. return parentTileView == null;
  374. }
  375. /// <summary>
  376. /// Returns the immediate parent <see cref="TileView"/> of this. Note that in case
  377. /// of deep nesting this might not be the root <see cref="TileView"/>. Returns null
  378. /// if this instance is not a nested child (created with
  379. /// <see cref="TrySplitTile(int, int, out TileView)"/>)
  380. /// </summary>
  381. /// <remarks>
  382. /// Use <see cref="IsRootTileView"/> to determine if the returned value is the root.
  383. /// </remarks>
  384. /// <returns></returns>
  385. public TileView GetParentTileView ()
  386. {
  387. return this.parentTileView;
  388. }
  389. private TileView GetRootTileView ()
  390. {
  391. TileView root = this;
  392. while (root.parentTileView != null) {
  393. root = root.parentTileView;
  394. }
  395. return root;
  396. }
  397. private void Setup (Rect bounds)
  398. {
  399. if (bounds.IsEmpty) {
  400. return;
  401. }
  402. RespectMinimumTileSizes ();
  403. for (int i = 0; i < splitterLines.Count; i++) {
  404. var line = splitterLines[i];
  405. line.Orientation = Orientation;
  406. line.Width = orientation == Orientation.Vertical
  407. ? 1 : Dim.Fill ();
  408. line.Height = orientation == Orientation.Vertical
  409. ? Dim.Fill () : 1;
  410. line.LineRune = orientation == Orientation.Vertical ?
  411. Driver.VLine : Driver.HLine;
  412. if (orientation == Orientation.Vertical) {
  413. line.X = splitterDistances [i];
  414. line.Y = 0;
  415. }
  416. else {
  417. line.Y = splitterDistances [i];
  418. line.X = 0;
  419. }
  420. }
  421. for (int i = 0; i < tiles.Count; i++) {
  422. var tile = tiles [i];
  423. // TODO: Deal with lines being Visibility false
  424. if (Orientation == Orientation.Vertical) {
  425. tile.View.X = i == 0 ? bounds.X : Pos.Right (splitterLines [i - 1]);
  426. tile.View.Y = bounds.Y;
  427. tile.View.Height = bounds.Height;
  428. tile.View.Width = GetTileWidthOrHeight(i, Bounds.Width);
  429. } else {
  430. tile.View.X = bounds.X;
  431. tile.View.Y = i == 0 ? 0 : Pos.Bottom (splitterLines [i - 1]);
  432. tile.View.Width = bounds.Width;
  433. tile.View.Height = GetTileWidthOrHeight(i, Bounds.Height);
  434. }
  435. }
  436. }
  437. private Dim GetTileWidthOrHeight (int i, int space)
  438. {
  439. // last tile
  440. if(i + 1 >= tiles.Count)
  441. {
  442. return Dim.Fill (HasBorder () ? 1 : 0);
  443. }
  444. var nextSplitter = splitterDistances [i].Anchor (space);
  445. var lastSplitter = i >= 1 ? splitterDistances [i-1].Anchor (space) : 0;
  446. var distance = nextSplitter - lastSplitter;
  447. if(i>0) {
  448. return distance - 1;
  449. }
  450. return distance - (HasBorder() ? 1 : 0);
  451. }
  452. private void RespectMinimumTileSizes ()
  453. {
  454. // if we are not yet initialized then we don't know
  455. // how big we are and therefore cannot sensibly calculate
  456. // how big the views will be with a given SplitterDistance
  457. if (!IsInitialized) {
  458. return;
  459. }
  460. // how much space is there?
  461. var availableSpace = Orientation == Orientation.Horizontal
  462. ? this.Bounds.Height
  463. : this.Bounds.Width;
  464. var fullSpace = availableSpace;
  465. var lastSplitterLocation = 0;
  466. for(int i=0;i< splitterDistances.Count; i++) {
  467. var splitterLocation = splitterDistances [i].Anchor(fullSpace);
  468. var availableLeft = splitterLocation - lastSplitterLocation;
  469. // Border steals space
  470. availableLeft -= HasBorder () && i == 0 ? 1 : 0;
  471. var availableRight = fullSpace - splitterLocation;
  472. // Border steals space
  473. availableRight -= HasBorder () && i == 0 ? 1 : 0;
  474. // Splitter line steals space
  475. availableRight--;
  476. // TODO: Test 3+ panel max/mins because this calculation is probably wrong
  477. var requiredLeft = tiles [i].MinSize;
  478. var requiredRight = tiles [i+1].MinSize;
  479. if (availableLeft < requiredLeft) {
  480. // There is not enough space for panel on left
  481. var insteadTake = requiredLeft + (HasBorder() ? 1 :0);
  482. // Don't take more than the available space in view
  483. insteadTake = Math.Max(0,Math.Min (fullSpace, insteadTake));
  484. splitterDistances [i] = insteadTake;
  485. splitterLocation = insteadTake;
  486. }
  487. else if (availableRight < requiredRight) {
  488. // There is not enough space for panel on right
  489. var insteadTake = fullSpace - (requiredRight + (HasBorder()?1:0));
  490. // leave 1 space for the splitter
  491. insteadTake --;
  492. insteadTake = Math.Max (0, Math.Min (fullSpace, insteadTake));
  493. splitterDistances [i] = insteadTake;
  494. splitterLocation = insteadTake;
  495. }
  496. availableSpace -= splitterLocation;
  497. lastSplitterLocation = splitterLocation;
  498. }
  499. }
  500. private class TileViewLineView : LineView {
  501. public TileView Parent { get; private set; }
  502. public int Idx { get; }
  503. Point? dragPosition;
  504. Pos dragOrignalPos;
  505. public Point? moveRuneRenderLocation;
  506. public TileViewLineView (TileView parent, int idx)
  507. {
  508. CanFocus = true;
  509. TabStop = true;
  510. this.Parent = parent;
  511. Idx = idx;
  512. base.AddCommand (Command.Right, () => {
  513. return MoveSplitter (1, 0);
  514. });
  515. base.AddCommand (Command.Left, () => {
  516. return MoveSplitter (-1, 0);
  517. });
  518. base.AddCommand (Command.LineUp, () => {
  519. return MoveSplitter (0, -1);
  520. });
  521. base.AddCommand (Command.LineDown, () => {
  522. return MoveSplitter (0, 1);
  523. });
  524. AddKeyBinding (Key.CursorRight, Command.Right);
  525. AddKeyBinding (Key.CursorLeft, Command.Left);
  526. AddKeyBinding (Key.CursorUp, Command.LineUp);
  527. AddKeyBinding (Key.CursorDown, Command.LineDown);
  528. }
  529. public override bool ProcessKey (KeyEvent kb)
  530. {
  531. if (!CanFocus || !HasFocus) {
  532. return base.ProcessKey (kb);
  533. }
  534. var result = InvokeKeybindings (kb);
  535. if (result != null)
  536. return (bool)result;
  537. return base.ProcessKey (kb);
  538. }
  539. public override void PositionCursor ()
  540. {
  541. base.PositionCursor ();
  542. var location = moveRuneRenderLocation ??
  543. new Point (Bounds.Width / 2, Bounds.Height / 2);
  544. Move (location.X, location.Y);
  545. }
  546. public override bool OnEnter (View view)
  547. {
  548. Driver.SetCursorVisibility (CursorVisibility.Default);
  549. PositionCursor ();
  550. return base.OnEnter (view);
  551. }
  552. public override void Redraw (Rect bounds)
  553. {
  554. base.Redraw (bounds);
  555. DrawSplitterSymbol ();
  556. }
  557. public void DrawSplitterSymbol ()
  558. {
  559. if (CanFocus && HasFocus) {
  560. var location = moveRuneRenderLocation ??
  561. new Point (Bounds.Width / 2, Bounds.Height / 2);
  562. AddRune (location.X, location.Y, Driver.Diamond);
  563. }
  564. }
  565. public override bool MouseEvent (MouseEvent mouseEvent)
  566. {
  567. if (!CanFocus) {
  568. return true;
  569. }
  570. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  571. // Start a Drag
  572. SetFocus ();
  573. Application.EnsuresTopOnFront ();
  574. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  575. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  576. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  577. Application.GrabMouse (this);
  578. if (Orientation == Orientation.Horizontal) {
  579. } else {
  580. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  581. }
  582. }
  583. return true;
  584. } else if (
  585. dragPosition.HasValue &&
  586. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  587. // Continue Drag
  588. // how far has user dragged from original location?
  589. if (Orientation == Orientation.Horizontal) {
  590. int dy = mouseEvent.Y - dragPosition.Value.Y;
  591. Parent.splitterDistances [Idx] = Offset (Y, dy);
  592. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  593. } else {
  594. int dx = mouseEvent.X - dragPosition.Value.X;
  595. Parent.splitterDistances [Idx] = Offset (X, dx);
  596. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  597. }
  598. Parent.SetNeedsDisplay ();
  599. return true;
  600. }
  601. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  602. // End Drag
  603. Application.UngrabMouse ();
  604. Driver.UncookMouse ();
  605. FinalisePosition (
  606. dragOrignalPos,
  607. Orientation == Orientation.Horizontal ? Y : X);
  608. dragPosition = null;
  609. moveRuneRenderLocation = null;
  610. }
  611. return false;
  612. }
  613. private bool MoveSplitter (int distanceX, int distanceY)
  614. {
  615. if (Orientation == Orientation.Vertical) {
  616. // Cannot move in this direction
  617. if (distanceX == 0) {
  618. return false;
  619. }
  620. var oldX = X;
  621. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  622. return true;
  623. } else {
  624. // Cannot move in this direction
  625. if (distanceY == 0) {
  626. return false;
  627. }
  628. var oldY = Y;
  629. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  630. return true;
  631. }
  632. }
  633. private Pos Offset (Pos pos, int delta)
  634. {
  635. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  636. Parent.Bounds.Height : Parent.Bounds.Width);
  637. return posAbsolute + delta;
  638. }
  639. /// <summary>
  640. /// <para>
  641. /// Moves <see cref="Parent"/> <see cref="TileView.SplitterDistances"/> to
  642. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  643. /// (absolute / relative) that <paramref name="oldValue"/> had.
  644. /// </para>
  645. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  646. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  647. /// </summary>
  648. /// <param name="oldValue"></param>
  649. /// <param name="newValue"></param>
  650. private void FinalisePosition (Pos oldValue, Pos newValue)
  651. {
  652. if (oldValue is Pos.PosFactor) {
  653. if (Orientation == Orientation.Horizontal) {
  654. Parent.SetSplitterPos(Idx, ConvertToPosFactor (newValue, Parent.Bounds.Height));
  655. } else {
  656. Parent.SetSplitterPos (Idx, ConvertToPosFactor (newValue, Parent.Bounds.Width));
  657. }
  658. } else {
  659. Parent.SetSplitterPos (Idx, newValue);
  660. }
  661. }
  662. /// <summary>
  663. /// <para>
  664. /// Determines the absolute position of <paramref name="p"/> and
  665. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  666. /// </para>
  667. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  668. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  669. /// </summary>
  670. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  671. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  672. /// <returns></returns>
  673. private Pos ConvertToPosFactor (Pos p, int parentLength)
  674. {
  675. // calculate position in the 'middle' of the cell at p distance along parentLength
  676. float position = p.Anchor (parentLength) + 0.5f;
  677. return new Pos.PosFactor (position / parentLength);
  678. }
  679. }
  680. private bool HasBorder ()
  681. {
  682. return IntegratedBorder != BorderStyle.None;
  683. }
  684. private bool HasAnyTitles ()
  685. {
  686. return tiles.Any (t => t.Title.Length > 0);
  687. }
  688. private class ChildSplitterLine {
  689. readonly TileViewLineView currentLine;
  690. internal ChildSplitterLine (TileViewLineView currentLine)
  691. {
  692. this.currentLine = currentLine;
  693. }
  694. internal void DrawTitles ()
  695. {
  696. //TODO: Implement this
  697. /*if(currentLine.Orientation == Orientation.Horizontal)
  698. {
  699. var screenRect = currentLine.ViewToScreen (
  700. new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
  701. Driver.DrawWindowTitle (screenRect, currentLine.Parent.View2Title, 0, 0, 0, 0);
  702. }*/
  703. }
  704. }
  705. }
  706. /// <summary>
  707. /// Provides data for <see cref="TileView"/> events.
  708. /// </summary>
  709. public class SplitterEventArgs : EventArgs {
  710. /// <summary>
  711. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  712. /// </summary>
  713. /// <param name="tileView"><see cref="TileView"/> in which splitter is being moved.</param>
  714. /// <param name="idx">Index of the splitter being moved in <see cref="TileView.SplitterDistances"/>.</param>
  715. /// <param name="splitterDistance">The new <see cref="Pos"/> of the splitter line.</param>
  716. public SplitterEventArgs (TileView tileView, int idx, Pos splitterDistance)
  717. {
  718. SplitterDistance = splitterDistance;
  719. TileView = tileView;
  720. Idx = idx;
  721. }
  722. /// <summary>
  723. /// New position of the splitter line (see <see cref="TileView.SplitterDistances"/>).
  724. /// </summary>
  725. public Pos SplitterDistance { get; }
  726. /// <summary>
  727. /// Container (sender) of the event.
  728. /// </summary>
  729. public TileView TileView { get; }
  730. /// <summary>
  731. /// Gets the index of the splitter that is being moved. This can be
  732. /// used to index <see cref="TileView.SplitterDistances"/>
  733. /// </summary>
  734. public int Idx { get; }
  735. }
  736. /// <summary>
  737. /// Represents a method that will handle splitter events.
  738. /// </summary>
  739. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  740. }