Skip to content

Commit 46688c0

Browse files
authored
Merge branch 'main' into FixCamerPermissionsBug
2 parents 95abf08 + 6a0cca4 commit 46688c0

File tree

7 files changed

+137
-135
lines changed

7 files changed

+137
-135
lines changed

samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
<PackageReference Include="Microsoft.Maui.Controls" Version="*" />
6262
<PackageReference Include="CommunityToolkit.Maui.Markup" Version="7.0.1" />
63-
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.1" />
63+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
6464
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.4.0" />
6565
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.5" />
6666
</ItemGroup>

samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/StatusBarBehaviorPage.xaml

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2+
23
<pages:BasePage
34
x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.StatusBarBehaviorPage"
45
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
@@ -118,9 +119,9 @@
118119
Value="{Binding BlueSliderValue}" />
119120
<HorizontalStackLayout>
120121
<Label Text="Alpha: " />
121-
<Label Text="{Binding AlphaSliderValue}"/>
122+
<Label Text="{Binding AlphaSliderValue}" />
122123
</HorizontalStackLayout>
123-
124+
124125
<Stepper
125126
HorizontalOptions="Start"
126127
Margin="20,0"
@@ -130,36 +131,47 @@
130131
Value="{Binding AlphaSliderValue}" />
131132

132133
<Label Text="Select StatusBar and NavigationBar style" />
133-
134-
<HorizontalStackLayout Spacing="15">
135134

136-
137-
138-
<RadioButton
139-
Content="Default"
140-
IsChecked="{Binding IsDefaultChecked}"
141-
VerticalOptions="Center" />
135+
<HorizontalStackLayout Spacing="15">
136+
<RadioButton IsChecked="{Binding IsDefaultChecked}"
137+
VerticalOptions="Center">
138+
<RadioButton.Content>
139+
<Label
140+
Margin="10,0,0,0"
141+
Text="Default"
142+
VerticalOptions="Center"
143+
VerticalTextAlignment="Center" />
144+
</RadioButton.Content>
145+
</RadioButton>
142146

143-
<RadioButton VerticalOptions="Center" IsChecked="{Binding IsLightContentChecked}">
147+
<RadioButton IsChecked="{Binding IsLightContentChecked}"
148+
VerticalOptions="Center">
144149
<RadioButton.Content>
145-
<Label Text="Light Content" Margin="10,0,0,0" VerticalTextAlignment="Center"
146-
VerticalOptions="Center" />
150+
<Label
151+
Margin="10,0,0,0"
152+
Text="Light&#10;Content"
153+
VerticalOptions="Center"
154+
VerticalTextAlignment="Center" />
147155
</RadioButton.Content>
148156
</RadioButton>
149157

150-
<RadioButton IsChecked="{Binding IsDarkContentChecked}">
158+
<RadioButton IsChecked="{Binding IsDarkContentChecked}"
159+
VerticalOptions="Center">
151160
<RadioButton.Content>
152-
<Label Text="Dark Content" Margin="10,0,0,0" VerticalTextAlignment="Center"
153-
VerticalOptions="Center" />
161+
<Label
162+
Margin="10,0,0,0"
163+
Text="Dark&#10;Content"
164+
VerticalOptions="Center"
165+
VerticalTextAlignment="Center" />
154166
</RadioButton.Content>
155167
</RadioButton>
156168

157169
</HorizontalStackLayout>
158-
170+
159171
<Button
160172
x:Name="ModalPageButton"
161173
HorizontalOptions="Center"
162-
VerticalOptions="Center"/>
174+
VerticalOptions="Center" />
163175
</VerticalStackLayout>
164176
</ScrollView>
165177
</pages:BasePage>

samples/CommunityToolkit.Maui.Sample/Pages/PlatformSpecific/NavigationBarPage.xaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@
108108
ThumbColor="Blue"
109109
Value="{Binding BlueSliderValue}" />
110110

