SplitContainer.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  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 2 resizeable panels.
  9. /// </summary>
  10. public class SplitContainer : View {
  11. private SplitContainerLineView splitterLine;
  12. SplitContainer parentSplitPanel;
  13. /// TODO: Might be able to make Border virtual and override here
  14. /// To make this more API friendly
  15. /// <summary>
  16. /// Use this field instead of Border to create an integrated
  17. /// Border in which lines connect with subpanels and splitters
  18. /// seamlessly
  19. /// </summary>
  20. public BorderStyle IntegratedBorder {get;set;}
  21. /// <summary>
  22. /// The <see cref="View"/> showing in the left hand pane of a
  23. /// <see cref="Orientation.Vertical"/> or top of an
  24. /// <see cref="Orientation.Horizontal"/> pane. May be another
  25. /// <see cref="SplitContainer"/> if further splitter subdivisions are
  26. /// desired (e.g. to create a resizeable grid.
  27. /// </summary>
  28. public View Panel1 { get; set; } // TODO: Should not be public set, should be helpers for this
  29. public int Panel1MinSize { get; set; } = 1;
  30. public ustring Panel1Title { get; set; } = string.Empty;
  31. /// <summary>
  32. /// The <see cref="View"/> showing in the right hand pane of a
  33. /// <see cref="Orientation.Vertical"/> or bottom of an
  34. /// <see cref="Orientation.Horizontal"/> pane. May be another
  35. /// <see cref="SplitContainer"/> if further splitter subdivisions are
  36. /// desired (e.g. to create a resizeable grid.
  37. /// </summary>
  38. public View Panel2 { get; set; } // TODO: Should not be public set, should be helpers for this
  39. public int Panel2MinSize { get; set; } = 1;
  40. public ustring Panel2Title { get; set; } = string.Empty;
  41. private Pos splitterDistance = Pos.Percent (50);
  42. private Orientation orientation = Orientation.Vertical;
  43. /// <summary>
  44. /// Creates a new instance of the SplitContainer class.
  45. /// </summary>
  46. public SplitContainer ()
  47. {
  48. splitterLine = new SplitContainerLineView (this);
  49. Panel1 = new View () { Width = Dim.Fill (), Height = Dim.Fill() };
  50. Panel2 = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
  51. this.Add (Panel1);
  52. this.Add (splitterLine);
  53. this.Add (Panel2);
  54. CanFocus = true;
  55. }
  56. /// <summary>
  57. /// Invoked when the <see cref="SplitterDistance"/> is changed
  58. /// </summary>
  59. public event SplitterEventHandler SplitterMoved;
  60. /// <summary>
  61. /// Raises the <see cref="SplitterMoved"/> event
  62. /// </summary>
  63. protected virtual void OnSplitterMoved ()
  64. {
  65. SplitterMoved?.Invoke (this, new SplitterEventArgs (this, splitterDistance));
  66. }
  67. /// <summary>
  68. /// Orientation of the dividing line (Horizontal or Vertical).
  69. /// </summary>
  70. public Orientation Orientation {
  71. get { return orientation; }
  72. set {
  73. orientation = value;
  74. LayoutSubviews ();
  75. }
  76. }
  77. public override void LayoutSubviews ()
  78. {
  79. if(this.IsRootSplitContainer()) {
  80. var contentArea = Bounds;
  81. if(HasBorder())
  82. {
  83. // TODO: Bound with Max/Min
  84. contentArea = new Rect(
  85. contentArea.X + 1,
  86. contentArea.Y + 1,
  87. contentArea.Width - 2,
  88. contentArea.Height - 2);
  89. }
  90. else if(HasAnyTitles())
  91. {
  92. // TODO: Bound with Max/Min
  93. contentArea = new Rect(
  94. contentArea.X,
  95. contentArea.Y + 1,
  96. contentArea.Width,
  97. contentArea.Height - 1);
  98. }
  99. Setup (contentArea);
  100. }
  101. base.LayoutSubviews ();
  102. }
  103. /// <summary>
  104. /// <para>Distance Horizontally or Vertically to the splitter line when
  105. /// neither panel is collapsed.
  106. /// </para>
  107. /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
  108. /// are supported for this property.</para>
  109. /// </summary>
  110. public Pos SplitterDistance {
  111. get { return splitterDistance; }
  112. set {
  113. if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
  114. throw new ArgumentException ($"Only Percent and Absolute values are supported for {nameof (SplitterDistance)} property. Passed value was {value.GetType ().Name}");
  115. }
  116. splitterDistance = value;
  117. GetRootSplitContainer ().LayoutSubviews ();
  118. OnSplitterMoved ();
  119. }
  120. }
  121. /// <inheritdoc/>
  122. public override bool OnEnter (View view)
  123. {
  124. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  125. return base.OnEnter (view);
  126. }
  127. /// <inheritdoc/>
  128. public override void Redraw (Rect bounds)
  129. {
  130. Driver.SetAttribute (ColorScheme.Normal);
  131. Clear ();
  132. base.Redraw (bounds);
  133. var lc = new LineCanvas(Application.Driver);
  134. var allLines = GetAllChildSplitContainerLineViewRecursively (this);
  135. if (IsRootSplitContainer())
  136. {
  137. if(HasBorder ()) {
  138. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, IntegratedBorder);
  139. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, IntegratedBorder);
  140. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, IntegratedBorder);
  141. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
  142. }
  143. foreach (var line in allLines)
  144. {
  145. bool isRoot = line == splitterLine;
  146. line.ViewToScreen(0,0,out var x1,out var y1);
  147. var origin = ScreenToView(x1,y1);
  148. var length = line.Orientation == Orientation.Horizontal ?
  149. line.Frame.Width - 1 :
  150. line.Frame.Height - 1;
  151. if(!isRoot) {
  152. if(line.Orientation == Orientation.Horizontal) {
  153. origin.X -= 1;
  154. } else {
  155. origin.Y -= 1;
  156. }
  157. length += 2;
  158. // TODO: Render this title too
  159. }
  160. lc.AddLine(origin,length,line.Orientation,IntegratedBorder);
  161. }
  162. }
  163. Driver.SetAttribute (ColorScheme.Normal);
  164. lc.Draw(this,bounds);
  165. // Redraw the lines so that focus/drag symbol renders
  166. foreach(var line in allLines) {
  167. line.DrawSplitterSymbol ();
  168. }
  169. // Draw Titles over Border
  170. var screen = ViewToScreen (bounds);
  171. if (Panel1.Visible && Panel1Title.Length > 0) {
  172. Driver.SetAttribute (Panel1.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  173. Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel1.Frame.Width, 0), Panel1Title, 0, 0, 0, 0);
  174. }
  175. if (splitterLine.Visible) {
  176. screen = ViewToScreen (splitterLine.Frame);
  177. } else {
  178. screen.X--;
  179. //screen.Y--;
  180. }
  181. if (Orientation == Orientation.Horizontal) {
  182. if (Panel2.Visible && Panel2Title?.Length > 0) {
  183. Driver.SetAttribute (Panel2.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  184. Driver.DrawWindowTitle (new Rect (screen.X + 1, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
  185. }
  186. } else {
  187. if (Panel2.Visible && Panel2Title?.Length > 0) {
  188. Driver.SetAttribute (Panel2.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  189. Driver.DrawWindowTitle (new Rect (screen.X + 1, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
  190. }
  191. }
  192. }
  193. private List<SplitContainerLineView> GetAllChildSplitContainerLineViewRecursively (View v)
  194. {
  195. var lines = new List<SplitContainerLineView>();
  196. foreach(var sub in v.Subviews)
  197. {
  198. if(sub is SplitContainerLineView s)
  199. {
  200. lines.Add(s);
  201. }
  202. else {
  203. lines.AddRange(GetAllChildSplitContainerLineViewRecursively(sub));
  204. }
  205. }
  206. return lines;
  207. }
  208. private bool IsRootSplitContainer ()
  209. {
  210. // TODO: don't want to layout subviews since the parent recursively lays them all out
  211. return parentSplitPanel == null;
  212. }
  213. private SplitContainer GetRootSplitContainer ()
  214. {
  215. SplitContainer root = this;
  216. while (root.parentSplitPanel != null) {
  217. root = root.parentSplitPanel;
  218. }
  219. return root;
  220. }
  221. private void Setup (Rect bounds)
  222. {
  223. splitterLine.Orientation = Orientation;
  224. // splitterLine.Text = Panel2.Title;
  225. // TODO: Recursion
  226. if (!Panel1.Visible || !Panel2.Visible) {
  227. View toFullSize = !Panel1.Visible ? Panel2 : Panel1;
  228. splitterLine.Visible = false;
  229. toFullSize.X = bounds.X;
  230. toFullSize.Y = bounds.Y;
  231. toFullSize.Width = bounds.Width;
  232. toFullSize.Height = bounds.Height;
  233. } else {
  234. splitterLine.Visible = true;
  235. splitterDistance = BoundByMinimumSizes (splitterDistance);
  236. Panel1.X = bounds.X;
  237. Panel1.Y = bounds.Y;
  238. switch (Orientation) {
  239. case Orientation.Horizontal:
  240. splitterLine.X = 0;
  241. splitterLine.Y = splitterDistance;
  242. splitterLine.Width = Dim.Fill ();
  243. splitterLine.Height = 1;
  244. splitterLine.LineRune = Driver.HLine;
  245. Panel1.Width = Dim.Fill (HasBorder()? 1:0);
  246. Panel1.Height = new Dim.DimFunc (() =>
  247. splitterDistance.Anchor (Bounds.Height));
  248. Panel2.Y = Pos.Bottom (splitterLine);
  249. Panel2.X = bounds.X;
  250. Panel2.Width = bounds.Width;
  251. Panel2.Height = Dim.Fill(HasBorder () ? 1 : 0);
  252. break;
  253. case Orientation.Vertical:
  254. splitterLine.X = splitterDistance;
  255. splitterLine.Y = 0;
  256. splitterLine.Width = 1;
  257. splitterLine.Height = Dim.Fill ();
  258. splitterLine.LineRune = Driver.VLine;
  259. Panel1.Height = Dim.Fill();
  260. Panel1.Width = new Dim.DimFunc (() =>
  261. splitterDistance.Anchor (Bounds.Width));
  262. Panel2.X = Pos.Right (splitterLine);
  263. Panel2.Y = bounds.Y;
  264. Panel2.Height = bounds.Height;
  265. Panel2.Width = Dim.Fill(HasBorder()? 1:0);
  266. break;
  267. default: throw new ArgumentOutOfRangeException (nameof (orientation));
  268. };
  269. }
  270. }
  271. /// <summary>
  272. /// Considers <paramref name="pos"/> as a candidate for <see cref="splitterDistance"/>
  273. /// then either returns (if valid) or returns adjusted if invalid with respect to the
  274. /// <see cref="SplitterPanel.MinSize"/> of the panels.
  275. /// </summary>
  276. /// <param name="pos"></param>
  277. /// <returns></returns>
  278. private Pos BoundByMinimumSizes (Pos pos)
  279. {
  280. // if we are not yet initialized then we don't know
  281. // how big we are and therefore cannot sensibly calculate
  282. // how big the panels will be with a given SplitterDistance
  283. if (!IsInitialized) {
  284. return pos;
  285. }
  286. var panel1MinSize = Panel1MinSize;
  287. var panel2MinSize = Panel2MinSize;
  288. // if there is a border then there is less space
  289. // for the panels so we need to make size restrictions
  290. // tighter.
  291. if(HasBorder()) {
  292. panel1MinSize++;
  293. panel2MinSize++;
  294. }
  295. var availableSpace = Orientation == Orientation.Horizontal ? this.Bounds.Height : this.Bounds.Width;
  296. var idealPosition = pos.Anchor (availableSpace);
  297. // bad position because not enough space for Panel1
  298. if (idealPosition < panel1MinSize) {
  299. // TODO: we should preserve Absolute/Percent status here not just force it to absolute
  300. return (Pos)Math.Min (panel1MinSize, availableSpace);
  301. }
  302. // bad position because not enough space for Panel2
  303. if (availableSpace - idealPosition <= panel2MinSize) {
  304. // TODO: we should preserve Absolute/Percent status here not just force it to absolute
  305. // +1 is to allow space for the splitter
  306. return (Pos)Math.Max (availableSpace - (panel2MinSize + 1), 0);
  307. }
  308. // this splitter position is fine, there is enough space for everyone
  309. return pos;
  310. }
  311. private class SplitContainerLineView : LineView {
  312. public SplitContainer Parent { get; private set; }
  313. Point? dragPosition;
  314. Pos dragOrignalPos;
  315. public Point? moveRuneRenderLocation;
  316. public SplitContainerLineView (SplitContainer parent)
  317. {
  318. CanFocus = true;
  319. TabStop = true;
  320. this.Parent = parent;
  321. base.AddCommand (Command.Right, () => {
  322. return MoveSplitter (1, 0);
  323. });
  324. base.AddCommand (Command.Left, () => {
  325. return MoveSplitter (-1, 0);
  326. });
  327. base.AddCommand (Command.LineUp, () => {
  328. return MoveSplitter (0, -1);
  329. });
  330. base.AddCommand (Command.LineDown, () => {
  331. return MoveSplitter (0, 1);
  332. });
  333. AddKeyBinding (Key.CursorRight, Command.Right);
  334. AddKeyBinding (Key.CursorLeft, Command.Left);
  335. AddKeyBinding (Key.CursorUp, Command.LineUp);
  336. AddKeyBinding (Key.CursorDown, Command.LineDown);
  337. }
  338. public override bool ProcessKey (KeyEvent kb)
  339. {
  340. if (!CanFocus || !HasFocus) {
  341. return base.ProcessKey (kb);
  342. }
  343. var result = InvokeKeybindings (kb);
  344. if (result != null)
  345. return (bool)result;
  346. return base.ProcessKey (kb);
  347. }
  348. public override void PositionCursor ()
  349. {
  350. base.PositionCursor ();
  351. var location = moveRuneRenderLocation ??
  352. new Point (Bounds.Width / 2, Bounds.Height / 2);
  353. Move (location.X, location.Y);
  354. }
  355. public override bool OnEnter (View view)
  356. {
  357. Driver.SetCursorVisibility (CursorVisibility.Default);
  358. PositionCursor ();
  359. return base.OnEnter (view);
  360. }
  361. public override void Redraw (Rect bounds)
  362. {
  363. base.Redraw (bounds);
  364. DrawSplitterSymbol ();
  365. }
  366. public void DrawSplitterSymbol()
  367. {
  368. if (CanFocus && HasFocus) {
  369. var location = moveRuneRenderLocation ??
  370. new Point (Bounds.Width / 2, Bounds.Height / 2);
  371. AddRune (location.X, location.Y, Driver.Diamond);
  372. }
  373. }
  374. public override bool MouseEvent (MouseEvent mouseEvent)
  375. {
  376. if (!CanFocus) {
  377. return true;
  378. }
  379. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  380. // Start a Drag
  381. SetFocus ();
  382. Application.EnsuresTopOnFront ();
  383. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  384. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  385. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  386. Application.GrabMouse (this);
  387. if (Orientation == Orientation.Horizontal) {
  388. } else {
  389. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  390. }
  391. }
  392. return true;
  393. } else if (
  394. dragPosition.HasValue &&
  395. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  396. // Continue Drag
  397. // how far has user dragged from original location?
  398. if (Orientation == Orientation.Horizontal) {
  399. int dy = mouseEvent.Y - dragPosition.Value.Y;
  400. Parent.SplitterDistance = Offset (Y, dy);
  401. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  402. } else {
  403. int dx = mouseEvent.X - dragPosition.Value.X;
  404. Parent.SplitterDistance = Offset (X, dx);
  405. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  406. }
  407. Parent.SetNeedsDisplay ();
  408. return true;
  409. }
  410. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  411. // End Drag
  412. Application.UngrabMouse ();
  413. Driver.UncookMouse ();
  414. FinalisePosition (
  415. dragOrignalPos,
  416. Orientation == Orientation.Horizontal ? Y : X);
  417. dragPosition = null;
  418. moveRuneRenderLocation = null;
  419. }
  420. return false;
  421. }
  422. private bool MoveSplitter (int distanceX, int distanceY)
  423. {
  424. if (Orientation == Orientation.Vertical) {
  425. // Cannot move in this direction
  426. if (distanceX == 0) {
  427. return false;
  428. }
  429. var oldX = X;
  430. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  431. return true;
  432. } else {
  433. // Cannot move in this direction
  434. if (distanceY == 0) {
  435. return false;
  436. }
  437. var oldY = Y;
  438. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  439. return true;
  440. }
  441. }
  442. private Pos Offset (Pos pos, int delta)
  443. {
  444. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  445. Parent.Bounds.Height : Parent.Bounds.Width);
  446. return posAbsolute + delta;
  447. }
  448. /// <summary>
  449. /// <para>
  450. /// Moves <see cref="parent"/> <see cref="SplitContainer.SplitterDistance"/> to
  451. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  452. /// (absolute / relative) that <paramref name="oldValue"/> had.
  453. /// </para>
  454. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  455. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  456. /// </summary>
  457. /// <param name="oldValue"></param>
  458. /// <param name="newValue"></param>
  459. private void FinalisePosition (Pos oldValue, Pos newValue)
  460. {
  461. if (oldValue is Pos.PosFactor) {
  462. if (Orientation == Orientation.Horizontal) {
  463. Parent.SplitterDistance = ConvertToPosFactor (newValue, Parent.Bounds.Height);
  464. } else {
  465. Parent.SplitterDistance = ConvertToPosFactor (newValue, Parent.Bounds.Width);
  466. }
  467. } else {
  468. Parent.SplitterDistance = newValue;
  469. }
  470. }
  471. /// <summary>
  472. /// <para>
  473. /// Determines the absolute position of <paramref name="p"/> and
  474. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  475. /// </para>
  476. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  477. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  478. /// </summary>
  479. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  480. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  481. /// <returns></returns>
  482. private Pos ConvertToPosFactor (Pos p, int parentLength)
  483. {
  484. // calculate position in the 'middle' of the cell at p distance along parentLength
  485. float position = p.Anchor (parentLength) + 0.5f;
  486. return new Pos.PosFactor (position / parentLength);
  487. }
  488. }
  489. private bool HasBorder ()
  490. {
  491. return IntegratedBorder != BorderStyle.None;
  492. }
  493. private bool HasAnyTitles()
  494. {
  495. return Panel1Title.Length > 0 || Panel2Title.Length > 0;
  496. }
  497. }
  498. /// <summary>
  499. /// Provides data for <see cref="SplitContainer"/> events.
  500. /// </summary>
  501. public class SplitterEventArgs : EventArgs {
  502. /// <summary>
  503. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  504. /// </summary>
  505. /// <param name="splitContainer"></param>
  506. /// <param name="splitterDistance"></param>
  507. public SplitterEventArgs (SplitContainer splitContainer, Pos splitterDistance)
  508. {
  509. SplitterDistance = splitterDistance;
  510. SplitContainer = splitContainer;
  511. }
  512. /// <summary>
  513. /// New position of the <see cref="SplitContainer.SplitterDistance"/>
  514. /// </summary>
  515. public Pos SplitterDistance { get; }
  516. /// <summary>
  517. /// Container (sender) of the event.
  518. /// </summary>
  519. public SplitContainer SplitContainer { get; }
  520. }
  521. /// <summary>
  522. /// Represents a method that will handle splitter events.
  523. /// </summary>
  524. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  525. }