feat: separate theme options.

This commit is contained in:
Zhang Dian
2024-11-24 20:52:39 +08:00
parent 4803f0e65a
commit 7ac603b18e
4 changed files with 110 additions and 42 deletions

View File

@@ -1,10 +1,12 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Styling;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Semi.Avalonia.Demo.Views;
@@ -45,32 +47,61 @@ public partial class MainView : UserControl
public partial class MainViewModel : ObservableObject
{
public ObservableCollection<ThemeItem> Themes { get; } =
[
new("Default", ThemeVariant.Default),
new("Light", ThemeVariant.Light),
new("Dark", ThemeVariant.Dark),
new("Aquatic", SemiTheme.Aquatic),
new("Desert", SemiTheme.Desert),
new("Dust", SemiTheme.Dust),
new("NightSky", SemiTheme.NightSky)
];
public IReadOnlyList<MenuItemViewModel> MenuItems { get; }
[ObservableProperty] private ThemeItem? _selectedTheme;
partial void OnSelectedThemeChanged(ThemeItem? oldValue, ThemeItem? newValue)
public MainViewModel()
{
MenuItems =
[
new MenuItemViewModel
{
Header = "High Contrast Theme",
Items =
[
new MenuItemViewModel
{
Header = "Aquatic",
Command = SelectThemeCommand,
CommandParameter = SemiTheme.Aquatic
},
new MenuItemViewModel
{
Header = "Desert",
Command = SelectThemeCommand,
CommandParameter = SemiTheme.Desert
},
new MenuItemViewModel
{
Header = "Dust",
Command = SelectThemeCommand,
CommandParameter = SemiTheme.Dust
},
new MenuItemViewModel
{
Header = "NightSky",
Command = SelectThemeCommand,
CommandParameter = SemiTheme.NightSky
},
]
}
];
}
[RelayCommand]
public void SelectTheme(object? obj)
{
if (newValue is null) return;
var app = Application.Current;
if (app is not null)
{
app.RequestedThemeVariant = newValue.Theme;
app.RequestedThemeVariant = obj as ThemeVariant;
}
}
}
public class ThemeItem(string name, ThemeVariant theme)
public class MenuItemViewModel
{
public string Name { get; set; } = name;
public ThemeVariant Theme { get; set; } = theme;
public string? Header { get; set; }
public ICommand? Command { get; set; }
public object? CommandParameter { get; set; }
public IList<MenuItemViewModel>? Items { get; set; }
}