Skip to content

Commit fef47cd

Browse files
Jdbyemfkl
authored andcommitted
Allow attaching overlay window to specified background element
Allow attaching overlay window to specified background element rather than it being constrained to the video dimensions. This means the foreground window overlay is a predictable position and size and makes overlaying controls on top of the video much more intuitive.
1 parent f08446b commit fef47cd

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

src/LibVLCSharp.WPF/ForegroundWindow.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace LibVLCSharp.WPF
1111
internal class ForegroundWindow : Window
1212
{
1313
Window? _wndhost;
14-
readonly FrameworkElement _bckgnd;
14+
readonly FrameworkElement _videoHost;
15+
readonly FrameworkElement _backgroundElement;
1516
readonly Point _zeroPoint = new Point(0, 0);
1617
private readonly Grid _grid = new Grid();
1718

@@ -30,7 +31,7 @@ internal UIElement? OverlayContent
3031
}
3132
}
3233

33-
internal ForegroundWindow(FrameworkElement background)
34+
internal ForegroundWindow(FrameworkElement videoHost, FrameworkElement? background)
3435
{
3536
Title = "LibVLCSharp.WPF";
3637
Height = 300;
@@ -42,23 +43,27 @@ internal ForegroundWindow(FrameworkElement background)
4243
ShowInTaskbar = false;
4344
Content = _grid;
4445

45-
DataContext = background.DataContext;
46+
DataContext = videoHost.DataContext;
4647

47-
_bckgnd = background;
48-
_bckgnd.DataContextChanged += Background_DataContextChanged;
49-
_bckgnd.Loaded += Background_Loaded;
50-
_bckgnd.Unloaded += Background_Unloaded;
48+
if (background == null)
49+
background = videoHost;
50+
51+
_backgroundElement = background;
52+
_videoHost = videoHost;
53+
_videoHost.DataContextChanged += VideoHost_DataContextChanged;
54+
_videoHost.Loaded += VideoHost_Loaded;
55+
_videoHost.Unloaded += VideoHost_Unloaded;
5156
}
5257

53-
void Background_DataContextChanged(object? sender, DependencyPropertyChangedEventArgs e)
58+
void VideoHost_DataContextChanged(object? sender, DependencyPropertyChangedEventArgs e)
5459
{
5560
DataContext = e.NewValue;
5661
}
5762

