Skip to content

Commit

Permalink
Merge branch 'main' into FullScreenEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
ne0rrmatrix authored Jan 22, 2025
2 parents 77878ed + 00f64ee commit 4367ea1
Show file tree
Hide file tree
Showing 7 changed files with 921 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.0.0" />
<PackageReference Include="FluentAssertions" Version="8.0.1" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.2" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" Condition=" '$(Configuration)'=='Release' " PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="ResourceDictionary.windows.xaml">
<LogicalName>ResourceDictionary.windows.xaml</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace CommunityToolkit.Maui.Primitives;

sealed partial class CustomTransportControls : MediaTransportControls
{
public event EventHandler<EventArgs>? OnTemplateLoaded;
public AppBarButton FullScreenButton = new();
bool isFullScreen = false;

public CustomTransportControls()
{
this.DefaultStyleKey = typeof(CustomTransportControls);
}

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

var temp = GetTemplateChild("FullWindowButton") as AppBarButton;
if(temp is not null)
{
FullScreenButton = temp;
FullScreenButton.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
OnTemplateLoaded?.Invoke(this, EventArgs.Empty);
FullScreenButton.Click += FullScreenButton_Click;
}
}

void FullScreenButton_Click(object sender, RoutedEventArgs e)
{
if (isFullScreen)
{
FullScreenButton.Icon = new FontIcon { Glyph = "\uE740" };
isFullScreen = false;
}
else
{
FullScreenButton.Icon = new SymbolIcon(Symbol.BackToWindow);
isFullScreen = true;
}
}
}
799 changes: 799 additions & 0 deletions src/CommunityToolkit.Maui.MediaElement/ResourceDictionary.windows.xaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Primitives;
using CommunityToolkit.Maui.Views;
Expand All @@ -6,16 +7,11 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Markup;
using WinRT.Interop;
using Application = Microsoft.Maui.Controls.Application;
using Button = Microsoft.UI.Xaml.Controls.Button;
using Colors = Microsoft.UI.Colors;
using Grid = Microsoft.UI.Xaml.Controls.Grid;
using Page = Microsoft.Maui.Controls.Page;
using SolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;
using Thickness = Microsoft.UI.Xaml.Thickness;

namespace CommunityToolkit.Maui.Core.Views;

Expand All @@ -27,12 +23,8 @@ public partial class MauiMediaElement : Grid, IDisposable
static readonly AppWindow appWindow = GetAppWindowForCurrentWindow();
readonly Popup popup = new();
readonly Grid fullScreenGrid = new();
readonly Grid buttonContainer;
readonly Button fullScreenButton;
readonly MediaPlayerElement mediaPlayerElement;
// Cannot be static readonly because we need to be able to add icon to multiple instances of the button
readonly FontIcon fullScreenIcon = new() { Glyph = "\uE740", FontFamily = new FontFamily("Segoe Fluent Icons") };
readonly FontIcon exitFullScreenIcon = new() { Glyph = "\uE73F", FontFamily = new FontFamily("Segoe Fluent Icons") };
readonly CustomTransportControls? customTransportControls;
bool doesNavigationBarExistBeforeFullScreen;
bool isDisposed;

Expand All @@ -42,33 +34,73 @@ public partial class MauiMediaElement : Grid, IDisposable
/// <param name="mediaPlayerElement"></param>
public MauiMediaElement(MediaPlayerElement mediaPlayerElement)
{
LoadResourceDictionary();
this.mediaPlayerElement = mediaPlayerElement;
customTransportControls = SetTransportControls();
Children.Add(this.mediaPlayerElement);
}

fullScreenButton = new Button
void LoadResourceDictionary()
{
var assembly = Assembly.GetExecutingAssembly();
using Stream? stream = assembly.GetManifestResourceStream("ResourceDictionary.windows.xaml");
if (stream is null)
{
Content = fullScreenIcon,
Background = new SolidColorBrush(Colors.Transparent),
Width = 45,
Height = 45
};
return;
}
using StreamReader reader = new(stream);
var xaml = reader.ReadToEnd();
var resourceDictionary = (Microsoft.UI.Xaml.ResourceDictionary)XamlReader.Load(xaml);
if (resourceDictionary is null)
{
return;
}
this.Resources.MergedDictionaries.Add(resourceDictionary);
}
void ApplyCustomStyle()
{
if (this.Resources.TryGetValue("customTransportcontrols", out object styleObj) &&
styleObj is Microsoft.UI.Xaml.Style customStyle && mediaPlayerElement is not null && mediaPlayerElement.TransportControls is not null)
{
mediaPlayerElement.TransportControls.Style = customStyle;
}
}

