Browse Source

fix reseting actual theme variant

pull/16340/head
Emmanuel Hansen 1 year ago
parent
commit
ce22e0824c
  1. 31
      samples/SafeAreaDemo/ViewModels/MainViewModel.cs
  2. 6
      samples/SafeAreaDemo/Views/MainView.xaml
  3. 12
      src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs
  4. 2
      src/Avalonia.Base/Styling/ThemeVariant.cs
  5. 22
      src/Avalonia.Controls/Application.cs
  6. 2
      src/Avalonia.Controls/TopLevel.cs

31
samples/SafeAreaDemo/ViewModels/MainViewModel.cs

@ -3,6 +3,7 @@ using Avalonia;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.Styling;
using MiniMvvm;
namespace SafeAreaDemo.ViewModels
@ -33,7 +34,7 @@ namespace SafeAreaDemo.ViewModels
public Rect InputPaneRect => _inputPane?.OccludedRect ?? default;
public Rect CanvasSize { get; set; }
public Thickness SafeAreaPadding
{
get
@ -105,7 +106,7 @@ namespace SafeAreaDemo.ViewModels
set
{
_autoSafeAreaPadding = value;
RaisePropertyChanged();
RaiseSafeAreaChanged();
}
@ -159,7 +160,7 @@ namespace SafeAreaDemo.ViewModels
this.RaisePropertyChanged(nameof(ViewPadding));
this.RaisePropertyChanged(nameof(InputPaneMarkerMargin));
}
private void RaiseKeyboardChanged()
{
this.RaisePropertyChanged(nameof(InputPaneState));
@ -168,5 +169,29 @@ namespace SafeAreaDemo.ViewModels
this.RaisePropertyChanged(nameof(InputPaneDuration));
this.RaisePropertyChanged(nameof(InputPaneMarkerMargin));
}
public void SetLight()
{
if (Application.Current is { } application)
{
application.RequestedThemeVariant = ThemeVariant.Light;
}
}
public void SetDark()
{
if (Application.Current is { } application)
{
application.RequestedThemeVariant = ThemeVariant.Dark;
}
}
public void SetDefault()
{
if (Application.Current is { } application)
{
application.RequestedThemeVariant = ThemeVariant.Default;
}
}
}
}

6
samples/SafeAreaDemo/Views/MainView.xaml

@ -39,6 +39,7 @@
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<StackPanel Orientation="Vertical"
Spacing="5"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Label HorizontalAlignment="Left">Options:</Label>
@ -47,6 +48,11 @@
<CheckBox IsChecked="{Binding AutoSafeAreaPadding}">Automatic Paddings</CheckBox>
<CheckBox IsChecked="{Binding HideSystemBars}">Hide System Bars</CheckBox>
<TextBox Width="200" Watermark="Tap to Show Keyboard"/>
<StackPanel Orientation="Horizontal" Spacing="10">
<Button Command="{Binding SetLight}">Light</Button>
<Button Command="{Binding SetDark}">Dark</Button>
<Button Command="{Binding SetDefault}">Default</Button>
</StackPanel>
</StackPanel>
</Grid>
</DockPanel>

12
src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs

