| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #nullable enable
- using System.Text.RegularExpressions;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("TextView Autocomplete Popup", "Shows five TextView Autocomplete Popup effects")]
- [ScenarioCategory ("TextView")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Mouse and Keyboard")]
- public class TextViewAutocompletePopup : Scenario
- {
- private int _height = 10;
- private CheckBox? _miMultilineCheckBox;
- private CheckBox? _miWrapCheckBox;
- private Shortcut? _siMultiline;
- private Shortcut? _siWrap;
- private TextView? _textViewBottomLeft;
- private TextView? _textViewBottomRight;
- private TextView? _textViewCentered;
- private TextView? _textViewTopLeft;
- private TextView? _textViewTopRight;
- public override void Main ()
- {
- Application.Init ();
- Window appWindow = new ()
- {
- BorderStyle = LineStyle.None
- };
- var width = 20;
- var text = " jamp jemp jimp jomp jump";
- // MenuBar
- MenuBar menu = new ();
- _textViewTopLeft = new ()
- {
- Y = Pos.Bottom (menu),
- Width = width,
- Height = _height,
- Text = text
- };
- _textViewTopLeft.DrawingContent += TextViewTopLeft_DrawContent;
- appWindow.Add (_textViewTopLeft);
- _textViewTopRight = new ()
- {
- X = Pos.AnchorEnd (width),
- Y = Pos.Bottom (menu),
- Width = width,
- Height = _height,
- Text = text
- };
- _textViewTopRight.DrawingContent += TextViewTopRight_DrawContent;
- appWindow.Add (_textViewTopRight);
- _textViewBottomLeft = new ()
- {
- Y = Pos.AnchorEnd (_height),
- Width = width,
- Height = _height,
- Text = text
- };
- _textViewBottomLeft.DrawingContent += TextViewBottomLeft_DrawContent;
- appWindow.Add (_textViewBottomLeft);
- _textViewBottomRight = new ()
- {
- X = Pos.AnchorEnd (width),
- Y = Pos.AnchorEnd (_height),
- Width = width,
- Height = _height,
- Text = text
- };
- _textViewBottomRight.DrawingContent += TextViewBottomRight_DrawContent;
- appWindow.Add (_textViewBottomRight);
- _textViewCentered = new ()
- {
- X = Pos.Center (),
- Y = Pos.Center (),
- Width = width,
- Height = _height,
- Text = text
- };
- _textViewCentered.DrawingContent += TextViewCentered_DrawContent;
- appWindow.Add (_textViewCentered);
- // Setup menu checkboxes
- _miMultilineCheckBox = new ()
- {
- Title = "_Multiline",
- CheckedState = _textViewTopLeft.Multiline ? CheckState.Checked : CheckState.UnChecked
- };
- _miMultilineCheckBox.CheckedStateChanged += (s, e) => Multiline ();
- _miWrapCheckBox = new ()
- {
- Title = "_Word Wrap",
- CheckedState = _textViewTopLeft.WordWrap ? CheckState.Checked : CheckState.UnChecked
- };
- _miWrapCheckBox.CheckedStateChanged += (s, e) => WordWrap ();
- menu.Add (
- new MenuBarItem (
- "_File",
- [
- new MenuItem
- {
- CommandView = _miMultilineCheckBox
- },
- new MenuItem
- {
- CommandView = _miWrapCheckBox
- },
- new MenuItem
- {
- Title = "_Quit",
- Action = Quit
- }
- ]
- )
- );
- // StatusBar
- _siMultiline = new (Key.Empty, "", null);
- _siWrap = new (Key.Empty, "", null);
- StatusBar statusBar = new (
- [
- new (
- Application.QuitKey,
- "Quit",
- () => Quit ()
- ),
- _siMultiline,
- _siWrap
- ]
- );
- appWindow.Add (menu, statusBar);
- appWindow.SubViewLayout += Win_LayoutStarted;
- Application.Run (appWindow);
- appWindow.Dispose ();
- Application.Shutdown ();
- }
- private void Multiline ()
- {
- if (_miMultilineCheckBox is null
- || _textViewTopLeft is null
- || _textViewTopRight is null
- || _textViewBottomLeft is null
- || _textViewBottomRight is null
- || _textViewCentered is null)
- {
- return;
- }
- SetMultilineStatusText ();
- _textViewTopLeft.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
- _textViewTopRight.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
- _textViewBottomLeft.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
- _textViewBottomRight.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
- _textViewCentered.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
- }
- private void Quit () { Application.RequestStop (); }
- private void SetAllSuggestions (TextView view)
- {
- if (view.Autocomplete.SuggestionGenerator is SingleWordSuggestionGenerator generator)
- {
- generator.AllSuggestions = Regex
- .Matches (view.Text, "\\w+")
- .Select (s => s.Value)
- .Distinct ()
- .ToList ();
- }
- }
- private void SetMultilineStatusText ()
- {
- if (_siMultiline is { } && _miMultilineCheckBox is { })
- {
- _siMultiline.Title = $"Multiline: {_miMultilineCheckBox.CheckedState == CheckState.Checked}";
- }
- }
- private void SetWrapStatusText ()
- {
- if (_siWrap is { } && _miWrapCheckBox is { })
- {
- _siWrap.Title = $"WordWrap: {_miWrapCheckBox.CheckedState == CheckState.Checked}";
- }
- }
- private void TextViewBottomLeft_DrawContent (object? sender, DrawEventArgs e)
- {
- if (_textViewBottomLeft is { })
- {
- SetAllSuggestions (_textViewBottomLeft);
- }
- }
- private void TextViewBottomRight_DrawContent (object? sender, DrawEventArgs e)
- {
- if (_textViewBottomRight is { })
- {
- SetAllSuggestions (_textViewBottomRight);
- }
- }
- private void TextViewCentered_DrawContent (object? sender, DrawEventArgs e)
- {
- if (_textViewCentered is { })
- {
- SetAllSuggestions (_textViewCentered);
- }
- }
- private void TextViewTopLeft_DrawContent (object? sender, DrawEventArgs e)
- {
- if (_textViewTopLeft is { })
- {
- SetAllSuggestions (_textViewTopLeft);
- }
- }
- private void TextViewTopRight_DrawContent (object? sender, DrawEventArgs e)
- {
- if (_textViewTopRight is { })
- {
- SetAllSuggestions (_textViewTopRight);
- }
- }
- private void Win_LayoutStarted (object? sender, LayoutEventArgs obj)
- {
- if (_textViewTopLeft is null || _miMultilineCheckBox is null || _miWrapCheckBox is null || _textViewBottomLeft is null || _textViewBottomRight is null)
- {
- return;
- }
- _miMultilineCheckBox.CheckedState = _textViewTopLeft.Multiline ? CheckState.Checked : CheckState.UnChecked;
- _miWrapCheckBox.CheckedState = _textViewTopLeft.WordWrap ? CheckState.Checked : CheckState.UnChecked;
- SetMultilineStatusText ();
- SetWrapStatusText ();
- if (_miMultilineCheckBox.CheckedState == CheckState.Checked)
- {
- _height = 10;
- }
- else
- {
- _height = 1;
- }
- _textViewBottomLeft.Y = _textViewBottomRight.Y = Pos.AnchorEnd (_height);
- }
- private void WordWrap ()
- {
- if (_miWrapCheckBox is null
- || _textViewTopLeft is null
- || _textViewTopRight is null
- || _textViewBottomLeft is null
- || _textViewBottomRight is null
- || _textViewCentered is null)
- {
- return;
- }
- _textViewTopLeft.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
- _textViewTopRight.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
- _textViewBottomLeft.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
- _textViewBottomRight.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
- _textViewCentered.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
- _miWrapCheckBox.CheckedState = _textViewTopLeft.WordWrap ? CheckState.Checked : CheckState.UnChecked;
- SetWrapStatusText ();
- }
- }
|