| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #nullable enable
- using System.Globalization;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Localization", "Test for localization resources.")]
- [ScenarioCategory ("Text and Formatting")]
- [ScenarioCategory ("Tests")]
- public class Localization : Scenario
- {
- private CheckBox? _allowAnyCheckBox;
- private string []? _cultureInfoNameSource;
- private CultureInfo []? _cultureInfoSource;
- private OpenMode _currentOpenMode = OpenMode.File;
- private ComboBox? _languageComboBox;
- public CultureInfo CurrentCulture { get; private set; } = Thread.CurrentThread.CurrentUICulture;
- public void Quit ()
- {
- SetCulture (CultureInfo.InvariantCulture);
- Application.RequestStop ();
- }
- public void SetCulture (CultureInfo culture)
- {
- if (_languageComboBox is null || _cultureInfoSource is null)
- {
- return;
- }
- if (_cultureInfoSource [_languageComboBox.SelectedItem] != culture)
- {
- _languageComboBox.SelectedItem = Array.IndexOf (_cultureInfoSource, culture);
- }
- if (CurrentCulture == culture)
- {
- return;
- }
- CurrentCulture = culture;
- Thread.CurrentThread.CurrentUICulture = culture;
- Application.LayoutAndDraw ();
- }
- public override void Main ()
- {
- Application.Init ();
- Window win = new ()
- {
- Title = GetQuitKeyAndName (),
- BorderStyle = LineStyle.None
- };
- _cultureInfoSource = Application.SupportedCultures!.Append (CultureInfo.InvariantCulture).ToArray ();
- _cultureInfoNameSource = Application.SupportedCultures!.Select (c => $"{c.NativeName} ({c.Name})")
- .Append ("Invariant")
- .ToArray ();
- MenuItem [] languageMenus = Application.SupportedCultures!
- .Select (c => new MenuItem
- {
- Title = $"{c.NativeName} ({c.Name})",
- Action = () => SetCulture (c)
- }
- )
- .Concat (
- [
- new ()
- {
- Title = "Invariant",
- Action = () => SetCulture (CultureInfo.InvariantCulture)
- }
- ]
- )
- .ToArray ();
- // MenuBar
- MenuBar menu = new ();
- menu.Add (
- new MenuBarItem (
- "_File",
- [
- new MenuBarItem (
- "_Language",
- languageMenus
- ),
- new MenuItem
- {
- Title = "_Quit",
- Action = Quit
- }
- ]
- )
- );
- Label selectLanguageLabel = new ()
- {
- X = 2,
- Y = Pos.Bottom (menu) + 1,
- Width = Dim.Fill (2),
- Text = "Please select a language."
- };
- win.Add (selectLanguageLabel);
- _languageComboBox = new ()
- {
- X = 2,
- Y = Pos.Bottom (selectLanguageLabel) + 1,
- Width = _cultureInfoNameSource.Select (cn => cn.Length + 3).Max (),
- Height = _cultureInfoNameSource.Length + 1,
- HideDropdownListOnClick = true,
- Source = new ListWrapper<string> (new (_cultureInfoNameSource)),
- SelectedItem = _cultureInfoNameSource.Length - 1
- };
- _languageComboBox.SetSource<string> (new (_cultureInfoNameSource));
- _languageComboBox.SelectedItemChanged += LanguageComboBox_SelectChanged;
- win.Add (_languageComboBox);
- Label textAndFileDialogLabel = new ()
- {
- X = 2,
- Y = Pos.Top (_languageComboBox) + 3,
- Width = Dim.Fill (2),
- Height = 1,
- Text =
- "Right click on the text field to open a context menu, click the button to open a file dialog.\r\nOpen mode will loop through 'File', 'Directory' and 'Mixed' as 'Open' or 'Save' button clicked."
- };
- win.Add (textAndFileDialogLabel);
- TextView textField = new ()
- {
- X = 2,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1,
- Width = Dim.Fill (32),
- Height = 1
- };
- win.Add (textField);
- _allowAnyCheckBox = new ()
- {
- X = Pos.Right (textField) + 1,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1,
- CheckedState = CheckState.UnChecked,
- Text = "Allow any"
- };
- win.Add (_allowAnyCheckBox);
- Button openDialogButton = new ()
- {
- X = Pos.Right (_allowAnyCheckBox) + 1,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1,
- Text = "Open"
- };
- openDialogButton.Accepting += (sender, e) => ShowFileDialog (false);
- win.Add (openDialogButton);
- Button saveDialogButton = new ()
- {
- X = Pos.Right (openDialogButton) + 1,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1,
- Text = "Save"
- };
- saveDialogButton.Accepting += (sender, e) => ShowFileDialog (true);
- win.Add (saveDialogButton);
- Label wizardLabel = new ()
- {
- X = 2,
- Y = Pos.Bottom (textField) + 1,
- Width = Dim.Fill (2),
- Text = "Click the button to open a wizard."
- };
- win.Add (wizardLabel);
- Button wizardButton = new () { X = 2, Y = Pos.Bottom (wizardLabel) + 1, Text = "Open _wizard" };
- wizardButton.Accepting += (sender, e) => ShowWizard ();
- win.Add (wizardButton);
- win.IsRunningChanged += (sender, e) => Quit ();
- win.Add (menu);
- Application.Run (win);
- win.Dispose ();
- Application.Shutdown ();
- }
- public void ShowFileDialog (bool isSaveFile)
- {
- if (_allowAnyCheckBox is null)
- {
- return;
- }
- FileDialog dialog = isSaveFile ? new SaveDialog () : new OpenDialog { OpenMode = _currentOpenMode };
- dialog.AllowedTypes =
- [
- _allowAnyCheckBox.CheckedState == CheckState.Checked
- ? new AllowedTypeAny ()
- : new AllowedType ("Dynamic link library", ".dll"),
- new AllowedType ("Json", ".json"),
- new AllowedType ("Text", ".txt"),
- new AllowedType ("Yaml", ".yml", ".yaml")
- ];
- dialog.MustExist = !isSaveFile;
- dialog.AllowsMultipleSelection = !isSaveFile;
- _currentOpenMode++;
- if (_currentOpenMode > OpenMode.Mixed)
- {
- _currentOpenMode = OpenMode.File;
- }
- Application.Run (dialog);
- dialog.Dispose ();
- }
- public void ShowWizard ()
- {
- Wizard wizard = new () { Height = 8, Width = 36, Title = "The wizard" };
- wizard.AddStep (new () { HelpText = "Wizard first step" });
- wizard.AddStep (new () { HelpText = "Wizard step 2", NextButtonText = ">>> (_N)" });
- wizard.AddStep (new () { HelpText = "Wizard last step" });
- Application.Run (wizard);
- wizard.Dispose ();
- }
- private void LanguageComboBox_SelectChanged (object? sender, ListViewItemEventArgs e)
- {
- if (_cultureInfoNameSource is null || _cultureInfoSource is null)
- {
- return;
- }
- if (e.Value is string cultureName)
- {
- int index = Array.IndexOf (_cultureInfoNameSource, cultureName);
- if (index >= 0)
- {
- SetCulture (_cultureInfoSource [index]);
- }
- }
- }
- }
|