mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-12-01 09:27:05 +01:00
fix(weather): rain or snow is named by a control of its own, with no reflection binding
The two precipitation options were renamed by a Binding made in code, which reads its path by reflection: unsafe for the NativeAOT starter and flagged as such by the analysis. A PrecipitationItem now names itself from the starter's texts and follows a change of language, and the weather tab only tells it whether it snows.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.LogicalTree;
|
||||
|
||||
namespace StarterNG.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public class PrecipitationItem : ComboBoxItem
|
||||
{
|
||||
public static readonly StyledProperty<bool> HeavyProperty =
|
||||
AvaloniaProperty.Register<PrecipitationItem, bool>(nameof(Heavy));
|
||||
|
||||
public static readonly StyledProperty<bool> SnowProperty =
|
||||
AvaloniaProperty.Register<PrecipitationItem, bool>(nameof(Snow));
|
||||
|
||||
public PrecipitationItem() => UpdateText();
|
||||
|
||||
/// <summary>Rain or snow rather than light rain or light snow.</summary>
|
||||
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);
|
||||
|
||||
/// <summary>The key of the text the option is named by.</summary>
|
||||
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);
|
||||
}
|
||||
@@ -193,8 +193,8 @@
|
||||
<ComboBoxItem Tag="0.3" Content="{Binding [OvercastLight], Source={x:Static starterNg:App.Loc}}" />
|
||||
<ComboBoxItem Tag="0.7" Content="{Binding [OvercastModerate], Source={x:Static starterNg:App.Loc}}" />
|
||||
<ComboBoxItem Tag="1" Content="{Binding [OvercastCloudy], Source={x:Static starterNg:App.Loc}}" />
|
||||
<ComboBoxItem Tag="1.1" Name="weatherPrecipitationLight" Content="{Binding [OvercastRainLight], Source={x:Static starterNg:App.Loc}}" />
|
||||
<ComboBoxItem Tag="1.5" Name="weatherPrecipitation" Content="{Binding [OvercastRain], Source={x:Static starterNg:App.Loc}}" />
|
||||
<ctl:PrecipitationItem Tag="1.1" Name="weatherPrecipitationLight" />
|
||||
<ctl:PrecipitationItem Tag="1.5" Name="weatherPrecipitation" Heavy="True" />
|
||||
</ComboBox>
|
||||
|
||||
<Border Grid.Row="11" Grid.Column="0" Grid.ColumnSpan="5"
|
||||
|
||||
@@ -831,7 +831,7 @@ public partial class Scenarios : UserControl
|
||||
weatherTemp.ValueText = weatherTempLabel.Text;
|
||||
weatherFog.ValueText = weatherFogLabel.Text;
|
||||
weatherDayDate.Text = string.Format(LanguageCulture, App.Loc["DayOfYear"], SelectedDayOfYear());
|
||||
ShowPrecipitationAs(SceneryWeather.Snows(weatherTemp.Value));
|
||||
weatherPrecipitationLight.Snow = weatherPrecipitation.Snow = SceneryWeather.Snows(weatherTemp.Value);
|
||||
|
||||
weatherRestore.IsEnabled = _currentScenery?.Weather.Changed ?? false;
|
||||
}
|
||||
@@ -861,20 +861,6 @@ public partial class Scenarios : UserControl
|
||||
/// <summary>Fog read-out in the language the user is reading the rest in.</summary>
|
||||
private static string FogScale2(int metres) => FogScale.Format(metres, LanguageCulture);
|
||||
|
||||
private bool? _precipitationIsSnow;
|
||||
|
||||
/// <summary>Above 1 the overcast rains or snows, as the temperature decides in the simulator.</summary>
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user