| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #nullable enable
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Selectors", "Demonstrates OptionSelector and FlagSelector.")]
- [ScenarioCategory ("Controls")]
- public sealed class Selectors : Scenario
- {
- public override void Main ()
- {
- // Init
- Application.Init ();
- // Setup - Create a top-level application window and configure it.
- Window appWindow = new ()
- {
- Title = GetQuitKeyAndName (),
- BorderStyle = LineStyle.None
- };
- FrameView? optionSelectorsFrame = null;
- FrameView? flagSelectorsFrame = null;
- OptionSelector<Orientation> orientationSelector = new ()
- {
- Orientation = Orientation.Horizontal,
- BorderStyle = LineStyle.Dotted,
- Title = "Selector Or_ientation",
- Value = Orientation.Vertical
- };
- orientationSelector.ValueChanged += OrientationSelectorOnSelectedItemChanged;
- FlagSelector<SelectorStyles> stylesSelector = new ()
- {
- X = Pos.Right (orientationSelector) + 1,
- Orientation = Orientation.Horizontal,
- BorderStyle = LineStyle.Dotted,
- Title = "Selector St_yles",
- };
- stylesSelector.ValueChanged += StylesSelectorOnValueChanged;
- NumericUpDown<int> horizontalSpace = new ()
- {
- X = 0,
- Y = Pos.Bottom (orientationSelector),
- Width = 11,
- Title = "H_. Space",
- Value = stylesSelector.HorizontalSpace,
- BorderStyle = LineStyle.Dotted,
- };
- horizontalSpace.ValueChanging += HorizontalSpaceOnValueChanging;
- CheckBox showBorderAndTitle = new ()
- {
- X = Pos.Right (horizontalSpace) + 1,
- Y = Pos.Top (horizontalSpace),
- Title = "Border _& Title",
- CheckedState = CheckState.Checked,
- BorderStyle = LineStyle.Dotted,
- };
- showBorderAndTitle.CheckedStateChanged += ShowBorderAndTitleOnCheckedStateChanged;
- CheckBox canFocus = new ()
- {
- X = Pos.Right (showBorderAndTitle) + 1,
- Y = Pos.Top (horizontalSpace),
- Title = "_CanFocus",
- CheckedState = CheckState.Checked,
- BorderStyle = LineStyle.Dotted,
- };
- canFocus.CheckedStateChanged += CanFocusOnCheckedStateChanged;
- optionSelectorsFrame = new ()
- {
- Y = Pos.Bottom (canFocus),
- Width = Dim.Percent (50),
- Height = Dim.Fill (),
- Title = "O_ptionSelectors",
- TabStop = TabBehavior.TabStop,
- //InvertFocusAttribute = true
- };
- optionSelectorsFrame.ClearingViewport += (sender, args) =>
- {
- // optionSelectorsFrame.SetAttributeForRole (optionSelectorsFrame.HasFocus ? VisualRole.Focus : VisualRole.Normal);
- };
- Label label = new ()
- {
- Title = "Fo_ur Options:"
- };
- OptionSelector optionSelector = new ()
- {
- X = Pos.Right (label) + 1,
- Title = "Fou_r Options",
- BorderStyle = LineStyle.Dotted,
- UsedHotKeys = { label.HotKey },
- AssignHotKeys = true,
- Labels = ["Option _1 (0)", "Option _2 (1)", "Option _3 (5) 你", "Option _Quattro (4) 你"],
- Values = [0, 1, 5, 4],
- Arrangement = ViewArrangement.Resizable,
- };
- optionSelectorsFrame.Add (label, optionSelector);
- label = new ()
- {
- Y = Pos.Bottom (optionSelector),
- Title = "<VisualRole_>:"
- };
- OptionSelector<VisualRole> optionSelectorT = new ()
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Bottom (optionSelector),
- Title = "<Vi_sualRole>",
- BorderStyle = LineStyle.Dotted,
- UsedHotKeys = optionSelector.UsedHotKeys,
- AssignHotKeys = true,
- };
- optionSelectorsFrame.Add (label, optionSelectorT);
- flagSelectorsFrame = new ()
- {
- Y = Pos.Top (optionSelectorsFrame),
- X = Pos.Right (optionSelectorsFrame),
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "_FlagSelectors",
- TabStop = TabBehavior.TabStop
- };
- label = new ()
- {
- Title = "FlagSelector _(uint):"
- };
- FlagSelector flagSelector = new ()
- {
- X = Pos.Right (label) + 1,
- UsedHotKeys = optionSelectorT.UsedHotKeys,
- BorderStyle = LineStyle.Dotted,
- Title = "FlagSe_lector (uint)",
- AssignHotKeys = true,
- Values =
- [
- 0b_0001,
- 0b_0010,
- 0b_0100,
- 0b_1000,
- 0b_1111
- ],
- Labels =
- [
- "0x0001 One",
- "0x0010 Two",
- "0x0100 Quattro",
- "0x1000 8",
- "0x1111 Fifteen"
- ]
- };
- flagSelectorsFrame.Add (label, flagSelector);
- label = new ()
- {
- Y = Pos.Bottom (flagSelector),
- Title = "_<ViewDiagnosticFlags>:"
- };
- FlagSelector<ViewDiagnosticFlags> flagSelectorT = new ()
- {
- X = Pos.Right (label) + 1,
- BorderStyle = LineStyle.Dotted,
- Title = "<ViewD_iagnosticFlags>",
- Y = Pos.Bottom (flagSelector),
- UsedHotKeys = flagSelector.UsedHotKeys,
- AssignHotKeys = true,
- Value = View.Diagnostics
- };
- flagSelectorsFrame.Add (label, flagSelectorT);
- flagSelectorT.ValueChanged += (s, a) =>
- {
- View.Diagnostics = (ViewDiagnosticFlags)a.Value!;
- };
- appWindow.Add (orientationSelector, stylesSelector, horizontalSpace, showBorderAndTitle, canFocus, optionSelectorsFrame, flagSelectorsFrame);
- // Run - Start the application.
- Application.Run (appWindow);
- appWindow.Dispose ();
- // Shutdown - Calling Application.Shutdown is required.
- Application.Shutdown ();
- return;
- void OrientationSelectorOnSelectedItemChanged (object? sender, EventArgs<Orientation?> e)
- {
- if (sender is not OptionSelector<Orientation> s)
- {
- return;
- }
- List<SelectorBase> selectors = GetAllSelectors ();
- foreach (SelectorBase selector in selectors)
- {
- selector.Orientation = s.Value!.Value;
- }
- }
- void StylesSelectorOnValueChanged (object? sender, EventArgs<SelectorStyles?> e)
- {
- if (sender is not FlagSelector<SelectorStyles> s)
- {
- return;
- }
- List<SelectorBase> selectors = GetAllSelectors ();
- foreach (SelectorBase selector in selectors)
- {
- selector.Styles = s.Value!.Value;
- }
- }
- void HorizontalSpaceOnValueChanging (object? sender, CancelEventArgs<int> e)
- {
- if (sender is not NumericUpDown<int> upDown || e.NewValue < 0)
- {
- e.Cancel = true;
- return;
- }
- List<SelectorBase> selectors = GetAllSelectors ();
- foreach (SelectorBase selector in selectors)
- {
- selector.HorizontalSpace = e.NewValue;
- }
- }
- void ShowBorderAndTitleOnCheckedStateChanged (object? sender, EventArgs<CheckState> e)
- {
- if (sender is not CheckBox cb)
- {
- return;
- }
- List<SelectorBase> selectors = GetAllSelectors ();
- foreach (SelectorBase selector in selectors)
- {
- selector.Border!.Thickness = cb.CheckedState == CheckState.Checked ? new (1) : new Thickness (0);
- }
- }
- void CanFocusOnCheckedStateChanged (object? sender, EventArgs<CheckState> e)
- {
- if (sender is not CheckBox cb)
- {
- return;
- }
- List<SelectorBase> selectors = GetAllSelectors ();
- foreach (SelectorBase selector in selectors)
- {
- selector.CanFocus = cb.CheckedState == CheckState.Checked;
- }
- }
- List<SelectorBase> GetAllSelectors ()
- {
- List<SelectorBase> optionSelectors = [];
- // ReSharper disable once AccessToModifiedClosure
- optionSelectors.AddRange (optionSelectorsFrame!.SubViews.OfType<SelectorBase> ());
- // ReSharper disable once AccessToModifiedClosure
- optionSelectors.AddRange (flagSelectorsFrame!.SubViews.OfType<FlagSelector> ());
- return optionSelectors;
- }
- }
- }
|