Skip to content

Commit

Permalink
Add: 歌词颜色根据背景转变
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Mar 9, 2024
1 parent 3aadc18 commit 81b0d56
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 164 deletions.
3 changes: 2 additions & 1 deletion NonsPlayer/Components/Models/LyricModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LyricParser.Abstraction;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using NonsPlayer.Core.Models;

namespace NonsPlayer.Components.Models;
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions NonsPlayer/Components/ViewModels/LyricItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
{
}



}
12 changes: 8 additions & 4 deletions NonsPlayer/Components/Views/LyricItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Grid>
<Grid Margin="{x:Bind ViewModel.Margin, Mode=OneWay}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Name="TextBlockPureLyric"
Text="{x:Bind ViewModel.PureLyric.CurrentLyric,Mode=OneWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Center"
FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="20" />
HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Left" Width="500"
TextWrapping="WrapWholeWords"
Foreground="#f0f0f0"
FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="30" />

<TextBlock Grid.Row="1" Name="TextBlockTranLyric" Text="{x:Bind ViewModel.TransLyric, Mode=OneWay}"
Visibility="{x:Bind ViewModel.TransVisibility, Mode=OneWay}"
HorizontalAlignment="Left" VerticalAlignment="Bottom" TextAlignment="Center"
TextWrapping="WrapWholeWords"
HorizontalAlignment="Left" VerticalAlignment="Bottom" TextAlignment="Left"
Foreground="#f0f0f0"
FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="18" />

</Grid>
Expand Down
8 changes: 8 additions & 0 deletions NonsPlayer/Components/Views/LyricItem.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,4 +41,9 @@ public Visibility TransVisibility
{
set => ViewModel.TransVisibility = value;
}

public Thickness Margin
{
set => ViewModel.Margin = value;
}
}
40 changes: 16 additions & 24 deletions NonsPlayer/Helpers/CacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,9 @@ public static async Task<BitmapImage> 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)))
Expand All @@ -183,28 +178,25 @@ public static async Task<BitmapImage> GetBitmapImageFromServer(string imageUrl)

public static async Task<IRandomAccessStream> 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;
}
}
}
3 changes: 1 addition & 2 deletions NonsPlayer/Helpers/UiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ public object Convert(object value, Type targetType, object parameter, string la
{
return null;
}
}

}
4 changes: 3 additions & 1 deletion NonsPlayer/NonsPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="CommunityToolkit.WinUI" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.0.240109" />
<PackageReference Include="CommunityToolkit.WinUI.Media" Version="8.0.240109" />
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="F23.StringSimilarity" Version="5.1.0" />
<PackageReference Include="ksemenenko.ColorThief" Version="1.1.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Windows.CsWin32" PrivateAssets="all" Version="0.3.18-beta">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down Expand Up @@ -124,7 +126,7 @@
<Content Include="Assets\SplashScreen.scale-400.png" />
<Content Include="Assets\UnknowResource.png" />
<Content Include="Assets\WindowIcon.ico" />
<Content Include="Assets\NonsPlayer-Icon-Miaoyww.svg" >
<Content Include="Assets\NonsPlayer-Icon-Miaoyww.svg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="Assets\SplashScreen.scale-100.png" />
Expand Down
6 changes: 5 additions & 1 deletion NonsPlayer/ViewModels/LyricViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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)
});
}

Expand Down
Loading

0 comments on commit 81b0d56

Please sign in to comment.