76 lines
2.2 KiB
C#
Raw Normal View History

using System;
2024-09-20 04:08:08 +08:00
using System.Collections.ObjectModel;
2023-02-10 00:34:48 +08:00
using Avalonia;
2023-01-20 23:08:33 +08:00
using Avalonia.Controls;
2023-02-10 00:34:48 +08:00
using Avalonia.Interactivity;
using Avalonia.Styling;
2024-09-20 04:08:08 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
2023-01-20 23:08:33 +08:00
namespace Semi.Avalonia.Demo.Views;
public partial class MainView : UserControl
{
public MainView()
{
InitializeComponent();
2024-09-20 04:08:08 +08:00
this.DataContext = new MainViewModel();
2023-01-20 23:08:33 +08:00
}
2023-02-10 00:34:48 +08:00
private void ToggleButton_OnIsCheckedChanged(object sender, RoutedEventArgs e)
{
var app = Application.Current;
if (app is not null)
{
var theme = app.ActualThemeVariant;
2023-02-10 00:49:07 +08:00
app.RequestedThemeVariant = theme == ThemeVariant.Dark ? ThemeVariant.Light : ThemeVariant.Dark;
2023-02-10 00:34:48 +08:00
}
}
private async void OpenRepository(object sender, RoutedEventArgs e)
{
var top = TopLevel.GetTopLevel(this);
if (top is null) return;
var launcher = top.Launcher;
await launcher.LaunchUriAsync(new Uri("https://github.com/irihitech/Semi.Avalonia"));
}
private async void OpenDocumentation(object sender, RoutedEventArgs e)
{
var top = TopLevel.GetTopLevel(this);
if (top is null) return;
var launcher = top.Launcher;
await launcher.LaunchUriAsync(new Uri("https://docs.irihi.tech/semi"));
}
2024-09-20 04:08:08 +08:00
}
public partial class MainViewModel : ObservableObject
2024-09-20 04:08:08 +08:00
{
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)
];
2024-09-20 04:08:08 +08:00
[ObservableProperty] private ThemeItem? _selectedTheme;
2024-09-20 04:08:08 +08:00
partial void OnSelectedThemeChanged(ThemeItem? oldValue, ThemeItem? newValue)
{
if (newValue is null) return;
var app = Application.Current;
if (app is not null)
{
app.RequestedThemeVariant = newValue.Theme;
}
}
}
public class ThemeItem(string name, ThemeVariant theme)
{
public string Name { get; set; } = name;
public ThemeVariant Theme { get; set; } = theme;
2023-01-20 23:08:33 +08:00
}