Browse Source

merged onlayoutcomplete PR

Charlie Kindel 5 years ago
parent
commit
f89fc03a0c

+ 1 - 1
Example/demo.cs

@@ -627,7 +627,7 @@ static class Demo {
 		var bottom2 = new Label ("This should go on the bottom of another top-level!");
 		top.Add (bottom2);
 
-		Application.Loaded += (sender, e) => {
+		top.LayoutComplete += (sender, e) => {
 			bottom.X = win.X;
 			bottom.Y = Pos.Bottom (win) - Pos.Top (win) - margin;
 			bottom2.X = Pos.Left (win);

+ 34 - 2
Terminal.Gui/Core/View.cs

@@ -1318,15 +1318,45 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// Invoked when a view starts executing or
-		/// when the dimensions of the view have changed, for example in
+		/// Event arguments for the <see cref="LayoutComplete"/> event.
+		/// </summary>
+		public class LayoutEventArgs : EventArgs {
+			/// <summary>
+			/// The view-relative bounds of the <see cref="View"/> before it was laid out.
+			/// </summary>
+			public Rect OldBounds { get; set; }
+		}
+
+		/// <summary>
+		/// Fired after the Views's <see cref="LayoutSubviews"/> method has completed. 
+		/// </summary>
+		/// <remarks>
+		/// Subscribe to this event to perform tasks when the <see cref="View"/> has been resized or the layout has otherwise changed.
+		/// </remarks>
+		public event EventHandler<LayoutEventArgs> LayoutComplete;
+
+		/// <summary>
+		/// Raises the <see cref="LayoutComplete"/> event. Called from  <see cref="LayoutSubviews"/> after all sub-views have been laid out.
+		/// </summary>
+		internal virtual void OnLayoutComplete (LayoutEventArgs args)
+		{
+			LayoutComplete?.Invoke (this, args);
+		}
+
+		/// <summary>
+		/// Invoked when a view starts executing or when the dimensions of the view have changed, for example in
 		/// response to the container view or terminal resizing.
 		/// </summary>
+		/// <remarks>
+		/// Calls <see cref="OnLayoutComplete"/> (which raises the <see cref="LayoutComplete"/> event) before it returns.
+		/// </remarks>
 		public virtual void LayoutSubviews ()
 		{
 			if (!layoutNeeded)
 				return;
 
+			Rect oldBounds = Bounds;
+
 			// Sort out the dependencies of the X, Y, Width, Height properties
 			var nodes = new HashSet<View> ();
 			var edges = new HashSet<(View, View)> ();
@@ -1363,6 +1393,8 @@ namespace Terminal.Gui {
 			}
 
 			layoutNeeded = false;
+
+			OnLayoutComplete (new LayoutEventArgs () { OldBounds = oldBounds });
 		}
 
 		/// <inheritdoc cref="ToString"/>

+ 1 - 1
Terminal.Gui/Views/ComboBox.cs

@@ -58,7 +58,7 @@ namespace Terminal.Gui {
 					SetValue (searchset [listview.SelectedItem]);
 			};
 
-			Application.Loaded += (object sender, Application.ResizedEventArgs e) => {
+			LayoutComplete += (sender, a) => {
 				// Determine if this view is hosted inside a dialog
 				for (View view = this.SuperView; view != null; view = view.SuperView) {
 					if (view is Dialog) {

+ 2 - 2
Terminal.Gui/Views/StatusBar.cs

@@ -120,7 +120,7 @@ namespace Terminal.Gui {
 			Width = Dim.Fill ();
 			Height = 1;
 
-			Application.Loaded += (sender, e) => {
+			LayoutComplete += (sender, e) => {
 				X = 0;
 				Height = 1;
 #if SNAP_TO_TOP
@@ -132,7 +132,7 @@ namespace Terminal.Gui {
 				case StatusBarStyle.SnapToBottom:
 #endif
 					if (Parent == null) {
-						Y = e.Rows - 1; 
+						Y = Driver.Rows - 1; 
 					} else {
 						Y = Pos.Bottom (Parent);
 					}

+ 4 - 3
Terminal.Gui/Windows/Dialog.cs

@@ -61,6 +61,8 @@ namespace Terminal.Gui {
 					Add (b);
 				}
 			}
+
+			//LayoutComplete += (sender, a) => AdjustButtonLayout ();
 		}
 
 		/// <summary>
@@ -98,14 +100,13 @@ namespace Terminal.Gui {
 			}
 			return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count() - 1;
 		}
-
 		///<inheritdoc cref="LayoutSubviews"/>
 		public override void LayoutSubviews ()
 		{
 			int buttonsWidth = GetButtonsWidth ();
 
-			int shiftLeft = Math.Max((Bounds.Width - buttonsWidth) / 2 - 2, 0);
-			for (int i = buttons.Count - 1; i >= 0 ; i--) {
+			int shiftLeft = Math.Max ((Bounds.Width - buttonsWidth) / 2 - 2, 0);
+			for (int i = buttons.Count - 1; i >= 0; i--) {
 				Button button = buttons [i];
 				shiftLeft += button.Frame.Width + 1;
 				button.X = Pos.AnchorEnd (shiftLeft);

+ 1 - 1
UICatalog/Scenarios/ComputedLayout.cs

@@ -42,7 +42,7 @@ namespace UICatalog {
 				ColorScheme = Colors.Error
 			};
 
-			Application.Resized += (sender, a) => {
+			Win.LayoutComplete += (sender, a) => {
 				horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
 				verticalRuler.Text = vrule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height*2) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height*2)];
 			};

+ 3 - 2
UICatalog/Scenarios/Scrolling.cs

@@ -121,8 +121,9 @@ namespace UICatalog {
 			};
 			scrollView.Add (verticalRuler);
 
-			Application.Resized += (sender, a) => {
-				horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
+			Win.LayoutComplete += (sender, a) => {
+				horizontalRuler.Text = rule.Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)] +
+				"\n" + "|         ".Repeat ((int)Math.Ceiling ((double)(horizontalRuler.Bounds.Width) / (double)rule.Length)) [0..(horizontalRuler.Bounds.Width)];
 				verticalRuler.Text = vrule.Repeat ((int)Math.Ceiling ((double)(verticalRuler.Bounds.Height * 2) / (double)rule.Length)) [0..(verticalRuler.Bounds.Height * 2)];
 			};