Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
完善直播消息滚动机制 (#223)
Browse files Browse the repository at this point in the history
* 完善直播消息滚动机制

* 减少渲染延迟时间
  • Loading branch information
Richasy authored Sep 21, 2021
1 parent 57ef85c commit e395a9c
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@
<Grid
x:Name="Danmaku_Command_Border"
Grid.Row="1"
Padding="4"
Padding="4,4,8,4"
BorderBrush="{ThemeResource ControlStrokeColorDefaultBrush}"
BorderThickness="0,1,0,0">
<local:DanmakuBox />
Expand Down
1 change: 1 addition & 0 deletions src/App/Controls/Player/Related/LiveMessageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<ScrollViewer
x:Name="ScrollViewer"
HorizontalScrollMode="Disabled"
ViewChanged="OnViewChanged"
VerticalScrollBarVisibility="Auto">
<Grid>
<Grid x:Name="StandardContainer">
Expand Down
18 changes: 15 additions & 3 deletions src/App/Controls/Player/Related/LiveMessageView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Richasy. All rights reserved.

using System;
using System.Threading.Tasks;

namespace Richasy.Bili.App.Controls.Player.Related
{
Expand All @@ -15,12 +16,23 @@ public sealed partial class LiveMessageView : PlayerComponent
public LiveMessageView()
{
this.InitializeComponent();
ViewModel.RequestLiveMessageScrollToBottom += OnRequestLiveMessageScrollToBottom;
ViewModel.RequestLiveMessageScrollToBottom += OnRequestLiveMessageScrollToBottomAsync;
}

private void OnRequestLiveMessageScrollToBottom(object sender, EventArgs e)
private async void OnRequestLiveMessageScrollToBottomAsync(object sender, EventArgs e)
{
ScrollViewer.ChangeView(0, (ScrollViewer.ExtentHeight + ScrollViewer.ScrollableHeight) * 2, 1);
await Task.Delay(50);
ScrollViewer.ChangeView(0, double.MaxValue, 1);
}

private void OnViewChanged(object sender, Windows.UI.Xaml.Controls.ScrollViewerViewChangedEventArgs e)
{
if (!e.IsIntermediate)
{
// 这里的逻辑是,如果滚动到了底部,则表示允许视图自动滚动,
// 如果不在底部,表示用户自己滚动了视图,此时则不再自动滚动.
ViewModel.IsLiveMessageAutoScroll = ScrollViewer.VerticalOffset + ScrollViewer.ViewportHeight >= ScrollViewer.ExtentHeight - 50;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Richasy. All rights reserved.

using System;
using System.Collections.ObjectModel;
using System.Linq;
using Richasy.Bili.Models.App.Args;
using Richasy.Bili.Models.BiliBili;
using Richasy.Bili.Models.Enums;
Expand All @@ -25,6 +27,11 @@ await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
var data = e.Data as LiveDanmakuMessage;
LiveDanmakuCollection.Add(data);
NewLiveDanmakuAdded?.Invoke(this, data);
if (LiveDanmakuCollection.Count > 1000)
{
var saveMessages = LiveDanmakuCollection.ToList().Skip(600);
LiveDanmakuCollection = new ObservableCollection<LiveDanmakuMessage>(saveMessages);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private void Reset()
IsPlayInformationError = false;
IsCurrentEpisodeInPgcSection = false;
IsShowEmptyLiveMessage = true;
IsLiveMessageAutoScroll = true;
CurrentPlayLine = null;
CurrentLiveQuality = null;
_audioList.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,12 @@ public partial class PlayerViewModel
[Reactive]
public string LivePartition { get; set; }

/// <summary>
/// 直播消息是否自动滚动.
/// </summary>
[Reactive]
public bool IsLiveMessageAutoScroll { get; set; }

private BiliController Controller { get; } = BiliController.Instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private void OnLiveDanmakuCollectionChanged(object sender, NotifyCollectionChang
var count = LiveDanmakuCollection.Count;
IsShowEmptyLiveMessage = count == 0;

if (count > 0)
if (count > 0 && IsLiveMessageAutoScroll)
{
RequestLiveMessageScrollToBottom?.Invoke(this, EventArgs.Empty);
}
Expand Down

0 comments on commit e395a9c

Please sign in to comment.