1+ using System . Reflection ;
12using CommunityToolkit . Maui . Extensions ;
3+ using CommunityToolkit . Maui . Primitives ;
24using CommunityToolkit . Maui . Views ;
35using Microsoft . UI ;
46using Microsoft . UI . Windowing ;
57using Microsoft . UI . Xaml ;
68using Microsoft . UI . Xaml . Controls ;
79using Microsoft . UI . Xaml . Controls . Primitives ;
8- using Microsoft . UI . Xaml . Input ;
9- using Microsoft . UI . Xaml . Media ;
10+ using Microsoft . UI . Xaml . Markup ;
1011using WinRT . Interop ;
1112using Application = Microsoft . Maui . Controls . Application ;
12- using Button = Microsoft . UI . Xaml . Controls . Button ;
13- using Colors = Microsoft . UI . Colors ;
1413using Grid = Microsoft . UI . Xaml . Controls . Grid ;
1514using Page = Microsoft . Maui . Controls . Page ;
16- using SolidColorBrush = Microsoft . UI . Xaml . Media . SolidColorBrush ;
17- using Thickness = Microsoft . UI . Xaml . Thickness ;
1815
1916namespace CommunityToolkit . Maui . Core . Views ;
2017
@@ -26,12 +23,8 @@ public partial class MauiMediaElement : Grid, IDisposable
2623 static readonly AppWindow appWindow = GetAppWindowForCurrentWindow ( ) ;
2724 readonly Popup popup = new ( ) ;
2825 readonly Grid fullScreenGrid = new ( ) ;
29- readonly Grid buttonContainer ;
30- readonly Button fullScreenButton ;
3126 readonly MediaPlayerElement mediaPlayerElement ;
32- // Cannot be static readonly because we need to be able to add icon to multiple instances of the button
33- readonly FontIcon fullScreenIcon = new ( ) { Glyph = "\uE740 " , FontFamily = new FontFamily ( "Segoe Fluent Icons" ) } ;
34- readonly FontIcon exitFullScreenIcon = new ( ) { Glyph = "\uE73F " , FontFamily = new FontFamily ( "Segoe Fluent Icons" ) } ;
27+ readonly CustomTransportControls ? customTransportControls ;
3528 bool doesNavigationBarExistBeforeFullScreen ;
3629 bool isDisposed ;
3730
@@ -41,33 +34,73 @@ public partial class MauiMediaElement : Grid, IDisposable
4134 /// <param name="mediaPlayerElement"></param>
4235 public MauiMediaElement ( MediaPlayerElement mediaPlayerElement )
4336 {
37+ LoadResourceDictionary ( ) ;
4438 this . mediaPlayerElement = mediaPlayerElement ;
39+ customTransportControls = SetTransportControls ( ) ;
40+ Children . Add ( this . mediaPlayerElement ) ;
41+ }
4542
46- 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 )
4748 {
48- Content = fullScreenIcon ,
49- Background = new SolidColorBrush ( Colors . Transparent ) ,
50- Width = 45 ,
51- Height = 45
52- } ;
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+ }
5368
54- buttonContainer = new Grid
69+ CustomTransportControls SetTransportControls ( )
70+ {
71+ mediaPlayerElement . TransportControls . IsEnabled = false ;
72+ var temp = new CustomTransportControls ( )
5573 {
56- HorizontalAlignment = Microsoft . UI . Xaml . HorizontalAlignment . Right ,
57- VerticalAlignment = Microsoft . UI . Xaml . VerticalAlignment . Top ,
58- Visibility = mediaPlayerElement . TransportControls . Visibility ,
59- Width = 45 ,
60- Height = 45 ,
61- 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 ,
6292 } ;
63-
64- fullScreenButton . Click += OnFullScreenButtonClick ;
65- buttonContainer . Children . Add ( fullScreenButton ) ;
66-
67- Children . Add ( this . mediaPlayerElement ) ;
68- Children . Add ( buttonContainer ) ;
69-
70- 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 ;
71104 }
72105
73106 /// <summary>
@@ -99,9 +132,10 @@ protected virtual void Dispose(bool disposing)
99132 {
100133 return ;
101134 }
102-
103- fullScreenButton . Click -= OnFullScreenButtonClick ;
104- mediaPlayerElement . PointerMoved -= OnMediaPlayerElementPointerMoved ;
135+ if ( customTransportControls ? . FullScreenButton is not null )
136+ {
137+ customTransportControls . FullScreenButton . Click -= OnFullScreenButtonClick ;
138+ }
105139
106140 if ( disposing )
107141 {
@@ -128,29 +162,9 @@ static AppWindow GetAppWindowForCurrentWindow()
128162 return AppWindow . GetFromWindowId ( id ) ;
129163 }
130164
131- async void OnMediaPlayerElementPointerMoved ( object sender , PointerRoutedEventArgs e )
132- {
133- e . Handled = true ;
134- buttonContainer . Visibility = mediaPlayerElement . TransportControls . Visibility ;
135-
136- if ( mediaPlayerElement . TransportControls . Visibility == Microsoft . UI . Xaml . Visibility . Collapsed )
137- {
138- buttonContainer . Visibility = mediaPlayerElement . TransportControls . Visibility ;
139- return ;
140- }
141-
142- mediaPlayerElement . PointerMoved -= OnMediaPlayerElementPointerMoved ;
143- buttonContainer . Visibility = Microsoft . UI . Xaml . Visibility . Visible ;
144- await Task . Delay ( TimeSpan . FromSeconds ( 5 ) ) ;
145-
146- buttonContainer . Visibility = Microsoft . UI . Xaml . Visibility . Collapsed ;
147- mediaPlayerElement . PointerMoved += OnMediaPlayerElementPointerMoved ;
148- }
149-
150165 void OnFullScreenButtonClick ( object sender , RoutedEventArgs e )
151166 {
152167 var currentPage = CurrentPage ;
153-
154168 if ( appWindow . Presenter . Kind is AppWindowPresenterKind . FullScreen )
155169 {
156170 appWindow . SetPresenter ( AppWindowPresenterKind . Default ) ;
@@ -162,9 +176,7 @@ void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
162176 popup . Child = null ;
163177 fullScreenGrid . Children . Clear ( ) ;
164178 }
165- fullScreenButton . Content = fullScreenIcon ;
166179 Children . Add ( mediaPlayerElement ) ;
167- Children . Add ( buttonContainer ) ;
168180
169181 var parent = mediaPlayerElement . Parent as FrameworkElement ;
170182 mediaPlayerElement . Width = parent ? . Width ?? mediaPlayerElement . Width ;
@@ -181,9 +193,7 @@ void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
181193 mediaPlayerElement . Height = displayInfo . Height / displayInfo . Density ;
182194
183195 Children . Clear ( ) ;
184- fullScreenButton . Content = exitFullScreenIcon ;
185196 fullScreenGrid . Children . Add ( mediaPlayerElement ) ;
186- fullScreenGrid . Children . Add ( buttonContainer ) ;
187197
188198 popup . XamlRoot = mediaPlayerElement . XamlRoot ;
189199 popup . HorizontalOffset = 0 ;
0 commit comments