1+ using System . Reflection ;
12using CommunityToolkit . Maui . Extensions ;
23using CommunityToolkit . Maui . Primitives ;
34using CommunityToolkit . Maui . Views ;
67using Microsoft . UI . Xaml ;
78using Microsoft . UI . Xaml . Controls ;
89using Microsoft . UI . Xaml . Controls . Primitives ;
9- using Microsoft . UI . Xaml . Input ;
10- using Microsoft . UI . Xaml . Media ;
10+ using Microsoft . UI . Xaml . Markup ;
1111using WinRT . Interop ;
1212using Application = Microsoft . Maui . Controls . Application ;
13- using Button = Microsoft . UI . Xaml . Controls . Button ;
14- using Colors = Microsoft . UI . Colors ;
1513using Grid = Microsoft . UI . Xaml . Controls . Grid ;
1614using Page = Microsoft . Maui . Controls . Page ;
17- using SolidColorBrush = Microsoft . UI . Xaml . Media . SolidColorBrush ;
18- using Thickness = Microsoft . UI . Xaml . Thickness ;
1915
2016namespace 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 ;
0 commit comments