2025-01-21 18:51:45 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-01-21 19:36:16 +08:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using Avalonia;
|
2025-01-22 15:53:18 +08:00
|
|
|
|
using Avalonia.Collections;
|
2025-01-21 18:51:45 +08:00
|
|
|
|
using Avalonia.Controls;
|
2025-01-21 19:36:16 +08:00
|
|
|
|
using Avalonia.Media;
|
2025-01-21 18:51:45 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using Semi.Avalonia.Tokens;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Semi.Avalonia.Demo.ViewModels;
|
|
|
|
|
|
|
2025-09-25 17:57:00 +08:00
|
|
|
|
public partial class VariablesDemoViewModel : ObservableObject
|
2025-01-21 18:51:45 +08:00
|
|
|
|
{
|
2025-01-22 15:53:18 +08:00
|
|
|
|
public DataGridCollectionView GridData { get; set; }
|
2025-09-25 17:57:00 +08:00
|
|
|
|
[ObservableProperty] private string _searchText = string.Empty;
|
2025-01-21 18:51:45 +08:00
|
|
|
|
|
|
|
|
|
|
public VariablesDemoViewModel()
|
|
|
|
|
|
{
|
2025-01-22 15:53:18 +08:00
|
|
|
|
IResourceDictionary dictionary = new Variables();
|
|
|
|
|
|
foreach (var token in Tokens)
|
2025-01-21 18:51:45 +08:00
|
|
|
|
{
|
2025-01-22 15:53:18 +08:00
|
|
|
|
if (token.ResourceKey is not null && dictionary.TryGetValue(token.ResourceKey, out var value))
|
2025-01-21 18:51:45 +08:00
|
|
|
|
{
|
2025-01-22 16:29:56 +08:00
|
|
|
|
token.Type = value?.GetType();
|
2025-01-22 15:53:18 +08:00
|
|
|
|
token.Value = GetValueString(value);
|
2025-01-21 18:51:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-22 15:53:18 +08:00
|
|
|
|
|
|
|
|
|
|
GridData = new DataGridCollectionView(Tokens);
|
|
|
|
|
|
GridData.GroupDescriptions.Add(new DataGridPathGroupDescription(nameof(VariableItem.Category)));
|
2025-01-21 18:51:45 +08:00
|
|
|
|
}
|
2025-01-21 19:36:16 +08:00
|
|
|
|
|
2025-09-25 17:57:00 +08:00
|
|
|
|
private static string? GetValueString(object? value)
|
2025-01-21 19:36:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (value is null) return string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
return value switch
|
|
|
|
|
|
{
|
|
|
|
|
|
double d => d.ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
|
CornerRadius c => c.IsUniform ? $"{c.TopLeft}" : c.ToString(),
|
|
|
|
|
|
Thickness t => t.IsUniform ? $"{t.Left}" : t.ToString(),
|
|
|
|
|
|
FontWeight fontWeight => Convert.ToInt32(fontWeight).ToString(),
|
|
|
|
|
|
FontFamily fontFamily => fontFamily.FamilyNames.ToString(),
|
|
|
|
|
|
_ => value.ToString()
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2025-01-22 15:53:18 +08:00
|
|
|
|
|
2025-09-25 17:57:00 +08:00
|
|
|
|
partial void OnSearchTextChanged(string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
|
|
|
|
{
|
|
|
|
|
|
GridData.Filter = _ => true;
|
|
|
|
|
|
GridData.Refresh();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var search = value.Trim();
|
|
|
|
|
|
GridData.Filter = item =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item is not VariableItem variableItem) return false;
|
|
|
|
|
|
return (variableItem.Category?.Contains(search, StringComparison.InvariantCultureIgnoreCase) ?? false) ||
|
|
|
|
|
|
(variableItem.ResourceKey?.Contains(search, StringComparison.InvariantCultureIgnoreCase) ?? false) ||
|
|
|
|
|
|
(variableItem.Value?.Contains(search, StringComparison.InvariantCultureIgnoreCase) ?? false) ||
|
|
|
|
|
|
(variableItem.Type?.Name.Contains(search, StringComparison.InvariantCultureIgnoreCase) ?? false) ||
|
|
|
|
|
|
(variableItem.Description?.Contains(search, StringComparison.InvariantCultureIgnoreCase) ?? false);
|
|
|
|
|
|
};
|
|
|
|
|
|
GridData.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-22 15:53:18 +08:00
|
|
|
|
private static List<VariableItem> Tokens { get; set; } =
|
|
|
|
|
|
[
|
|
|
|
|
|
new("Height", "SemiHeightControlSmall"),
|
|
|
|
|
|
new("Height", "SemiHeightControlDefault"),
|
|
|
|
|
|
new("Height", "SemiHeightControlLarge"),
|
|
|
|
|
|
new("Icon Size", "SemiWidthIconExtraSmall"),
|
|
|
|
|
|
new("Icon Size", "SemiWidthIconSmall"),
|
|
|
|
|
|
new("Icon Size", "SemiWidthIconMedium"),
|
|
|
|
|
|
new("Icon Size", "SemiWidthIconLarge"),
|
|
|
|
|
|
new("Icon Size", "SemiWidthIconExtraLarge"),
|
2025-09-25 17:57:00 +08:00
|
|
|
|
new("Border CornerRadius Spacing", "SemiBorderRadiusSpacingExtraSmall"),
|
|
|
|
|
|
new("Border CornerRadius Spacing", "SemiBorderRadiusSpacingSmall"),
|
|
|
|
|
|
new("Border CornerRadius Spacing", "SemiBorderRadiusSpacingMedium"),
|
|
|
|
|
|
new("Border CornerRadius Spacing", "SemiBorderRadiusSpacingLarge"),
|
|
|
|
|
|
new("Border CornerRadius Spacing", "SemiBorderRadiusSpacingFull"),
|
2025-01-22 15:53:18 +08:00
|
|
|
|
new("Border CornerRadius", "SemiBorderRadiusExtraSmall"),
|
|
|
|
|
|
new("Border CornerRadius", "SemiBorderRadiusSmall"),
|
|
|
|
|
|
new("Border CornerRadius", "SemiBorderRadiusMedium"),
|
|
|
|
|
|
new("Border CornerRadius", "SemiBorderRadiusLarge"),
|
|
|
|
|
|
new("Border CornerRadius", "SemiBorderRadiusFull"),
|
|
|
|
|
|
new("Border Spacing", "SemiBorderSpacing"),
|
|
|
|
|
|
new("Border Spacing", "SemiBorderSpacingControl"),
|
|
|
|
|
|
new("Border Spacing", "SemiBorderSpacingControlFocus"),
|
|
|
|
|
|
new("Border Thickness", "SemiBorderThickness"),
|
|
|
|
|
|
new("Border Thickness", "SemiBorderThicknessControl"),
|
|
|
|
|
|
new("Border Thickness", "SemiBorderThicknessControlFocus"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingNone"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingSuperTight"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingExtraTight"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingTight"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingBaseTight"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingBase"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingBaseLoose"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingLoose"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingExtraLoose"),
|
|
|
|
|
|
new("Spacing", "SemiSpacingSuperLoose"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessNone"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessSuperTight"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessExtraTight"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessTight"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessBaseTight"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessBase"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessBaseLoose"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessLoose"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessExtraLoose"),
|
|
|
|
|
|
new("Thickness", "SemiThicknessSuperLoose"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeSmall"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeRegular"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeHeader6"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeHeader5"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeHeader4"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeHeader3"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeHeader2"),
|
|
|
|
|
|
new("FontSize", "SemiFontSizeHeader1"),
|
|
|
|
|
|
new("FontWeight", "SemiFontWeightLight"),
|
|
|
|
|
|
new("FontWeight", "SemiFontWeightRegular"),
|
|
|
|
|
|
new("FontWeight", "SemiFontWeightBold"),
|
|
|
|
|
|
new("FontFamily", "SemiFontFamilyRegular"),
|
|
|
|
|
|
];
|
2025-01-21 18:51:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-22 15:53:18 +08:00
|
|
|
|
public class VariableItem()
|
2025-01-21 18:51:45 +08:00
|
|
|
|
{
|
2025-01-22 15:53:18 +08:00
|
|
|
|
public string? Category { get; set; }
|
|
|
|
|
|
public string? ResourceKey { get; set; }
|
2025-01-22 16:29:56 +08:00
|
|
|
|
public Type? Type { get; set; }
|
2025-01-22 15:53:18 +08:00
|
|
|
|
public string? Value { get; set; }
|
2025-01-22 16:29:56 +08:00
|
|
|
|
public string? Description { get; set; }
|
2025-01-21 18:51:45 +08:00
|
|
|
|
|
2025-01-22 15:53:18 +08:00
|
|
|
|
public VariableItem(string category, string resourceKey, string description = "") : this()
|
2025-01-21 18:51:45 +08:00
|
|
|
|
{
|
2025-01-22 15:53:18 +08:00
|
|
|
|
Category = category;
|
2025-01-21 18:51:45 +08:00
|
|
|
|
ResourceKey = resourceKey;
|
|
|
|
|
|
Description = description;
|
|
|
|
|
|
}
|
2025-01-22 15:53:18 +08:00
|
|
|
|
|
|
|
|
|
|
public string CopyText =>
|
|
|
|
|
|
$"""
|
|
|
|
|
|
<StaticResource x:Key="" ResourceKey="{ResourceKey}" />
|
|
|
|
|
|
""";
|
2025-01-21 18:51:45 +08:00
|
|
|
|
}
|