123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- //
- // This would be a great scenario to show of threading (Issue #471)
- //
- [ScenarioMetadata ("Progress", "Shows off ProgressBar and Threading.")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Threading")]
- [ScenarioCategory ("Progress")]
- public class Progress : Scenario
- {
- private Window win;
- private uint _mainLooopTimeoutTick = 100; // ms
- private object _mainLoopTimeout;
- private Timer _systemTimer;
- private uint _systemTimerTick = 100; // ms
- public override void Main ()
- {
- Application.Init ();
- win = new Window { Title = GetQuitKeyAndName () };
- // Demo #1 - Use System.Timer (and threading)
- var systemTimerDemo = new ProgressDemo
- {
- X = 0, Y = 0, Width = Dim.Percent (100), Title = "System.Timer (threads)"
- };
- systemTimerDemo.StartBtnClick = () =>
- {
- _systemTimer?.Dispose ();
- _systemTimer = null;
- systemTimerDemo.ActivityProgressBar.Fraction = 0F;
- systemTimerDemo.PulseProgressBar.Fraction = 0F;
- _systemTimer = new Timer (
- o =>
- {
- // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
- // This code must be defensive for that.
- Application.Invoke (() => systemTimerDemo.Pulse ());
- },
- null,
- 0,
- _systemTimerTick
- );
- };
- systemTimerDemo.StopBtnClick = () =>
- {
- _systemTimer?.Dispose ();
- _systemTimer = null;
- systemTimerDemo.ActivityProgressBar.Fraction = 1F;
- systemTimerDemo.PulseProgressBar.Fraction = 1F;
- };
- systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
- systemTimerDemo.Speed.TextChanged += (s, a) =>
- {
- uint result;
- if (uint.TryParse (systemTimerDemo.Speed.Text, out result))
- {
- _systemTimerTick = result;
- Debug.WriteLine ($"{_systemTimerTick}");
- if (systemTimerDemo.Started)
- {
- systemTimerDemo.Start ();
- }
- }
- else
- {
- Debug.WriteLine ("bad entry");
- }
- };
- win.Add (systemTimerDemo);
- // Demo #2 - Use Application.AddTimeout (no threads)
- var mainLoopTimeoutDemo = new ProgressDemo
- {
- X = 0,
- Y = Pos.Bottom (systemTimerDemo),
- Width = Dim.Percent (100),
- Title = "Application.AddTimer (no threads)"
- };
- mainLoopTimeoutDemo.StartBtnClick = () =>
- {
- mainLoopTimeoutDemo.StopBtnClick ();
- mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
- mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
- _mainLoopTimeout = Application.AddTimeout (
- TimeSpan.FromMilliseconds (_mainLooopTimeoutTick),
- () =>
- {
- mainLoopTimeoutDemo.Pulse ();
- return true;
- }
- );
- };
- mainLoopTimeoutDemo.StopBtnClick = () =>
- {
- if (_mainLoopTimeout != null)
- {
- Application.RemoveTimeout (_mainLoopTimeout);
- _mainLoopTimeout = null;
- }
- mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
- mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
- };
- mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
- mainLoopTimeoutDemo.Speed.TextChanged += (s, a) =>
- {
- uint result;
- if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text, out result))
- {
- _mainLooopTimeoutTick = result;
- if (mainLoopTimeoutDemo.Started)
- {
- mainLoopTimeoutDemo.Start ();
- }
- }
- };
- win.Add (mainLoopTimeoutDemo);
- var startBoth = new Button { X = Pos.Center (), Y = Pos.Bottom (mainLoopTimeoutDemo) + 1, Text = "Start Both" };
- startBoth.Accepting += (s, e) =>
- {
- systemTimerDemo.Start ();
- mainLoopTimeoutDemo.Start ();
- };
- win.Add (startBoth);
- Application.Run (win);
- win.Dispose ();
- Application.Shutdown ();
- }
- protected override void Dispose (bool disposing)
- {
- foreach (ProgressDemo v in win.Subviews.OfType<ProgressDemo> ())
- {
- v?.StopBtnClick ();
- }
- base.Dispose (disposing);
- }
- private class ProgressDemo : FrameView
- {
- private const int VerticalSpace = 1;
- internal readonly Action PulseBtnClick = null;
- internal Action StartBtnClick;
- internal Action StopBtnClick;
- private readonly Label _startedLabel;
- internal ProgressDemo ()
- {
- ColorScheme = Colors.ColorSchemes ["Dialog"];
- LeftFrame = new FrameView
- {
- X = 0,
- Y = 0,
- Height = Dim.Percent (100),
- Width = Dim.Percent (25),
- Title = "Settings"
- };
- var lbl = new Label { X = 1, Y = 1, Text = "Tick every (ms):" };
- LeftFrame.Add (lbl);
- Speed = new TextField { X = Pos.X (lbl), Y = Pos.Bottom (lbl), Width = 7 };
- LeftFrame.Add (Speed);
- Add (LeftFrame);
- var startButton = new Button { X = Pos.Right (LeftFrame) + 1, Y = 0, Text = "Start Timer" };
- startButton.Accepting += (s, e) => Start ();
- var pulseButton = new Button { X = Pos.Right (startButton) + 2, Y = Pos.Y (startButton), Text = "Pulse" };
- pulseButton.Accepting += (s, e) => Pulse ();
- var stopbutton = new Button
- {
- X = Pos.Right (pulseButton) + 2, Y = Pos.Top (pulseButton), Text = "Stop Timer"
- };
- stopbutton.Accepting += (s, e) => Stop ();
- Add (startButton);
- Add (pulseButton);
- Add (stopbutton);
- ActivityProgressBar = new ProgressBar
- {
- X = Pos.Right (LeftFrame) + 1,
- Y = Pos.Bottom (startButton) + 1,
- Width = Dim.Fill () - 1,
- Height = 1,
- Fraction = 0.25F,
- ColorScheme = Colors.ColorSchemes ["Error"]
- };
- Add (ActivityProgressBar);
- Spinner = new SpinnerView
- {
- Style = new SpinnerStyle.Dots2 (), SpinReverse = true, Y = ActivityProgressBar.Y, Visible = false
- };
- ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
- Spinner.X = Pos.Right (ActivityProgressBar);
- Add (Spinner);
- PulseProgressBar = new ProgressBar
- {
- X = Pos.Right (LeftFrame) + 1,
- Y = Pos.Bottom (ActivityProgressBar) + 1,
- Width = Dim.Fill () - Spinner.Width,
- Height = 1,
- ColorScheme = Colors.ColorSchemes ["Error"]
- };
- Add (PulseProgressBar);
- _startedLabel = new Label
- {
- X = Pos.Right (LeftFrame) + 1, Y = Pos.Bottom (PulseProgressBar), Text = "Stopped"
- };
- Add (_startedLabel);
- // TODO: Great use of Dim.Auto
- Initialized += (s, e) =>
- {
- // Set height to height of controls + spacing + frame
- Height = 2
- + VerticalSpace
- + startButton.Frame.Height
- + VerticalSpace
- + ActivityProgressBar.Frame.Height
- + VerticalSpace
- + PulseProgressBar.Frame.Height
- + VerticalSpace;
- };
- }
- internal ProgressBar ActivityProgressBar { get; }
- internal FrameView LeftFrame { get; }
- internal ProgressBar PulseProgressBar { get; }
- internal TextField Speed { get; }
- internal SpinnerView Spinner { get; }
- internal bool Started
- {
- get => _startedLabel.Text == "Started";
- private set => _startedLabel.Text = value ? "Started" : "Stopped";
- }
- internal void Pulse ()
- {
- Spinner.Visible = true;
- if (PulseBtnClick != null)
- {
- PulseBtnClick?.Invoke ();
- }
- else
- {
- if (ActivityProgressBar.Fraction + 0.01F >= 1)
- {
- ActivityProgressBar.Fraction = 0F;
- }
- else
- {
- ActivityProgressBar.Fraction += 0.01F;
- }
- PulseProgressBar.Pulse ();
- Spinner.AdvanceAnimation ();
- }
- }
- internal void Start ()
- {
- Started = true;
- StartBtnClick?.Invoke ();
- Application.Invoke (
- () =>
- {
- Spinner.Visible = true;
- ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
- LayoutSubviews ();
- }
- );
- }
- internal void Stop ()
- {
- Started = false;
- StopBtnClick?.Invoke ();
- Application.Invoke (
- () =>
- {
- Spinner.Visible = false;
- ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
- LayoutSubviews ();
- }
- );
- }
- }
- }
|