Browse Source

Refactored onload/onresize events to use event vs. Action<T>

Charlie Kindel 5 years ago
parent
commit
8de1702042
3 changed files with 45 additions and 32 deletions
  1. 1 1
      Example/demo.cs
  2. 42 28
      Terminal.Gui/Core.cs
  3. 2 3
      Terminal.Gui/Views/StatusBar.cs

+ 1 - 1
Example/demo.cs

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

+ 42 - 28
Terminal.Gui/Core.cs

@@ -607,7 +607,7 @@ namespace Terminal.Gui {
 				return;
 				return;
 
 
 			while (subviews.Count > 0) {
 			while (subviews.Count > 0) {
-				Remove (subviews[0]);
+				Remove (subviews [0]);
 			}
 			}
 		}
 		}
 
 
@@ -705,9 +705,9 @@ namespace Terminal.Gui {
 		{
 		{
 			PerformActionForSubview (subview, x => {
 			PerformActionForSubview (subview, x => {
 				var idx = subviews.IndexOf (x);
 				var idx = subviews.IndexOf (x);
-				if (idx+1 < subviews.Count) {
+				if (idx + 1 < subviews.Count) {
 					subviews.Remove (x);
 					subviews.Remove (x);
-					subviews.Insert (idx+1, x);
+					subviews.Insert (idx + 1, x);
 				}
 				}
 			});
 			});
 		}
 		}
@@ -910,7 +910,7 @@ namespace Terminal.Gui {
 						OnEnter ();
 						OnEnter ();
 					else
 					else
 						OnLeave ();
 						OnLeave ();
-					SetNeedsDisplay ();
+				SetNeedsDisplay ();
 				base.HasFocus = value;
 				base.HasFocus = value;
 
 
 				// Remove focus down the chain of subviews if focus is removed
 				// Remove focus down the chain of subviews if focus is removed
@@ -1062,7 +1062,7 @@ namespace Terminal.Gui {
 			focused.EnsureFocus ();
 			focused.EnsureFocus ();
 
 
 			// Send focus upwards
 			// Send focus upwards
-			SuperView?.SetFocus(this);
+			SuperView?.SetFocus (this);
 		}
 		}
 
 
 		public class KeyEventEventArgs : EventArgs {
 		public class KeyEventEventArgs : EventArgs {
@@ -1181,7 +1181,7 @@ namespace Terminal.Gui {
 		public void FocusLast ()
 		public void FocusLast ()
 		{
 		{
 			if (subviews == null) {
 			if (subviews == null) {
-				SuperView?.SetFocus(this);
+				SuperView?.SetFocus (this);
 				return;
 				return;
 			}
 			}
 
 
@@ -1228,7 +1228,7 @@ namespace Terminal.Gui {
 						w.FocusLast ();
 						w.FocusLast ();
 
 
 					SetFocus (w);
 					SetFocus (w);
-				return true;
+					return true;
 				}
 				}
 			}
 			}
 			if (focused != null) {
 			if (focused != null) {
@@ -1641,7 +1641,7 @@ namespace Terminal.Gui {
 		internal void EnsureVisibleBounds (Toplevel top, int x, int y, out int nx, out int ny)
 		internal void EnsureVisibleBounds (Toplevel top, int x, int y, out int nx, out int ny)
 		{
 		{
 			nx = Math.Max (x, 0);
 			nx = Math.Max (x, 0);
-			nx = nx + top.Frame.Width > Driver.Cols ? Math.Max(Driver.Cols - top.Frame.Width, 0) : nx;
+			nx = nx + top.Frame.Width > Driver.Cols ? Math.Max (Driver.Cols - top.Frame.Width, 0) : nx;
 			bool m, s;
 			bool m, s;
 			if (SuperView == null)
 			if (SuperView == null)
 				m = Application.Top.MenuBar != null;
 				m = Application.Top.MenuBar != null;
@@ -1655,7 +1655,7 @@ namespace Terminal.Gui {
 				s = ((Toplevel)SuperView).StatusBar != null;
 				s = ((Toplevel)SuperView).StatusBar != null;
 			l = s ? Driver.Rows - 1 : Driver.Rows;
 			l = s ? Driver.Rows - 1 : Driver.Rows;
 			ny = Math.Min (ny, l);
 			ny = Math.Min (ny, l);
-			ny = ny + top.Frame.Height > l ? Math.Max(l - top.Frame.Height, m ? 1 : 0) : ny;
+			ny = ny + top.Frame.Height > l ? Math.Max (l - top.Frame.Height, m ? 1 : 0) : ny;
 		}
 		}
 
 
 		internal void PositionToplevels ()
 		internal void PositionToplevels ()
@@ -1809,7 +1809,7 @@ namespace Terminal.Gui {
 			this.Title = title;
 			this.Title = title;
 			int wb = 1 + padding;
 			int wb = 1 + padding;
 			this.padding = padding;
 			this.padding = padding;
- 			contentView = new ContentView () {
+			contentView = new ContentView () {
 				X = wb,
 				X = wb,
 				Y = wb,
 				Y = wb,
 				Width = Dim.Fill (wb),
 				Width = Dim.Fill (wb),
@@ -2092,7 +2092,7 @@ namespace Terminal.Gui {
 			if (UseSystemConsole) {
 			if (UseSystemConsole) {
 				mainLoopDriver = new Mono.Terminal.NetMainLoop ();
 				mainLoopDriver = new Mono.Terminal.NetMainLoop ();
 				Driver = new NetDriver ();
 				Driver = new NetDriver ();
-			} else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows){
+			} else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows) {
 				var windowsDriver = new WindowsDriver ();
 				var windowsDriver = new WindowsDriver ();
 				mainLoopDriver = windowsDriver;
 				mainLoopDriver = windowsDriver;
 				Driver = windowsDriver;
 				Driver = windowsDriver;
@@ -2150,7 +2150,7 @@ namespace Terminal.Gui {
 		static void ProcessKeyEvent (KeyEvent ke)
 		static void ProcessKeyEvent (KeyEvent ke)
 		{
 		{
 
 
-			var chain = toplevels.ToList();
+			var chain = toplevels.ToList ();
 			foreach (var topLevel in chain) {
 			foreach (var topLevel in chain) {
 				if (topLevel.ProcessHotKey (ke))
 				if (topLevel.ProcessHotKey (ke))
 					return;
 					return;
@@ -2207,7 +2207,7 @@ namespace Terminal.Gui {
 				return null;
 				return null;
 			}
 			}
 
 
-			if (start.InternalSubviews != null){
+			if (start.InternalSubviews != null) {
 				int count = start.InternalSubviews.Count;
 				int count = start.InternalSubviews.Count;
 				if (count > 0) {
 				if (count > 0) {
 					var rx = x - startFrame.X;
 					var rx = x - startFrame.X;
@@ -2223,8 +2223,8 @@ namespace Terminal.Gui {
 					}
 					}
 				}
 				}
 			}
 			}
-			resx = x-startFrame.X;
-			resy = y-startFrame.Y;
+			resx = x - startFrame.X;
+			resy = y - startFrame.Y;
 			return start;
 			return start;
 		}
 		}
 
 
@@ -2326,9 +2326,10 @@ namespace Terminal.Gui {
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
-		/// Action that is invoked once at beginning.
+		/// This event is fired once when the application is first loaded. The dimensions of the
+		/// terminal are provided.
 		/// </summary>
 		/// </summary>
-		static public Action OnLoad;
+		static public event EventHandler<ResizedEventArgs> Loaded;
 
 
 		/// <summary>
 		/// <summary>
 		/// Building block API: Prepares the provided toplevel for execution.
 		/// Building block API: Prepares the provided toplevel for execution.
@@ -2352,11 +2353,11 @@ namespace Terminal.Gui {
 			Init ();
 			Init ();
 			if (toplevel is ISupportInitializeNotification initializableNotification &&
 			if (toplevel is ISupportInitializeNotification initializableNotification &&
 			    !initializableNotification.IsInitialized) {
 			    !initializableNotification.IsInitialized) {
-				initializableNotification.BeginInit();
-				initializableNotification.EndInit();
+				initializableNotification.BeginInit ();
+				initializableNotification.EndInit ();
 			} else if (toplevel is ISupportInitialize initializable) {
 			} else if (toplevel is ISupportInitialize initializable) {
-				initializable.BeginInit();
-				initializable.EndInit();
+				initializable.BeginInit ();
+				initializable.EndInit ();
 			}
 			}
 			toplevels.Push (toplevel);
 			toplevels.Push (toplevel);
 			Current = toplevel;
 			Current = toplevel;
@@ -2364,7 +2365,7 @@ namespace Terminal.Gui {
 			if (toplevel.LayoutStyle == LayoutStyle.Computed)
 			if (toplevel.LayoutStyle == LayoutStyle.Computed)
 				toplevel.RelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
 				toplevel.RelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
 			toplevel.LayoutSubviews ();
 			toplevel.LayoutSubviews ();
-			OnLoad?.Invoke ();
+			Loaded?.Invoke (null, new ResizedEventArgs () { Rows = Driver.Rows, Cols = Driver.Cols } );
 			toplevel.WillPresent ();
 			toplevel.WillPresent ();
 			Redraw (toplevel);
 			Redraw (toplevel);
 			toplevel.PositionCursor ();
 			toplevel.PositionCursor ();
@@ -2431,9 +2432,8 @@ namespace Terminal.Gui {
 			toplevels.Pop ();
 			toplevels.Pop ();
 			if (toplevels.Count == 0)
 			if (toplevels.Count == 0)
 				Shutdown ();
 				Shutdown ();
-			else
-			{
-				Current = toplevels.Peek();
+			else {
+				Current = toplevels.Peek ();
 				Refresh ();
 				Refresh ();
 			}
 			}
 		}
 		}
@@ -2502,7 +2502,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public static void Run<T> () where T : Toplevel, new()
 		public static void Run<T> () where T : Toplevel, new()
 		{
 		{
-			Init (() => new T());
+			Init (() => new T ());
 			Run (Top);
 			Run (Top);
 		}
 		}
 
 
@@ -2546,15 +2546,29 @@ namespace Terminal.Gui {
 			Current.Running = false;
 			Current.Running = false;
 		}
 		}
 
 
+		/// <summary>
+		/// Event arguments for the <see cref="T:Terminal.Gui.Application.Resized"/> event.
+		/// </summary>
+		public class ResizedEventArgs : EventArgs {
+			/// <summary>
+			/// The number of rows in the resized terminal.
+			/// </summary>
+			public int Rows { get; set; }
+			/// <summary>
+			/// The number of columns in the resized terminal.
+			/// </summary>
+			public int Cols { get; set; }
+		}
+
 		/// <summary>
 		/// <summary>
 		/// Invoked when the terminal was resized.
 		/// Invoked when the terminal was resized.
 		/// </summary>
 		/// </summary>
-		static public Action OnResized;
+		static public event EventHandler Resized;
 
 
 		static void TerminalResized ()
 		static void TerminalResized ()
 		{
 		{
-			OnResized?.Invoke ();
 			var full = new Rect (0, 0, Driver.Cols, Driver.Rows);
 			var full = new Rect (0, 0, Driver.Cols, Driver.Rows);
+			Resized?.Invoke (null, new ResizedEventArgs () { Cols = full.Width, Rows = full.Height });
 			Driver.Clip = full;
 			Driver.Clip = full;
 			foreach (var t in toplevels) {
 			foreach (var t in toplevels) {
 				t.PositionToplevels ();
 				t.PositionToplevels ();

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

@@ -112,8 +112,7 @@ namespace Terminal.Gui {
 			Width = Dim.Fill ();
 			Width = Dim.Fill ();
 			Height = 1;
 			Height = 1;
 
 
-			// This is never called if it is invoked later on another place.
-			Application.OnLoad += () => {
+			Application.Loaded += (sender, e) => {
 				X = 0;
 				X = 0;
 				Height = 1;
 				Height = 1;
 #if SNAP_TO_TOP
 #if SNAP_TO_TOP
@@ -125,7 +124,7 @@ namespace Terminal.Gui {
 				case StatusBarStyle.SnapToBottom:
 				case StatusBarStyle.SnapToBottom:
 #endif
 #endif
 					if (Parent == null) {
 					if (Parent == null) {
-						Y = Application.Driver.Rows - 1; // TODO: using internals of Application
+						Y = e.Rows - 1; 
 					} else {
 					} else {
 						Y = Pos.Bottom (Parent);
 						Y = Pos.Bottom (Parent);
 					}
 					}