Browse Source

add split container prototype

tznind 2 years ago
parent
commit
7817295c00
2 changed files with 232 additions and 0 deletions
  1. 120 0
      Terminal.Gui/Views/SplitContainer.cs
  2. 112 0
      UICatalog/Scenarios/SplitContainerExample.cs

+ 120 - 0
Terminal.Gui/Views/SplitContainer.cs

@@ -0,0 +1,120 @@
+using System;
+using Terminal.Gui.Graphs;
+using static Terminal.Gui.Dim;
+
+namespace Terminal.Gui {
+	public class SplitContainer : View {
+
+		private LineView splitterLine = new LineView ();
+		private bool panel1Collapsed;
+		private bool panel2Collapsed;
+
+		public SplitContainer ()
+		{
+			// Default to a border of 1 so that View looks nice
+			Border = new Border ();
+
+			this.Add (splitterLine);
+			this.Add (Panel1);
+			this.Add (Panel2);
+
+			SetOrientation (Orientation.Vertical, Pos.Percent (50));
+
+			// TODO: Actually respect collapsed statuses
+		}
+
+		private void SetOrientation (Orientation orientation, Pos splitterDistance)
+		{
+			// TODO: Enforce minimum sizes
+
+			splitterLine.Orientation = orientation;
+
+			switch (orientation) {
+			case Orientation.Horizontal:
+				splitterLine.X = 0;
+				splitterLine.Y = splitterDistance;
+				splitterLine.Width = Dim.Fill ();
+				splitterLine.Height = 1;
+				splitterLine.LineRune = Driver.HLine;
+
+				this.Panel1.X = 0;
+				this.Panel1.Y = 0;
+				this.Panel1.Width = Dim.Fill ();
+				this.Panel1.Height = new DimFunc (() =>
+					splitterDistance.Anchor (Bounds.Height) - 1);
+
+				this.Panel2.Y = Pos.Bottom (splitterLine) + 1;
+				this.Panel2.X = 0;
+				this.Panel2.Width = Dim.Fill ();
+				this.Panel2.Height = Dim.Fill ();
+				break;
+
+			case Orientation.Vertical:
+				splitterLine.X = splitterDistance;
+				splitterLine.Y = 0;
+				splitterLine.Width = 1;
+				splitterLine.Height = Dim.Fill ();
+				splitterLine.LineRune = Driver.VLine;
+
+				this.Panel1.X = 0;
+				this.Panel1.Y = 0;
+				this.Panel1.Height = Dim.Fill ();
+				this.Panel1.Width = new DimFunc (() =>
+					splitterDistance.Anchor (Bounds.Width) - 1);
+
+				this.Panel2.X = Pos.Right (splitterLine) + 1;
+				this.Panel2.Y = 0;
+				this.Panel2.Width = Dim.Fill ();
+				this.Panel2.Height = Dim.Fill ();
+				break;
+
+			default: throw new ArgumentOutOfRangeException (nameof (orientation));
+			};
+		}
+
+		/// <summary>
+		/// The left or top panel of the <see cref="SplitContainer"/>
+		/// (depending on <see cref="Orientation"/>).  Add panel contents
+		/// to this <see cref="View"/> using <see cref="View.Add(View)"/>.
+		/// </summary>
+		public View Panel1 { get; } = new View ();
+		public int Panel1MinSize { get; set; }
+
+		/// <summary>
+		/// This determines if <see cref="Panel1"/> is collapsed.
+		/// </summary>
+		public bool Panel1Collapsed {
+			get { return panel1Collapsed; }
+			set {
+				panel1Collapsed = value;
+				if (value && panel2Collapsed) {
+					panel2Collapsed = false;
+				}	
+			}
+
+		}
+
+		/// <summary>
+		/// The right or bottom panel of the <see cref="SplitContainer"/>
+		/// (depending on <see cref="Orientation"/>).  Add panel contents
+		/// to this <see cref="View"/> using <see cref="View.Add(View)"/>
+		/// </summary>
+		public View Panel2 { get; } = new View ();
+		public int Panel2MinSize { get; set; }
+
+		/// <summary>
+		/// This determines if <see cref="Panel2"/> is collapsed.
+		/// </summary>
+		public bool Panel2Collapsed {
+			get { return panel2Collapsed; }
+			set {
+				panel2Collapsed = value;
+				if (value && panel1Collapsed) {
+					panel1Collapsed = false;
+				}
+			}
+		}
+		public Orientation Orientation { get; set; }
+		public Pos SplitterDistance { get; set; }
+	}
+}

