SplitContainer.cs 17 KB

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