-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathFlyleafHost.cs
More file actions
198 lines (161 loc) · 7.1 KB
/
FlyleafHost.cs
File metadata and controls
198 lines (161 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
using Vortice.DXGI;
using FlyleafLib.MediaPlayer;
namespace FlyleafLib.Controls.WinUI;
[TemplatePart(Name = nameof(KFC), Type = typeof(KeyboardFocusControl))]
[TemplatePart(Name = nameof(SCP), Type = typeof(SwapChainPanel))]
public sealed class FlyleafHost : ContentControl, IHostPlayer
{
/*
* Fix issue with key events / focus (in combination of those we have the issue with DoubleTap for FullScreen)
* Note: On PointerReleased we can't set the focus element as it will change it after that (that's why a dispatcher is required... for delay)
*
* 1) https://github.com/microsoft/microsoft-ui-xaml/issues/7330
* 2) https://github.com/microsoft/microsoft-ui-xaml/issues/3986
* 3) https://github.com/microsoft/microsoft-ui-xaml/issues/6179
*
*/
public SwapChainPanel SCP { get; set; } = null!;
public KeyboardFocusControl? KFC { get; set; } = null!;
public FullScreenContainer? FSC
{
get { return (FullScreenContainer)GetValue(FSCProperty); }
set { SetValue(FSCProperty, value); }
}
public static readonly DependencyProperty FSCProperty =
DependencyProperty.Register(nameof(FSC), typeof(FullScreenContainer), typeof(FlyleafHost), new PropertyMetadata(null));
public Player? Player
{
get { return (Player?)GetValue(PlayerProperty); }
set { SetValue(PlayerProperty, value); }
}
public static readonly DependencyProperty PlayerProperty =
DependencyProperty.Register(nameof(Player), typeof(Player), typeof(FlyleafHost), new PropertyMetadata(null, OnPlayerChanged));
private static void OnPlayerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var host = d as FlyleafHost;
if (host == null || host.SCP == null || host.Player == null)
return;
host.PlayerChanged((Player)e.OldValue);
}
public bool KeyBindings
{
get { return (bool)GetValue(KeyBindingsProperty); }
set { SetValue(KeyBindingsProperty, value); }
}
public static readonly DependencyProperty KeyBindingsProperty =
DependencyProperty.Register(nameof(KeyBindings), typeof(bool), typeof(FlyleafHost), new PropertyMetadata(true));
public int UniqueId { get; private set; }
static int idGenerator = 1;
readonly LogHandler Log;
public FlyleafHost()
{
DefaultStyleKey = typeof(FlyleafHost);
UniqueId = idGenerator++;
Log = new LogHandler(("[#" + UniqueId + "]").PadRight(8, ' ') + $" [FlyleafHost NP] ");
}
protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
if (FSC != null)
FSC.IsFullScreen = !FSC.IsFullScreen;
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
FrameworkElement parent = this;
while (parent.Parent is FrameworkElement)
{
parent = (FrameworkElement)parent.Parent;
if (parent is FullScreenContainer)
{
FSC = (FullScreenContainer)parent;
break;
}
}
if (GetTemplateChild(nameof(KFC)) is KeyboardFocusControl kfc && KeyBindings)
{
KFC = kfc;
KFC.KeyDown += KFC_KeyDown;
KFC.KeyUp += KFC_KeyUp;
KFC.Focus(FocusState.Keyboard);
}
if (GetTemplateChild(nameof(SCP)) is SwapChainPanel scp)
{
SCP = scp;
SCP.SizeChanged += SCP_SizeChanged;
if (KeyBindings) // Currently used only for keys which causes issues with KFC focus
{
SCP.PointerPressed += Scp_PointerPressed;
SCP.PointerReleased += SCP_PointerReleased;
}
//SCP.DoubleTapped += SCP_DoubleTapped; // Disabled causes issues
}
else
throw new Exception("SCP not found");
if (Player != null)
PlayerChanged(null);
}
private void KFC_KeyDown(object sender, KeyRoutedEventArgs e)
{ if (KeyBindings) Player.KeyDown(Player, System.Windows.Input.KeyInterop.KeyFromVirtualKey((int)e.Key)); }
private void KFC_KeyUp(object sender, KeyRoutedEventArgs e)
{ if (KeyBindings) Player.KeyUp(Player, System.Windows.Input.KeyInterop.KeyFromVirtualKey((int)e.Key)); }
private void SCP_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{ // not working?
if (FSC != null)
FSC.IsFullScreen = !FSC.IsFullScreen;
}
private void Scp_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var properties = e.GetCurrentPoint((UIElement)sender).Properties;
if (properties.IsLeftButtonPressed)
((UIElement)sender).CapturePointer(e.Pointer);
}
private void SCP_PointerReleased(object sender, PointerRoutedEventArgs e)
{
((UIElement)sender).ReleasePointerCaptures();
DispatcherQueue.TryEnqueue(() => KFC!.Focus(FocusState.Programmatic));
}
private void SCP_SizeChanged(object sender, SizeChangedEventArgs e)
{
Player?.Renderer.SwapChain.Resize((int)e.NewSize.Width, (int)e.NewSize.Height);
}
private void PlayerChanged(Player? oldPlayer)
{
if (oldPlayer != null)
{
Log.Debug($"De-assign Player #{oldPlayer.PlayerId}");
oldPlayer.Renderer?.SwapChain.Dispose(rendererFrame: false);
oldPlayer.Host = null;
}
if (Player == null)
return;
Log.Prefix = ("[#" + UniqueId + "]").PadRight(8, ' ') + $" [FlyleafHost #{Player.PlayerId}] ";
// De-assign new Player's Handle/FlyleafHost
Player.Host?.Player_Disposed();
if (Player == null) // We might just de-assign our Player
return;
// Assign new Player's (Handle/FlyleafHost)
Log.Debug($"Assign Player #{Player.PlayerId}");
Player.Host = this;
Background = new SolidColorBrush(new() { A = Player.Config.Video.BackColor.A, R = Player.Config.Video.BackColor.R, G = Player.Config.Video.BackColor.G, B = Player.Config.Video.BackColor.B });
Player.Renderer.SwapChain.SetupWinUI(SwapChainClbk);
}
private void SwapChainClbk(IDXGISwapChain2 swapChain)
{
using (var nativeObject = SharpGen.Runtime.ComObject.As<Vortice.WinUI.ISwapChainPanelNative2>(SCP))
nativeObject.SetSwapChain(swapChain);
if (swapChain != null)
Player?.Renderer.SwapChain.Resize((int)ActualWidth, (int)ActualHeight);
}
public bool Player_CanHideCursor() => Window.Current == FullScreenContainer.FSW;
public bool Player_GetFullScreen() => FSC != null && FSC.IsFullScreen;
public void Player_SetFullScreen(bool value) { if (FSC != null) FSC.IsFullScreen = value; }
public void Player_Disposed() => Utils.UIInvokeIfRequired(() => Player = null);
public void Player_RatioChanged(double keepRatio) { }
public bool Player_HandlesRatioResize(int width, int height) { return false; }
}
public class KeyboardFocusControl : Control { }