Skip to content

Commit 4367ea1

Browse files
authored
Merge branch 'main' into FullScreenEvents
2 parents 77878ed + 00f64ee commit 4367ea1

File tree

7 files changed

+921
-64
lines changed

7 files changed

+921
-64
lines changed

src/CommunityToolkit.Maui.Analyzers.UnitTests/CommunityToolkit.Maui.Analyzers.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="FluentAssertions" Version="8.0.0" />
13+
<PackageReference Include="FluentAssertions" Version="8.0.1" />
1414
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" />
1515
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.2" />
1616
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.2" />

src/CommunityToolkit.Maui.MediaElement/CommunityToolkit.Maui.MediaElement.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@
8181
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" Condition=" '$(Configuration)'=='Release' " PrivateAssets="All" />
8282
</ItemGroup>
8383

84+
<ItemGroup>
85+
<EmbeddedResource Include="ResourceDictionary.windows.xaml">
86+
<LogicalName>ResourceDictionary.windows.xaml</LogicalName>
87+
</EmbeddedResource>
88+
</ItemGroup>
8489
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
namespace CommunityToolkit.Maui.Primitives;
4+
5+
sealed partial class CustomTransportControls : MediaTransportControls
6+
{
7+
public event EventHandler<EventArgs>? OnTemplateLoaded;
8+
public AppBarButton FullScreenButton = new();
9+
bool isFullScreen = false;
10+
11+
public CustomTransportControls()
12+
{
13+
this.DefaultStyleKey = typeof(CustomTransportControls);
14+
}
15+
16+
protected override void OnApplyTemplate()
17+
{
18+
base.OnApplyTemplate();
19+
20+
var temp = GetTemplateChild("FullWindowButton") as AppBarButton;
21+
if(temp is not null)
22+
{
23+
FullScreenButton = temp;
24+
FullScreenButton.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
25+
OnTemplateLoaded?.Invoke(this, EventArgs.Empty);
26+
FullScreenButton.Click += FullScreenButton_Click;
27+
}
28+
}
29+
30+
void FullScreenButton_Click(object sender, RoutedEventArgs e)
31+
{
32+
if (isFullScreen)
33+
{
34+
FullScreenButton.Icon = new FontIcon { Glyph = "\uE740" };
35+
isFullScreen = false;
36+
}
37+
else
38+
{
39+
FullScreenButton.Icon = new SymbolIcon(Symbol.BackToWindow);
40+
isFullScreen = true;
41+
}
42+
}
43+
}

src/CommunityToolkit.Maui.MediaElement/ResourceDictionary.windows.xaml

Lines changed: 799 additions & 0 deletions
Large diffs are not rendered by default.

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reflection;
12
using CommunityToolkit.Maui.Extensions;
23
using CommunityToolkit.Maui.Primitives;
34
using CommunityToolkit.Maui.Views;
@@ -6,16 +7,11 @@
67
using Microsoft.UI.Xaml;
78
using Microsoft.UI.Xaml.Controls;
89
using Microsoft.UI.Xaml.Controls.Primitives;
9-
using Microsoft.UI.Xaml.Input;
10-
using Microsoft.UI.Xaml.Media;
10+
using Microsoft.UI.Xaml.Markup;
1111
using WinRT.Interop;
1212
using Application = Microsoft.Maui.Controls.Application;
13-
using Button = Microsoft.UI.Xaml.Controls.Button;
14-
using Colors = Microsoft.UI.Colors;
1513
using Grid = Microsoft.UI.Xaml.Controls.Grid;
1614
using Page = Microsoft.Maui.Controls.Page;
17-
using SolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;
18-
using Thickness = Microsoft.UI.Xaml.Thickness;
1915

2016
namespace CommunityToolkit.Maui.Core.Views;
2117

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

@@ -42,33 +34,73 @@ public partial class MauiMediaElement : Grid, IDisposable
4234
/// <param name="mediaPlayerElement"></param>
4335
public MauiMediaElement(MediaPlayerElement mediaPlayerElement)
4436
{
37+
LoadResourceDictionary();
4538
this.mediaPlayerElement = mediaPlayerElement;
39+
customTransportControls = SetTransportControls();
40+
Children.Add(this.mediaPlayerElement);
41+
}
4642

47-
fullScreenButton = new Button
43+
void LoadResourceDictionary()
44+
{
45+
var assembly = Assembly.GetExecutingAssembly();
46+
using Stream? stream = assembly.GetManifestResourceStream("ResourceDictionary.windows.xaml");
47+
if (stream is null)
4848
{
49-
Content = fullScreenIcon,
50-
Background = new SolidColorBrush(Colors.Transparent),
51-
Width = 45,
52-
Height = 45
53-
};
49+
return;
50+
}
51+
using StreamReader reader = new(stream);
52+
var xaml = reader.ReadToEnd();
53+
var resourceDictionary = (Microsoft.UI.Xaml.ResourceDictionary)XamlReader.Load(xaml);
54+
if (resourceDictionary is null)
55+
{
56+
return;
57+
}
58+
this.Resources.MergedDictionaries.Add(resourceDictionary);
59+
}
60+
void ApplyCustomStyle()
61+
{
62+
if (this.Resources.TryGetValue("customTransportcontrols", out object styleObj) &&
63+
styleObj is Microsoft.UI.Xaml.Style customStyle && mediaPlayerElement is not null && mediaPlayerElement.TransportControls is not null)
64+
{
65+
mediaPlayerElement.TransportControls.Style = customStyle;
66+
}
67+
}
5468

