Skip to content

Commit

Permalink
歌词基础显示功能 #66
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Oct 19, 2024
1 parent 632ccbb commit d37d284
Show file tree
Hide file tree
Showing 26 changed files with 874 additions and 373 deletions.
2 changes: 1 addition & 1 deletion NonsPlayer.Core/AMLL/Models/LyricLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LyricLine
public string? Pure { get; set; }
public string? Translation { get; set; }

public bool HaveTranslation => !string.IsNullOrEmpty(Translation);
public bool HasTranslation => !string.IsNullOrEmpty(Translation);

/// <summary>
/// 是否为纯音乐
Expand Down
32 changes: 23 additions & 9 deletions NonsPlayer/AMLL/Components/ViewModels/LyricCardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using NonsPlayer.AMLL.Components.Views;
using NonsPlayer.AMLL.Helpers;
using NonsPlayer.AMLL.Models;
using NonsPlayer.Core.AMLL.Models;
using NonsPlayer.Helpers;
using Windows.UI;

namespace NonsPlayer.AMLL.Components.ViewModels;

Expand All @@ -18,23 +22,33 @@ public sealed partial class LyricCardViewModel : ObservableObject

[ObservableProperty] public Visibility transVisibility;

public LyricCardViewModel()
{
Foreground = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240));
LyricHelper.Instance.LyricChanged += OnLyricChanged;
}

partial void OnLyricModelChanged(LyricItemModel value)
{
TransVisibility = value.Lyric.HaveTranslation ? Visibility.Visible : Visibility.Collapsed;
TransVisibility = value.Lyric.HasTranslation ? Visibility.Visible : Visibility.Collapsed;
}

private void OnLyricChanged(int i)
private void OnLyricChanged(LyricLine lyric)
{
try
{
if (i - 1 == Index)
{
Foreground = new SolidColorBrush(Colors.White);
}
else
if (LyricModel == null) return;
ServiceHelper.DispatcherQueue.TryEnqueue(() =>
{
Foreground = Application.Current.Resources["TextFillColorTertiaryBrush"] as SolidColorBrush;
}
if (lyric.Equals(LyricModel.Lyric))
{
Foreground = new SolidColorBrush(Colors.White);
}
else
{
Foreground = Application.Current.Resources["TextFillColorTertiaryBrush"] as SolidColorBrush;
}
});
}
catch
{
Expand Down
11 changes: 4 additions & 7 deletions NonsPlayer/AMLL/Components/Views/LyricCard.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource SolidBackgroundFillColorBaseBrush}"
xmlns:media="using:CommunityToolkit.WinUI.Media"
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
xmlns:models="using:NonsPlayer.AMLL.Models"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
Expand All @@ -17,17 +14,17 @@
<TextBlock Grid.Row="0" Name="TextBlockPureLyric"
Text="{x:Bind ViewModel.LyricModel.Lyric.Pure,Mode=OneWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Left" Width="500"
TextWrapping="WrapWholeWords"
Style="{StaticResource CommonTextStyle}"
Foreground="{x:Bind ViewModel.Foreground, Mode=OneWay}"
FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="30" />
FontWeight="Bold" FontSize="40" />

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

</Grid>
</UserControl>
21 changes: 21 additions & 0 deletions NonsPlayer/AMLL/Helpers/LyricHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NonsPlayer.AMLL.Models;
using NonsPlayer.Core.AMLL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static NonsPlayer.Core.Nons.Player.Player;

namespace NonsPlayer.AMLL.Helpers;

