From 2c5d09a521b040a5001948d61ed23d7b9947320f Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Mon, 18 May 2020 23:00:54 -0600 Subject: [PATCH 1/4] Refactored onload/onresize events to use event vs. Action --- Example/demo.cs | 2 +- Terminal.Gui/Core.cs | 70 ++++++++++++++++++++------------- Terminal.Gui/Views/StatusBar.cs | 4 +- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/Example/demo.cs b/Example/demo.cs index 6db1bb40b..91460b29a 100644 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -604,7 +604,7 @@ static class Demo { var bottom2 = new Label ("This should go on the bottom of another top-level!"); top.Add (bottom2); - Application.OnLoad = () => { + Application.Loaded += (sender, e) => { bottom.X = win.X; bottom.Y = Pos.Bottom (win) - Pos.Top (win) - margin; bottom2.X = Pos.Left (win); diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index 91135771e..e2b04e953 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -607,7 +607,7 @@ namespace Terminal.Gui { return; while (subviews.Count > 0) { - Remove (subviews[0]); + Remove (subviews [0]); } } @@ -705,9 +705,9 @@ namespace Terminal.Gui { { PerformActionForSubview (subview, x => { var idx = subviews.IndexOf (x); - if (idx+1 < subviews.Count) { + if (idx + 1 < subviews.Count) { subviews.Remove (x); - subviews.Insert (idx+1, x); + subviews.Insert (idx + 1, x); } }); } @@ -913,7 +913,7 @@ namespace Terminal.Gui { OnEnter (); else OnLeave (); - SetNeedsDisplay (); + SetNeedsDisplay (); base.HasFocus = value; // Remove focus down the chain of subviews if focus is removed @@ -1063,7 +1063,7 @@ namespace Terminal.Gui { focused.EnsureFocus (); // Send focus upwards - SuperView?.SetFocus(this); + SuperView?.SetFocus (this); } /// @@ -1176,7 +1176,7 @@ namespace Terminal.Gui { public void FocusLast () { if (subviews == null) { - SuperView?.SetFocus(this); + SuperView?.SetFocus (this); return; } @@ -1223,7 +1223,7 @@ namespace Terminal.Gui { w.FocusLast (); SetFocus (w); - return true; + return true; } } if (focused != null) { @@ -1614,7 +1614,7 @@ namespace Terminal.Gui { internal void EnsureVisibleBounds (Toplevel top, int x, int y, out int nx, out int ny) { 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; if (SuperView == null) m = Application.Top.HasMenuBar; @@ -1628,7 +1628,7 @@ namespace Terminal.Gui { s = ((Toplevel)SuperView).HasStatusBar; l = s ? Driver.Rows - 1 : Driver.Rows; 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 () @@ -1775,7 +1775,7 @@ namespace Terminal.Gui { this.Title = title; int wb = 1 + padding; this.padding = padding; - contentView = new ContentView () { + contentView = new ContentView () { X = wb, Y = wb, Width = Dim.Fill (wb), @@ -2056,7 +2056,7 @@ namespace Terminal.Gui { if (UseSystemConsole) { mainLoopDriver = new Mono.Terminal.NetMainLoop (); 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 (); mainLoopDriver = windowsDriver; Driver = windowsDriver; @@ -2114,7 +2114,7 @@ namespace Terminal.Gui { static void ProcessKeyEvent (KeyEvent ke) { - var chain = toplevels.ToList(); + var chain = toplevels.ToList (); foreach (var topLevel in chain) { if (topLevel.ProcessHotKey (ke)) return; @@ -2171,7 +2171,7 @@ namespace Terminal.Gui { return null; } - if (start.InternalSubviews != null){ + if (start.InternalSubviews != null) { int count = start.InternalSubviews.Count; if (count > 0) { var rx = x - startFrame.X; @@ -2187,8 +2187,8 @@ namespace Terminal.Gui { } } } - resx = x-startFrame.X; - resy = y-startFrame.Y; + resx = x - startFrame.X; + resy = y - startFrame.Y; return start; } @@ -2281,9 +2281,10 @@ namespace Terminal.Gui { } /// - /// 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. /// - static public Action OnLoad; + static public event EventHandler Loaded; /// /// Building block API: Prepares the provided toplevel for execution. @@ -2307,11 +2308,11 @@ namespace Terminal.Gui { Init (); if (toplevel is ISupportInitializeNotification initializableNotification && !initializableNotification.IsInitialized) { - initializableNotification.BeginInit(); - initializableNotification.EndInit(); + initializableNotification.BeginInit (); + initializableNotification.EndInit (); } else if (toplevel is ISupportInitialize initializable) { - initializable.BeginInit(); - initializable.EndInit(); + initializable.BeginInit (); + initializable.EndInit (); } toplevels.Push (toplevel); Current = toplevel; @@ -2319,7 +2320,7 @@ namespace Terminal.Gui { if (toplevel.LayoutStyle == LayoutStyle.Computed) toplevel.RelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows)); toplevel.LayoutSubviews (); - OnLoad?.Invoke (); + Loaded?.Invoke (null, new ResizedEventArgs () { Rows = Driver.Rows, Cols = Driver.Cols } ); toplevel.WillPresent (); Redraw (toplevel); toplevel.PositionCursor (); @@ -2386,9 +2387,8 @@ namespace Terminal.Gui { toplevels.Pop (); if (toplevels.Count == 0) Shutdown (); - else - { - Current = toplevels.Peek(); + else { + Current = toplevels.Peek (); Refresh (); } } @@ -2450,7 +2450,7 @@ namespace Terminal.Gui { /// public static void Run () where T : Toplevel, new() { - Init (() => new T()); + Init (() => new T ()); Run (Top); } @@ -2494,15 +2494,29 @@ namespace Terminal.Gui { Current.Running = false; } + /// + /// Event arguments for the event. + /// + public class ResizedEventArgs : EventArgs { + /// + /// The number of rows in the resized terminal. + /// + public int Rows { get; set; } + /// + /// The number of columns in the resized terminal. + /// + public int Cols { get; set; } + } + /// /// Invoked when the terminal was resized. /// - static public Action OnResized; + static public event EventHandler Resized; static void TerminalResized () { - OnResized?.Invoke (); var full = new Rect (0, 0, Driver.Cols, Driver.Rows); + Resized?.Invoke (null, new ResizedEventArgs () { Cols = full.Width, Rows = full.Height }); Driver.Clip = full; foreach (var t in toplevels) { t.PositionToplevels (); diff --git a/Terminal.Gui/Views/StatusBar.cs b/Terminal.Gui/Views/StatusBar.cs index fe1573a91..044321f98 100644 --- a/Terminal.Gui/Views/StatusBar.cs +++ b/Terminal.Gui/Views/StatusBar.cs @@ -102,7 +102,7 @@ namespace Terminal.Gui { CanFocus = false; ColorScheme = Colors.Menu; - Application.OnLoad += () => { + Application.Loaded += (sender, e) => { X = 0; Height = 1; #if SNAP_TO_TOP @@ -114,7 +114,7 @@ namespace Terminal.Gui { case StatusBarStyle.SnapToBottom: #endif if (Parent == null) { - Y = Application.Driver.Rows - 1; // TODO: using internals of Application + Y = e.Rows - 1; } else { Y = Pos.Bottom (Parent); } From c7b4b3472a1c8a9b9153503fb23f0c6b77b995fe Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Mon, 18 May 2020 23:08:40 -0600 Subject: [PATCH 2/4] oops. left args off Resized --- Terminal.Gui/Core.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index e2b04e953..72b5085b8 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -2509,9 +2509,9 @@ namespace Terminal.Gui { } /// - /// Invoked when the terminal was resized. + /// Invoked when the terminal was resized. The new size of the terminal is provided. /// - static public event EventHandler Resized; + static public event EventHandler Resized; static void TerminalResized () { From dc87bc4e07517c06f41034ac26e7ea3983520e49 Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Mon, 18 May 2020 23:10:45 -0600 Subject: [PATCH 3/4] sorry. for. my. OCD. --- Terminal.Gui/Core.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index 72b5085b8..96f6f38be 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -1985,7 +1985,7 @@ namespace Terminal.Gui { /// /// See also /// - static public event EventHandler Iteration; + public static event EventHandler Iteration; /// /// Returns a rectangle that is centered in the screen for the provided size. @@ -2219,7 +2219,7 @@ namespace Terminal.Gui { /// /// Merely a debugging aid to see the raw mouse events /// - static public Action RootMouseEvent; + public static Action RootMouseEvent; internal static View wantContinuousButtonPressedView; static View lastMouseOwnerView; @@ -2284,7 +2284,7 @@ namespace Terminal.Gui { /// This event is fired once when the application is first loaded. The dimensions of the /// terminal are provided. /// - static public event EventHandler Loaded; + public static event EventHandler Loaded; /// /// Building block API: Prepares the provided toplevel for execution. @@ -2299,7 +2299,7 @@ namespace Terminal.Gui { /// the method, and then the method upon termination which will /// undo these changes. /// - static public RunState Begin (Toplevel toplevel) + public static RunState Begin (Toplevel toplevel) { if (toplevel == null) throw new ArgumentNullException (nameof (toplevel)); @@ -2333,7 +2333,7 @@ namespace Terminal.Gui { /// Building block API: completes the execution of a Toplevel that was started with Begin. /// /// The runstate returned by the method. - static public void End (RunState runState) + public static void End (RunState runState) { if (runState == null) throw new ArgumentNullException (nameof (runState)); @@ -2511,7 +2511,7 @@ namespace Terminal.Gui { /// /// Invoked when the terminal was resized. The new size of the terminal is provided. /// - static public event EventHandler Resized; + public static event EventHandler Resized; static void TerminalResized () { From 9fcb56fba216e21b971a327179b09bdb02f1fa5c Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Thu, 21 May 2020 08:23:33 -0600 Subject: [PATCH 4/4] merged with latest master --- UICatalog/Properties/launchSettings.json | 1 - 1 file changed, 1 deletion(-) diff --git a/UICatalog/Properties/launchSettings.json b/UICatalog/Properties/launchSettings.json index 3941ffe72..68c82f53f 100644 --- a/UICatalog/Properties/launchSettings.json +++ b/UICatalog/Properties/launchSettings.json @@ -2,7 +2,6 @@ "profiles": { "UICatalog": { "commandName": "Project" - } } } \ No newline at end of file