mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-04-08 18:26:35 +08:00
Adjust Resource Structure (#771)
* chore: update GitHub Actions to use latest versions of actions. * feat: update theme resource structure and remove Schemes. * refactor: simplify locale resource handling and remove redundant code. * feat: add Light theme resource to ColorPicker and DataGrid semi themes. * feat: replace SetResources with BulkSetResources.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceInclude x:Key="Default" Source="Light.axaml" />
|
||||
<ResourceInclude x:Key="Light" Source="Light.axaml" />
|
||||
<ResourceInclude x:Key="Dark" Source="Dark.axaml" />
|
||||
</ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceInclude x:Key="Default" Source="Light.axaml" />
|
||||
<ResourceInclude x:Key="Light" Source="Light.axaml" />
|
||||
<ResourceInclude x:Key="Dark" Source="Dark.axaml" />
|
||||
</ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
|
||||
@@ -4,17 +4,18 @@
|
||||
<Styles.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceInclude x:Key="Default" Source="/Schemes/LightScheme.axaml" />
|
||||
<ResourceInclude x:Key="Dark" Source="/Schemes/DarkScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Aquatic}" Source="/Schemes/AquaticScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Desert}" Source="/Schemes/DesertScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Dusk}" Source="/Schemes/DuskScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.NightSky}" Source="/Schemes/NightSkyScheme.axaml" />
|
||||
<ResourceInclude x:Key="Default" Source="/Themes/Light/_index.axaml" />
|
||||
<ResourceInclude x:Key="Light" Source="/Themes/Light/_index.axaml" />
|
||||
<ResourceInclude x:Key="Dark" Source="/Themes/Dark/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Aquatic}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Desert}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Dusk}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.NightSky}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
</ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Controls/_index.axaml" />
|
||||
<ResourceInclude Source="/Themes/Shared/_index.axaml" />
|
||||
<ResourceInclude Source="/Tokens/Variables.axaml" />
|
||||
<ResourceInclude Source="/Tokens/_index.axaml" />
|
||||
<ResourceInclude Source="/Locale/zh-cn.axaml" />
|
||||
<semi:Icons />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
|
||||
[assembly: XmlnsPrefix("https://irihi.tech/semi", "semi")]
|
||||
[assembly: XmlnsDefinition("https://irihi.tech/semi", "Semi.Avalonia")]
|
||||
[assembly: XmlnsDefinition("https://irihi.tech/semi", "Semi.Avalonia.Converters")]
|
||||
[assembly: XmlnsDefinition("https://irihi.tech/semi", "Semi.Avalonia.Converters")]
|
||||
[assembly: XmlnsDefinition("https://irihi.tech/semi", "Semi.Avalonia.Tokens")]
|
||||
[assembly: XmlnsDefinition("https://irihi.tech/semi", "Semi.Avalonia.Tokens.Palette")]
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Tokens/HighContrast/Aquatic.axaml" />
|
||||
<ResourceInclude Source="/Themes/HighContrast/_index.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
@@ -1,6 +0,0 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Tokens/Palette/Dark.axaml" />
|
||||
<ResourceInclude Source="/Themes/Dark/_index.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
@@ -1,6 +0,0 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Tokens/HighContrast/Desert.axaml" />
|
||||
<ResourceInclude Source="/Themes/HighContrast/_index.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
@@ -1,6 +0,0 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Tokens/HighContrast/Dusk.axaml" />
|
||||
<ResourceInclude Source="/Themes/HighContrast/_index.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
@@ -1,6 +0,0 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Tokens/Palette/Light.axaml" />
|
||||
<ResourceInclude Source="/Themes/Light/_index.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
@@ -1,6 +0,0 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Tokens/HighContrast/NightSky.axaml" />
|
||||
<ResourceInclude Source="/Themes/HighContrast/_index.axaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
@@ -5,19 +5,20 @@
|
||||
<Styles.Resources>
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceInclude x:Key="Default" Source="/Schemes/LightScheme.axaml" />
|
||||
<ResourceInclude x:Key="Dark" Source="/Schemes/DarkScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Aquatic}" Source="/Schemes/AquaticScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Desert}" Source="/Schemes/DesertScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Dusk}" Source="/Schemes/DuskScheme.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.NightSky}" Source="/Schemes/NightSkyScheme.axaml" />
|
||||
<ResourceInclude x:Key="Default" Source="/Themes/Light/_index.axaml" />
|
||||
<ResourceInclude x:Key="Light" Source="/Themes/Light/_index.axaml" />
|
||||
<ResourceInclude x:Key="Dark" Source="/Themes/Dark/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Aquatic}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Desert}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Dusk}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.NightSky}" Source="/Themes/HighContrast/_index.axaml" />
|
||||
</ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceInclude Source="/Controls/_index.axaml" />
|
||||
<ResourceInclude Source="/Themes/Shared/_index.axaml" />
|
||||
<ResourceInclude Source="/Tokens/Variables.axaml" />
|
||||
<ResourceInclude Source="/Tokens/_index.axaml" />
|
||||
<ResourceInclude Source="/Locale/zh-cn.axaml" />
|
||||
<ResourceInclude Source="/Icons/_index.axaml" />
|
||||
<semi:Icons />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</Styles.Resources>
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Styling;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
using Semi.Avalonia.Locale;
|
||||
|
||||
namespace Semi.Avalonia;
|
||||
@@ -36,43 +37,27 @@ public class SemiTheme : Styles
|
||||
|
||||
private static readonly ResourceDictionary DefaultResource = new zh_cn();
|
||||
|
||||
private CultureInfo? _locale;
|
||||
|
||||
public CultureInfo? Locale
|
||||
{
|
||||
get => _locale;
|
||||
get;
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
if (TryGetLocaleResource(value, out var resource) && resource is not null)
|
||||
{
|
||||
_locale = value;
|
||||
if (Resources is ResourceDictionary rd)
|
||||
{
|
||||
rd.SetItems(resource);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in resource) Resources[kv.Key] = kv.Value;
|
||||
}
|
||||
field = value;
|
||||
Resources.BulkSetResources(resource);
|
||||
}
|
||||
else
|
||||
{
|
||||
_locale = new CultureInfo("zh-CN");
|
||||
if (Resources is ResourceDictionary rd)
|
||||
{
|
||||
rd.SetItems(DefaultResource);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in DefaultResource) Resources[kv.Key] = kv.Value;
|
||||
}
|
||||
field = new CultureInfo("zh-CN");
|
||||
Resources.BulkSetResources(DefaultResource);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
_locale = CultureInfo.InvariantCulture;
|
||||
field = CultureInfo.InvariantCulture;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,28 +90,13 @@ public class SemiTheme : Styles
|
||||
{
|
||||
if (culture is null) return;
|
||||
if (!LocaleToResource.TryGetValue(culture, out var resources)) return;
|
||||
|
||||
if (application.Resources is ResourceDictionary rd)
|
||||
{
|
||||
rd.SetItems(resources);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in resources) application.Resources[kv.Key] = kv.Value;
|
||||
}
|
||||
application.Resources.BulkSetResources(resources);
|
||||
}
|
||||
|
||||
public static void OverrideLocaleResources(StyledElement element, CultureInfo? culture)
|
||||
{
|
||||
if (culture is null) return;
|
||||
if (!LocaleToResource.TryGetValue(culture, out var resources)) return;
|
||||
if (element.Resources is ResourceDictionary rd)
|
||||
{
|
||||
rd.SetItems(resources);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var kv in resources) element.Resources[kv.Key] = kv.Value;
|
||||
}
|
||||
element.Resources.BulkSetResources(resources);
|
||||
}
|
||||
}
|
||||
16
src/Semi.Avalonia/Tokens/_index.axaml
Normal file
16
src/Semi.Avalonia/Tokens/_index.axaml
Normal file
@@ -0,0 +1,16 @@
|
||||
<ResourceDictionary xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:semi="https://irihi.tech/semi">
|
||||
<ResourceDictionary.ThemeDictionaries>
|
||||
<semi:Light x:Key="Default" />
|
||||
<semi:Light x:Key="Light" />
|
||||
<semi:Dark x:Key="Dark" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Aquatic}" Source="HighContrast/Aquatic.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Desert}" Source="HighContrast/Desert.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.Dusk}" Source="HighContrast/Dusk.axaml" />
|
||||
<ResourceInclude x:Key="{x:Static semi:SemiTheme.NightSky}" Source="HighContrast/NightSky.axaml" />
|
||||
</ResourceDictionary.ThemeDictionaries>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<semi:Variables />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
Reference in New Issue
Block a user