113 lines
3.3 KiB
C#
Raw Normal View History

using System;
2024-11-24 20:52:39 +08:00
using System.Collections.Generic;
2024-11-25 17:57:24 +08:00
using System.Threading.Tasks;
2024-11-24 20:52:39 +08:00
using System.Windows.Input;
2023-02-10 00:34:48 +08:00
using Avalonia;
2023-01-20 23:08:33 +08:00
using Avalonia.Controls;
2024-11-25 17:57:24 +08:00
using Avalonia.Controls.ApplicationLifetimes;
2023-02-10 00:34:48 +08:00
using Avalonia.Styling;
2024-09-20 04:08:08 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
2024-11-24 20:52:39 +08:00
using CommunityToolkit.Mvvm.Input;
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
}
2024-09-20 04:08:08 +08:00
}
public partial class MainViewModel : ObservableObject
2024-09-20 04:08:08 +08:00
{
2024-11-25 17:57:24 +08:00
public string DocumentationUrl => "https://docs.irihi.tech/semi";
public string RepoUrl => "https://github.com/irihitech/Semi.Avalonia";
2024-11-24 20:52:39 +08:00
public IReadOnlyList<MenuItemViewModel> MenuItems { get; }
2024-11-24 20:52:39 +08:00
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
},
]
}
];
}
2024-11-24 20:52:39 +08:00
[RelayCommand]
2024-11-25 17:57:24 +08:00
private void ToggleTheme()
{
var app = Application.Current;
if (app is null) return;
var theme = app.ActualThemeVariant;
app.RequestedThemeVariant = theme == ThemeVariant.Dark ? ThemeVariant.Light : ThemeVariant.Dark;
}
[RelayCommand]
private void SelectTheme(object? obj)
2024-09-20 04:08:08 +08:00
{
var app = Application.Current;
if (app is not null)
{
2024-11-24 20:52:39 +08:00
app.RequestedThemeVariant = obj as ThemeVariant;
2024-09-20 04:08:08 +08:00
}
}
2024-11-25 17:57:24 +08:00
[RelayCommand]
private static async Task OpenUrl(string url)
{
var launcher = ResolveDefaultTopLevel()?.Launcher;
if (launcher is not null)
{
await launcher.LaunchUriAsync(new Uri(url));
}
}
private static TopLevel? ResolveDefaultTopLevel()
{
return Application.Current?.ApplicationLifetime switch
{
IClassicDesktopStyleApplicationLifetime desktopLifetime => desktopLifetime.MainWindow,
ISingleViewApplicationLifetime singleView => TopLevel.GetTopLevel(singleView.MainView),
_ => null
};
}
2024-09-20 04:08:08 +08:00
}
2024-11-24 20:52:39 +08:00
public class MenuItemViewModel
2024-09-20 04:08:08 +08:00
{
2024-11-24 20:52:39 +08:00
public string? Header { get; set; }
public ICommand? Command { get; set; }
public object? CommandParameter { get; set; }
public IList<MenuItemViewModel>? Items { get; set; }
2023-01-20 23:08:33 +08:00
}