-
Notifications
You must be signed in to change notification settings - Fork 11
/
NativeVideoPresenter.cs
200 lines (163 loc) · 6.93 KB
/
NativeVideoPresenter.cs
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
199
200
using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.Platform;
using Avalonia.VisualTree;
using LibVLCSharp.Shared;
using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
namespace LibVLCSharp.Avalonia
{
public class NativeVideoPresenter : NativeControlHost
{
static NativeVideoPresenter()
{
ContentProperty.Changed.AddClassHandler<NativeVideoPresenter>((s, e) => s.InitializeNativeOverlay());
IsVisibleProperty.Changed.AddClassHandler<NativeVideoPresenter>((s, e) => s.ShowNativeOverlay(s.IsVisible));
}
public static readonly StyledProperty<object> ContentProperty =
ContentControl.ContentProperty.AddOwner<NativeVideoPresenter>();
[Content]
public object Content
{
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
private IPlatformHandle _handle;
private bool _attached;
private Window _floatingContent;
private IDisposable _disposables;
private IDisposable _isEffectivellyVisibleSub;
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
{
return _handle = base.CreateNativeControlCore(parent);
}
internal void UpdatePlayerHandle(MediaPlayer mediaPlayer)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
mediaPlayer.Hwnd = _handle.Handle;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
mediaPlayer.NsObject = _handle.Handle;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
mediaPlayer.XWindow = (uint)_handle.Handle;
else
throw new NotImplementedException();
}
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
_attached = true;
InitializeNativeOverlay();
_isEffectivellyVisibleSub = this.GetVisualAncestors().OfType<IControl>()
.Select(v => v.GetObservable(IsVisibleProperty))
.CombineLatest(v => !v.Any(o => !o))
.DistinctUntilChanged()
.Subscribe(v => IsVisible = v);
}
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
_isEffectivellyVisibleSub?.Dispose();
ShowNativeOverlay(false);
_attached = false;
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnDetachedFromLogicalTree(e);
_disposables?.Dispose();
_disposables = null;
_floatingContent?.Close();
_floatingContent = null;
}
private void InitializeNativeOverlay()
{
if (!((IVisual)this).IsAttachedToVisualTree) return;
if (_floatingContent == null && Content != null)
{
_floatingContent = new Window()
{
SystemDecorations = SystemDecorations.None,
TransparencyLevelHint = WindowTransparencyLevel.Transparent,
Background = Brushes.Transparent,
SizeToContent = SizeToContent.WidthAndHeight,
ShowInTaskbar = false,
};
_disposables = new CompositeDisposable()
{
_floatingContent.Bind(Window.ContentProperty, this.GetObservable(ContentProperty)),
this.GetObservable(ContentProperty).Skip(1).Subscribe(_=> UpdateOverlayPosition()),
this.GetObservable(BoundsProperty).Skip(1).Subscribe(_ => UpdateOverlayPosition()),
Observable.FromEventPattern(VisualRoot, nameof(Window.PositionChanged))
.Subscribe(_ => UpdateOverlayPosition())
};
}
ShowNativeOverlay(IsEffectivelyVisible);
}
private void ShowNativeOverlay(bool show)
{
if (_floatingContent == null || _floatingContent.IsVisible == show)
return;
if (show && _attached)
_floatingContent.Show(VisualRoot as Window);
else
_floatingContent.Hide();
}
private void UpdateOverlayPosition()
{
if (_floatingContent == null) return;
bool forceSetWidth = false, forceSetHeight = false;
var topLeft = new Point();
var child = _floatingContent.Presenter?.Child;
if (child?.IsArrangeValid == true)
{
switch (child.HorizontalAlignment)
{
case HorizontalAlignment.Right:
topLeft = topLeft.WithX(Bounds.Width - _floatingContent.Bounds.Width);
break;
case HorizontalAlignment.Center:
topLeft = topLeft.WithX((Bounds.Width - _floatingContent.Bounds.Width) / 2);
break;
case HorizontalAlignment.Stretch:
forceSetWidth = true;
break;
}
switch (child.VerticalAlignment)
{
case VerticalAlignment.Bottom:
topLeft = topLeft.WithY(Bounds.Height - _floatingContent.Bounds.Height);
break;
case VerticalAlignment.Center:
topLeft = topLeft.WithY((Bounds.Height - _floatingContent.Bounds.Height) / 2);
break;
case VerticalAlignment.Stretch:
forceSetHeight = true;
break;
}
}
if (forceSetWidth && forceSetHeight)
_floatingContent.SizeToContent = SizeToContent.Manual;
else if (forceSetHeight)
_floatingContent.SizeToContent = SizeToContent.Width;
else if (forceSetWidth)
_floatingContent.SizeToContent = SizeToContent.Height;
else
_floatingContent.SizeToContent = SizeToContent.Manual;
_floatingContent.Width = forceSetWidth ? Bounds.Width : double.NaN;
_floatingContent.Height = forceSetHeight ? Bounds.Height : double.NaN;
_floatingContent.MaxWidth = Bounds.Width;
_floatingContent.MaxHeight = Bounds.Height;
var newPosition = this.PointToScreen(topLeft);
if (newPosition != _floatingContent.Position)
{
_floatingContent.Position = newPosition;
}
}
}
}