123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Threading;
- using Terminal.Gui;
- namespace UICatalog.Scenarios {
- [ScenarioMetadata (Name: "Localization", Description: "Test for localization resources.")]
- [ScenarioCategory ("Text and Formatting")]
- [ScenarioCategory ("Tests")]
- public class Localization : Scenario {
- public CultureInfo CurrentCulture { get; private set; } = Thread.CurrentThread.CurrentUICulture;
- private CultureInfo [] _cultureInfoSource;
- private string [] _cultureInfoNameSource;
- private ComboBox _languageComboBox;
- private CheckBox _allowAnyCheckBox;
- private OpenMode _currentOpenMode = OpenMode.File;
- public override void Setup ()
- {
- base.Setup ();
- _cultureInfoSource = Application.SupportedCultures.Append (CultureInfo.InvariantCulture).ToArray ();
- _cultureInfoNameSource = Application.SupportedCultures.Select (c => $"{c.NativeName} ({c.Name})").Append ("Invariant").ToArray ();
- var languageMenus = Application.SupportedCultures
- .Select (c => new MenuItem ($"{c.NativeName} ({c.Name})", "", () => SetCulture (c)))
- .Concat (
- new MenuItem [] {
- null,
- new MenuItem ("Invariant", "", () => SetCulture (CultureInfo.InvariantCulture))
- }
- )
- .ToArray ();
- var menu = new MenuBar (new MenuBarItem [] {
- new MenuBarItem ("_File", new MenuItem [] {
- new MenuBarItem("_Language", languageMenus),
- null,
- new MenuItem ("_Quit", "", Quit),
- }),
- });
- Application.Top.Add (menu);
- var selectLanguageLabel = new Label ("Please select a language.") {
- X = 2,
- Y = 1,
- Width = Dim.Fill (2),
- AutoSize = true
- };
- Win.Add (selectLanguageLabel);
- _languageComboBox = new ComboBox (_cultureInfoNameSource) {
- X = 2,
- Y = Pos.Bottom (selectLanguageLabel) + 1,
- Width = _cultureInfoNameSource.Select (cn => cn.Length + 3).Max (),
- Height = _cultureInfoNameSource.Length + 1,
- HideDropdownListOnClick = true,
- AutoSize = true,
- SelectedItem = _cultureInfoNameSource.Length - 1
- };
- _languageComboBox.SelectedItemChanged += LanguageComboBox_SelectChanged;
- Win.Add (_languageComboBox);
- var textAndFileDialogLabel = new Label ("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.") {
- X = 2,
- Y = Pos.Top (_languageComboBox) + 3,
- Width = Dim.Fill (2),
- AutoSize = true
- };
- Win.Add (textAndFileDialogLabel);
- var textField = new TextView {
- X = 2,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1,
- Width = Dim.Fill (32),
- Height = 1
- };
- Win.Add (textField);
- _allowAnyCheckBox = new CheckBox {
- X = Pos.Right (textField) + 1,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1,
- Checked = false,
- Text = "Allow any"
- };
- Win.Add (_allowAnyCheckBox);
- var openDialogButton = new Button ("Open") {
- X = Pos.Right (_allowAnyCheckBox) + 1,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1
- };
- openDialogButton.Clicked += (sender, e) => ShowFileDialog (false);
- Win.Add (openDialogButton);
- var saveDialogButton = new Button ("Save") {
- X = Pos.Right (openDialogButton) + 1,
- Y = Pos.Bottom (textAndFileDialogLabel) + 1
- };
- saveDialogButton.Clicked += (sender, e) => ShowFileDialog (true);
- Win.Add (saveDialogButton);
- var wizardLabel = new Label ("Click the button to open a wizard.") {
- X = 2,
- Y = Pos.Bottom (textField) + 1,
- Width = Dim.Fill (2),
- AutoSize = true
- };
- Win.Add (wizardLabel);
- var wizardButton = new Button ("Open _wizard") {
- X = 2,
- Y = Pos.Bottom (wizardLabel) + 1
- };
- wizardButton.Clicked += (sender, e) => ShowWizard ();
- Win.Add (wizardButton);
- Win.Unloaded += (sender, e) => Quit ();
- }
- public void SetCulture (CultureInfo culture)
- {
- if (_cultureInfoSource [_languageComboBox.SelectedItem] != culture) {
- _languageComboBox.SelectedItem = Array.IndexOf (_cultureInfoSource, culture);
- }
- if (this.CurrentCulture == culture) return;
- this.CurrentCulture = culture;
- Thread.CurrentThread.CurrentUICulture = culture;
- Application.Refresh ();
- }
- private void LanguageComboBox_SelectChanged (object sender, ListViewItemEventArgs e)
- {
- if (e.Value is string cultureName) {
- var index = Array.IndexOf (_cultureInfoNameSource, cultureName);
- if (index >= 0) {
- SetCulture (_cultureInfoSource [index]);
- }
- }
- }
- public void ShowFileDialog (bool isSaveFile)
- {
- FileDialog dialog = isSaveFile ? new SaveDialog () : new OpenDialog ("", null, _currentOpenMode);
- dialog.AllowedTypes = new List<IAllowedType> () {
- (_allowAnyCheckBox.Checked ?? false) ? 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);
- }
- public void ShowWizard ()
- {
- Wizard wizard = new Wizard {
- Height = 8,
- Width = 36,
- Title = "The wizard"
- };
- wizard.AddStep (new WizardStep () {
- HelpText = "Wizard first step",
- });
- wizard.AddStep (new WizardStep () {
- HelpText = "Wizard step 2",
- NextButtonText = ">>> (_N)"
- });
- wizard.AddStep (new WizardStep () {
- HelpText = "Wizard last step"
- });
- Application.Run (wizard);
- }
- public void Quit ()
- {
- SetCulture (CultureInfo.InvariantCulture);
- Application.RequestStop ();
- }
- }
- }
|