SplitContainer.cs 17 KB

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