+ 112 - 0
UICatalog/Scenarios/SplitContainerExample.cs

@@ -0,0 +1,112 @@
+using Terminal.Gui;
+using System;
+
+namespace UICatalog.Scenarios {
+	[ScenarioMetadata (Name: "Color Picker2", Description: "Color Picker.")]
+	[ScenarioCategory ("Colors")]
+	[ScenarioCategory ("Controls")]
+	public class ColorPickers : Scenario {
+		/// <summary>
+		/// Foreground ColorPicker.
+		/// </summary>
+		private ColorPicker foregroundColorPicker;
+
+		/// <summary>
+		/// Background ColorPicker.
+		/// </summary>
+		private ColorPicker backgroundColorPicker;
+
+		/// <summary>
+		/// Foreground color label.
+		/// </summary>
+		private Label foregroundColorLabel;
+
+		/// <summary>
+		/// Background color Label.
+		/// </summary>
+		private Label backgroundColorLabel;
+
+		/// <summary>
+		/// Demo label.
+		/// </summary>
+		private Label demoLabel;
+
+		/// <summary>
+		/// Setup the scenario.
+		/// </summary>
+		public override void Setup ()
+		{
+			// Scenario Window's.
+			Win.Title = this.GetName ();
+
+			// Forground ColorPicker.
+			foregroundColorPicker = new ColorPicker ("Foreground Color");
+			foregroundColorPicker.X = 0;
+			foregroundColorPicker.Y = 0;
+			foregroundColorPicker.ColorChanged += ForegroundColor_ColorChanged;
+			Win.Add (foregroundColorPicker);
+
+			foregroundColorLabel = new Label ();
+			foregroundColorLabel.X = Pos.Left (foregroundColorPicker);
+			foregroundColorLabel.Y = Pos.Bottom (foregroundColorPicker) + 1;
+			Win.Add (foregroundColorLabel);
+
+			// Background ColorPicker.
+			backgroundColorPicker = new ColorPicker ();
+			backgroundColorPicker.Text = "Background Color";
+			backgroundColorPicker.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorPicker) - Pos.Left (backgroundColorPicker));
+			backgroundColorPicker.Y = 0;
+			backgroundColorPicker.ColorChanged += BackgroundColor_ColorChanged;
+			Win.Add (backgroundColorPicker);
+
+			backgroundColorLabel = new Label ();
+			backgroundColorLabel.X = Pos.AnchorEnd () - (Pos.Right (backgroundColorLabel) - Pos.Left (backgroundColorLabel));
+			backgroundColorLabel.Y = Pos.Bottom (backgroundColorPicker) + 1;
+			Win.Add (backgroundColorLabel);
+
+			// Demo Label.
+			demoLabel = new Label ("Lorem Ipsum");
+			demoLabel.X = Pos.Center ();
+			demoLabel.Y = 1;
+			Win.Add (demoLabel);
+
+			// Set default colors.
+			backgroundColorPicker.SelectedColor = demoLabel.SuperView.ColorScheme.Normal.Background;
+			foregroundColorPicker.SelectedColor = demoLabel.SuperView.ColorScheme.Normal.Foreground;
+		}
+
+		/// <summary>
+		/// Fired when foreground color is changed.
+		/// </summary>
+		private void ForegroundColor_ColorChanged ()
+		{
+			UpdateColorLabel (foregroundColorLabel, foregroundColorPicker);
+			UpdateDemoLabel ();
+		}
+
+		/// <summary>
+		/// Fired when background color is changed.
+		/// </summary>
+		private void BackgroundColor_ColorChanged ()
+		{
+			UpdateColorLabel (backgroundColorLabel, backgroundColorPicker);
+			UpdateDemoLabel ();
+		}
+
+		/// <summary>
+		/// Update a color label from his ColorPicker.
+		/// </summary>
+		private void UpdateColorLabel (Label label, ColorPicker colorPicker)
+		{
+			label.Clear ();
+			label.Text = $"{colorPicker.SelectedColor} - {(int)colorPicker.SelectedColor}";
+		}
+
+		/// <summary>
+		/// Update Demo Label.
+		/// </summary>
+		private void UpdateDemoLabel () => demoLabel.ColorScheme = new ColorScheme () {
+			Normal = new Terminal.Gui.Attribute (foregroundColorPicker.SelectedColor, backgroundColorPicker.SelectedColor)
+		};
+	}
+}