Skip to content

Commit

Permalink
Desktop lyric scrolling,theme
Browse files Browse the repository at this point in the history
  • Loading branch information
HeHang0 committed Nov 7, 2023
1 parent 995ff29 commit 6048dda
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
22 changes: 7 additions & 15 deletions windows/LyricWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,24 @@
ShowInTaskbar="False" Height="100" Width="500" MinWidth="360" MinHeight="100"
MouseEnter="Window_MouseEnter" MouseLeave="Window_MouseLeave"
Loaded="Window_Loaded" Closing="Window_Closing" MouseLeftButtonDown="DragWindow">
<!--<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0" ResizeBorderThickness="8" CornerRadius="8" GlassFrameThickness="-1" UseAeroCaptionButtons="True" />
</WindowChrome.WindowChrome>-->
<Window.Resources>
<Style TargetType="Label">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect ShadowDepth="0" BlurRadius="2"/>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Border x:Name="LyricBorder" CornerRadius="8" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel x:Name="IconPannel" Orientation="Horizontal" HorizontalAlignment="Center" Visibility="Collapsed">
<Label Name="LockLabel" Background="#01000000" Content="" Cursor="Hand" PreviewMouseLeftButtonDown="LockWindow" Tag="last" Width="40" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
<Label Name="LockLabel" Background="#01000000" Content="" Cursor="Hand" PreviewMouseLeftButtonDown="LockWindow" Tag="last" Width="40" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
<Label.Effect>
<DropShadowEffect Color="Transparent" ShadowDepth="0" BlurRadius="2"/>
</Label.Effect>
</Label>
<Label Content="" Cursor="Hand" PreviewMouseLeftButtonDown="MusicOperate" Tag="last" Width="40" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label Name="PlayOrPause" Content="" Cursor="Hand" PreviewMouseLeftButtonDown="MusicOperate" Tag="playOrPause" Width="40" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label Content="" Cursor="Hand" PreviewMouseLeftButtonDown="MusicOperate" Tag="next" Width="40" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label Content="" Cursor="Hand" PreviewMouseLeftButtonDown="CloseWindow" Width="40" FontSize="14" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label Content="" Cursor="Hand" PreviewMouseLeftButtonDown="CloseWindow" Width="40" FontSize="13" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
</StackPanel>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" PreviewMouseLeftButtonDown="DragWindow">
<ScrollViewer x:Name="LyricScroll" Grid.Row="1" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" PreviewMouseLeftButtonDown="DragWindow">
<Label Background="#01000000" FontSize="20" Name="Lyric" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="2"/>
Expand Down
53 changes: 48 additions & 5 deletions windows/LyricWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Threading;
using Rectangle = System.Windows.Shapes.Rectangle;

namespace Musiche
Expand All @@ -21,12 +22,15 @@ public partial class LyricWindow : Window
private bool locked = false;
private readonly DropShadowEffect lyricEffect;
private readonly WebSocketHandler webSocketHandler;
private readonly Brush hoverBackground = new SolidColorBrush(Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF));
private readonly Brush hoverBackgroundTransparent = new SolidColorBrush(Color.FromArgb(0x01, 0x00, 0x00, 0x00));
private static readonly Brush hoverBackgroundDark = new SolidColorBrush(Color.FromArgb(0x80, 0x00, 0x00, 0x00));
private static readonly Brush hoverBackgroundLight = new SolidColorBrush(Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF));
private static readonly Brush hoverBackgroundTransparent = new SolidColorBrush(Color.FromArgb(0x01, 0x00, 0x00, 0x00));
private Brush hoverBackground = hoverBackgroundLight;
private readonly FontFamily defaultFontfamily = new FontFamily("Microsoft YaHei UI");
private readonly string statusSavePath = Path.Combine(Utils.File.DataPath, "lyric.config.json");
private Utils.WindowResize windowResize = null;
public LyricWindow(WebSocketHandler webSocketHandler, string title)
private DispatcherTimer lyricTimer = null;
public LyricWindow(WebSocketHandler webSocketHandler, string title, bool dark)
{
this.webSocketHandler = webSocketHandler;
InitializeComponent();
Expand All @@ -36,6 +40,21 @@ public LyricWindow(WebSocketHandler webSocketHandler, string title)
defaultFontfamily = Lyric.FontFamily;
SetIcons();
SetStatus();
SetTheme(dark);
}

public void SetTheme(bool dark)
{
hoverBackground = dark ? hoverBackgroundDark : hoverBackgroundLight;
foreach (object item in IconPannel.Children)
{
if (item is Label label)
{
label.Foreground = dark ? Brushes.White : Brushes.Black;
}
}
if(LockLabel.Effect is DropShadowEffect effect)
effect.Color = dark ? Colors.Black : Colors.White;
}

