123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using Terminal.Gui;
- using static UICatalog.Scenarios.Adornments;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("ProgressBar Styles", "Shows the ProgressBar Styles.")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Progress")]
- [ScenarioCategory ("Threading")]
- // TODO: Add enable/disable to show that that is working
- // TODO: Clean up how FramesEditor works
- // TODO: Better align rpPBFormat
- public class ProgressBarStyles : Scenario
- {
- private const uint _timerTick = 20;
- private Timer _fractionTimer;
- private Timer _pulseTimer;
- private ViewDiagnosticFlags _diagnosticFlags;
- public override void Main ()
- {
- Application.Init ();
- _diagnosticFlags = View.Diagnostics;
- Window app = new ()
- {
- Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}", BorderStyle = LineStyle.Single,
- };
- var editor = new AdornmentsEditor ();
- app.Add (editor);
- View container = new ()
- {
- X = Pos.Right (editor),
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- };
- app.Add (container);
- const float fractionStep = 0.01F;
- var pbList = new ListView
- {
- Title = "Focused ProgressBar",
- Y = Pos.Align (Alignment.Start),
- X = Pos.Center (),
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- BorderStyle = LineStyle.Single
- };
- container.Add (pbList);
- #region ColorPicker
- ColorName ChooseColor (string text, ColorName colorName)
- {
- var colorPicker = new ColorPicker { Title = text, SelectedColor = colorName };
- var dialog = new Dialog { Title = text };
- dialog.Initialized += (sender, args) =>
- {
- // TODO: Replace with Dim.Auto
- dialog.X = pbList.Frame.X;
- dialog.Y = pbList.Frame.Height;
- };
- dialog.LayoutComplete += (sender, args) =>
- {
- dialog.Viewport = Rectangle.Empty with
- {
- Width = colorPicker.Frame.Width,
- Height = colorPicker.Frame.Height
- };
- Application.Top.LayoutSubviews ();
- };
- dialog.Add (colorPicker);
- colorPicker.ColorChanged += (s, e) => { dialog.RequestStop (); };
- Application.Run (dialog);
- dialog.Dispose ();
- ColorName retColor = colorPicker.SelectedColor;
- colorPicker.Dispose ();
- return retColor;
- }
- var fgColorPickerBtn = new Button
- {
- Text = "Foreground HotNormal Color",
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- };
- container.Add (fgColorPickerBtn);
- fgColorPickerBtn.Accept += (s, e) =>
- {
- ColorName newColor = ChooseColor (
- fgColorPickerBtn.Text,
- editor.ViewToEdit.ColorScheme.HotNormal.Foreground
- .GetClosestNamedColor ()
- );
- var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
- {
- HotNormal = new Attribute (
- newColor,
- editor.ViewToEdit.ColorScheme.HotNormal
- .Background
- )
- };
- editor.ViewToEdit.ColorScheme = cs;
- };
- var bgColorPickerBtn = new Button
- {
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- Text = "Background HotNormal Color"
- };
- container.Add (bgColorPickerBtn);
- bgColorPickerBtn.Accept += (s, e) =>
- {
- ColorName newColor = ChooseColor (
- fgColorPickerBtn.Text,
- editor.ViewToEdit.ColorScheme.HotNormal.Background
- .GetClosestNamedColor ()
- );
- var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
- {
- HotNormal = new Attribute (
- editor.ViewToEdit.ColorScheme.HotNormal
- .Foreground,
- newColor
- )
- };
- editor.ViewToEdit.ColorScheme = cs;
- };
- #endregion
- List<ProgressBarFormat> pbFormatEnum =
- Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
- var rbPBFormat = new RadioGroup
- {
- BorderStyle = LineStyle.Single,
- Title = "ProgressBarFormat",
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- RadioLabels = pbFormatEnum.Select (e => e.ToString ()).ToArray ()
- };
- container.Add (rbPBFormat);
- var button = new Button
- {
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- Text = "Start timer"
- };
- container.Add (button);
- var blocksPB = new ProgressBar
- {
- Title = "Blocks",
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- Width = Dim.Percent (50),
- BorderStyle = LineStyle.Single,
- CanFocus = true
- };
- container.Add (blocksPB);
- rbPBFormat.SelectedItem = (int)blocksPB.ProgressBarFormat;
- var continuousPB = new ProgressBar
- {
- Title = "Continuous",
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- Width = Dim.Percent (50),
- ProgressBarStyle = ProgressBarStyle.Continuous,
- BorderStyle = LineStyle.Single,
- CanFocus = true
- };
- container.Add (continuousPB);
- button.Accept += (s, e) =>
- {
- if (_fractionTimer == null)
- {
- //blocksPB.Enabled = false;
- blocksPB.Fraction = 0;
- continuousPB.Fraction = 0;
- float fractionSum = 0;
- _fractionTimer = new Timer (
- _ =>
- {
- fractionSum += fractionStep;
- blocksPB.Fraction = fractionSum;
- continuousPB.Fraction = fractionSum;
- if (fractionSum > 1)
- {
- _fractionTimer.Dispose ();
- _fractionTimer = null;
- button.Enabled = true;
- }
- Application.Wakeup ();
- },
- null,
- 0,
- _timerTick
- );
- }
- };
- var ckbBidirectional = new CheckBox
- {
- X = Pos.Center (), Y = Pos.Bottom (continuousPB) + 1, Text = "BidirectionalMarquee", Checked = true
- };
- container.Add (ckbBidirectional);
- var marqueesBlocksPB = new ProgressBar
- {
- Title = "Marquee Blocks",
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- Width = Dim.Percent (50),
- ProgressBarStyle = ProgressBarStyle.MarqueeBlocks,
- BorderStyle = LineStyle.Single,
- CanFocus = true
- };
- container.Add (marqueesBlocksPB);
- var marqueesContinuousPB = new ProgressBar
- {
- Title = "Marquee Continuous",
- X = Pos.Center (),
- Y = Pos.Align (Alignment.Start),
- Width = Dim.Percent (50),
- ProgressBarStyle = ProgressBarStyle.MarqueeContinuous,
- BorderStyle = LineStyle.Single,
- CanFocus = true
- };
- container.Add (marqueesContinuousPB);
- pbList.SetSource (
- container.Subviews.Where (v => v.GetType () == typeof (ProgressBar))
- .Select (v => v.Title)
- .ToList ()
- );
- pbList.SelectedItemChanged += (sender, e) =>
- {
- editor.ViewToEdit = container.Subviews.First (
- v =>
- v.GetType () == typeof (ProgressBar)
- && v.Title == (string)e.Value
- );
- };
- pbList.SelectedItem = 0;
- rbPBFormat.SelectedItemChanged += (s, e) =>
- {
- blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
- continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
- marqueesBlocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
- marqueesContinuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
- };
- ckbBidirectional.Toggled += (s, e) =>
- {
- ckbBidirectional.Checked = marqueesBlocksPB.BidirectionalMarquee =
- marqueesContinuousPB.BidirectionalMarquee = (bool)!e.OldValue;
- };
- _pulseTimer = new Timer (
- _ =>
- {
- marqueesBlocksPB.Text = marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
- marqueesBlocksPB.Pulse ();
- marqueesContinuousPB.Pulse ();
- Application.Wakeup ();
- },
- null,
- 0,
- 300
- );
- app.Unloaded += App_Unloaded;
- Application.Run (app);
- app.Dispose ();
- Application.Shutdown ();
- return;
- void App_Unloaded (object sender, EventArgs args)
- {
- if (_fractionTimer != null)
- {
- _fractionTimer.Dispose ();
- _fractionTimer = null;
- }
- if (_pulseTimer != null)
- {
- _pulseTimer.Dispose ();
- _pulseTimer = null;
- }
- app.Unloaded -= App_Unloaded;
- }
- }
- }
|