58-
void Background_Unloaded(object? sender, RoutedEventArgs e)
63+
void VideoHost_Unloaded(object? sender, RoutedEventArgs e)
5964
{
60-
_bckgnd.SizeChanged -= Bckgnd_SizeChanged;
61-
_bckgnd.LayoutUpdated -= Bckgnd_LayoutUpdated;
65+
_backgroundElement.SizeChanged -= Bckgnd_SizeChanged;
66+
_backgroundElement.LayoutUpdated -= Bckgnd_LayoutUpdated;
6267
if (_wndhost != null)
6368
{
6469
_wndhost.Closing -= Wndhost_Closing;
@@ -68,14 +73,14 @@ void Background_Unloaded(object? sender, RoutedEventArgs e)
6873
Hide();
6974
}
7075

71-
void Background_Loaded(object? sender, RoutedEventArgs e)
76+
void VideoHost_Loaded(object? sender, RoutedEventArgs e)
7277
{
7378
if (_wndhost != null && IsVisible)
7479
{
7580
return;
7681
}
7782

78-
_wndhost = GetWindow(_bckgnd);
83+
_wndhost = GetWindow(_videoHost);
7984
Trace.Assert(_wndhost != null);
8085
if (_wndhost == null)
8186
{
@@ -86,8 +91,8 @@ void Background_Loaded(object? sender, RoutedEventArgs e)
8691

8792
_wndhost.Closing += Wndhost_Closing;
8893
_wndhost.LocationChanged += Wndhost_LocationChanged;
89-
_bckgnd.LayoutUpdated += Bckgnd_LayoutUpdated;
90-
_bckgnd.SizeChanged += Bckgnd_SizeChanged;
94+
_backgroundElement.LayoutUpdated += Bckgnd_LayoutUpdated;
95+
_backgroundElement.SizeChanged += Bckgnd_SizeChanged;
9196

9297
try
9398
{
@@ -131,13 +136,13 @@ void AlignWithBackground()
131136
return;
132137
}
133138

134-
if (PresentationSource.FromVisual(_bckgnd) == null)
139+
if (PresentationSource.FromVisual(_backgroundElement) == null)
135140
{
136141
return;
137142
}
138143

139-
if (double.IsNaN(_bckgnd.ActualWidth) || double.IsNaN(_bckgnd.ActualHeight) ||
140-
_bckgnd.ActualWidth == 0 || _bckgnd.ActualHeight == 0)
144+
if (double.IsNaN(_backgroundElement.ActualWidth) || double.IsNaN(_backgroundElement.ActualHeight) ||
145+
_backgroundElement.ActualWidth == 0 || _backgroundElement.ActualHeight == 0)
141146
{
142147
return;
143148
}
@@ -162,19 +167,19 @@ void AlignWithBackground()
162167
* The video view itself natively supports scaling.
163168
*/
164169

165-
var startLocationFromScreen = _bckgnd.PointToScreen(_zeroPoint);
170+
var startLocationFromScreen = _backgroundElement.PointToScreen(_zeroPoint);
166171
var startLocationPoint = source.CompositionTarget.TransformFromDevice.Transform(startLocationFromScreen);
167-
var endLocationFromScreen = _bckgnd.PointToScreen(new Point(_bckgnd.ActualWidth, _bckgnd.ActualHeight));
172+
var endLocationFromScreen = _backgroundElement.PointToScreen(new Point(_backgroundElement.ActualWidth, _backgroundElement.ActualHeight));
168173
var endLocationPoint = source.CompositionTarget.TransformFromDevice.Transform(endLocationFromScreen);
169174

170175
Left = Math.Min(startLocationPoint.X, endLocationPoint.X);
171176
Top = Math.Min(startLocationPoint.Y, endLocationPoint.Y);
172177
Width = Math.Abs(endLocationPoint.X - startLocationPoint.X);
173178
Height = Math.Abs(endLocationPoint.Y - startLocationPoint.Y);
174179

175-
if (Math.Abs(Width - _bckgnd.ActualWidth) + Math.Abs(Height - _bckgnd.ActualHeight) > 0.5)
180+
if (Math.Abs(Width - _backgroundElement.ActualWidth) + Math.Abs(Height - _backgroundElement.ActualHeight) > 0.5)
176181
{
177-
ScaleWindowContent(Width / _bckgnd.ActualWidth, Height / _bckgnd.ActualHeight);
182+
ScaleWindowContent(Width / _backgroundElement.ActualWidth, Height / _backgroundElement.ActualHeight);
178183
}
179184
}
180185

@@ -208,9 +213,9 @@ void Wndhost_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
208213

209214
Close();
210215

211-
_bckgnd.DataContextChanged -= Background_DataContextChanged;
212-
_bckgnd.Loaded -= Background_Loaded;
213-
_bckgnd.Unloaded -= Background_Unloaded;
216+
_videoHost.DataContextChanged -= VideoHost_DataContextChanged;
217+
_videoHost.Loaded -= VideoHost_Loaded;
218+
_videoHost.Unloaded -= VideoHost_Unloaded;
214219
}
215220

216221
protected override void OnKeyDown(KeyEventArgs e)

src/LibVLCSharp.WPF/VideoView.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private static void OnMediaPlayerChanged(DependencyObject d, DependencyPropertyC
5757
private ForegroundWindow? ForegroundWindow { get; set; }
5858
private bool IsUpdatingContent { get; set; }
5959
private UIElement? ViewContent { get; set; }
60+
61+
/// <summary>
62+
/// Background element for this VideoView
63+
/// </summary>
64+
public FrameworkElement? BackgroundElement { get; set; } = null;
6065

6166
/// <summary>
6267
/// ForegroundWindow management and MediaPlayer setup.
@@ -78,7 +83,7 @@ public override void OnApplyTemplate()
7883

7984
_videoHwndHost = controlHost;
8085

81-
ForegroundWindow = new ForegroundWindow(_videoHwndHost)
86+
ForegroundWindow = new ForegroundWindow(_videoHwndHost, BackgroundElement)
8287
{
8388
OverlayContent = ViewContent
8489
};

0 commit comments

Comments
 (0)