55-
buttonContainer = new Grid
69+
CustomTransportControls SetTransportControls()
70+
{
71+
mediaPlayerElement.TransportControls.IsEnabled = false;
72+
var temp = new CustomTransportControls()
5673
{
57-
HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Right,
58-
VerticalAlignment = Microsoft.UI.Xaml.VerticalAlignment.Top,
59-
Visibility = mediaPlayerElement.TransportControls.Visibility,
60-
Width = 45,
61-
Height = 45,
62-
Margin = new Thickness(0, 20, 30, 0)
74+
IsZoomButtonVisible = true,
75+
IsZoomEnabled = true,
76+
IsVolumeButtonVisible = true,
77+
IsVolumeEnabled = true,
78+
IsSeekBarVisible = true,
79+
IsSeekEnabled = true,
80+
IsEnabled = true,
81+
IsRepeatButtonVisible = true,
82+
IsRepeatEnabled = true,
83+
IsNextTrackButtonVisible = true,
84+
IsPreviousTrackButtonVisible = true,
85+
IsFastForwardButtonVisible = true,
86+
IsFastForwardEnabled = true,
87+
IsFastRewindButtonVisible = true,
88+
IsFastRewindEnabled = true,
89+
IsPlaybackRateButtonVisible = true,
90+
IsPlaybackRateEnabled = true,
91+
IsCompact = false,
6392
};
64-
65-
fullScreenButton.Click += OnFullScreenButtonClick;
66-
buttonContainer.Children.Add(fullScreenButton);
67-
68-
Children.Add(this.mediaPlayerElement);
69-
Children.Add(buttonContainer);
70-
71-
mediaPlayerElement.PointerMoved += OnMediaPlayerElementPointerMoved;
93+
temp.OnTemplateLoaded += (s, e) =>
94+
{
95+
if (temp.FullScreenButton is null)
96+
{
97+
return;
98+
}
99+
temp.FullScreenButton.Click += OnFullScreenButtonClick;
100+
};
101+
mediaPlayerElement.TransportControls = temp;
102+
ApplyCustomStyle();
103+
return temp;
72104
}
73105

74106
/// <summary>
@@ -100,9 +132,10 @@ protected virtual void Dispose(bool disposing)
100132
{
101133
return;
102134
}
103-
104-
fullScreenButton.Click -= OnFullScreenButtonClick;
105-
mediaPlayerElement.PointerMoved -= OnMediaPlayerElementPointerMoved;
135+
if (customTransportControls?.FullScreenButton is not null)
136+
{
137+
customTransportControls.FullScreenButton.Click -= OnFullScreenButtonClick;
138+
}
106139

107140
if (disposing)
108141
{
@@ -129,29 +162,9 @@ static AppWindow GetAppWindowForCurrentWindow()
129162
return AppWindow.GetFromWindowId(id);
130163
}
131164

132-
async void OnMediaPlayerElementPointerMoved(object sender, PointerRoutedEventArgs e)
133-
{
134-
e.Handled = true;
135-
buttonContainer.Visibility = mediaPlayerElement.TransportControls.Visibility;
136-
137-
if (mediaPlayerElement.TransportControls.Visibility == Microsoft.UI.Xaml.Visibility.Collapsed)
138-
{
139-
buttonContainer.Visibility = mediaPlayerElement.TransportControls.Visibility;
140-
return;
141-
}
142-
143-
mediaPlayerElement.PointerMoved -= OnMediaPlayerElementPointerMoved;
144-
buttonContainer.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
145-
await Task.Delay(TimeSpan.FromSeconds(5));
146-
147-
buttonContainer.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
148-
mediaPlayerElement.PointerMoved += OnMediaPlayerElementPointerMoved;
149-
}
150-
151165
void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
152166
{
153167
var currentPage = CurrentPage;
154-
155168
if (appWindow.Presenter.Kind is AppWindowPresenterKind.FullScreen)
156169
{
157170
appWindow.SetPresenter(AppWindowPresenterKind.Default);
@@ -163,9 +176,7 @@ void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
163176
popup.Child = null;
164177
fullScreenGrid.Children.Clear();
165178
}
166-
fullScreenButton.Content = fullScreenIcon;
167179
Children.Add(mediaPlayerElement);
168-
Children.Add(buttonContainer);
169180

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

185196
Children.Clear();
186-
fullScreenButton.Content = exitFullScreenIcon;
187197
fullScreenGrid.Children.Add(mediaPlayerElement);
188-
fullScreenGrid.Children.Add(buttonContainer);
189198

190199
popup.XamlRoot = mediaPlayerElement.XamlRoot;
191200
popup.HorizontalOffset = 0;

src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public PlatformMediaElement CreatePlatformView()
6060
Player.MediaPlayer.VolumeChanged += OnMediaElementVolumeChanged;
6161
Player.MediaPlayer.IsMutedChanged += OnMediaElementIsMutedChanged;
6262

63+
Player.MediaPlayer.SystemMediaTransportControls.IsEnabled = false;
6364
systemMediaControls = Player.MediaPlayer.SystemMediaTransportControls;
65+
6466
return Player;
6567
}
6668

@@ -250,12 +252,11 @@ protected virtual partial void PlatformUpdateShouldKeepScreenOn()
250252

251253
protected virtual partial void PlatformUpdateShouldMute()
252254
{
253-
if (Player?.MediaPlayer is null)
255+
if (Player is null)
254256
{
255257
return;
256258
}
257-
258-
Player.MediaPlayer.IsMuted = MediaElement.ShouldMute;
259+
Dispatcher.Dispatch(() => Player.MediaPlayer.IsMuted = MediaElement.ShouldMute);
259260
}
260261

261262
protected virtual async partial ValueTask PlatformUpdateSource()

src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="FluentAssertions" Version="8.0.0" />
12+
<PackageReference Include="FluentAssertions" Version="8.0.1" />
1313
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" />
1414
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />

0 commit comments

Comments
 (0)