feat: Create a new GUI project based on Avalonia. (Powered by Claude)

This commit is contained in:
real-zony committed 2026-01-09 23:38:57 +08:00
1 parent 00e2118645
commit 1f7414ead3
46 files changed
+3225

No files matched your search

@@ -0,0 +1,137 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ZonyLrcTools.Common.Album;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
using ZonyLrcTools.Desktop.Services;
namespace ZonyLrcTools.Desktop.ViewModels;
public partial class AlbumDownloadViewModel : ViewModelBase
{
private readonly IAlbumDownloader _albumDownloader;
private readonly IDialogService _dialogService;
private readonly IUILocalizationService? _localization;
private CancellationTokenSource? _downloadCts;
[ObservableProperty]
private string? _selectedFolderPath;
[ObservableProperty]
private int _parallelCount = 2;
[ObservableProperty]
private int _totalCount;
[ObservableProperty]
private int _completedCount;
[ObservableProperty]
private int _failedCount;
[ObservableProperty]
private double _progressPercentage;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CanStartDownload))]
private bool _isDownloading;
// Localized strings
public string AlbumTitle => _localization?["Album_Title"] ?? "Album Cover Download";
public string AlbumDescription => _localization?["Album_Description"] ?? "Download album artwork for your music collection";
public string AlbumSelectFolder => _localization?["Album_SelectFolder"] ?? "Select music folder...";
public string AlbumBrowse => _localization?["Album_Browse"] ?? "Browse...";
public string AlbumParallel => _localization?["Album_Parallel"] ?? "Parallel:";
public string AlbumStartDownload => _localization?["Album_StartDownload"] ?? "Start Download";
public string AlbumStopDownload => _localization?["Album_StopDownload"] ?? "Stop Download";
public string CommonTotal => _localization?["Common_Total"] ?? "Total:";
public string CommonFiles => _localization?["Common_Files"] ?? "files";
public string CommonSuccess => _localization?["Common_Success"] ?? "Success:";
public string CommonFailed => _localization?["Common_Failed"] ?? "Failed:";
public string ColumnSongName => _localization?["Column_SongName"] ?? "Song Name";
public string ColumnArtist => _localization?["Column_Artist"] ?? "Artist";
public string ColumnStatus => _localization?["Column_Status"] ?? "Status";
public bool CanStartDownload => !IsDownloading && !string.IsNullOrEmpty(SelectedFolderPath);
public ObservableCollection<MusicFileViewModel> MusicFiles { get; } = new();
public AlbumDownloadViewModel(
IAlbumDownloader albumDownloader,
IDialogService dialogService,
IUILocalizationService? localization = null)
{
_albumDownloader = albumDownloader;
_dialogService = dialogService;
_localization = localization;
if (_localization != null)
{
_localization.LanguageChanged += OnLanguageChanged;
}
}
private void OnLanguageChanged(object? sender, EventArgs e)
{
OnPropertyChanged(nameof(AlbumTitle));
OnPropertyChanged(nameof(AlbumDescription));
OnPropertyChanged(nameof(AlbumSelectFolder));
OnPropertyChanged(nameof(AlbumBrowse));
OnPropertyChanged(nameof(AlbumParallel));
OnPropertyChanged(nameof(AlbumStartDownload));
OnPropertyChanged(nameof(AlbumStopDownload));
OnPropertyChanged(nameof(CommonTotal));
OnPropertyChanged(nameof(CommonFiles));
OnPropertyChanged(nameof(CommonSuccess));
OnPropertyChanged(nameof(CommonFailed));
OnPropertyChanged(nameof(ColumnSongName));
OnPropertyChanged(nameof(ColumnArtist));
OnPropertyChanged(nameof(ColumnStatus));
}
[RelayCommand]
private async Task SelectFolderAsync()
{
var folder = await _dialogService.ShowFolderPickerAsync("Select Music Folder");
if (string.IsNullOrEmpty(folder)) return;
SelectedFolderPath = folder;
OnPropertyChanged(nameof(CanStartDownload));
}
[RelayCommand]
private async Task StartDownloadAsync()
{
if (string.IsNullOrEmpty(SelectedFolderPath)) return;
IsDownloading = true;
CompletedCount = 0;
FailedCount = 0;
ProgressPercentage = 0;
_downloadCts = new CancellationTokenSource();
try
{
// TODO: Implement actual download logic using _albumDownloader
await Task.Delay(1000, _downloadCts.Token); // Placeholder
}
catch (OperationCanceledException)
{
// User cancelled
}
finally
{
IsDownloading = false;
_downloadCts?.Dispose();
_downloadCts = null;
}
}
[RelayCommand]
private void CancelDownload()
{
_downloadCts?.Cancel();
}
}
@@ -0,0 +1,15 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace ZonyLrcTools.Desktop.ViewModels;
public partial class HomeViewModel : ViewModelBase
{
[ObservableProperty]
private string _welcomeMessage = "Welcome to ZonyLrcTools X";
[ObservableProperty]
private string _description = "A cross-platform tool for downloading lyrics and album covers for your music collection.";
[ObservableProperty]
private string _version = "4.0.3";
}
@@ -0,0 +1,164 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ZonyLrcTools.Common.Lyrics;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
using ZonyLrcTools.Desktop.Services;
namespace ZonyLrcTools.Desktop.ViewModels;
public partial class LyricsDownloadViewModel : ViewModelBase
{
private readonly ILyricsDownloader _lyricsDownloader;
private readonly IDialogService _dialogService;
private readonly IUILocalizationService? _localization;
private CancellationTokenSource? _downloadCts;
[ObservableProperty]
private string? _selectedFolderPath;
[ObservableProperty]
private int _parallelCount = 2;
[ObservableProperty]
private int _totalCount;
[ObservableProperty]
private int _completedCount;
[ObservableProperty]
private int _failedCount;
[ObservableProperty]
private double _progressPercentage;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(StartDownloadCommand))]
[NotifyPropertyChangedFor(nameof(CanStartDownload))]
private bool _isDownloading;
[ObservableProperty]
private string? _currentProcessingFile;
// Localized strings
public string LyricsTitle => _localization?["Lyrics_Title"] ?? "Lyrics Download";
public string LyricsDescription => _localization?["Lyrics_Description"] ?? "Batch download lyrics for your music files";
public string LyricsSelectFolder => _localization?["Lyrics_SelectFolder"] ?? "Select music folder...";
public string LyricsBrowse => _localization?["Lyrics_Browse"] ?? "Browse...";
public string LyricsParallel => _localization?["Lyrics_Parallel"] ?? "Parallel:";
public string LyricsStartDownload => _localization?["Lyrics_StartDownload"] ?? "Start Download";
public string LyricsStopDownload => _localization?["Lyrics_StopDownload"] ?? "Stop Download";
public string CommonTotal => _localization?["Common_Total"] ?? "Total:";
public string CommonFiles => _localization?["Common_Files"] ?? "files";
public string CommonSuccess => _localization?["Common_Success"] ?? "Success:";
public string CommonFailed => _localization?["Common_Failed"] ?? "Failed:";
public string ColumnSongName => _localization?["Column_SongName"] ?? "Song Name";
public string ColumnArtist => _localization?["Column_Artist"] ?? "Artist";
public string ColumnFilePath => _localization?["Column_FilePath"] ?? "File Path";
public string ColumnStatus => _localization?["Column_Status"] ?? "Status";
public bool CanStartDownload => !IsDownloading && !string.IsNullOrEmpty(SelectedFolderPath);
public ObservableCollection<MusicFileViewModel> MusicFiles { get; } = new();
public LyricsDownloadViewModel(
ILyricsDownloader lyricsDownloader,
IDialogService dialogService,
IUILocalizationService? localization = null)
{
_lyricsDownloader = lyricsDownloader;
_dialogService = dialogService;
_localization = localization;
if (_localization != null)
{
_localization.LanguageChanged += OnLanguageChanged;
}
}
private void OnLanguageChanged(object? sender, EventArgs e)
{
OnPropertyChanged(nameof(LyricsTitle));
OnPropertyChanged(nameof(LyricsDescription));
OnPropertyChanged(nameof(LyricsSelectFolder));
OnPropertyChanged(nameof(LyricsBrowse));
OnPropertyChanged(nameof(LyricsParallel));
OnPropertyChanged(nameof(LyricsStartDownload));
OnPropertyChanged(nameof(LyricsStopDownload));
OnPropertyChanged(nameof(CommonTotal));
OnPropertyChanged(nameof(CommonFiles));
OnPropertyChanged(nameof(CommonSuccess));
OnPropertyChanged(nameof(CommonFailed));
OnPropertyChanged(nameof(ColumnSongName));
OnPropertyChanged(nameof(ColumnArtist));
OnPropertyChanged(nameof(ColumnFilePath));
OnPropertyChanged(nameof(ColumnStatus));
}
[RelayCommand]
private async Task SelectFolderAsync()
{
var folder = await _dialogService.ShowFolderPickerAsync("Select Music Folder");
if (string.IsNullOrEmpty(folder)) return;
SelectedFolderPath = folder;
OnPropertyChanged(nameof(CanStartDownload));
}
[RelayCommand(CanExecute = nameof(CanStartDownload))]
private async Task StartDownloadAsync()
{
if (string.IsNullOrEmpty(SelectedFolderPath)) return;
IsDownloading = true;
CompletedCount = 0;
FailedCount = 0;
ProgressPercentage = 0;
_downloadCts = new CancellationTokenSource();
try
{
// TODO: Implement actual download logic using _lyricsDownloader
await Task.Delay(1000, _downloadCts.Token); // Placeholder
}
catch (OperationCanceledException)
{
// User cancelled
}
finally
{
IsDownloading = false;
_downloadCts?.Dispose();
_downloadCts = null;
}
}
[RelayCommand]
private void CancelDownload()
{
_downloadCts?.Cancel();
}
}
public partial class MusicFileViewModel : ObservableObject
{
[ObservableProperty]
private string _filePath = string.Empty;
[ObservableProperty]
private string _name = string.Empty;
[ObservableProperty]
private string _artist = string.Empty;
[ObservableProperty]
private bool _isProcessed;
[ObservableProperty]
private bool _isSuccessful;
[ObservableProperty]
private string? _statusMessage;
}
@@ -0,0 +1,72 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
using ZonyLrcTools.Desktop.Services;
namespace ZonyLrcTools.Desktop.ViewModels;
public partial class MainWindowViewModel : ViewModelBase
{
private readonly IThemeService _themeService;
private readonly IUILocalizationService _localization;
[ObservableProperty]
private ViewModelBase? _currentPage;
[ObservableProperty]
private int _selectedNavigationIndex;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ThemeButtonText))]
private bool _isDarkTheme;
[ObservableProperty]
private string _title = "ZonyLrcTools X";
// Navigation localized strings
public string NavHome => _localization["Nav_Home"];
public string NavLyricsDownload => _localization["Nav_LyricsDownload"];
public string NavAlbumDownload => _localization["Nav_AlbumDownload"];
public string NavSettings => _localization["Nav_Settings"];
public string HomeDescription => _localization["Home_Description"];
public string ThemeButtonText => IsDarkTheme
? _localization["Settings_ThemeLight"]
: _localization["Settings_ThemeDark"];
public MainWindowViewModel(
IThemeService themeService,
IUILocalizationService localization,
HomeViewModel homeViewModel)
{
_themeService = themeService;
_localization = localization;
// Subscribe to language changes
_localization.LanguageChanged += OnLanguageChanged;
// Set initial page
CurrentPage = homeViewModel;
SelectedNavigationIndex = 0;
}
private void OnLanguageChanged(object? sender, EventArgs e)
{
// Notify all localized properties have changed
OnPropertyChanged(nameof(NavHome));
OnPropertyChanged(nameof(NavLyricsDownload));
OnPropertyChanged(nameof(NavAlbumDownload));
OnPropertyChanged(nameof(NavSettings));
OnPropertyChanged(nameof(HomeDescription));
OnPropertyChanged(nameof(ThemeButtonText));
}
[RelayCommand]
private void ToggleTheme()
{
IsDarkTheme = !IsDarkTheme;
_themeService.SetTheme(IsDarkTheme
? Avalonia.Styling.ThemeVariant.Dark
: Avalonia.Styling.ThemeVariant.Light);
}
}
@@ -0,0 +1,253 @@
using System.Collections.ObjectModel;
using System.Globalization;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Options;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using ZonyLrcTools.Common.Configuration;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
namespace ZonyLrcTools.Desktop.ViewModels;
public partial class SettingsViewModel : ViewModelBase
{
private readonly GlobalOptions _globalOptions;
private readonly IUILocalizationService? _localizationService;
private bool _isLoading = true; // Prevent saving during initial load
[ObservableProperty]
private bool _followSystemLanguage = true;
[ObservableProperty]
private bool _isChineseSelected = true;
[ObservableProperty]
private bool _isEnglishSelected;
[ObservableProperty]
private bool _isProxyEnabled;
[ObservableProperty]
private string _proxyIp = "127.0.0.1";
[ObservableProperty]
private int _proxyPort = 7890;
[ObservableProperty]
private bool _isTranslationEnabled;
[ObservableProperty]
private bool _isOneLineMode;
[ObservableProperty]
private bool _skipExistingLyrics;
[ObservableProperty]
private string _selectedEncoding = "utf-8";
// Localized strings
public string SettingsTitle => _localizationService?["Settings_Title"] ?? "Settings";
public string SettingsLanguage => _localizationService?["Settings_Language"] ?? "Language";
public string SettingsFollowSystem => _localizationService?["Settings_FollowSystem"] ?? "Follow System";
public string SettingsChinese => _localizationService?["Settings_Chinese"] ?? "Chinese";
public string SettingsEnglish => _localizationService?["Settings_English"] ?? "English";
public string SettingsProxy => _localizationService?["Settings_Proxy"] ?? "Proxy";
public string SettingsProxyEnable => _localizationService?["Settings_ProxyEnable"] ?? "Enable Proxy";
public string SettingsProxyAddress => _localizationService?["Settings_ProxyAddress"] ?? "Proxy Address";
public string SettingsLyricsProvider => _localizationService?["Settings_LyricsProvider"] ?? "Lyrics Provider";
public string SettingsReset => _localizationService?["Settings_Reset"] ?? "Reset";
public ObservableCollection<string> AvailableEncodings { get; } = new()
{
"utf-8",
"utf-8-bom",
"gb2312",
"gbk"
};
public ObservableCollection<LyricsProviderSettingViewModel> LyricsProviders { get; } = new();
public SettingsViewModel(IOptions<GlobalOptions> options, IUILocalizationService? localizationService = null)
{
_globalOptions = options.Value;
_localizationService = localizationService;
// Subscribe to language changes
if (_localizationService != null)
{
_localizationService.LanguageChanged += OnLanguageChanged;
}
LoadSettings();
LoadLanguageSettings();
_isLoading = false; // Allow saving after initial load
}
private void OnLanguageChanged(object? sender, EventArgs e)
{
OnPropertyChanged(nameof(SettingsTitle));
OnPropertyChanged(nameof(SettingsLanguage));
OnPropertyChanged(nameof(SettingsFollowSystem));
OnPropertyChanged(nameof(SettingsChinese));
OnPropertyChanged(nameof(SettingsEnglish));
OnPropertyChanged(nameof(SettingsProxy));
OnPropertyChanged(nameof(SettingsProxyEnable));
OnPropertyChanged(nameof(SettingsProxyAddress));
OnPropertyChanged(nameof(SettingsLyricsProvider));
OnPropertyChanged(nameof(SettingsReset));
}
private void LoadLanguageSettings()
{
var currentCulture = CultureInfo.CurrentUICulture.Name;
IsChineseSelected = currentCulture.StartsWith("zh");
IsEnglishSelected = currentCulture.StartsWith("en");
}
partial void OnFollowSystemLanguageChanged(bool value)
{
if (value)
{
// Auto-detect system language
var systemCulture = CultureInfo.InstalledUICulture.Name;
ApplyLanguage(systemCulture.StartsWith("zh") ? "zh-CN" : "en-US");
}
SaveSettingsIfNotLoading();
}
partial void OnIsChineseSelectedChanged(bool value)
{
if (value && !FollowSystemLanguage)
{
ApplyLanguage("zh-CN");
}
SaveSettingsIfNotLoading();
}
partial void OnIsEnglishSelectedChanged(bool value)
{
if (value && !FollowSystemLanguage)
{
ApplyLanguage("en-US");
}
SaveSettingsIfNotLoading();
}
partial void OnIsProxyEnabledChanged(bool value) => SaveSettingsIfNotLoading();
partial void OnProxyIpChanged(string value) => SaveSettingsIfNotLoading();
partial void OnProxyPortChanged(int value) => SaveSettingsIfNotLoading();
partial void OnIsTranslationEnabledChanged(bool value) => SaveSettingsIfNotLoading();
partial void OnIsOneLineModeChanged(bool value) => SaveSettingsIfNotLoading();
partial void OnSkipExistingLyricsChanged(bool value) => SaveSettingsIfNotLoading();
partial void OnSelectedEncodingChanged(string value) => SaveSettingsIfNotLoading();
private void ApplyLanguage(string cultureName)
{
_localizationService?.SetLanguage(cultureName);
}
private void SaveSettingsIfNotLoading()
{
if (!_isLoading)
{
SaveSettingsToFile();
}
}
private void LoadSettings()
{
// Load network settings
IsProxyEnabled = _globalOptions.NetworkOptions?.IsEnable ?? false;
ProxyIp = _globalOptions.NetworkOptions?.Ip ?? "127.0.0.1";
ProxyPort = _globalOptions.NetworkOptions?.Port ?? 7890;
// Load lyrics settings
var lyricsConfig = _globalOptions.Provider?.Lyric?.Config;
if (lyricsConfig != null)
{
IsTranslationEnabled = lyricsConfig.IsEnableTranslation;
IsOneLineMode = lyricsConfig.IsOneLine;
SkipExistingLyrics = lyricsConfig.IsSkipExistLyricFiles;
SelectedEncoding = lyricsConfig.FileEncoding ?? "utf-8";
}
// Load lyrics providers
var providers = _globalOptions.Provider?.Lyric?.Plugin;
if (providers != null)
{
foreach (var provider in providers.OrderBy(p => p.Priority))
{
LyricsProviders.Add(new LyricsProviderSettingViewModel
{
Name = provider.Name ?? "Unknown",
Priority = provider.Priority,
IsEnabled = provider.Priority != -1
});
}
}
}
private void SaveSettingsToFile()
{
try
{
// Update GlobalOptions with current values
if (_globalOptions.NetworkOptions != null)
{
_globalOptions.NetworkOptions.IsEnable = IsProxyEnabled;
_globalOptions.NetworkOptions.Ip = ProxyIp;
_globalOptions.NetworkOptions.Port = ProxyPort;
}
if (_globalOptions.Provider?.Lyric?.Config != null)
{
_globalOptions.Provider.Lyric.Config.IsEnableTranslation = IsTranslationEnabled;
_globalOptions.Provider.Lyric.Config.IsOneLine = IsOneLineMode;
_globalOptions.Provider.Lyric.Config.IsSkipExistLyricFiles = SkipExistingLyrics;
_globalOptions.Provider.Lyric.Config.FileEncoding = SelectedEncoding;
}
// Serialize and save to file
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(_globalOptions);
var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.yaml");
File.WriteAllText(configPath, yaml);
}
catch (Exception ex)
{
// Log error but don't crash - settings save is best effort
System.Diagnostics.Debug.WriteLine($"Failed to save settings: {ex.Message}");
}
}
[RelayCommand]
private void ResetToDefaults()
{
_isLoading = true; // Prevent multiple saves during reset
IsProxyEnabled = false;
ProxyIp = "127.0.0.1";
ProxyPort = 7890;
IsTranslationEnabled = false;
IsOneLineMode = false;
SkipExistingLyrics = true;
SelectedEncoding = "utf-8";
_isLoading = false;
SaveSettingsToFile(); // Save once after all resets
}
}
public partial class LyricsProviderSettingViewModel : ObservableObject
{
[ObservableProperty]
private string _name = string.Empty;
[ObservableProperty]
private int _priority;
[ObservableProperty]
private bool _isEnabled;
}
@@ -0,0 +1,28 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace ZonyLrcTools.Desktop.ViewModels;
/// <summary>
/// Base class for all ViewModels.
/// </summary>
public abstract partial class ViewModelBase : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsNotBusy))]
private bool _isBusy;
[ObservableProperty]
private string? _busyMessage;
public bool IsNotBusy => !IsBusy;
/// <summary>
/// Called when the ViewModel is activated (navigated to).
/// </summary>
public virtual Task OnActivatedAsync() => Task.CompletedTask;
/// <summary>
/// Called when the ViewModel is deactivated (navigated away).
/// </summary>
public virtual Task OnDeactivatedAsync() => Task.CompletedTask;
}