private void SetStatus()
Expand Down Expand Up @@ -77,7 +96,6 @@ private void SetIcons()
if(item is Label label)
{
label.FontFamily = fontFamily;
label.FontWeight = FontWeights.Bold;
}
}
}
Expand Down Expand Up @@ -155,9 +173,33 @@ public void SetOptions(LyricOptions options)
}
}

internal void SetLine(string line)
private double scrollStep = 0;
public void SetLine(string line, double duration=0)
{
LyricScroll.ScrollToHorizontalOffset(0);
Lyric.Content = line;
lyricTimer?.Stop();
//更新行, 591.4000000000001, 350, 591.4000000000001
System.Diagnostics.Trace.WriteLine($"更新行, {LyricScroll.ExtentWidth}, {LyricScroll.ActualWidth}, {Lyric.ActualWidth}");
if (duration > 0 && duration < 60000 && LyricScroll.ExtentWidth > LyricScroll.ActualWidth)
{
scrollStep = (LyricScroll.ExtentWidth - LyricScroll.ActualWidth) / (duration / 33);
lyricTimer = new DispatcherTimer();
lyricTimer.Interval = TimeSpan.FromMilliseconds(33);
lyricTimer.Tick += TimerSetScroll;
lyricTimer.Start();
}
}

private void TimerSetScroll(object sender, EventArgs e)
{
if (scrollStep <= 0) return;
LyricScroll.ScrollToHorizontalOffset(LyricScroll.HorizontalOffset+scrollStep);
if(LyricScroll.ExtentWidth - LyricScroll.ActualWidth <=
LyricScroll.HorizontalOffset && sender is DispatcherTimer timer)
{
timer.Stop();
}
}

private void Window_MouseEnter(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -253,6 +295,7 @@ public void AudioPlayStateChanged(bool playing)
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

private const int GWL_EX_STYLE = -20;
private const int WS_EX_APPWINDOW = 0x00040000, WS_EX_TOOLWINDOW = 0x00000080;
}
Expand Down
12 changes: 7 additions & 5 deletions windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public partial class MainWindow : Window
Stream logStream = null;
Hotkey.Hotkey hotkey = null;
LyricWindow lyricWindow;
private bool dark = false;
public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -217,23 +218,24 @@ public void SetTitle(string title)
public void SetTheme(int preferredColorSchemeNumber)
{
var preferredColorScheme = CoreWebView2PreferredColorScheme.Auto;
var dark = ThemeListener.IsDarkMode;
dark = ThemeListener.IsDarkMode;
if (Enum.IsDefined(typeof(CoreWebView2PreferredColorScheme), preferredColorSchemeNumber))
{
preferredColorScheme = (CoreWebView2PreferredColorScheme)Enum.ToObject(typeof(CoreWebView2PreferredColorScheme), preferredColorSchemeNumber);
dark = preferredColorScheme == CoreWebView2PreferredColorScheme.Dark;
}
webview2.SetTheme(preferredColorScheme);
lyricWindow?.SetTheme(dark);
var windowHandle = new WindowInteropHelper(this).EnsureHandle();
if (windowHandle == null || windowHandle == IntPtr.Zero) return;
if (windowHandle == IntPtr.Zero) return;
Utils.Areo.Apply(windowHandle, dark);
}

public void SetLyric(LyricOptions options)
{
if(options.Show && lyricWindow == null)
{
lyricWindow = new LyricWindow(webSocketHandler, options.Title);
lyricWindow = new LyricWindow(webSocketHandler, options.Title, dark);
lyricWindow.Show();
lyricWindow.Closed += LyricWindow_Closed;
}
Expand All @@ -245,9 +247,9 @@ private void LyricWindow_Closed(object sender, EventArgs e)
lyricWindow = null;
}

internal void SetLyricLine(string line)
internal void SetLyricLine(string line, double duration=0)
{
lyricWindow?.SetLine(line);
lyricWindow?.SetLine(line, duration);
}
}
}
4 changes: 3 additions & 1 deletion windows/Server/HttpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ public async Task SetLyricLine(HttpListenerContext ctx)
string line = ctx.Request.DataAsString();
if (!string.IsNullOrWhiteSpace(line))
{
string durationString = ctx.Request.QueryString["duration"] ?? string.Empty;
double.TryParse(durationString, out double duration);
window.Dispatcher.Invoke(() =>
{
window.SetLyricLine(line);
window.SetLyricLine(line, duration);
});
}
await SendString(ctx, string.Empty);
Expand Down
5 changes: 1 addition & 4 deletions windows/Utils/IconFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ static IconFont()
//privateFonts.AddMemoryFont(ptrData, length);
//Marshal.FreeCoTaskMem(ptrData);
File.CreateDirectoryIFNotExists(Path.GetDirectoryName(IconFontPath));
if (!System.IO.File.Exists(IconFontPath))
{
System.IO.File.WriteAllBytes(IconFontPath, Properties.Resources.iconfont);
}
System.IO.File.WriteAllBytes(IconFontPath, Properties.Resources.iconfont);
}
}
}

0 comments on commit 6048dda

Please sign in to comment.