SplitContainer.cs 18 KB

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