diff --git a/StarterNG.Tests/PrecipitationItemTests.cs b/StarterNG.Tests/PrecipitationItemTests.cs new file mode 100644 index 0000000..0b06eda --- /dev/null +++ b/StarterNG.Tests/PrecipitationItemTests.cs @@ -0,0 +1,53 @@ +using Avalonia.Controls; +using StarterNG.Controls; + +namespace StarterNG.Tests; + +[Collection(AvaloniaControls.Name)] +public class PrecipitationItemTests +{ + // Shown in whatever language the tests run in: the key alone, or English once a test built the application. + private static void ShowsText(string key, PrecipitationItem item) + { + Assert.Equal(key, item.Key); + Assert.Equal(AppLanguage.Text(key), item.Content); + } + + [Fact] + public void Light_precipitation_is_light_rain_until_told_it_snows() + { + var item = new PrecipitationItem(); + + ShowsText("OvercastRainLight", item); + + item.Snow = true; + + ShowsText("OvercastSnowLight", item); + } + + [Fact] + public void Heavy_precipitation_is_rain_or_snow() + { + var item = new PrecipitationItem { Heavy = true }; + + ShowsText("OvercastRain", item); + + item.Snow = true; + + ShowsText("OvercastSnow", item); + } + + [Fact] + public void The_option_keeps_its_name_in_a_dropdown_and_out_of_it() + { + var item = new PrecipitationItem { Heavy = true, Snow = true }; + var combo = new ComboBox { Items = { item } }; + + ShowsText("OvercastSnow", item); + + combo.Items.Remove(item); + item.Snow = false; + + ShowsText("OvercastRain", item); + } +} diff --git a/StarterNG/Controls/PrecipitationItem.cs b/StarterNG/Controls/PrecipitationItem.cs new file mode 100644 index 0000000..9a6ebf6 --- /dev/null +++ b/StarterNG/Controls/PrecipitationItem.cs @@ -0,0 +1,61 @@ +using System; +using Avalonia; +using Avalonia.Controls; +using Avalonia.LogicalTree; + +namespace StarterNG.Controls; + +/// +/// An overcast option above 1, where the simulator rains or, at 1 °C and below, snows: +/// named for what falls, light or not, in the starter's language. +/// +public class PrecipitationItem : ComboBoxItem +{ + public static readonly StyledProperty HeavyProperty = + AvaloniaProperty.Register(nameof(Heavy)); + + public static readonly StyledProperty SnowProperty = + AvaloniaProperty.Register(nameof(Snow)); + + public PrecipitationItem() => UpdateText(); + + /// Rain or snow rather than light rain or light snow. + public bool Heavy + { + get => GetValue(HeavyProperty); + set => SetValue(HeavyProperty, value); + } + + public bool Snow + { + get => GetValue(SnowProperty); + set => SetValue(SnowProperty, value); + } + + protected override Type StyleKeyOverride => typeof(ComboBoxItem); + + /// The key of the text the option is named by. + public string Key => (Snow ? "OvercastSnow" : "OvercastRain") + (Heavy ? "" : "Light"); + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + if (change.Property == HeavyProperty || change.Property == SnowProperty) + UpdateText(); + } + + protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) + { + base.OnAttachedToLogicalTree(e); + AppLanguage.Changed += UpdateText; + UpdateText(); + } + + protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) + { + AppLanguage.Changed -= UpdateText; + base.OnDetachedFromLogicalTree(e); + } + + private void UpdateText() => Content = AppLanguage.Text(Key); +} diff --git a/StarterNG/Views/Scenarios.axaml b/StarterNG/Views/Scenarios.axaml index 0d090dd..2370ef5 100644 --- a/StarterNG/Views/Scenarios.axaml +++ b/StarterNG/Views/Scenarios.axaml @@ -193,8 +193,8 @@ - - + + Fog read-out in the language the user is reading the rest in. private static string FogScale2(int metres) => FogScale.Format(metres, LanguageCulture); - private bool? _precipitationIsSnow; - - /// Above 1 the overcast rains or snows, as the temperature decides in the simulator. - private void ShowPrecipitationAs(bool snow) - { - if (_precipitationIsSnow == snow) - return; - _precipitationIsSnow = snow; - weatherPrecipitationLight[!ContentProperty] = - new Avalonia.Data.Binding(snow ? "[OvercastSnowLight]" : "[OvercastRainLight]") { Source = App.Loc }; - weatherPrecipitation[!ContentProperty] = - new Avalonia.Data.Binding(snow ? "[OvercastSnow]" : "[OvercastRain]") { Source = App.Loc }; - } - private const string RandomTag = "random"; private static void SelectByTag(ComboBox combo, string tag, string? excludeFromNearest = null)