@ -9,6 +9,7 @@ using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using AndroidX.AppCompat.App;
using AndroidX.Core.View;
using Avalonia.Android.Platform.Input;
using Avalonia.Android.Platform.Specific;
using Avalonia.Android.Platform.Specific.Helpers;
@ -281,10 +282,17 @@ namespace Avalonia.Android.Platform.SkiaPlatform
};
}
AppCompatDelegate.DefaultNightMode = themeVariant == null ? AppCompatDelegate.ModeNightFollowSystem :
// Sets the default app NightMode to AppCompatDelegate.ModeNightFollowSystem when themeVariant is null. This
// allows app to follow the current OS night mode. Using either ModeNightNo or ModeNightYes will force the app
// to use one night mode, ignoring the system's configuration and preventing us from detecting system theme changes
// in View.OnConfigurationChanged. In this case, only Activity.OnConfigurationChanged is called when system theme is
// changed, but we don't have access to that method if the toplevel view is embedded in a custom activity
var nightMode = themeVariant == null ? AppCompatDelegate.ModeNightFollowSystem :
themeVariant == PlatformThemeVariant.Light ? AppCompatDelegate.ModeNightNo : AppCompatDelegate.ModeNightYes;
if (AppCompatDelegate.DefaultNightMode == AppCompatDelegate.ModeNightFollowSystem && _view.Context is { } context
AppCompatDelegate.DefaultNightMode = nightMode;
if (nightMode == AppCompatDelegate.ModeNightFollowSystem && _view.Context is { } context
&& context.Resources?.Configuration is { } config)
{
var settings =

2
src/Avalonia.Base/Styling/ThemeVariant.cs

@ -100,7 +100,7 @@ public sealed record ThemeVariant
{
PlatformThemeVariant.Light => Light,
PlatformThemeVariant.Dark => Dark,
_ => Default,
_ => throw new ArgumentOutOfRangeException(nameof(themeVariant), themeVariant, null),
};
}

22
src/Avalonia.Controls/Application.cs

@ -54,7 +54,7 @@ namespace Avalonia
/// <inheritdoc cref="ThemeVariantScope.ActualThemeVariantProperty" />
public static readonly StyledProperty<ThemeVariant> ActualThemeVariantProperty =
ThemeVariantScope.ActualThemeVariantProperty.AddOwner<Application>();
/// <inheritdoc cref="ThemeVariantScope.RequestedThemeVariantProperty" />
public static readonly StyledProperty<ThemeVariant?> RequestedThemeVariantProperty =
ThemeVariantScope.RequestedThemeVariantProperty.AddOwner<Application>();
@ -106,7 +106,7 @@ namespace Avalonia
get => GetValue(RequestedThemeVariantProperty);
set => SetValue(RequestedThemeVariantProperty, value);
}
/// <inheritdoc />
[System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1031", Justification = "This property is supposed to be a styled readonly property.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1030", Justification = "False positive.")]
@ -216,7 +216,7 @@ namespace Avalonia
/// as specific top levels might have different settings set-up.
/// </remarks>
public IPlatformSettings? PlatformSettings => this.TryGetFeature<IPlatformSettings>();
event Action<IReadOnlyList<IStyle>>? IGlobalStyles.GlobalStylesAdded
{
add => _stylesAdded += value;
@ -233,7 +233,7 @@ namespace Avalonia
/// Initializes the application by loading XAML etc.
/// </summary>
public virtual void Initialize() { }
/// <inheritdoc/>
public bool TryGetResource(object key, ThemeVariant? theme, out object? value)
{
@ -286,7 +286,7 @@ namespace Avalonia
.Bind<IThemeVariantHost>().ToConstant(this)
.Bind<IFocusManager>().ToConstant(focusManager)
.Bind<IInputManager>().ToConstant(InputManager)
.Bind< IToolTipService>().ToConstant(new ToolTipService(InputManager))
.Bind<IToolTipService>().ToConstant(new ToolTipService(InputManager))
.Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
.Bind<IDragDropDevice>().ToConstant(DragDropDevice.Instance);
@ -304,10 +304,10 @@ namespace Avalonia
public virtual void OnFrameworkInitializationCompleted()
{
}
void IApplicationPlatformEvents.RaiseUrlsOpened(string[] urls)
{
UrlsOpened?.Invoke(this, new UrlOpenedEventArgs (urls));
UrlsOpened?.Invoke(this, new UrlOpenedEventArgs(urls));
}
private string? _name;
@ -361,13 +361,13 @@ namespace Avalonia
{
if (change.GetNewValue<ThemeVariant>() is { } themeVariant)
{
if (themeVariant == ThemeVariant.Default && PlatformSettings?.GetColorValues() is { } currentColors)
if (themeVariant == ThemeVariant.Default)
{
SetValue(ActualThemeVariantProperty, (ThemeVariant)currentColors.ThemeVariant);
ClearValue(ActualThemeVariantProperty);
}
else
{
SetValue(ActualThemeVariantProperty, themeVariant);
SetValue(ActualThemeVariantProperty, themeVariant);
}
}
}
@ -380,7 +380,7 @@ namespace Avalonia
private void OnColorValuesChanged(object? sender, PlatformColorValues e)
{
if ((RequestedThemeVariant ?? ThemeVariant.Default) == ThemeVariant.Default)
SetCurrentValue(ActualThemeVariantProperty, (ThemeVariant)e.ThemeVariant);
SetValue(ActualThemeVariantProperty, (ThemeVariant)e.ThemeVariant);
}
}
}

2
src/Avalonia.Controls/TopLevel.cs

@ -929,7 +929,7 @@ namespace Avalonia.Controls
private void GlobalActualThemeVariantChanged(object? sender, EventArgs e)
{
SetCurrentValue(ActualThemeVariantProperty, ((IThemeVariantHost)sender!).ActualThemeVariant);
SetValue(ActualThemeVariantProperty, ((IThemeVariantHost)sender!).ActualThemeVariant, BindingPriority.Template);
}
private void SceneInvalidated(object? sender, SceneInvalidatedEventArgs e)

Loading…
Cancel
Save