111-
<VerticalStackLayout Spacing="15">
112-
<Label Text="Select NavigationBar style" />
111+
<Label Text="Select NavigationBar style" />
112+
<HorizontalStackLayout Spacing="15">
113113

114114
<RadioButton IsChecked="{Binding IsDefaultChecked}" VerticalOptions="Center">
115115
<RadioButton.Content>
@@ -125,7 +125,7 @@
125125
<RadioButton.Content>
126126
<Label
127127
Margin="10,0,0,0"
128-
Text="Light Content"
128+
Text="Light&#10;Content"
129129
VerticalOptions="Center"
130130
VerticalTextAlignment="Center" />
131131
</RadioButton.Content>
@@ -135,12 +135,12 @@
135135
<RadioButton.Content>
136136
<Label
137137
Margin="10,0,0,0"
138-
Text="Dark Content"
138+
Text="Dark&#10;Content"
139139
VerticalOptions="Center"
140140
VerticalTextAlignment="Center" />
141141
</RadioButton.Content>
142142
</RadioButton>
143-
</VerticalStackLayout>
143+
</HorizontalStackLayout>
144144
</VerticalStackLayout>
145145
</ScrollView>
146146
</pages:BasePage>

samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/StatusBarBehaviorViewModel.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,54 @@ namespace CommunityToolkit.Maui.Sample.ViewModels.Behaviors;
55

66
public partial class StatusBarBehaviorViewModel : BaseViewModel
77
{
8+
public Color StatusBarColor => Color.FromRgba(RedSliderValue, GreenSliderValue, BlueSliderValue, AlphaSliderValue);
9+
10+
public StatusBarStyle StatusBarStyle
11+
{
12+
get
13+
{
14+
if (IsDefaultChecked)
15+
{
16+
return StatusBarStyle.Default;
17+
}
18+
if (IsLightContentChecked)
19+
{
20+
return StatusBarStyle.LightContent;
21+
}
22+
if (IsDarkContentChecked)
23+
{
24+
return StatusBarStyle.DarkContent;
25+
}
26+
27+
throw new NotSupportedException($"{nameof(StatusBarStyle)} type is not supported.");
28+
}
29+
}
30+
831
[ObservableProperty]
932
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
10-
public partial double RedSliderValue { get; set; }
33+
public partial double RedSliderValue { get; set; } = 0.5;
1134

1235
[ObservableProperty]
1336
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
14-
public partial double GreenSliderValue { get; set; }
37+
public partial double GreenSliderValue { get; set; } = 0.5;
1538

1639
[ObservableProperty]
1740
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
18-
public partial double BlueSliderValue { get; set; }
41+
public partial double BlueSliderValue { get; set; } = 0.5;
1942

2043
[ObservableProperty]
2144
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
2245
public partial double AlphaSliderValue { get; set; } = 1;
2346

2447
[ObservableProperty]
2548
[NotifyPropertyChangedFor(nameof(StatusBarStyle))]
26-
public partial bool IsLightContentChecked { get; set; } = true;
49+
public partial bool IsLightContentChecked { get; set; }
2750

2851
[ObservableProperty]
2952
[NotifyPropertyChangedFor(nameof(StatusBarStyle))]
30-
public partial bool IsDarkContentChecked { get; set; } = true;
53+
public partial bool IsDarkContentChecked { get; set; }
3154

3255
[ObservableProperty]
3356
[NotifyPropertyChangedFor(nameof(StatusBarStyle))]
3457
public partial bool IsDefaultChecked { get; set; } = true;
35-
36-
public Color StatusBarColor => Color.FromRgba(RedSliderValue, GreenSliderValue, BlueSliderValue, AlphaSliderValue);
37-
38-
public StatusBarStyle StatusBarStyle
39-
{
40-
get
41-
{
42-
if (IsDefaultChecked)
43-
{
44-
return StatusBarStyle.Default;
45-
}
46-
if (IsLightContentChecked)
47-
{
48-
return StatusBarStyle.LightContent;
49-
}
50-
if (IsDarkContentChecked)
51-
{
52-
return StatusBarStyle.DarkContent;
53-
}
54-
55-
throw new NotSupportedException($"{nameof(StatusBarStyle)} {StatusBarStyle} is not supported.");
56-
}
57-
}
5858
}
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using Android.Views;
2+
using Microsoft.Maui.Platform;
3+
using Activity = Android.App.Activity;
4+
using DialogFragment = AndroidX.Fragment.App.DialogFragment;
25

