feat(desktop): 歌词页支持三种来源,加网易云扫码登录

歌词页原先只能扫本地目录。CLI 有三种来源 —— 本地、CSV、网易云歌单 —— 而
网易云那条路在界面上完全走不通。

现在页面有一个来源下拉,以及三行互斥的输入:

  本地文件夹 —— 沿用目录选择器
  CSV 文件   —— 选一个"歌名,歌手"两列的 .csv
  网易云     —— 粘贴一个或多个歌单 ID,用分号分隔

因为网易云那条路需要已登录的会话,这个提交同时加上二维码登录对话框:

- QrCodeLoginViewModel 用 QRCoder 把二维码内容渲染成真正的 PNG(CLI 是往日志里
  画 ASCII 艺术,在窗口里没法用),下方显示登录状态:等待扫码、已扫码待确认、
  成功。
- 关闭对话框即取消登录,而这次能真的停下来,因为扫描器已经接受
  CancellationToken(上一个提交)。
- cookie 持久化到 netease.session.json,重启不必重新扫码。页面显示当前是否
  已登录:未登录给"登录…"按钮,已登录给"退出登录",退出会同时清掉内存里的
  cookie 和文件,下次解析歌单重新扫码。
- 非本地来源还需要指定 .lrc 的落盘目录,因此多一行输出目录,默认
  <音乐>/DownloadedLrc。

扫描与下载拆成两步:"解析"把来源返回的歌曲填进表格,"开始下载"才写文件。
进度、逐行状态与取消按钮对三种来源一视同仁。
This commit is contained in:
aL1CesT committed 2026-11-09 04:27:44 +08:00
1 parent c614315601
commit c303705ab6
6 files changed
+880 -91

No files matched your search

@@ -0,0 +1,134 @@
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using QRCoder;
using ZonyLrcTools.Common.MusicScanner;
using ZonyLrcTools.Desktop.Infrastructure.Localization;
namespace ZonyLrcTools.Desktop.ViewModels;
/// <summary>
/// Drives the NetEase QR-code login dialog.
/// </summary>
/// <remarks>
/// Two events cross the dialog boundary in opposite directions: <see cref="CloseRequested"/>
/// lets the login flow close the window once it is done, and <see cref="Cancelled"/> fires when
/// the user closed the window first, so the caller can stop polling instead of waiting forever.
/// </remarks>
public partial class QrCodeLoginViewModel : ObservableObject
{
private readonly IUILocalizationService? _localization;
[ObservableProperty]
private Bitmap? _qrCodeImage;
[ObservableProperty]
private string _statusText = string.Empty;
[ObservableProperty]
private bool _hasImage;
/// <summary>
/// Raised when the login flow wants the dialog closed.
/// </summary>
public event EventHandler? CloseRequested;
/// <summary>
/// Raised when the user closed the dialog before logging in.
/// </summary>
public event EventHandler? Cancelled;
/// <summary>
/// True once the login succeeded; the dialog stays closable but no longer counts as a cancel.
/// </summary>
public bool IsCompleted { get; private set; }
public string LoginTitle => _localization?["NetEase_LoginTitle"] ?? "Sign in to NetEase Cloud Music";
public string LoginTip => _localization?["NetEase_LoginTip"] ?? "Scan the QR code with the NetEase Cloud Music app on your phone.";
public string CancelText => _localization?["Common_Cancel"] ?? "Cancel";
public QrCodeLoginViewModel(IUILocalizationService? localization = null)
{
_localization = localization;
_statusText = WaitText;
}
private string WaitText => _localization?["NetEase_LoginWaiting"] ?? "Waiting for you to scan...";
private string ScannedText => _localization?["NetEase_LoginScanned"] ?? "Scanned, please confirm on your phone.";
private string SuccessText => _localization?["NetEase_LoginSucceeded"] ?? "Signed in.";
/// <summary>
/// Render the QR payload the scanner produced.
/// </summary>
public void SetPayload(NetEaseQrCodePayload payload)
{
QrCodeImage = RenderQrCode(payload.Content);
HasImage = QrCodeImage != null;
StatusText = WaitText;
}
/// <summary>
/// Reflect a login-state change reported by the scanner.
/// </summary>
public void SetState(NetEaseLoginState state)
{
StatusText = state switch
{
NetEaseLoginState.ScanConfirmed => ScannedText,
NetEaseLoginState.Succeeded => SuccessText,
_ => WaitText
};
}
/// <summary>
/// Push an error message into the dialog.
/// </summary>
public void SetError(string message)
{
StatusText = message;
}
/// <summary>
/// Mark the flow as finished and close the dialog.
/// </summary>
public void Complete()
{
IsCompleted = true;
RequestClose();
}
/// <summary>
/// Ask the dialog to close.
/// </summary>
public void RequestClose()
{
CloseRequested?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Called by the dialog when its window closed, for whatever reason.
/// </summary>
public void NotifyClosed()
{
if (!IsCompleted)
{
Cancelled?.Invoke(this, EventArgs.Empty);
}
}
private static Bitmap? RenderQrCode(string content)
{
try
{
using var generator = new QRCodeGenerator();
using var data = generator.CreateQrCode(content, QRCodeGenerator.ECCLevel.M);
var pngBytes = new PngByteQRCode(data).GetGraphic(12);
return new Bitmap(new MemoryStream(pngBytes));
}
catch (Exception)
{
// A render failure shows up as the placeholder, not as a crash.
return null;
}
}
}