123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("All Views Tester", "Provides a test UI for all classes derived from View.")]
- [ScenarioCategory ("Layout")]
- [ScenarioCategory ("Tests")]
- [ScenarioCategory ("Top Level Windows")]
- public class AllViewsTester : Scenario
- {
- private readonly List<string> _dimNames = new () { "Factor", "Fill", "Absolute" };
- // TODO: This is missing some
- private readonly List<string> _posNames = new () { "Factor", "AnchorEnd", "Center", "Absolute" };
- private ListView _classListView;
- private CheckBox _computedCheckBox;
- private View _curView;
- private FrameView _hostPane;
- private RadioGroup _hRadioGroup;
- private TextField _hText;
- private int _hVal;
- private FrameView _leftPane;
- private FrameView _locationFrame;
- // Settings
- private FrameView _settingsPane;
- private FrameView _sizeFrame;
- private Dictionary<string, Type> _viewClasses;
- private RadioGroup _wRadioGroup;
- private TextField _wText;
- private int _wVal;
- private RadioGroup _xRadioGroup;
- private TextField _xText;
- private int _xVal;
- private RadioGroup _yRadioGroup;
- private TextField _yText;
- private int _yVal;
- public override void Init ()
- {
- // Don't create a sub-win (Scenario.Win); just use Application.Top
- Application.Init ();
- ConfigurationManager.Themes.Theme = Theme;
- ConfigurationManager.Apply ();
- Top = new ();
- Top.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
- var statusBar = new StatusBar (
- new StatusItem []
- {
- new (
- Application.QuitKey,
- $"{Application.QuitKey} to Quit",
- () => Quit ()
- ),
- new (
- KeyCode.F2,
- "~F2~ Toggle Frame Ruler",
- () =>
- {
- View.Diagnostics ^=
- ViewDiagnosticFlags.Ruler;
- Top.SetNeedsDisplay ();
- }
- ),
- new (
- KeyCode.F3,
- "~F3~ Toggle Frame Padding",
- () =>
- {
- View.Diagnostics ^=
- ViewDiagnosticFlags.Padding;
- Top.SetNeedsDisplay ();
- }
- )
- }
- );
- Top.Add (statusBar);
- _viewClasses = GetAllViewClassesCollection ()
- .OrderBy (t => t.Name)
- .Select (t => new KeyValuePair<string, Type> (t.Name, t))
- .ToDictionary (t => t.Key, t => t.Value);
- _leftPane = new FrameView
- {
- X = 0,
- Y = 0,
- Width = 15,
- Height = Dim.Fill (1), // for status bar
- CanFocus = false,
- ColorScheme = Colors.ColorSchemes ["TopLevel"],
- Title = "Classes"
- };
- _classListView = new ListView
- {
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- AllowsMarking = false,
- ColorScheme = Colors.ColorSchemes ["TopLevel"],
- SelectedItem = 0,
- Source = new ListWrapper (_viewClasses.Keys.ToList ())
- };
- _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
- _classListView.SelectedItemChanged += (s, args) =>
- {
- // Remove existing class, if any
- if (_curView != null)
- {
- _curView.LayoutComplete -= LayoutCompleteHandler;
- _hostPane.Remove (_curView);
- _curView.Dispose ();
- _curView = null;
- _hostPane.Clear ();
- }
- _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
- };
- _leftPane.Add (_classListView);
- _settingsPane = new FrameView
- {
- X = Pos.Right (_leftPane),
- Y = 0, // for menu
- Width = Dim.Fill (),
- Height = 10,
- CanFocus = false,
- ColorScheme = Colors.ColorSchemes ["TopLevel"],
- Title = "Settings"
- };
- _computedCheckBox = new CheckBox { X = 0, Y = 0, Text = "_Computed Layout", Checked = true };
- _computedCheckBox.Toggled += (s, e) =>
- {
- if (_curView != null)
- {
- _hostPane.LayoutSubviews ();
- }
- };
- _settingsPane.Add (_computedCheckBox);
- string [] radioItems = { "_Percent(x)", "_AnchorEnd", "_Center", "A_t(x)" };
- _locationFrame = new FrameView
- {
- X = Pos.Left (_computedCheckBox),
- Y = Pos.Bottom (_computedCheckBox),
- Height = 3 + radioItems.Length,
- Width = 36,
- Title = "Location (Pos)"
- };
- _settingsPane.Add (_locationFrame);
- var label = new Label { X = 0, Y = 0, Text = "X:" };
- _locationFrame.Add (label);
- _xRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
- _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
- _xText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
- _xText.TextChanged += (s, args) =>
- {
- try
- {
- _xVal = int.Parse (_xText.Text);
- DimPosChanged (_curView);
- }
- catch
- { }
- };
- _locationFrame.Add (_xText);
- _locationFrame.Add (_xRadioGroup);
- radioItems = new [] { "P_ercent(y)", "A_nchorEnd", "C_enter", "At(_y)" };
- label = new Label { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "Y:" };
- _locationFrame.Add (label);
- _yText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
- _yText.TextChanged += (s, args) =>
- {
- try
- {
- _yVal = int.Parse (_yText.Text);
- DimPosChanged (_curView);
- }
- catch
- { }
- };
- _locationFrame.Add (_yText);
- _yRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
- _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
- _locationFrame.Add (_yRadioGroup);
- _sizeFrame = new FrameView
- {
- X = Pos.Right (_locationFrame),
- Y = Pos.Y (_locationFrame),
- Height = 3 + radioItems.Length,
- Width = 40,
- Title = "Size (Dim)"
- };
- radioItems = new [] { "_Percent(width)", "_Fill(width)", "_Sized(width)" };
- label = new Label { X = 0, Y = 0, Text = "Width:" };
- _sizeFrame.Add (label);
- _wRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
- _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
- _wText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
- _wText.TextChanged += (s, args) =>
- {
- try
- {
- switch (_wRadioGroup.SelectedItem)
- {
- case 0:
- _wVal = Math.Min (int.Parse (_wText.Text), 100);
- break;
- case 1:
- case 2:
- _wVal = int.Parse (_wText.Text);
- break;
- }
- DimPosChanged (_curView);
- }
- catch
- { }
- };
- _sizeFrame.Add (_wText);
- _sizeFrame.Add (_wRadioGroup);
- radioItems = new [] { "P_ercent(height)", "F_ill(height)", "Si_zed(height)" };
- label = new Label { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
- _sizeFrame.Add (label);
- _hText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
- _hText.TextChanged += (s, args) =>
- {
- try
- {
- switch (_hRadioGroup.SelectedItem)
- {
- case 0:
- _hVal = Math.Min (int.Parse (_hText.Text), 100);
- break;
- case 1:
- case 2:
- _hVal = int.Parse (_hText.Text);
- break;
- }
- DimPosChanged (_curView);
- }
- catch
- { }
- };
- _sizeFrame.Add (_hText);
- _hRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
- _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
- _sizeFrame.Add (_hRadioGroup);
- _settingsPane.Add (_sizeFrame);
- _hostPane = new FrameView
- {
- X = Pos.Right (_leftPane),
- Y = Pos.Bottom (_settingsPane),
- Width = Dim.Fill (),
- Height = Dim.Fill (1), // + 1 for status bar
- ColorScheme = Colors.ColorSchemes ["Dialog"]
- };
- Top.Add (_leftPane, _settingsPane, _hostPane);
- _curView = CreateClass (_viewClasses.First ().Value);
- }
- // TODO: Add Command.HotKey handler (pop a message box?)
- private View CreateClass (Type type)
- {
- // If we are to create a generic Type
- if (type.IsGenericType)
- {
- // For each of the <T> arguments
- List<Type> typeArguments = new ();
- // use <object>
- foreach (Type arg in type.GetGenericArguments ())
- {
- typeArguments.Add (typeof (object));
- }
- // And change what type we are instantiating from MyClass<T> to MyClass<object>
- type = type.MakeGenericType (typeArguments.ToArray ());
- }
- // Instantiate view
- var view = (View)Activator.CreateInstance (type);
- // Set the colorscheme to make it stand out if is null by default
- if (view.ColorScheme == null)
- {
- view.ColorScheme = Colors.ColorSchemes ["Base"];
- }
- // If the view supports a Text property, set it so we have something to look at
- if (view.GetType ().GetProperty ("Text") != null)
- {
- try
- {
- view.GetType ()
- .GetProperty ("Text")
- ?.GetSetMethod ()
- ?.Invoke (view, new [] { "Test Text" });
- }
- catch (TargetInvocationException e)
- {
- MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
- view = null;
- }
- }
- // If the view supports a Title property, set it so we have something to look at
- if (view != null && view.GetType ().GetProperty ("Title") != null)
- {
- if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
- {
- view?.GetType ()
- .GetProperty ("Title")
- ?.GetSetMethod ()
- ?.Invoke (view, new [] { "Test Title" });
- }
- else
- {
- view?.GetType ()
- .GetProperty ("Title")
- ?.GetSetMethod ()
- ?.Invoke (view, new [] { "Test Title" });
- }
- }
- // If the view supports a Source property, set it so we have something to look at
- if (view != null && view.GetType ().GetProperty ("Source") != null && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
- {
- var source = new ListWrapper (new List<string> { "Test Text #1", "Test Text #2", "Test Text #3" });
- view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
- }
- // Set Settings
- _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;
- view.Initialized += View_Initialized;
- // Add
- _hostPane.Add (view);
- _hostPane.SetNeedsDisplay ();
- return view;
- }
- private void DimPosChanged (View view)
- {
- if (view == null)
- {
- return;
- }
- LayoutStyle layout = view.LayoutStyle;
- try
- {
- //view.LayoutStyle = LayoutStyle.Absolute;
- view.X = _xRadioGroup.SelectedItem switch
- {
- 0 => Pos.Percent (_xVal),
- 1 => Pos.AnchorEnd (),
- 2 => Pos.Center (),
- 3 => Pos.At (_xVal),
- _ => view.X
- };
- view.Y = _yRadioGroup.SelectedItem switch
- {
- 0 => Pos.Percent (_yVal),
- 1 => Pos.AnchorEnd (),
- 2 => Pos.Center (),
- 3 => Pos.At (_yVal),
- _ => view.Y
- };
- view.Width = _wRadioGroup.SelectedItem switch
- {
- 0 => Dim.Percent (_wVal),
- 1 => Dim.Fill (_wVal),
- 2 => Dim.Sized (_wVal),
- _ => view.Width
- };
- view.Height = _hRadioGroup.SelectedItem switch
- {
- 0 => Dim.Percent (_hVal),
- 1 => Dim.Fill (_hVal),
- 2 => Dim.Sized (_hVal),
- _ => view.Height
- };
- }
- catch (Exception e)
- {
- MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
- }
- UpdateTitle (view);
- }
- private List<Type> GetAllViewClassesCollection ()
- {
- List<Type> types = new ();
- foreach (Type type in typeof (View).Assembly.GetTypes ()
- .Where (
- myType =>
- myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
- ))
- {
- types.Add (type);
- }
- types.Add (typeof (View));
- return types;
- }
- private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
- {
- UpdateSettings (_curView);
- UpdateTitle (_curView);
- }
- private void Quit () { Application.RequestStop (); }
- private void UpdateSettings (View view)
- {
- var x = view.X.ToString ();
- var y = view.Y.ToString ();
- _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => x.Contains (s)).First ());
- _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => y.Contains (s)).First ());
- _xText.Text = $"{view.Frame.X}";
- _yText.Text = $"{view.Frame.Y}";
- var w = view.Width.ToString ();
- var h = view.Height.ToString ();
- _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => w.Contains (s)).First ());
- _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => h.Contains (s)).First ());
- _wText.Text = $"{view.Frame.Width}";
- _hText.Text = $"{view.Frame.Height}";
- }
- private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
- private void View_Initialized (object sender, EventArgs e)
- {
- var view = sender as View;
- //view.X = Pos.Center ();
- //view.Y = Pos.Center ();
- if (view.Width == null || view.Frame.Width == 0)
- {
- view.Width = Dim.Fill ();
- }
- if (view.Height == null || view.Frame.Height == 0)
- {
- view.Height = Dim.Fill ();
- }
- UpdateSettings (view);
- UpdateTitle (view);
- }
- }
|