public class LyricHelper
{
public static LyricHelper Instance { get; } = new();

public int LyricPosition;

public delegate void LyricChangedHandler(LyricLine time);

public LyricChangedHandler? LyricChanged;
}
2 changes: 1 addition & 1 deletion NonsPlayer/AMLL/ViewModels/AMLLViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public partial class AMLLViewModel : ObservableRecipient
{
public ObservableCollection<LyricCombiner> LyricItems = new();
[ObservableProperty] private IMusic currentMusic;
public int LyricPosition;

#region 命令

Expand All @@ -39,6 +38,7 @@ public AMLLViewModel()
private async void OnMusicChanged(IMusic value)
{
LyricItems.Clear();

if (value.Lyric == null)
{
LyricItems.Add(new LyricCombiner
Expand Down
18 changes: 0 additions & 18 deletions NonsPlayer/AMLL/Views/AMLL.xaml.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<UserControl
x:Class="NonsPlayer.AMLL.Views.AMLL"
x:Class="NonsPlayer.AMLL.Views.AMLLCard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand All @@ -10,20 +10,18 @@
xmlns:models="using:NonsPlayer.AMLL.Models"
xmlns:views="using:NonsPlayer.AMLL.Components.Views"
mc:Ignorable="d">
<Grid>
<Grid x:Name="MainGrid">
<ScrollViewer x:Name="LyricBoxContainer"
VerticalScrollMode="Enabled"
VerticalScrollBarVisibility="Hidden"
HorizontalScrollMode="Disabled">
<Grid x:Name="LyricHost">
<ItemsRepeater x:Name="LyricBox"
ItemsSource="{x:Bind ViewModel.LyricItems, Mode=OneWay}">
<ItemsRepeater.Layout>
<StackLayout Orientation="Vertical" Spacing="40" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<ListView x:Name="LyricBox"
ItemsSource="{x:Bind ViewModel.LyricItems, Mode=OneWay}"
SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:LyricCombiner">
<Border>
<Border Margin="0,40,0,10">
<views:LyricCard
Width="Auto"
Height="Auto"
Expand All @@ -34,8 +32,8 @@
Index="{x:Bind Index}" />
</Border>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</ListView.ItemTemplate>
</ListView>
</Grid>

</ScrollViewer>
Expand Down
114 changes: 114 additions & 0 deletions NonsPlayer/AMLL/Views/AMLLCard.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using NonsPlayer.AMLL.Helpers;
using NonsPlayer.AMLL.ViewModels;
using NonsPlayer.Core.Contracts.Models.Music;
using NonsPlayer.Core.Nons.Player;
using NonsPlayer.Helpers;

namespace NonsPlayer.AMLL.Views;

public sealed partial class AMLLCard : UserControl
{
public AMLLViewModel ViewModel
{
get;
}

public AMLLCard()
{
ViewModel = App.GetService<AMLLViewModel>();
InitializeComponent();
Player.Instance.PositionChanged += OnPositionChanged;
Player.Instance.MusicChanged += MusicChanged;
}

private void MusicChanged(IMusic currentmusic)
{
LyricHelper.Instance.LyricPosition = 0;
}

private void OnPositionChanged(TimeSpan time)
{
// 判断是否需要滚动歌词
if (ViewModel.LyricItems.Count == 0) return;

var lyric = ViewModel.LyricItems[LyricHelper.Instance.LyricPosition];
// 控制ListView滚动
if (lyric.LyricItemModel.Lyric.StartTime <= time)
{
if (LyricHelper.Instance.LyricPosition < ViewModel.LyricItems.Count - 1)
{
LyricHelper.Instance.LyricPosition++;
LyricHelper.Instance.LyricChanged.Invoke(lyric.LyricItemModel.Lyric);
DispatcherQueue.TryEnqueue(() =>
{
ScrollLyric();
});
}
}
}

private void ScrollLyric()
{
try
{
try
{
if (LyricHelper.Instance.LyricPosition == -1)
{
LyricBoxContainer.ChangeView(null, 0, null, false);
return;
}

var item = LyricBox.Items[LyricHelper.Instance.LyricPosition];
DispatcherQueue.TryEnqueue(() =>
{
LyricBox.ScrollIntoView(item);
});
}
catch (Exception ex)
{
// Log the error if needed
}

// var item = ViewModel.LyricItems[ViewModel.LyricPosition];
// var k = LyricBox.ItemsSourceView.IndexOf(item);
// UIElement actualElement;
// bool isNewLoaded = false;
// if (LyricBox.TryGetElement(k) is { } ele)
// {
// actualElement = ele;
// }
// else
// {
// actualElement = LyricBox.GetOrCreateElement(k) as Border;
// isNewLoaded = true;
// }
//
// if (actualElement != null && item != null &&
// !string.IsNullOrEmpty(item.LyricItemModel.Lyric.Pure))
// {
// actualElement.UpdateLayout();
// actualElement.StartBringIntoView();
//
// if (!isNewLoaded)
// {
// var transform = actualElement?.TransformToVisual((UIElement)LyricBoxContainer.ContentTemplateRoot);
// var position = transform?.TransformPoint(new Windows.Foundation.Point(0, 0));
// LyricBoxContainer.ChangeView(0,
// (position?.Y + LyricHost.Margin.Top - MainGrid.ActualHeight / 4) - 200, 1,
// false);
// }
// else
// {
// // actualElement.StartBringIntoView(NoAnimationBringIntoViewOptions);
// }
// }
}
catch
{
// igrone pls
}
}
}
Loading

0 comments on commit d37d284

Please sign in to comment.