From 022050db730f7fc53d2dfb8e171e6da2ca82d137 Mon Sep 17 00:00:00 2001 From: Tig Date: Wed, 24 Jul 2024 15:09:48 -0600 Subject: [PATCH] Fixed nullable warnings 7 --- CommunityToolkitExample/Program.cs | 2 +- .../Application/Application.Initialization.cs | 17 +- .../Application/Application.Keyboard.cs | 4 +- Terminal.Gui/Application/Application.Mouse.cs | 22 +- .../Application/Application.Navigation.cs | 197 ++++++++++++++++-- Terminal.Gui/Application/Application.Run.cs | 6 +- .../Application/Application.Toplevel.cs | 142 ------------- Terminal.Gui/Application/Application.cs | 6 +- .../Application/MainLoopSyncContext.cs | 2 +- Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs | 10 +- Terminal.Gui/View/Adornment/ShadowView.cs | 4 +- Terminal.Gui/View/EventArgs.cs | 2 +- Terminal.Gui/View/Layout/Dim.cs | 2 +- Terminal.Gui/View/Layout/DimView.cs | 4 +- Terminal.Gui/View/Layout/PosView.cs | 8 +- Terminal.Gui/View/View.cs | 2 +- Terminal.Gui/Views/TextValidateField.cs | 8 +- Terminal.Gui/Views/Tile.cs | 2 +- UICatalog/Scenarios/Buttons.cs | 4 +- UICatalog/Scenarios/ListViewWithSelection.cs | 2 +- UICatalog/Scenarios/MenuBarScenario.cs | 8 +- UICatalog/UICatalog.cs | 2 +- UnitTests/Application/ApplicationTests.cs | 20 +- UnitTests/Application/KeyboardTests.cs | 2 +- UnitTests/UICatalog/ScenarioTests.cs | 2 +- UnitTests/View/DrawTests.cs | 8 +- UnitTests/Views/MenuBarTests.cs | 19 +- 27 files changed, 263 insertions(+), 244 deletions(-) diff --git a/CommunityToolkitExample/Program.cs b/CommunityToolkitExample/Program.cs index 0d4f21c30..a9ababfec 100644 --- a/CommunityToolkitExample/Program.cs +++ b/CommunityToolkitExample/Program.cs @@ -12,7 +12,7 @@ public static class Program Services = ConfigureServices (); Application.Init (); Application.Run (Services.GetRequiredService ()); - Application.Top.Dispose(); + Application.Top?.Dispose(); Application.Shutdown (); } diff --git a/Terminal.Gui/Application/Application.Initialization.cs b/Terminal.Gui/Application/Application.Initialization.cs index 0d9b2caf5..37b34a358 100644 --- a/Terminal.Gui/Application/Application.Initialization.cs +++ b/Terminal.Gui/Application/Application.Initialization.cs @@ -38,8 +38,8 @@ public static partial class Application // Initialization (Init/Shutdown) [RequiresDynamicCode ("AOT")] public static void Init (ConsoleDriver? driver = null, string? driverName = null) { InternalInit (driver, driverName); } - internal static bool _initialized; - internal static int _mainThreadId = -1; + internal static bool IsInitialized { get; set; } + internal static int MainThreadId { get; set; } = -1; // INTERNAL function for initializing an app with a Toplevel factory object, driver, and mainloop. // @@ -58,12 +58,12 @@ public static partial class Application // Initialization (Init/Shutdown) bool calledViaRunT = false ) { - if (_initialized && driver is null) + if (IsInitialized && driver is null) { return; } - if (_initialized) + if (IsInitialized) { throw new InvalidOperationException ("Init has already been called and must be bracketed by Shutdown."); } @@ -154,9 +154,9 @@ public static partial class Application // Initialization (Init/Shutdown) SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ()); SupportedCultures = GetSupportedCultures (); - _mainThreadId = Thread.CurrentThread.ManagedThreadId; - _initialized = true; - InitializedChanged?.Invoke (null, new (in _initialized)); + MainThreadId = Thread.CurrentThread.ManagedThreadId; + bool init = IsInitialized = true; + InitializedChanged?.Invoke (null, new (init)); } private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); } @@ -198,7 +198,8 @@ public static partial class Application // Initialization (Init/Shutdown) // TODO: Throw an exception if Init hasn't been called. ResetState (); PrintJsonErrors (); - InitializedChanged?.Invoke (null, new (in _initialized)); + bool init = IsInitialized; + InitializedChanged?.Invoke (null, new (in init)); } /// diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index 10419bf80..59c78a83e 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -110,7 +110,7 @@ public static partial class Application // Keyboard handling /// if the key was handled. public static bool OnKeyDown (Key keyEvent) { - if (!_initialized) + if (!IsInitialized) { return true; } @@ -210,7 +210,7 @@ public static partial class Application // Keyboard handling /// if the key was handled. public static bool OnKeyUp (Key a) { - if (!_initialized) + if (!IsInitialized) { return true; } diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 713e6375d..be7c38df6 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -64,7 +64,7 @@ public static partial class Application // Mouse handling } } - private static bool OnGrabbingMouse (View view) + private static bool OnGrabbingMouse (View? view) { if (view is null) { @@ -77,7 +77,7 @@ public static partial class Application // Mouse handling return evArgs.Cancel; } - private static bool OnUnGrabbingMouse (View view) + private static bool OnUnGrabbingMouse (View? view) { if (view is null) { @@ -90,7 +90,7 @@ public static partial class Application // Mouse handling return evArgs.Cancel; } - private static void OnGrabbedMouse (View view) + private static void OnGrabbedMouse (View? view) { if (view is null) { @@ -100,7 +100,7 @@ public static partial class Application // Mouse handling GrabbedMouse?.Invoke (view, new (view)); } - private static void OnUnGrabbedMouse (View view) + private static void OnUnGrabbedMouse (View? view) { if (view is null) { @@ -113,7 +113,7 @@ public static partial class Application // Mouse handling #nullable enable // Used by OnMouseEvent to track the last view that was clicked on. - internal static View? _mouseEnteredView; + internal static View? MouseEnteredView { get; set; } /// Event fired when a mouse move or click occurs. Coordinates are screen relative. /// @@ -166,7 +166,7 @@ public static partial class Application // Mouse handling if ((MouseGrabView.Viewport with { Location = Point.Empty }).Contains (viewRelativeMouseEvent.Position) is false) { // The mouse has moved outside the bounds of the view that grabbed the mouse - _mouseEnteredView?.NewMouseLeaveEvent (mouseEvent); + MouseEnteredView?.NewMouseLeaveEvent (mouseEvent); } //System.Diagnostics.Debug.WriteLine ($"{nme.Flags};{nme.X};{nme.Y};{mouseGrabView}"); @@ -242,16 +242,16 @@ public static partial class Application // Mouse handling return; } - if (_mouseEnteredView is null) + if (MouseEnteredView is null) { - _mouseEnteredView = view; + MouseEnteredView = view; view.NewMouseEnterEvent (me); } - else if (_mouseEnteredView != view) + else if (MouseEnteredView != view) { - _mouseEnteredView.NewMouseLeaveEvent (me); + MouseEnteredView.NewMouseLeaveEvent (me); view.NewMouseEnterEvent (me); - _mouseEnteredView = view; + MouseEnteredView = view; } if (!view.WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition) diff --git a/Terminal.Gui/Application/Application.Navigation.cs b/Terminal.Gui/Application/Application.Navigation.cs index 0bbce67e6..d3f42fd64 100644 --- a/Terminal.Gui/Application/Application.Navigation.cs +++ b/Terminal.Gui/Application/Application.Navigation.cs @@ -1,3 +1,4 @@ +#nullable enable namespace Terminal.Gui; public static partial class Application @@ -6,30 +7,32 @@ public static partial class Application /// Gets the list of the Overlapped children which are not modal from the /// . /// - public static List OverlappedChildren + public static List? OverlappedChildren { get { if (OverlappedTop is { }) { - List _overlappedChildren = new (); + List overlappedChildren = new (); - foreach (Toplevel top in _topLevels) + lock (_topLevels) { - if (top != OverlappedTop && !top.Modal) + foreach (Toplevel top in _topLevels) { - _overlappedChildren.Add (top); + if (top != OverlappedTop && !top.Modal) + { + overlappedChildren.Add (top); + } } } - return _overlappedChildren; + return overlappedChildren; } return null; } } -#nullable enable /// /// The object used for the application on startup which /// is true. @@ -46,7 +49,6 @@ public static partial class Application return null; } } -#nullable restore /// Brings the superview of the most focused overlapped view is on front. public static void BringOverlappedTopToFront () @@ -56,9 +58,9 @@ public static partial class Application return; } - View top = FindTopFromView (Top?.MostFocused); + View? top = FindTopFromView (Top?.MostFocused); - if (top is Toplevel && Top.Subviews.Count > 1 && Top.Subviews [^1] != top) + if (top is Toplevel && Top?.Subviews.Count > 1 && Top.Subviews [^1] != top) { Top.BringSubviewToFront (top); } @@ -68,9 +70,9 @@ public static partial class Application /// The type. /// The strings to exclude. /// The matched view. - public static Toplevel GetTopOverlappedChild (Type type = null, string [] exclude = null) + public static Toplevel? GetTopOverlappedChild (Type? type = null, string []? exclude = null) { - if (OverlappedTop is null) + if (OverlappedChildren is null || OverlappedTop is null) { return null; } @@ -118,7 +120,7 @@ public static partial class Application /// Move to the next Overlapped child from the . public static void OverlappedMoveNext () { - if (OverlappedTop is { } && !Current.Modal) + if (OverlappedTop is { } && !Current!.Modal) { lock (_topLevels) { @@ -133,7 +135,7 @@ public static partial class Application } else if (isOverlapped && _topLevels.Peek () == OverlappedTop) { - MoveCurrent (Top); + MoveCurrent (Top!); break; } @@ -149,7 +151,7 @@ public static partial class Application /// Move to the previous Overlapped child from the . public static void OverlappedMovePrevious () { - if (OverlappedTop is { } && !Current.Modal) + if (OverlappedTop is { } && !Current!.Modal) { lock (_topLevels) { @@ -164,7 +166,7 @@ public static partial class Application } else if (isOverlapped && _topLevels.Peek () == OverlappedTop) { - MoveCurrent (Top); + MoveCurrent (Top!); break; } @@ -184,13 +186,16 @@ public static partial class Application return false; } - foreach (Toplevel top in _topLevels) + lock (_topLevels) { - if (top != Current && top.Visible && (top.NeedsDisplay || top.SubViewNeedsDisplay || top.LayoutNeeded)) + foreach (Toplevel top in _topLevels) { - OverlappedTop.SetSubViewNeedsDisplay (); + if (top != Current && top.Visible && (top.NeedsDisplay || top.SubViewNeedsDisplay || top.LayoutNeeded)) + { + OverlappedTop.SetSubViewNeedsDisplay (); - return true; + return true; + } } } @@ -208,4 +213,154 @@ public static partial class Application return false; } -} \ No newline at end of file + + /// + /// Finds the first Toplevel in the stack that is Visible and who's Frame contains the . + /// + /// + /// + /// + private static Toplevel? FindDeepestTop (Toplevel start, in Point location) + { + if (!start.Frame.Contains (location)) + { + return null; + } + + lock (_topLevels) + { + if (_topLevels is not { Count: > 0 }) + { + return start; + } + + int rx = location.X - start.Frame.X; + int ry = location.Y - start.Frame.Y; + + foreach (Toplevel t in _topLevels) + { + if (t == Current) + { + continue; + } + + if (t != start && t.Visible && t.Frame.Contains (rx, ry)) + { + start = t; + + break; + } + } + } + + return start; + } + + /// + /// Given , returns the first Superview up the chain that is . + /// + private static View? FindTopFromView (View? view) + { + if (view is null) + { + return null; + } + + View top = view.SuperView is { } && view.SuperView != Top + ? view.SuperView + : view; + + while (top?.SuperView is { } && top?.SuperView != Top) + { + top = top!.SuperView; + } + + return top; + } + + /// + /// If the is not the then is moved to the top of + /// the Toplevel stack and made Current. + /// + /// + /// + private static bool MoveCurrent (Toplevel top) + { + // The Current is modal and the top is not modal Toplevel then + // the Current must be moved above the first not modal Toplevel. + if (OverlappedTop is { } + && top != OverlappedTop + && top != Current + && Current?.Modal == true + && !_topLevels.Peek ().Modal) + { + lock (_topLevels) + { + _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ()); + } + + var index = 0; + Toplevel [] savedToplevels = _topLevels.ToArray (); + + foreach (Toplevel t in savedToplevels) + { + if (!t!.Modal && t != Current && t != top && t != savedToplevels [index]) + { + lock (_topLevels) + { + _topLevels.MoveTo (top, index, new ToplevelEqualityComparer ()); + } + } + + index++; + } + + return false; + } + + // The Current and the top are both not running Toplevel then + // the top must be moved above the first not running Toplevel. + if (OverlappedTop is { } + && top != OverlappedTop + && top != Current + && Current?.Running == false + && top?.Running == false) + { + lock (_topLevels) + { + _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ()); + } + + var index = 0; + + foreach (Toplevel t in _topLevels.ToArray ()) + { + if (!t.Running && t != Current && index > 0) + { + lock (_topLevels) + { + _topLevels.MoveTo (top, index - 1, new ToplevelEqualityComparer ()); + } + } + + index++; + } + + return false; + } + + if ((OverlappedTop is { } && top?.Modal == true && _topLevels.Peek () != top) + || (OverlappedTop is { } && Current != OverlappedTop && Current?.Modal == false && top == OverlappedTop) + || (OverlappedTop is { } && Current?.Modal == false && top != Current) + || (OverlappedTop is { } && Current?.Modal == true && top == OverlappedTop)) + { + lock (_topLevels) + { + _topLevels.MoveTo (top, 0, new ToplevelEqualityComparer ()); + Current = top; + } + } + + return true; + } +} diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index 9a79af3d4..87bfde4c0 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -333,7 +333,7 @@ public static partial class Application // Run (Begin, Run, End, Stop) public static T Run (Func? errorHandler = null, ConsoleDriver? driver = null) where T : Toplevel, new () { - if (!_initialized) + if (!IsInitialized) { // Init() has NOT been called. InternalInit (driver, null, true); @@ -388,7 +388,7 @@ public static partial class Application // Run (Begin, Run, End, Stop) { ArgumentNullException.ThrowIfNull (view); - if (_initialized) + if (IsInitialized) { if (Driver is null) { @@ -824,7 +824,7 @@ public static partial class Application // Run (Begin, Run, End, Stop) } else { - if (_topLevels.Count > 1 && _topLevels.Peek () == OverlappedTop && OverlappedChildren.Any (t => t.Visible) is { }) + if (_topLevels.Count > 1 && _topLevels.Peek () == OverlappedTop && OverlappedChildren?.Any (t => t.Visible) != null) { OverlappedMoveNext (); } diff --git a/Terminal.Gui/Application/Application.Toplevel.cs b/Terminal.Gui/Application/Application.Toplevel.cs index d45360d64..a050e28f1 100644 --- a/Terminal.Gui/Application/Application.Toplevel.cs +++ b/Terminal.Gui/Application/Application.Toplevel.cs @@ -53,148 +53,6 @@ public static partial class Application // Toplevel handling } } - /// - /// Finds the first Toplevel in the stack that is Visible and who's Frame contains the . - /// - /// - /// - /// - private static Toplevel? FindDeepestTop (Toplevel start, in Point location) - { - if (!start.Frame.Contains (location)) - { - return null; - } - - if (_topLevels is { Count: > 0 }) - { - int rx = location.X - start.Frame.X; - int ry = location.Y - start.Frame.Y; - - foreach (Toplevel t in _topLevels) - { - if (t != Current) - { - if (t != start && t.Visible && t.Frame.Contains (rx, ry)) - { - start = t; - - break; - } - } - } - } - - return start; - } - - /// - /// Given , returns the first Superview up the chain that is . - /// - private static View? FindTopFromView (View? view) - { - if (view is null) - { - return null; - } - - View top = view.SuperView is { } && view.SuperView != Top - ? view.SuperView - : view; - - while (top?.SuperView is { } && top?.SuperView != Top) - { - top = top!.SuperView; - } - - return top; - } - - /// - /// If the is not the then is moved to the top of the Toplevel stack and made Current. - /// - /// - /// - private static bool MoveCurrent (Toplevel top) - { - // The Current is modal and the top is not modal Toplevel then - // the Current must be moved above the first not modal Toplevel. - if (OverlappedTop is { } - && top != OverlappedTop - && top != Current - && Current?.Modal == true - && !_topLevels.Peek ().Modal) - { - lock (_topLevels) - { - _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ()); - } - - var index = 0; - Toplevel [] savedToplevels = _topLevels.ToArray (); - - foreach (Toplevel t in savedToplevels) - { - if (!t!.Modal && t != Current && t != top && t != savedToplevels [index]) - { - lock (_topLevels) - { - _topLevels.MoveTo (top, index, new ToplevelEqualityComparer ()); - } - } - - index++; - } - - return false; - } - - // The Current and the top are both not running Toplevel then - // the top must be moved above the first not running Toplevel. - if (OverlappedTop is { } - && top != OverlappedTop - && top != Current - && Current?.Running == false - && top?.Running == false) - { - lock (_topLevels) - { - _topLevels.MoveTo (Current, 0, new ToplevelEqualityComparer ()); - } - - var index = 0; - - foreach (Toplevel t in _topLevels.ToArray ()) - { - if (!t.Running && t != Current && index > 0) - { - lock (_topLevels) - { - _topLevels.MoveTo (top, index - 1, new ToplevelEqualityComparer ()); - } - } - - index++; - } - - return false; - } - - if ((OverlappedTop is { } && top?.Modal == true && _topLevels.Peek () != top) - || (OverlappedTop is { } && Current != OverlappedTop && Current?.Modal == false && top == OverlappedTop) - || (OverlappedTop is { } && Current?.Modal == false && top != Current) - || (OverlappedTop is { } && Current?.Modal == true && top == OverlappedTop)) - { - lock (_topLevels) - { - _topLevels.MoveTo (top, 0, new ToplevelEqualityComparer ()); - Current = top; - } - } - - return true; - } - /// Invoked when the terminal's size changed. The new size of the terminal is provided. /// /// Event handlers can set to to prevent diff --git a/Terminal.Gui/Application/Application.cs b/Terminal.Gui/Application/Application.cs index 76c05922b..75fbd7191 100644 --- a/Terminal.Gui/Application/Application.cs +++ b/Terminal.Gui/Application/Application.cs @@ -84,7 +84,7 @@ public static partial class Application // MainLoop stuff MainLoop?.Dispose (); MainLoop = null; - _mainThreadId = -1; + MainThreadId = -1; Iteration = null; EndAfterFirstIteration = false; @@ -108,10 +108,10 @@ public static partial class Application NotifyNewRunState = null; NotifyStopRunState = null; MouseGrabView = null; - _initialized = false; + IsInitialized = false; // Mouse - _mouseEnteredView = null; + MouseEnteredView = null; WantContinuousButtonPressedView = null; MouseEvent = null; GrabbedMouse = null; diff --git a/Terminal.Gui/Application/MainLoopSyncContext.cs b/Terminal.Gui/Application/MainLoopSyncContext.cs index 5290a2076..749c76268 100644 --- a/Terminal.Gui/Application/MainLoopSyncContext.cs +++ b/Terminal.Gui/Application/MainLoopSyncContext.cs @@ -23,7 +23,7 @@ internal sealed class MainLoopSyncContext : SynchronizationContext //_mainLoop.Driver.Wakeup (); public override void Send (SendOrPostCallback d, object state) { - if (Thread.CurrentThread.ManagedThreadId == Application._mainThreadId) + if (Thread.CurrentThread.ManagedThreadId == Application.MainThreadId) { d (state); } diff --git a/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs b/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs index 5e9a7dad3..e99521d1e 100644 --- a/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs @@ -351,7 +351,7 @@ public abstract class ConsoleDriver { Contents [row, c].IsDirty = true; } - _dirtyLines [row] = true; + _dirtyLines! [row] = true; } } } @@ -380,7 +380,7 @@ public abstract class ConsoleDriver Rune = (rune != default ? rune : (Rune)' '), Attribute = CurrentAttribute, IsDirty = true }; - _dirtyLines [r] = true; + _dirtyLines! [r] = true; } } } @@ -561,7 +561,7 @@ public abstract class ConsoleDriver #region Mouse and Keyboard /// Event fired when a key is pressed down. This is a precursor to . - public event EventHandler KeyDown; + public event EventHandler? KeyDown; /// /// Called when a key is pressed down. Fires the event. This is a precursor to @@ -575,7 +575,7 @@ public abstract class ConsoleDriver /// Drivers that do not support key release events will fire this event after processing is /// complete. /// - public event EventHandler KeyUp; + public event EventHandler? KeyUp; /// Called when a key is released. Fires the event. /// @@ -586,7 +586,7 @@ public abstract class ConsoleDriver public void OnKeyUp (Key a) { KeyUp?.Invoke (this, a); } /// Event fired when a mouse event occurs. - public event EventHandler MouseEvent; + public event EventHandler? MouseEvent; /// Called when a mouse event occurs. Fires the event. /// diff --git a/Terminal.Gui/View/Adornment/ShadowView.cs b/Terminal.Gui/View/Adornment/ShadowView.cs index ad06dc754..e6009759a 100644 --- a/Terminal.Gui/View/Adornment/ShadowView.cs +++ b/Terminal.Gui/View/Adornment/ShadowView.cs @@ -109,7 +109,7 @@ internal class ShadowView : View for (int i = screen.X; i < screen.X + screen.Width - 1; i++) { Driver.Move (i, screen.Y); - Driver.AddRune (Driver.Contents [screen.Y, i].Rune); + Driver.AddRune (Driver.Contents! [screen.Y, i].Rune); } } @@ -133,7 +133,7 @@ internal class ShadowView : View for (int i = screen.Y; i < screen.Y + viewport.Height; i++) { Driver.Move (screen.X, i); - Driver.AddRune (Driver.Contents [i, screen.X].Rune); + Driver.AddRune (Driver.Contents! [i, screen.X].Rune); } } } diff --git a/Terminal.Gui/View/EventArgs.cs b/Terminal.Gui/View/EventArgs.cs index 03309a0f5..1de2347c6 100644 --- a/Terminal.Gui/View/EventArgs.cs +++ b/Terminal.Gui/View/EventArgs.cs @@ -11,7 +11,7 @@ public class EventArgs : EventArgs where T : notnull /// Initializes a new instance of the class. /// The current value of the property. /// The type of the value. - public EventArgs (ref readonly T currentValue) { CurrentValue = currentValue; } + public EventArgs (in T currentValue) { CurrentValue = currentValue; } /// The current value of the property. public T CurrentValue { get; } diff --git a/Terminal.Gui/View/Layout/Dim.cs b/Terminal.Gui/View/Layout/Dim.cs index 3102d5d3c..f6f50f798 100644 --- a/Terminal.Gui/View/Layout/Dim.cs +++ b/Terminal.Gui/View/Layout/Dim.cs @@ -257,7 +257,7 @@ public abstract class Dim } var newDim = new DimCombine (AddOrSubtract.Subtract, left, right); - (left as DimView)?.Target.SetNeedsLayout (); + (left as DimView)?.Target?.SetNeedsLayout (); return newDim; } diff --git a/Terminal.Gui/View/Layout/DimView.cs b/Terminal.Gui/View/Layout/DimView.cs index e95efd4fb..7a7568a95 100644 --- a/Terminal.Gui/View/Layout/DimView.cs +++ b/Terminal.Gui/View/Layout/DimView.cs @@ -52,8 +52,8 @@ public class DimView : Dim { return Dimension switch { - Dimension.Height => Target.Frame.Height, - Dimension.Width => Target.Frame.Width, + Dimension.Height => Target!.Frame.Height, + Dimension.Width => Target!.Frame.Width, _ => 0 }; } diff --git a/Terminal.Gui/View/Layout/PosView.cs b/Terminal.Gui/View/Layout/PosView.cs index 8ceba980f..a46f6898a 100644 --- a/Terminal.Gui/View/Layout/PosView.cs +++ b/Terminal.Gui/View/Layout/PosView.cs @@ -47,10 +47,10 @@ public class PosView (View? view, Side side) : Pos { return Side switch { - Side.Left => Target.Frame.X, - Side.Top => Target.Frame.Y, - Side.Right => Target.Frame.Right, - Side.Bottom => Target.Frame.Bottom, + Side.Left => Target!.Frame.X, + Side.Top => Target!.Frame.Y, + Side.Right => Target!.Frame.Right, + Side.Bottom => Target!.Frame.Bottom, _ => 0 }; } diff --git a/Terminal.Gui/View/View.cs b/Terminal.Gui/View/View.cs index 68eb1853f..d340a5aa4 100644 --- a/Terminal.Gui/View/View.cs +++ b/Terminal.Gui/View/View.cs @@ -490,7 +490,7 @@ public partial class View : Responder, ISupportInitializeNotification /// Called when the has been changed. Invokes the event. protected void OnTitleChanged () { - TitleChanged?.Invoke (this, new (ref _title)); + TitleChanged?.Invoke (this, new (in _title)); } /// diff --git a/Terminal.Gui/Views/TextValidateField.cs b/Terminal.Gui/Views/TextValidateField.cs index 045f6df3f..b0df130b3 100644 --- a/Terminal.Gui/Views/TextValidateField.cs +++ b/Terminal.Gui/Views/TextValidateField.cs @@ -206,7 +206,7 @@ namespace Terminal.Gui if (result) { - OnTextChanged (new EventArgs (ref oldValue)); + OnTextChanged (new EventArgs (in oldValue)); } return result; @@ -220,7 +220,7 @@ namespace Terminal.Gui if (result) { - OnTextChanged (new EventArgs (ref oldValue)); + OnTextChanged (new EventArgs (in oldValue)); } return result; @@ -333,7 +333,7 @@ namespace Terminal.Gui { string oldValue = Text; _text.RemoveAt (pos); - OnTextChanged (new EventArgs (ref oldValue)); + OnTextChanged (new EventArgs (in oldValue)); } return true; @@ -349,7 +349,7 @@ namespace Terminal.Gui { string oldValue = Text; _text.Insert (pos, (Rune)ch); - OnTextChanged (new EventArgs (ref oldValue)); + OnTextChanged (new EventArgs (in oldValue)); return true; } diff --git a/Terminal.Gui/Views/Tile.cs b/Terminal.Gui/Views/Tile.cs index ebc4b9f59..5224db8b4 100644 --- a/Terminal.Gui/Views/Tile.cs +++ b/Terminal.Gui/Views/Tile.cs @@ -62,7 +62,7 @@ public class Tile /// The new to be replaced. public virtual void OnTitleChanged (string oldTitle, string newTitle) { - var args = new EventArgs (ref newTitle); + var args = new EventArgs (in newTitle); TitleChanged?.Invoke (this, args); } diff --git a/UICatalog/Scenarios/Buttons.cs b/UICatalog/Scenarios/Buttons.cs index aab815a96..2ea67238e 100644 --- a/UICatalog/Scenarios/Buttons.cs +++ b/UICatalog/Scenarios/Buttons.cs @@ -527,8 +527,8 @@ public class Buttons : Scenario } _value = value; - _number.Text = _value.ToString (); - ValueChanged?.Invoke (this, new (ref _value)); + _number.Text = _value.ToString ()!; + ValueChanged?.Invoke (this, new (in _value)); } } diff --git a/UICatalog/Scenarios/ListViewWithSelection.cs b/UICatalog/Scenarios/ListViewWithSelection.cs index afcd8a542..27e1bf5d2 100644 --- a/UICatalog/Scenarios/ListViewWithSelection.cs +++ b/UICatalog/Scenarios/ListViewWithSelection.cs @@ -205,7 +205,7 @@ public class ListViewWithSelection : Scenario /// public event NotifyCollectionChangedEventHandler CollectionChanged; - public int Count => Scenarios != null ? Scenarios.Count : 0; + public int Count => Scenarios?.Count ?? 0; public int Length { get; private set; } public bool SuspendCollectionChangedEvent { get => throw new System.NotImplementedException (); set => throw new System.NotImplementedException (); } diff --git a/UICatalog/Scenarios/MenuBarScenario.cs b/UICatalog/Scenarios/MenuBarScenario.cs index 73c767f79..b9c6bee67 100644 --- a/UICatalog/Scenarios/MenuBarScenario.cs +++ b/UICatalog/Scenarios/MenuBarScenario.cs @@ -1,5 +1,6 @@ using System; using Terminal.Gui; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace UICatalog.Scenarios; @@ -64,14 +65,17 @@ public class MenuBarScenario : Scenario menuBar.Key = KeyCode.F9; menuBar.Title = "TestMenuBar"; - bool fnAction (string s) + bool FnAction (string s) { _lastAction.Text = s; return true; } + + // Declare a variable for the function + Func fnActionVariable = FnAction; - menuBar.EnableForDesign ((Func)fnAction); + menuBar.EnableForDesign (ref fnActionVariable); menuBar.MenuOpening += (s, e) => { diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index a2cc9a474..a9014d017 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -104,7 +104,7 @@ internal class UICatalogApp // If no driver is provided, the default driver is used. Option driverOption = new Option ("--driver", "The ConsoleDriver to use.").FromAmong ( Application.GetDriverTypes () - .Select (d => d.Name) + .Select (d => d!.Name) .ToArray () ); diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index f8f1f8029..c600254a2 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -162,7 +162,7 @@ public class ApplicationTests // Set some values Application.Init (driverName: driverType.Name); - Application._initialized = true; + Application.IsInitialized = true; // Reset Application.ResetState (); @@ -191,12 +191,12 @@ public class ApplicationTests Assert.Null (Application.OverlappedTop); // Internal properties - Assert.False (Application._initialized); + Assert.False (Application.IsInitialized); Assert.Equal (Application.GetSupportedCultures (), Application.SupportedCultures); Assert.False (Application._forceFakeConsole); - Assert.Equal (-1, Application._mainThreadId); + Assert.Equal (-1, Application.MainThreadId); Assert.Empty (Application._topLevels); - Assert.Null (Application._mouseEnteredView); + Assert.Null (Application.MouseEnteredView); // Keyboard Assert.Empty (Application.GetViewKeyBindings ()); @@ -218,12 +218,12 @@ public class ApplicationTests CheckReset (); // Set the values that can be set - Application._initialized = true; + Application.IsInitialized = true; Application._forceFakeConsole = true; - Application._mainThreadId = 1; + Application.MainThreadId = 1; //Application._topLevels = new List (); - Application._mouseEnteredView = new (); + Application.MouseEnteredView = new (); //Application.SupportedCultures = new List (); Application.Force16Colors = true; @@ -237,7 +237,7 @@ public class ApplicationTests //Application.OverlappedChildren = new List (); //Application.OverlappedTop = - Application._mouseEnteredView = new (); + Application.MouseEnteredView = new (); //Application.WantContinuousButtonPressedView = new View (); @@ -413,7 +413,7 @@ public class ApplicationTests [AutoInitShutdown] public void Internal_Properties_Correct () { - Assert.True (Application._initialized); + Assert.True (Application.IsInitialized); Assert.Null (Application.Top); RunState rs = Application.Begin (new ()); Assert.Equal (Application.Top, rs.Toplevel); @@ -1206,7 +1206,7 @@ public class ApplicationTests Thread.Sleep ((int)timeoutTime / 10); // Worst case scenario - something went wrong - if (Application._initialized && iteration > 25) + if (Application.IsInitialized && iteration > 25) { _output.WriteLine ($"Too many iterations ({iteration}): Calling Application.RequestStop."); Application.RequestStop (); diff --git a/UnitTests/Application/KeyboardTests.cs b/UnitTests/Application/KeyboardTests.cs index 4dfb5b11a..a61519037 100644 --- a/UnitTests/Application/KeyboardTests.cs +++ b/UnitTests/Application/KeyboardTests.cs @@ -169,7 +169,7 @@ public class KeyboardTests _output.WriteLine ("Iteration: {0}", iteration); iteration++; Assert.True (iteration < 2, "Too many iterations, something is wrong."); - if (Application._initialized) + if (Application.IsInitialized) { _output.WriteLine (" Pressing QuitKey"); Application.OnKeyDown (Application.QuitKey); diff --git a/UnitTests/UICatalog/ScenarioTests.cs b/UnitTests/UICatalog/ScenarioTests.cs index 22a9e25c6..5e0dc2a5c 100644 --- a/UnitTests/UICatalog/ScenarioTests.cs +++ b/UnitTests/UICatalog/ScenarioTests.cs @@ -115,7 +115,7 @@ public class ScenarioTests : TestsAllViews void OnApplicationOnIteration (object s, IterationEventArgs a) { - if (Application._initialized) + if (Application.IsInitialized) { // Press QuitKey //_output.WriteLine ($"Forcing Quit with {Application.QuitKey}"); diff --git a/UnitTests/View/DrawTests.cs b/UnitTests/View/DrawTests.cs index 52b4659f8..19ed80383 100644 --- a/UnitTests/View/DrawTests.cs +++ b/UnitTests/View/DrawTests.cs @@ -48,16 +48,16 @@ public class DrawTests (ITestOutputHelper _output) view.Draw (); // Only valid location w/in Viewport is 0, 0 (view) - 2, 2 (screen) - Assert.Equal ((Rune)' ', Application.Driver?.Contents [2, 2].Rune); + Assert.Equal ((Rune)' ', Application.Driver?.Contents! [2, 2].Rune); view.AddRune (0, 0, Rune.ReplacementChar); - Assert.Equal (Rune.ReplacementChar, Application.Driver?.Contents [2, 2].Rune); + Assert.Equal (Rune.ReplacementChar, Application.Driver?.Contents! [2, 2].Rune); view.AddRune (-1, -1, Rune.ReplacementChar); - Assert.Equal ((Rune)'M', Application.Driver?.Contents [1, 1].Rune); + Assert.Equal ((Rune)'M', Application.Driver?.Contents! [1, 1].Rune); view.AddRune (1, 1, Rune.ReplacementChar); - Assert.Equal ((Rune)'M', Application.Driver?.Contents [3, 3].Rune); + Assert.Equal ((Rune)'M', Application.Driver?.Contents! [3, 3].Rune); View.Diagnostics = ViewDiagnosticFlags.Off; } diff --git a/UnitTests/Views/MenuBarTests.cs b/UnitTests/Views/MenuBarTests.cs index 6f9de4683..76598902c 100644 --- a/UnitTests/Views/MenuBarTests.cs +++ b/UnitTests/Views/MenuBarTests.cs @@ -1301,15 +1301,16 @@ wo var menu = new MenuBar (); - menu.EnableForDesign ( - new Func ( - s => - { - miAction = s as string; + bool FnAction (string s) + { + miAction = s; - return true; - }) - ); + return true; + } + // Declare a variable for the function + Func fnActionVariable = FnAction; + + menu.EnableForDesign (ref fnActionVariable); menu.Key = KeyCode.F9; menu.MenuOpening += (s, e) => mbiCurrent = e.CurrentMenu; @@ -1329,7 +1330,7 @@ wo foreach (KeyCode key in keys) { Assert.True (top.NewKeyDownEvent (new (key))); - Application.MainLoop.RunIteration (); + Application.MainLoop!.RunIteration (); } Assert.Equal (expectedAction, miAction);