Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Avalonia - Add missing docs + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fsobolev committed Oct 6, 2023
1 parent cbbd3cd commit 118f9e3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
28 changes: 25 additions & 3 deletions Examples/Avalonia/Controls/MPVControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

namespace AvaloniaMPV.Controls;

/// <summary>
/// MPV Avalonia Control.
/// This is based on <see cref="Avalonia.OpenGL.Controls.OpenGlControlBase"/>.
/// It creates the <see cref="Player"/> and initializes it, then <see cref="RenderContext"/> is created
/// on OpenGL initialization and it gets configured to request new frame to be drawn when MPV requires it.
/// </summary>
public class MPVControl : OpenGlControlBase
{
private RenderContext? _ctx;

public readonly Client Player;
public Client Player { get; init; }

/// <summary>
/// Constructs MPVControl
/// </summary>
public MPVControl()
{
Player = new Client();
Expand All @@ -24,6 +33,10 @@ public MPVControl()
Player.Initialize();
}

/// <summary>
/// Occurs on OpenGL initialization
/// </summary>
/// <param name="gl">OpenGL interface</param>
protected override void OnOpenGlInit(GlInterface gl)
{
if (_ctx != null)
Expand All @@ -33,16 +46,25 @@ protected override void OnOpenGlInit(GlInterface gl)
return;
}
_ctx = Player.CreateRenderContext();
_ctx.GetProcAddressFn = gl.GetProcAddress;
_ctx.SetupGL(() => Dispatcher.UIThread.Post(RequestNextFrameRendering));
_ctx.GetProcAddressFn = gl.GetProcAddress; // Required to make it work on Windows
_ctx.SetupGL(() => Dispatcher.UIThread.Post(RequestNextFrameRendering)); // Use Post, not Invoke, to avoid lags
}

/// <summary>
/// Occurs on OpenGL deiniitalization
/// </summary>
/// <param name="gl">OpenGL interface</param>
protected override void OnOpenGlDeinit(GlInterface gl)
{
_ctx?.Dispose();
Player.Dispose();
}

/// <summary>
/// Used to draw a frame
/// </summary>
/// <param name="gl">OpenGL interface</param>
/// <param name="fb">Framebuffer object name</param>
protected override void OnOpenGlRender(GlInterface gl, int fb)
{
gl.ClearColor(0, 0, 0, 1);
Expand Down
6 changes: 6 additions & 0 deletions Examples/Avalonia/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace AvaloniaMPV.Views;

/// <summary>
/// Main application window
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Constructs MainWindow
/// </summary>
public MainWindow()
{
InitializeComponent();
Expand Down
20 changes: 17 additions & 3 deletions Examples/Avalonia/Views/PlayerView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using AvaloniaMPV.Controls;
using Nickvision.MPVSharp;

namespace AvaloniaMPV.Views;

/// <summary>
/// Player view
/// </summary>
public partial class PlayerView : UserControl
{
private readonly Client _player;

/// <summary>
/// Constructs PlayerView
/// </summary>
public PlayerView()
{
InitializeComponent();
_player = MPVControl.Player;
_player.LoadFile("https://www.youtube.com/watch?v=_cMxraX_5RE");
_player.PropertyChanged += (sender, e) => Dispatcher.UIThread.Invoke(() => OnMPVPropertyChanged(sender ,e));
_player.PropertyChanged += (sender, e) => Dispatcher.UIThread.Invoke(() => OnMPVPropertyChanged(e)); // Use Invoke, not Post, to avoid threading issues
_player.ObserveProperty("pause");
_player.ObserveProperty("time-pos");
}

public void OnMPVPropertyChanged(object? sender, PropertyChangedEventArgs e)
/// <summary>
/// Occurs when an MPV property has changed
/// </summary>
/// <param name="e">PropertyChangedEventArgs</param>
public void OnMPVPropertyChanged(PropertyChangedEventArgs e)
{
switch (e.Name)
{
Expand All @@ -34,6 +43,11 @@ public void OnMPVPropertyChanged(object? sender, PropertyChangedEventArgs e)
}
}

/// <summary>
/// Occurs when pause button was clicked
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">RoutedEventArgs</param>
public void OnPauseClicked(object sender, RoutedEventArgs e)
{
_player.CyclePause();
Expand Down

0 comments on commit 118f9e3

Please sign in to comment.