buttonContainer = new Grid
CustomTransportControls SetTransportControls()
{
mediaPlayerElement.TransportControls.IsEnabled = false;
var temp = new CustomTransportControls()
{
HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Right,
VerticalAlignment = Microsoft.UI.Xaml.VerticalAlignment.Top,
Visibility = mediaPlayerElement.TransportControls.Visibility,
Width = 45,
Height = 45,
Margin = new Thickness(0, 20, 30, 0)
IsZoomButtonVisible = true,
IsZoomEnabled = true,
IsVolumeButtonVisible = true,
IsVolumeEnabled = true,
IsSeekBarVisible = true,
IsSeekEnabled = true,
IsEnabled = true,
IsRepeatButtonVisible = true,
IsRepeatEnabled = true,
IsNextTrackButtonVisible = true,
IsPreviousTrackButtonVisible = true,
IsFastForwardButtonVisible = true,
IsFastForwardEnabled = true,
IsFastRewindButtonVisible = true,
IsFastRewindEnabled = true,
IsPlaybackRateButtonVisible = true,
IsPlaybackRateEnabled = true,
IsCompact = false,
};

fullScreenButton.Click += OnFullScreenButtonClick;
buttonContainer.Children.Add(fullScreenButton);

Children.Add(this.mediaPlayerElement);
Children.Add(buttonContainer);

mediaPlayerElement.PointerMoved += OnMediaPlayerElementPointerMoved;
temp.OnTemplateLoaded += (s, e) =>
{
if (temp.FullScreenButton is null)
{
return;
}
temp.FullScreenButton.Click += OnFullScreenButtonClick;
};
mediaPlayerElement.TransportControls = temp;
ApplyCustomStyle();
return temp;
}

/// <summary>
Expand Down Expand Up @@ -100,9 +132,10 @@ protected virtual void Dispose(bool disposing)
{
return;
}

fullScreenButton.Click -= OnFullScreenButtonClick;
mediaPlayerElement.PointerMoved -= OnMediaPlayerElementPointerMoved;
if (customTransportControls?.FullScreenButton is not null)
{
customTransportControls.FullScreenButton.Click -= OnFullScreenButtonClick;
}

if (disposing)
{
Expand All @@ -129,29 +162,9 @@ static AppWindow GetAppWindowForCurrentWindow()
return AppWindow.GetFromWindowId(id);
}

async void OnMediaPlayerElementPointerMoved(object sender, PointerRoutedEventArgs e)
{
e.Handled = true;
buttonContainer.Visibility = mediaPlayerElement.TransportControls.Visibility;

if (mediaPlayerElement.TransportControls.Visibility == Microsoft.UI.Xaml.Visibility.Collapsed)
{
buttonContainer.Visibility = mediaPlayerElement.TransportControls.Visibility;
return;
}

mediaPlayerElement.PointerMoved -= OnMediaPlayerElementPointerMoved;
buttonContainer.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
await Task.Delay(TimeSpan.FromSeconds(5));

buttonContainer.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
mediaPlayerElement.PointerMoved += OnMediaPlayerElementPointerMoved;
}

void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
{
var currentPage = CurrentPage;

if (appWindow.Presenter.Kind is AppWindowPresenterKind.FullScreen)
{
appWindow.SetPresenter(AppWindowPresenterKind.Default);
Expand All @@ -163,9 +176,7 @@ void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
popup.Child = null;
fullScreenGrid.Children.Clear();
}
fullScreenButton.Content = fullScreenIcon;
Children.Add(mediaPlayerElement);
Children.Add(buttonContainer);

var parent = mediaPlayerElement.Parent as FrameworkElement;
mediaPlayerElement.Width = parent?.Width ?? mediaPlayerElement.Width;
Expand All @@ -183,9 +194,7 @@ void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
mediaPlayerElement.Height = displayInfo.Height / displayInfo.Density;

Children.Clear();
fullScreenButton.Content = exitFullScreenIcon;
fullScreenGrid.Children.Add(mediaPlayerElement);
fullScreenGrid.Children.Add(buttonContainer);

popup.XamlRoot = mediaPlayerElement.XamlRoot;
popup.HorizontalOffset = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public PlatformMediaElement CreatePlatformView()
Player.MediaPlayer.VolumeChanged += OnMediaElementVolumeChanged;
Player.MediaPlayer.IsMutedChanged += OnMediaElementIsMutedChanged;

Player.MediaPlayer.SystemMediaTransportControls.IsEnabled = false;
systemMediaControls = Player.MediaPlayer.SystemMediaTransportControls;

return Player;
}

Expand Down Expand Up @@ -250,12 +252,11 @@ protected virtual partial void PlatformUpdateShouldKeepScreenOn()

protected virtual partial void PlatformUpdateShouldMute()
{
if (Player?.MediaPlayer is null)
if (Player is null)
{
return;
}

Player.MediaPlayer.IsMuted = MediaElement.ShouldMute;
Dispatcher.Dispatch(() => Player.MediaPlayer.IsMuted = MediaElement.ShouldMute);
}

protected virtual async partial ValueTask PlatformUpdateSource()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.0.0" />
<PackageReference Include="FluentAssertions" Version="8.0.1" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
Expand Down

0 comments on commit 4367ea1

Please sign in to comment.