Skip to content

Commit 01fea0b

Browse files
Merge branch '1901-fix-snackbar' of https://github.com/CommunityToolkit/Maui into 1901-fix-snackbar
2 parents cb73515 + 9493c5d commit 01fea0b

File tree

8 files changed

+926
-64
lines changed

8 files changed

+926
-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.Core/Views/Alert/AlertView.macios.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public override void LayoutSubviews()
8484
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
8585
}
8686
}
87+
else
88+
{
89+
this.SafeBottomAnchor().ConstraintEqualTo(AnchorView.SafeTopAnchor(), -defaultSpacing).Active = true;
90+
}
8791
}
8892

8993
void ConstraintInParent(bool shouldFillAndExpandHorizontally)

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: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
using System.Reflection;
12
using CommunityToolkit.Maui.Extensions;
3+
using CommunityToolkit.Maui.Primitives;
24
using CommunityToolkit.Maui.Views;
35
using Microsoft.UI;
46
using Microsoft.UI.Windowing;
57
using Microsoft.UI.Xaml;
68
using Microsoft.UI.Xaml.Controls;
79
using Microsoft.UI.Xaml.Controls.Primitives;
8-
using Microsoft.UI.Xaml.Input;
9-
using Microsoft.UI.Xaml.Media;
10+
using Microsoft.UI.Xaml.Markup;
1011
using WinRT.Interop;
1112
using Application = Microsoft.Maui.Controls.Application;
12-
using Button = Microsoft.UI.Xaml.Controls.Button;
13-
using Colors = Microsoft.UI.Colors;
1413
using Grid = Microsoft.UI.Xaml.Controls.Grid;
1514
using Page = Microsoft.Maui.Controls.Page;
16-
using SolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;
17-
using Thickness = Microsoft.UI.Xaml.Thickness;
1815

1916
namespace 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;

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)