123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics.Tracing;
- using System.Linq;
- using System.Text;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Sliders", "Demonstrates the Slider view.")]
- [ScenarioCategory ("Controls")]
- public class Sliders : Scenario
- {
- public void MakeSliders (View v, List<object> options)
- {
- List<SliderType> types = Enum.GetValues (typeof (SliderType)).Cast<SliderType> ().ToList ();
- Slider prev = null;
- foreach (SliderType type in types)
- {
- var view = new Slider (options)
- {
- Title = type.ToString (),
- X = 0,
- Y = prev == null ? 0 : Pos.Bottom (prev),
- BorderStyle = LineStyle.Single,
- Type = type,
- AllowEmpty = true
- };
- //view.Padding.Thickness = new (0,1,0,0);
- v.Add (view);
- prev = view;
- }
- List<object> singleOptions = new ()
- {
- 1,
- 2,
- 3,
- 4,
- 5,
- 6,
- 7,
- 8,
- 9,
- 10,
- 11,
- 12,
- 13,
- 14,
- 15,
- 16,
- 17,
- 18,
- 19,
- 20,
- 21,
- 22,
- 23,
- 24,
- 25,
- 26,
- 27,
- 28,
- 29,
- 30,
- 31,
- 32,
- 33,
- 34,
- 35,
- 36,
- 37,
- 38,
- 39
- };
- var single = new Slider (singleOptions)
- {
- Title = "Continuous",
- X = 0,
- Y = prev == null ? 0 : Pos.Bottom (prev),
- Type = SliderType.Single,
- BorderStyle = LineStyle.Single,
- AllowEmpty = false
- };
- single.LayoutStarted += (s, e) =>
- {
- if (single.Orientation == Orientation.Horizontal)
- {
- single.Style.SpaceChar = new Cell { Rune = CM.Glyphs.HLine };
- single.Style.OptionChar = new Cell { Rune = CM.Glyphs.HLine };
- }
- else
- {
- single.Style.SpaceChar = new Cell { Rune = CM.Glyphs.VLine };
- single.Style.OptionChar = new Cell { Rune = CM.Glyphs.VLine };
- }
- };
- single.Style.SetChar = new Cell { Rune = CM.Glyphs.ContinuousMeterSegment };
- single.Style.DragChar = new Cell { Rune = CM.Glyphs.ContinuousMeterSegment };
- v.Add (single);
- single.OptionsChanged += (s, e) =>
- {
- single.Title = $"Continuous {e.Options.FirstOrDefault ().Key}";
- };
- List<object> oneOption = new () { "The Only Option" };
- var one = new Slider (oneOption)
- {
- Title = "One Option",
- X = 0,
- Y = prev == null ? 0 : Pos.Bottom (single),
- Type = SliderType.Single,
- BorderStyle = LineStyle.Single,
- AllowEmpty = false
- };
- v.Add (one);
- }
- public override void Main ()
- {
- Application.Init ();
- Window app = new ()
- {
- Title = GetQuitKeyAndName ()
- };
- MakeSliders (
- app,
- new List<object>
- {
- 500,
- 1000,
- 1500,
- 2000,
- 2500,
- 3000,
- 3500,
- 4000,
- 4500,
- 5000
- }
- );
- var configView = new FrameView
- {
- Title = "Configuration",
- X = Pos.Percent (50),
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- ColorScheme = Colors.ColorSchemes ["Dialog"]
- };
- app.Add (configView);
- #region Config Slider
- Slider<string> optionsSlider = new ()
- {
- Title = "Options",
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Type = SliderType.Multiple,
- AllowEmpty = true,
- BorderStyle = LineStyle.Single
- };
- optionsSlider.Style.SetChar = optionsSlider.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
- optionsSlider.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Black);
- optionsSlider.Options = new List<SliderOption<string>>
- {
- new () { Legend = "Legends" },
- new () { Legend = "RangeAllowSingle" },
- new () { Legend = "EndSpacing" },
- new () { Legend = "DimAuto" }
- };
- configView.Add (optionsSlider);
- optionsSlider.OptionsChanged += (sender, e) =>
- {
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- s.ShowLegends = e.Options.ContainsKey (0);
- s.RangeAllowSingle = e.Options.ContainsKey (1);
- s.ShowEndSpacing = e.Options.ContainsKey (2);
- if (e.Options.ContainsKey (3))
- {
- s.Width = Dim.Auto (DimAutoStyle.Content);
- s.Height = Dim.Auto (DimAutoStyle.Content);
- }
- else
- {
- if (s.Orientation == Orientation.Horizontal)
- {
- s.Width = Dim.Percent (50);
- int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
- ? s.Options.Max (o => o.Legend.Length) + 3
- : 4;
- s.Height = h;
- }
- else
- {
- int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
- s.Width = w;
- s.Height = Dim.Fill ();
- }
- }
- }
- if (app.IsInitialized)
- {
- app.LayoutSubviews ();
- }
- };
- optionsSlider.SetOption (0); // Legends
- optionsSlider.SetOption (1); // RangeAllowSingle
- optionsSlider.SetOption (3); // DimAuto
- CheckBox dimAutoUsesMin = new ()
- {
- Text = "Use minimum size (vs. ideal)",
- X = 0,
- Y = Pos.Bottom (optionsSlider)
- };
- dimAutoUsesMin.Toggle += (sender, e) =>
- {
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- s.UseMinimumSize = !s.UseMinimumSize;
- }
- };
- configView.Add (dimAutoUsesMin);
- #region Slider Orientation Slider
- Slider<string> orientationSlider = new (new List<string> { "Horizontal", "Vertical" })
- {
- Title = "Slider Orientation",
- X = 0,
- Y = Pos.Bottom (dimAutoUsesMin) + 1,
- BorderStyle = LineStyle.Single
- };
- orientationSlider.SetOption (0);
- configView.Add (orientationSlider);
- orientationSlider.OptionsChanged += (sender, e) =>
- {
- View prev = null;
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- if (e.Options.ContainsKey (0))
- {
- s.Orientation = Orientation.Horizontal;
- s.Style.SpaceChar = new Cell { Rune = CM.Glyphs.HLine };
- if (prev == null)
- {
- s.Y = 0;
- }
- else
- {
- s.Y = Pos.Bottom (prev) + 1;
- }
- s.X = 0;
- prev = s;
- }
- else if (e.Options.ContainsKey (1))
- {
- s.Orientation = Orientation.Vertical;
- s.Style.SpaceChar = new Cell { Rune = CM.Glyphs.VLine };
- if (prev == null)
- {
- s.X = 0;
- }
- else
- {
- s.X = Pos.Right (prev) + 2;
- }
- s.Y = 0;
- prev = s;
- }
- if (optionsSlider.GetSetOptions ().Contains (3))
- {
- s.Width = Dim.Auto (DimAutoStyle.Content);
- s.Height = Dim.Auto (DimAutoStyle.Content);
- }
- else
- {
- if (s.Orientation == Orientation.Horizontal)
- {
- s.Width = Dim.Percent (50);
- int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
- ? s.Options.Max (o => o.Legend.Length) + 3
- : 4;
- s.Height = h;
- }
- else
- {
- int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
- s.Width = w;
- s.Height = Dim.Fill ();
- }
- }
- }
- app.LayoutSubviews ();
- };
- #endregion Slider Orientation Slider
- #region Legends Orientation Slider
- Slider<string> legendsOrientationSlider = new (new List<string> { "Horizontal", "Vertical" })
- {
- Title = "Legends Orientation",
- X = 0,
- Y = Pos.Bottom (orientationSlider) + 1,
- BorderStyle = LineStyle.Single
- };
- legendsOrientationSlider.SetOption (0);
- configView.Add (legendsOrientationSlider);
- legendsOrientationSlider.OptionsChanged += (sender, e) =>
- {
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- if (e.Options.ContainsKey (0))
- {
- s.LegendsOrientation = Orientation.Horizontal;
- }
- else if (e.Options.ContainsKey (1))
- {
- s.LegendsOrientation = Orientation.Vertical;
- }
- if (optionsSlider.GetSetOptions ().Contains (3))
- {
- s.Width = Dim.Auto (DimAutoStyle.Content);
- s.Height = Dim.Auto (DimAutoStyle.Content);
- }
- else
- {
- if (s.Orientation == Orientation.Horizontal)
- {
- s.Width = Dim.Percent (50);
- int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical
- ? s.Options.Max (o => o.Legend.Length) + 3
- : 4;
- s.Height = h;
- }
- else
- {
- int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3;
- s.Width = w;
- s.Height = Dim.Fill ();
- }
- }
- }
- app.LayoutSubviews ();
- };
- #endregion Legends Orientation Slider
- #region Spacing Options
- FrameView spacingOptions = new ()
- {
- Title = "Spacing Options",
- X = Pos.Right (orientationSlider),
- Y = Pos.Top (orientationSlider),
- Width = Dim.Fill (),
- Height = Dim.Auto (),
- BorderStyle = LineStyle.Single
- };
- Label label = new ()
- {
- Text = "Min _Inner Spacing:",
- };
- Buttons.NumericUpDown<int> innerSpacingUpDown = new ()
- {
- X = Pos.Right (label) + 1
- };
- innerSpacingUpDown.Value = app.Subviews.OfType<Slider> ().First ().MinimumInnerSpacing;
- innerSpacingUpDown.ValueChanging += (sender, e) =>
- {
- if (e.NewValue < 0)
- {
- e.Cancel = true;
- return;
- }
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- s.MinimumInnerSpacing = e.NewValue;
- }
- };
- spacingOptions.Add (label, innerSpacingUpDown);
- configView.Add (spacingOptions);
- #endregion
- #region Color Slider
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- s.Style.OptionChar = s.Style.OptionChar with { Attribute = app.GetNormalColor () };
- s.Style.SetChar = s.Style.SetChar with { Attribute = app.GetNormalColor () };
- s.Style.LegendAttributes.SetAttribute = app.GetNormalColor ();
- s.Style.RangeChar = s.Style.RangeChar with { Attribute = app.GetNormalColor () };
- }
- Slider<(Color, Color)> sliderFGColor = new ()
- {
- Title = "FG Color",
- X = 0,
- Y = Pos.Bottom (
- legendsOrientationSlider
- )
- + 1,
- Type = SliderType.Single,
- BorderStyle = LineStyle.Single,
- AllowEmpty = false,
- Orientation = Orientation.Vertical,
- LegendsOrientation = Orientation.Horizontal,
- MinimumInnerSpacing = 0,
- UseMinimumSize = true
- };
- sliderFGColor.Style.SetChar = sliderFGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
- sliderFGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
- List<SliderOption<(Color, Color)>> colorOptions = new ();
- foreach (ColorName colorIndex in Enum.GetValues<ColorName> ())
- {
- var colorName = colorIndex.ToString ();
- colorOptions.Add (
- new SliderOption<(Color, Color)>
- {
- Data = (new Color (colorIndex),
- new Color (colorIndex)),
- Legend = colorName,
- LegendAbbr = (Rune)colorName [0]
- }
- );
- }
- sliderFGColor.Options = colorOptions;
- configView.Add (sliderFGColor);
- sliderFGColor.OptionsChanged += (sender, e) =>
- {
- if (e.Options.Count != 0)
- {
- (Color, Color) data = e.Options.First ().Value.Data;
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- s.ColorScheme = new ColorScheme (s.ColorScheme);
- s.ColorScheme = new ColorScheme (s.ColorScheme)
- {
- Normal = new Attribute (
- data.Item2,
- s.ColorScheme.Normal.Background
- )
- };
- s.Style.OptionChar = s.Style.OptionChar with
- {
- Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
- };
- s.Style.SetChar = s.Style.SetChar with
- {
- Attribute = new Attribute (
- data.Item1,
- s.Style.SetChar.Attribute?.Background
- ?? s.ColorScheme.Normal.Background
- )
- };
- s.Style.LegendAttributes.SetAttribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
- s.Style.RangeChar = s.Style.RangeChar with
- {
- Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
- };
- s.Style.SpaceChar = s.Style.SpaceChar with
- {
- Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
- };
- s.Style.LegendAttributes.NormalAttribute =
- new Attribute (data.Item1, s.ColorScheme.Normal.Background);
- }
- }
- };
- Slider<(Color, Color)> sliderBGColor = new ()
- {
- Title = "BG Color",
- X = Pos.Right (sliderFGColor),
- Y = Pos.Top (sliderFGColor),
- Type = SliderType.Single,
- BorderStyle = LineStyle.Single,
- AllowEmpty = false,
- Orientation = Orientation.Vertical,
- LegendsOrientation = Orientation.Horizontal,
- MinimumInnerSpacing = 0,
- UseMinimumSize = true
- };
- sliderBGColor.Style.SetChar = sliderBGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
- sliderBGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
- sliderBGColor.Options = colorOptions;
- configView.Add (sliderBGColor);
- sliderBGColor.OptionsChanged += (sender, e) =>
- {
- if (e.Options.Count != 0)
- {
- (Color, Color) data = e.Options.First ().Value.Data;
- foreach (Slider s in app.Subviews.OfType<Slider> ())
- {
- s.ColorScheme = new ColorScheme (s.ColorScheme)
- {
- Normal = new Attribute (
- s.ColorScheme.Normal.Foreground,
- data.Item2
- )
- };
- }
- }
- };
- #endregion Color Slider
- #endregion Config Slider
- ObservableCollection<string> eventSource = new ();
- var eventLog = new ListView
- {
- X = Pos.Right (sliderBGColor),
- Y = Pos.Bottom (spacingOptions),
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- ColorScheme = Colors.ColorSchemes ["Toplevel"],
- Source = new ListWrapper<string> (eventSource)
- };
- configView.Add (eventLog);
- foreach (Slider slider in app.Subviews.Where (v => v is Slider)!)
- {
- slider.Accept += (o, args) =>
- {
- eventSource.Add ($"Accept: {string.Join(",", slider.GetSetOptions ())}");
- eventLog.MoveDown ();
- args.Handled = true;
- };
- slider.OptionsChanged += (o, args) =>
- {
- eventSource.Add ($"OptionsChanged: {string.Join (",", slider.GetSetOptions ())}");
- eventLog.MoveDown ();
- args.Cancel = true;
- };
- }
- app.FocusFirst (null);
- Application.Run (app);
- app.Dispose ();
- Application.Shutdown ();
- }
- }
|