36
namespace CommunityToolkit.Maui.Core.Extensions;
47

@@ -8,16 +11,34 @@ namespace CommunityToolkit.Maui.Core.Extensions;
811
public static class AndroidWindowExtensions
912
{
1013
/// <summary>
11-
/// Gets the current window associated with the specified activity.
14+
/// Gets the current visible window associated with the specified activity.
1215
/// </summary>
1316
/// <param name="activity">The activity.</param>
1417
/// <returns>The current window.</returns>
1518
/// <exception cref="InvalidOperationException">Thrown when the activity window is null.</exception>
1619
public static Window GetCurrentWindow(this Activity activity)
1720
{
18-
var window = activity.Window ?? throw new InvalidOperationException($"{nameof(activity.Window)} cannot be null");
19-
window.ClearFlags(WindowManagerFlags.TranslucentStatus);
20-
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
21-
return window;
21+
Window? currentWindow = null;
22+
23+
var fragmentManager = activity.GetFragmentManager();
24+
if (fragmentManager is not null && fragmentManager.Fragments.OfType<DialogFragment>().Any())
25+
{
26+
var fragments = fragmentManager.Fragments;
27+
for (var i = fragments.Count - 1; i >= 0; i--)
28+
{
29+
if (fragments[i] is DialogFragment { Dialog: { IsShowing: true, Window: not null }, IsVisible: true } dialogFragment)
30+
{
31+
currentWindow = dialogFragment.Dialog.Window;
32+
break;
33+
}
34+
}
35+
}
36+
37+
currentWindow ??= activity.Window ?? throw new InvalidOperationException($"{nameof(activity.Window)} cannot be null");
38+
39+
currentWindow.ClearFlags(WindowManagerFlags.TranslucentStatus);
40+
currentWindow.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
41+
42+
return currentWindow;
2243
}
2344
}

src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,57 +35,58 @@ static void PlatformSetColor(Color color)
3535
return;
3636
}
3737

38-
if (Activity.Window is not null)
38+
if (Activity.GetCurrentWindow() is not Window { DecorView.RootView: not null } window)
3939
{
40-
var platformColor = color.ToPlatform();
40+
return;
41+
}
4142

42-
if (OperatingSystem.IsAndroidVersionAtLeast(35))
43-
{
44-
const string statusBarOverlayTag = "StatusBarOverlay";
43+
var platformColor = color.ToPlatform();
4544

46-
var window = Activity.GetCurrentWindow();
45+
if (OperatingSystem.IsAndroidVersionAtLeast(35))
46+
{
47+
const string statusBarOverlayTag = "StatusBarOverlay";
4748

48-
var decorGroup = (ViewGroup)window.DecorView;
49-
var statusBarOverlay = decorGroup.FindViewWithTag(statusBarOverlayTag);
49+
var decorGroup = (ViewGroup)window.DecorView.RootView;
50+
var statusBarOverlay = decorGroup.FindViewWithTag(statusBarOverlayTag);
5051

51-
if (statusBarOverlay is null)
52-
{
53-
var statusBarHeight = Activity.Resources?.GetIdentifier("status_bar_height", "dimen", "android") ?? 0;
54-
var statusBarPixelSize = statusBarHeight > 0 ? Activity.Resources?.GetDimensionPixelSize(statusBarHeight) ?? 0 : 0;
52+
if (statusBarOverlay is null)
53+
{
54+
var statusBarHeight = Activity.Resources?.GetIdentifier("status_bar_height", "dimen", "android") ?? 0;
55+
var statusBarPixelSize = statusBarHeight > 0 ? Activity.Resources?.GetDimensionPixelSize(statusBarHeight) ?? 0 : 0;
5556

56-
statusBarOverlay = new(Activity)
57+
statusBarOverlay = new(Activity)
58+
{
59+
LayoutParameters = new FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, statusBarPixelSize + 3)
5760
{
58-
LayoutParameters = new FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, statusBarPixelSize + 3)
59-
{
60-
Gravity = GravityFlags.Top
61-
}
62-
};
63-
64-
decorGroup.AddView(statusBarOverlay);
65-
statusBarOverlay.SetZ(0);
66-
}
61+
Gravity = GravityFlags.Top
62+
}
63+
};
6764

