From 81b0d5686dc801ea65c959e93890b762495c9077 Mon Sep 17 00:00:00 2001 From: Miaoyww Date: Sun, 10 Mar 2024 00:58:42 +0800 Subject: [PATCH] =?UTF-8?q?Add:=20=E6=AD=8C=E8=AF=8D=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=83=8C=E6=99=AF=E8=BD=AC=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NonsPlayer/Components/Models/LyricModel.cs | 3 +- .../ViewModels/LyricItemViewModel.cs | 8 +- NonsPlayer/Components/Views/LyricItem.xaml | 12 +- NonsPlayer/Components/Views/LyricItem.xaml.cs | 8 + NonsPlayer/Helpers/CacheHelper.cs | 40 +-- NonsPlayer/Helpers/UiHelper.cs | 3 +- NonsPlayer/NonsPlayer.csproj | 4 +- NonsPlayer/ViewModels/LyricViewModel.cs | 6 +- NonsPlayer/Views/Pages/LyricPage.xaml | 291 ++++++++++-------- NonsPlayer/Views/Pages/LyricPage.xaml.cs | 86 +++++- 10 files changed, 297 insertions(+), 164 deletions(-) diff --git a/NonsPlayer/Components/Models/LyricModel.cs b/NonsPlayer/Components/Models/LyricModel.cs index cc460c9..85fbe83 100644 --- a/NonsPlayer/Components/Models/LyricModel.cs +++ b/NonsPlayer/Components/Models/LyricModel.cs @@ -1,5 +1,6 @@ using LyricParser.Abstraction; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Media; using NonsPlayer.Core.Models; namespace NonsPlayer.Components.Models; @@ -17,7 +18,7 @@ public class LyricModel public ILyricLine LyricLine; public string Translation; - + public Thickness Margin; public bool HaveTranslation => !string.IsNullOrEmpty(Translation); public Type LyricType => LyricLine.GetType(); public Visibility TransVisibility; diff --git a/NonsPlayer/Components/ViewModels/LyricItemViewModel.cs b/NonsPlayer/Components/ViewModels/LyricItemViewModel.cs index ac0a18f..72b6959 100644 --- a/NonsPlayer/Components/ViewModels/LyricItemViewModel.cs +++ b/NonsPlayer/Components/ViewModels/LyricItemViewModel.cs @@ -1,8 +1,10 @@ -using CommunityToolkit.Mvvm.ComponentModel; +using System.Drawing; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LyricParser.Abstraction; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Media; using NonsPlayer.Core.Helpers; using NonsPlayer.Core.Nons.Player; using NonsPlayer.Core.Services; @@ -17,9 +19,9 @@ public partial class LyricItemViewModel : ObservableObject [ObservableProperty] private Visibility transVisibility = Visibility.Visible; [ObservableProperty] private ILyricLine? pureLyric; [ObservableProperty] private string? transLyric; + [ObservableProperty] private Thickness margin = new Thickness(0); public LyricItemViewModel() { } - - + } \ No newline at end of file diff --git a/NonsPlayer/Components/Views/LyricItem.xaml b/NonsPlayer/Components/Views/LyricItem.xaml index d76d476..98ce124 100644 --- a/NonsPlayer/Components/Views/LyricItem.xaml +++ b/NonsPlayer/Components/Views/LyricItem.xaml @@ -5,19 +5,23 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> - + + HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Left" Width="500" + TextWrapping="WrapWholeWords" + Foreground="#f0f0f0" + FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="30" /> diff --git a/NonsPlayer/Components/Views/LyricItem.xaml.cs b/NonsPlayer/Components/Views/LyricItem.xaml.cs index f46661f..6ff5fa4 100644 --- a/NonsPlayer/Components/Views/LyricItem.xaml.cs +++ b/NonsPlayer/Components/Views/LyricItem.xaml.cs @@ -1,10 +1,13 @@ using System.Diagnostics; +using System.Drawing; using System.Windows.Forms; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using LyricParser.Abstraction; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; using NonsPlayer.Components.ViewModels; using NonsPlayer.Core.Nons.Player; using NonsPlayer.Core.Services; @@ -38,4 +41,9 @@ public Visibility TransVisibility { set => ViewModel.TransVisibility = value; } + + public Thickness Margin + { + set => ViewModel.Margin = value; + } } \ No newline at end of file diff --git a/NonsPlayer/Helpers/CacheHelper.cs b/NonsPlayer/Helpers/CacheHelper.cs index 609a4d2..8935b9a 100644 --- a/NonsPlayer/Helpers/CacheHelper.cs +++ b/NonsPlayer/Helpers/CacheHelper.cs @@ -151,14 +151,9 @@ public static async Task GetBitmapImageFromServer(string imageUrl) { try { - // 发起 HTTP 请求并获取响应数据 var response = await httpClient.GetAsync(imageUrl); response.EnsureSuccessStatusCode(); - - // 从响应数据中获取图像的字节流 var imageBytes = await response.Content.ReadAsByteArrayAsync(); - - // 创建 InMemoryRandomAccessStream 并将图像数据写入其中 using (var stream = new InMemoryRandomAccessStream()) { using (var writer = new DataWriter(stream.GetOutputStreamAt(0))) @@ -183,28 +178,25 @@ public static async Task GetBitmapImageFromServer(string imageUrl) public static async Task GetImageStreamFromServer(string imageUrl) { - using (var httpClient = new HttpClient()) + using var httpClient = new HttpClient(); + try { - try - { - var response = await httpClient.GetAsync(imageUrl); - response.EnsureSuccessStatusCode(); - var imageBytes = await response.Content.ReadAsByteArrayAsync(); - var stream = new InMemoryRandomAccessStream(); - using (var writer = new DataWriter(stream.GetOutputStreamAt(0))) - { - writer.WriteBytes(imageBytes); - await writer.StoreAsync(); - } - - return stream; - } - catch (Exception ex) + var response = await httpClient.GetAsync(imageUrl); + response.EnsureSuccessStatusCode(); + var imageBytes = await response.Content.ReadAsByteArrayAsync(); + var stream = new InMemoryRandomAccessStream(); + using (var writer = new DataWriter(stream.GetOutputStreamAt(0))) { - // 处理异常 - Console.WriteLine($"Error: {ex.Message}"); - return null; + writer.WriteBytes(imageBytes); + await writer.StoreAsync(); } + return stream; + } + catch (Exception ex) + { + // 处理异常 + Console.WriteLine($"Error: {ex.Message}"); + return null; } } } \ No newline at end of file diff --git a/NonsPlayer/Helpers/UiHelper.cs b/NonsPlayer/Helpers/UiHelper.cs index f509009..ab32ae1 100644 --- a/NonsPlayer/Helpers/UiHelper.cs +++ b/NonsPlayer/Helpers/UiHelper.cs @@ -143,5 +143,4 @@ public object Convert(object value, Type targetType, object parameter, string la { return null; } -} - +} \ No newline at end of file diff --git a/NonsPlayer/NonsPlayer.csproj b/NonsPlayer/NonsPlayer.csproj index fa083ed..44c7251 100644 --- a/NonsPlayer/NonsPlayer.csproj +++ b/NonsPlayer/NonsPlayer.csproj @@ -29,8 +29,10 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -124,7 +126,7 @@ - + Always diff --git a/NonsPlayer/ViewModels/LyricViewModel.cs b/NonsPlayer/ViewModels/LyricViewModel.cs index 3e40baa..07165ad 100644 --- a/NonsPlayer/ViewModels/LyricViewModel.cs +++ b/NonsPlayer/ViewModels/LyricViewModel.cs @@ -3,11 +3,13 @@ using CommunityToolkit.Mvvm.Input; using LyricParser.Abstraction; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Media; using NonsPlayer.Components.Models; using NonsPlayer.Core.Models; using NonsPlayer.Core.Nons.Player; using NonsPlayer.Helpers; using NonsPlayer.Services; +using NonsPlayer.Views.Pages; namespace NonsPlayer.ViewModels; @@ -50,6 +52,7 @@ private void OnMusicChanged(Music music) { CurrentMusic = music; LyricItems.Clear(); + for (int i = 0; i < music.Lyrics.Lyrics.Lines.Count; i++) { var visibility = Visibility.Visible; @@ -65,7 +68,8 @@ private void OnMusicChanged(Music music) Translation = visibility == Visibility.Visible ? music.Lyrics.TransLyrics?.Lines[i].CurrentLyric : string.Empty, - TransVisibility = visibility + TransVisibility = visibility, + Margin = i == 0 ? new Thickness(0,40,0,0) : new Thickness(0) }); } diff --git a/NonsPlayer/Views/Pages/LyricPage.xaml b/NonsPlayer/Views/Pages/LyricPage.xaml index b97195e..e8ba2e1 100644 --- a/NonsPlayer/Views/Pages/LyricPage.xaml +++ b/NonsPlayer/Views/Pages/LyricPage.xaml @@ -7,6 +7,9 @@ xmlns:models="using:NonsPlayer.Components.Models" xmlns:views="using:NonsPlayer.Components.Views" xmlns:converters="using:NonsPlayer.Converters" + xmlns:media="using:CommunityToolkit.WinUI.Media" + xmlns:animations="using:CommunityToolkit.WinUI.Animations" + Loaded="LyricPage_OnLoaded" mc:Ignorable="d"> @@ -17,146 +20,180 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + - + + + + + - - + + + + + + + + + - - - - + - - - - - + + + + + + + - - - - - - - + \ No newline at end of file diff --git a/NonsPlayer/Views/Pages/LyricPage.xaml.cs b/NonsPlayer/Views/Pages/LyricPage.xaml.cs index 4403a54..40139c2 100644 --- a/NonsPlayer/Views/Pages/LyricPage.xaml.cs +++ b/NonsPlayer/Views/Pages/LyricPage.xaml.cs @@ -1,25 +1,105 @@ -using Microsoft.UI.Xaml.Controls; +using System.Drawing; +using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; +using NonsPlayer.Core.Models; using NonsPlayer.Core.Nons.Player; using NonsPlayer.Services; using NonsPlayer.ViewModels; +using static NonsPlayer.Core.Services.ControlFactory; +using System.Text; +using Windows.Graphics.Imaging; +using ColorThiefDotNet; +using Color = Windows.UI.Color; +using NonsPlayer.Helpers; +using Microsoft.UI.Xaml; +using Windows.Storage.Streams; +using Buffer = Windows.Storage.Streams.Buffer; +using System.Runtime.InteropServices.WindowsRuntime; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.WinUI.Animations; +using Microsoft.UI.Xaml.Media; +using CommunityToolkit.WinUI.Media; namespace NonsPlayer.Views.Pages; +[INotifyPropertyChanged] public sealed partial class LyricPage : Page { + private Guid GetPictureCodecFromBuffer(Buffer buffer) + { + if (buffer.Length < 10) throw new ArgumentOutOfRangeException(); + var byteArray = buffer.ToArray(); + if (byteArray[0] == 0x89 && byteArray[1] == 0x50 && byteArray[2] == 0x4e && + byteArray[3] == 0x47) + { + // PNG + return BitmapDecoder.PngDecoderId; + } + + if (byteArray[6] == 0x4a && byteArray[7] == 0x46 && byteArray[8] == 0x49 && + byteArray[9] == 0x46) + { + // JPEG + return BitmapDecoder.JpegDecoderId; + } + + if (byteArray[0] == 0x52 && byteArray[1] == 0x49 && byteArray[2] == 0x46 && + byteArray[3] == 0x46 && byteArray[8] == 0x57) + { + // WEBP + return BitmapDecoder.WebpDecoderId; + } + + throw new ArgumentOutOfRangeException(); + } + public LyricPage() { ViewModel = App.GetService(); InitializeComponent(); Player.Instance.PositionChangedHandle += OnPositionChanged; + Player.Instance.MusicChangedHandle += OnMusicChanged; } + private ColorThief colorThief = new(); private ManipulationStartedRoutedEventArgs? _slidingEventArgs = null; private TimeSpan StartingTimeSpan = TimeSpan.Zero; private bool _isSliding = false; + public static Color AlbumColor; public LyricViewModel ViewModel { get; } + [ObservableProperty] private SolidColorBrush foregroundAccentTextBrush = + Application.Current.Resources["SystemControlPageTextBaseHighBrush"] as SolidColorBrush; + + [ObservableProperty] private SolidColorBrush foregroundIdleTextBrush = + Application.Current.Resources["TextFillColorTertiaryBrush"] as SolidColorBrush; + + + public async void OnMusicChanged(Music value) + { + // var stream = await CacheHelper.GetImageStreamFromServer(value.Album.SmallAvatarUrl); + // var image = new Bitmap(stream.AsStream()); + // var color = colorThief.GetColor(image, ignoreWhite: false); + // AlbumColor = Color.FromArgb(color.Color.A, color.Color.R, color.Color.G, color.Color.B); + var imageBrush = await CacheHelper.GetImageBrushAsync(value.Album.CacheMiddleAvatarId, value.Album.AvatarUrl); + imageBrush.Stretch = Stretch.UniformToFill; + AcrylicCover.Fill = imageBrush; + // double brightness = (0.299 * color.Color.A + 0.587 * color.Color.G + 0.114 * color.Color.B) / 255; + ForegroundAccentTextBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 240, 240, 240)); + // if (brightness < 0.68) + // { + // ForegroundAccentTextBrush = + // new SolidColorBrush(Windows.UI.Color.FromArgb(255, 255, 255, 255)); + // ForegroundIdleTextBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(66, 255, 255, 255)); + // // 亮度低,使用浅色主题 + // } + // else + // { + // ForegroundAccentTextBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0)); + // ForegroundIdleTextBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(114, 0, 0, 0)); + // } + } + public void OnPositionChanged(TimeSpan position) { if (Player.Instance.CurrentMusic.IsEmpty) return; @@ -57,4 +137,8 @@ private void CurrentTimeSlider_OnManipulationStarted(object sender, Manipulation _isSliding = true; _slidingEventArgs = e; } + + private void LyricPage_OnLoaded(object sender, RoutedEventArgs e) + { + } } \ No newline at end of file