Skip to content

Commit

Permalink
支持长按左键倍速播放 (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy authored Oct 13, 2024
1 parent 6495406 commit 2e77019
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public sealed partial class BiliPlayer
private StackPanel _notificationContainer;
private LoadingWidget _loadingWidget;
private EmptyHolder _failedHolder;
private GestureRecognizer _gestureRecognizer;

/// <summary>
/// 注入弹幕控件创建函数.
Expand All @@ -57,6 +58,10 @@ public void InjectSubtitleControlFunc(Func<SubtitlePresenter> createSubtitleCont
private void CreateOverlayContainer()
{
var rootGrid = new CursorGrid();
_gestureRecognizer = new GestureRecognizer
{
GestureSettings = GestureSettings.HoldWithMouse | GestureSettings.Hold,
};

if (!ViewModel.IsExternalPlayer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ private void RestoreCursor()
}
}

private void OnInteractionControlContextRequested(UIElement sender, ContextRequestedEventArgs args)
{
if (_isHolding)
{
args.Handled = true;
sender.ContextFlyout?.Hide();
}
}

private void OnInteractionControlManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
_manipulationVolume = 0;
Expand Down Expand Up @@ -224,10 +233,14 @@ private void OnInteractionControlManipulationStarted(object sender, Manipulation
}

private void OnRootPointerCanceled(object sender, PointerRoutedEventArgs e)
=> HandlePointerEvent(e, true);
{
_gestureRecognizer.ProcessUpEvent(e.GetCurrentPoint((UIElement)sender));
HandlePointerEvent(e, true);
}

private void OnRootPointerPressed(object sender, PointerRoutedEventArgs e)
{
_gestureRecognizer.ProcessDownEvent(e.GetCurrentPoint((UIElement)sender));
if (ViewModel is IslandPlayerViewModel)
{
var point = e.GetCurrentPoint((UIElement)sender);
Expand Down Expand Up @@ -264,5 +277,11 @@ private void OnRootPointerEntered(object sender, PointerRoutedEventArgs e)
=> HandlePointerEvent(e);

private void OnRootPointerMoved(object sender, PointerRoutedEventArgs e)
=> HandlePointerEvent(e);
{
_gestureRecognizer.ProcessMoveEvents(e.GetIntermediatePoints((UIElement)sender));
HandlePointerEvent(e);
}

private void OnRootPointerReleased(object sender, PointerRoutedEventArgs e)
=> _gestureRecognizer.ProcessUpEvent(e.GetCurrentPoint((UIElement)sender));
}
46 changes: 29 additions & 17 deletions src/Desktop/BiliCopilot.UI/Controls/Core/Player/BiliPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed partial class BiliPlayer : PlayerControlBase
private Rect _transportControlTriggerRect;
private DispatcherTimer? _cursorTimer;

private bool _isHolding;
private double _lastSpeed;
private double _cursorStayTime;
private double _mtcStayTime;
Expand Down Expand Up @@ -127,6 +128,23 @@ private void HookRootPointerEvents()
_overlayContainer.PointerExited += OnRootPointerExited;
_overlayContainer.PointerPressed += OnRootPointerPressed;
_overlayContainer.PointerCanceled += OnRootPointerCanceled;
_overlayContainer.PointerReleased += OnRootPointerReleased;
}

private void OnGestureRecognizerHolding(GestureRecognizer sender, HoldingEventArgs args)
{
_isHolding = true;
if (args.HoldingState == HoldingState.Started)
{
_lastSpeed = ViewModel.Speed;
ViewModel.SetSpeedCommand.Execute(3d);
}
else
{
_lastSpeed = _lastSpeed == 0 ? 1.0 : _lastSpeed;
ViewModel.SetSpeedCommand.Execute(_lastSpeed);
_lastSpeed = 0;
}
}

private void UnhookRootPointerEvents()
Expand All @@ -138,18 +156,20 @@ private void UnhookRootPointerEvents()
_overlayContainer.PointerExited -= OnRootPointerExited;
_overlayContainer.PointerPressed -= OnRootPointerPressed;
_overlayContainer.PointerCanceled -= OnRootPointerCanceled;
_overlayContainer.PointerReleased -= OnRootPointerReleased;
}
}

private void HookInteractionControlEvents()
{
_interactionControl.Tapped += OnCoreTapped;
_interactionControl.DoubleTapped += OnCoreDoubleTapped;
_interactionControl.Holding += OnCoreHolding;
_interactionControl.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
_interactionControl.ManipulationStarted += OnInteractionControlManipulationStarted;
_interactionControl.ManipulationDelta += OnInteractionControlManipulationDelta;
_interactionControl.ManipulationCompleted += OnInteractionControlManipulationCompleted;
_interactionControl.ContextRequested += OnInteractionControlContextRequested;
_gestureRecognizer.Holding += OnGestureRecognizerHolding;
}

private void UnhookInteractionControlEvents()
Expand All @@ -158,10 +178,11 @@ private void UnhookInteractionControlEvents()
{
_interactionControl.Tapped -= OnCoreTapped;
_interactionControl.DoubleTapped -= OnCoreDoubleTapped;
_interactionControl.Holding -= OnCoreHolding;
_interactionControl.ManipulationStarted -= OnInteractionControlManipulationStarted;
_interactionControl.ManipulationDelta -= OnInteractionControlManipulationDelta;
_interactionControl.ManipulationCompleted -= OnInteractionControlManipulationCompleted;
_interactionControl.ContextRequested -= OnInteractionControlContextRequested;
_gestureRecognizer.Holding -= OnGestureRecognizerHolding;
}
}

Expand Down Expand Up @@ -237,6 +258,12 @@ private void OnViewModelInitialized(object? sender, EventArgs e)

private void OnCoreTapped(object sender, TappedRoutedEventArgs e)
{
if (_isHolding)
{
_isHolding = false;
return;
}

var isManual = SettingsToolkit.ReadLocalSetting(SettingNames.MTCBehavior, MTCBehavior.Automatic) == MTCBehavior.Manual;
if (isManual)
{
Expand Down Expand Up @@ -274,21 +301,6 @@ private void OnCoreDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
}
}

private void OnCoreHolding(object sender, HoldingRoutedEventArgs e)
{
if (e.HoldingState == HoldingState.Started)
{
_lastSpeed = ViewModel.Speed;
ViewModel.SetSpeedCommand.Execute(3d);
}
else
{
_lastSpeed = _lastSpeed == 0 ? 1.0 : _lastSpeed;
ViewModel.SetSpeedCommand.Execute(_lastSpeed);
_lastSpeed = 0;
}
}

private void OnViewModelInnerPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (_overlayContainer is not null)
Expand Down

0 comments on commit 2e77019

Please sign in to comment.