68-
statusBarOverlay.SetBackgroundColor(platformColor);
69-
}
70-
else
71-
{
72-
Activity.Window.SetStatusBarColor(platformColor);
65+
statusBarOverlay.Tag = statusBarOverlayTag;
66+
decorGroup.AddView(statusBarOverlay);
67+
statusBarOverlay.SetZ(0);
7368
}
7469

75-
bool isColorTransparent = platformColor == PlatformColor.Transparent;
76-
if (isColorTransparent)
77-
{
78-
Activity.Window.ClearFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
79-
Activity.Window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
80-
}
81-
else
82-
{
83-
Activity.Window.ClearFlags(WindowManagerFlags.LayoutNoLimits);
84-
Activity.Window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
85-
}
70+
statusBarOverlay.SetBackgroundColor(platformColor);
71+
}
72+
else
73+
{
74+
window.SetStatusBarColor(platformColor);
75+
}
8676

87-
WindowCompat.SetDecorFitsSystemWindows(Activity.Window, !isColorTransparent);
77+
bool isColorTransparent = platformColor == PlatformColor.Transparent;
78+
if (isColorTransparent)
79+
{
80+
window.ClearFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
81+
window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
8882
}
83+
else
84+
{
85+
window.ClearFlags(WindowManagerFlags.LayoutNoLimits);
86+
window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
87+
}
88+
89+
WindowCompat.SetDecorFitsSystemWindows(window, !isColorTransparent);
8990
}
9091

9192
static void PlatformSetStyle(StatusBarStyle style)
@@ -98,23 +99,23 @@ static void PlatformSetStyle(StatusBarStyle style)
9899
switch (style)
99100
{
100101
case StatusBarStyle.DarkContent:
101-
SetStatusBarAppearance(Activity, true);
102+
SetStatusBarAppearance(true);
102103
break;
103104

104105
case StatusBarStyle.Default:
105106
case StatusBarStyle.LightContent:
106-
SetStatusBarAppearance(Activity, false);
107+
SetStatusBarAppearance(false);
107108
break;
108109

109110
default:
110111
throw new NotSupportedException($"{nameof(StatusBarStyle)} {style} is not yet supported on Android");
111112
}
112113
}
113114

114-
static void SetStatusBarAppearance(Activity activity, bool isLightStatusBars)
115+
static void SetStatusBarAppearance(bool isLightStatusBars)
115116
{
116-
var window = activity.GetCurrentWindow();
117-
if (WindowCompat.GetInsetsController(window, window.DecorView) is WindowInsetsControllerCompat windowController)
117+
if (Activity.GetCurrentWindow() is Window window
118+
&& WindowCompat.GetInsetsController(window, window.DecorView) is WindowInsetsControllerCompat windowController)
118119
{
119120
windowController.AppearanceLightStatusBars = isLightStatusBars;
120121
}

0 commit comments

Comments
 (0)