Refactor Scrolling.cs for timer management and nullability

Enabled nullable reference types with `#nullable enable` for improved safety. Replaced the `pulsing` flag with a new `_progressTimer` object to better manage the progress bar's timer lifecycle. Updated `AppOnInitialized` and `AppUnloaded` methods to handle timer initialization and cleanup properly, preventing potential memory leaks.

Simplified code by removing unnecessary comments and aligning method signatures with nullable reference type annotations.
This commit is contained in:
Tig
2025-10-21 01:49:58 -06:00
parent 6cd468e825
commit a40e90f06e

View File

@@ -1,4 +1,6 @@
namespace UICatalog.Scenarios;
#nullable enable
namespace UICatalog.Scenarios;
[ScenarioMetadata ("Scrolling", "Content scrolling, IScrollBars, etc...")]
[ScenarioCategory ("Controls")]
@@ -6,6 +8,8 @@
[ScenarioCategory ("Tests")]
public class Scrolling : Scenario
{
private static object? _progressTimer = null;
public override void Main ()
{
Application.Init ();
@@ -38,10 +42,6 @@ public class Scrolling : Scenario
app.Add (demoView);
//// NOTE: This call to EnableScrollBar is technically not needed because the reference
//// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created.
//// NOTE: The call included in this sample to for illustration purposes.
//demoView.EnableScrollBar (Orientation.Horizontal);
var hCheckBox = new CheckBox
{
X = Pos.X (demoView),
@@ -52,10 +52,6 @@ public class Scrolling : Scenario
app.Add (hCheckBox);
hCheckBox.CheckedStateChanged += (sender, args) => { demoView.HorizontalScrollBar.Visible = args.Value == CheckState.Checked; };
//// NOTE: This call to EnableScrollBar is technically not needed because the reference
//// NOTE: to demoView.HorizontalScrollBar below will cause it to be lazy created.
//// NOTE: The call included in this sample to for illustration purposes.
//demoView.EnableScrollBar (Orientation.Vertical);
var vCheckBox = new CheckBox
{
X = Pos.Right (hCheckBox) + 3,
@@ -96,8 +92,6 @@ public class Scrolling : Scenario
app.Add (progress);
var pulsing = true;
app.Initialized += AppOnInitialized;
app.Unloaded += AppUnloaded;
@@ -108,17 +102,25 @@ public class Scrolling : Scenario
return;
void AppOnInitialized (object sender, EventArgs e)
void AppOnInitialized (object? sender, EventArgs e)
{
bool TimerFn ()
{
progress.Pulse ();
return pulsing;
return _progressTimer is { };
}
Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn);
_progressTimer = Application.AddTimeout (TimeSpan.FromMilliseconds (200), TimerFn);
}
void AppUnloaded (object? sender, EventArgs args)
{
if (_progressTimer is { })
{
Application.RemoveTimeout (_progressTimer);
_progressTimer = null;
}
}
void AppUnloaded (object sender, EventArgs args) { pulsing = false; }
}
}