Skip to content

Commit

Permalink
Update API layer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Aug 13, 2022
1 parent beee38f commit dd2f821
Show file tree
Hide file tree
Showing 29 changed files with 240 additions and 229 deletions.
4 changes: 2 additions & 2 deletions samples/ControlCatalog/Pages/ThemePage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</ResourceDictionary>
</UserControl.Resources>

<ThemeControl x:Name="ThemeControl">
<ThemeVariantScope x:Name="ThemeVariantScope">
<Border Background="{DynamicResource DemoBackground}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Expand All @@ -75,5 +75,5 @@
<Button Grid.Column="1" Grid.Row="6" Content="Login" HorizontalAlignment="Stretch" />
</Grid>
</Border>
</ThemeControl>
</ThemeVariantScope>
</UserControl>
7 changes: 4 additions & 3 deletions samples/ControlCatalog/Pages/ThemePage.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;

namespace ControlCatalog.Pages
{
Expand All @@ -13,7 +14,7 @@ public ThemePage()
AvaloniaXamlLoader.Load(this);

var selector = this.FindControl<ComboBox>("Selector")!;
var themeControl = this.FindControl<ThemeControl>("ThemeControl")!;
var themeVariantScope = this.FindControl<ThemeVariantScope>("ThemeVariantScope")!;

selector.Items = new[]
{
Expand All @@ -29,11 +30,11 @@ public ThemePage()
var theme = (ThemeVariant)selector.SelectedItem!;
if ((string)theme.Key == "Default")
{
themeControl.ClearValue(ThemeControl.ThemeVariantProperty);
themeVariantScope.ClearValue(ThemeVariantProperty);
}
else
{
themeControl.ThemeVariant = theme;
themeVariantScope.ThemeVariant = theme;
}
};
}
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Base/Controls/IResourceDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Avalonia.Styling;

#nullable enable

Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Base/Controls/IResourceNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Metadata;
using Avalonia.Styling;

namespace Avalonia.Controls
{
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Base/Controls/ResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Avalonia.Collections;
using Avalonia.Controls.Templates;
using Avalonia.Styling;

namespace Avalonia.Controls
{
Expand Down
39 changes: 18 additions & 21 deletions src/Avalonia.Base/Controls/ResourceNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Avalonia.Reactive;
using Avalonia.Styling;

#nullable enable

Expand Down Expand Up @@ -38,9 +39,7 @@ public static bool TryFindResource(this IResourceHost control, object key, out o
control = control ?? throw new ArgumentNullException(nameof(control));
key = key ?? throw new ArgumentNullException(nameof(key));

var theme = AvaloniaLocator.Current.GetService<IApplicationThemeHost>()?.ThemeVariant;

return control.TryFindResource(key, theme, out value);
return control.TryFindResource(key, null, out value);
}

/// <summary>
Expand Down Expand Up @@ -98,9 +97,7 @@ public static bool TryGetResource(this IResourceHost control, object key, out ob
control = control ?? throw new ArgumentNullException(nameof(control));
key = key ?? throw new ArgumentNullException(nameof(key));

var theme = AvaloniaLocator.Current.GetService<IApplicationThemeHost>()?.ThemeVariant;

return control.TryGetResource(key, theme, out value);
return control.TryGetResource(key, null, out value);
}

public static IObservable<object?> GetResourceObservable(
Expand Down Expand Up @@ -141,7 +138,7 @@ public ResourceObservable(IResourceHost target, object key, Func<object?, object
protected override void Initialize()
{
_target.ResourcesChanged += ResourcesChanged;
if (_target is IThemeStyleable themeStyleable)
if (_target is IStyleable themeStyleable)
{
themeStyleable.ThemeVariantChanged += ThemeVariantChanged;
}
Expand All @@ -150,7 +147,7 @@ protected override void Initialize()
protected override void Deinitialize()
{
_target.ResourcesChanged -= ResourcesChanged;
if (_target is IThemeStyleable themeStyleable)
if (_target is IStyleable themeStyleable)
{
themeStyleable.ThemeVariantChanged -= ThemeVariantChanged;
}
Expand All @@ -173,8 +170,8 @@ private void ThemeVariantChanged(object? sender, EventArgs e)

private object? GetValue()
{
if (!(_target is IThemeStyleable themeStyleable)
|| !themeStyleable.TryFindResource(_key, themeStyleable.ThemeVariant, out var value))
if (_target is not IStyleable themeStyleable
|| !_target.TryFindResource(_key, themeStyleable.ThemeVariant, out var value))
{
value = _target.FindResource(_key) ?? AvaloniaProperty.UnsetValue;
}
Expand Down Expand Up @@ -202,7 +199,7 @@ protected override void Initialize()
_target.OwnerChanged += OwnerChanged;
_owner = _target.Owner;

if (_owner is object)
if (_owner is not null)
{
_owner.ResourcesChanged += ResourcesChanged;
}
Expand All @@ -216,40 +213,40 @@ protected override void Deinitialize()

protected override void Subscribed(IObserver<object?> observer, bool first)
{
if (_target.Owner is object)
if (_target.Owner is not null)
{
observer.OnNext(GetValue());
}
}

private void PublishNext()
{
if (_target.Owner is object)
if (_target.Owner is not null)
{
PublishNext(GetValue());
}
}

private void OwnerChanged(object? sender, EventArgs e)
{
if (_owner is object)
if (_owner is not null)
{
_owner.ResourcesChanged -= ResourcesChanged;
}
if (_owner is IThemeStyleable themeStyleable)
if (_owner is IStyleable styleable)
{
themeStyleable.ThemeVariantChanged -= ThemeVariantChanged;
styleable.ThemeVariantChanged -= ThemeVariantChanged;
}

_owner = _target.Owner;

if (_owner is object)
if (_owner is not null)
{
_owner.ResourcesChanged += ResourcesChanged;
}
if (_owner is IThemeStyleable themeStyleable2)
if (_owner is IStyleable styleable2)
{
themeStyleable2.ThemeVariantChanged += ThemeVariantChanged;
styleable2.ThemeVariantChanged += ThemeVariantChanged;
}

PublishNext();
Expand All @@ -267,9 +264,9 @@ private void ThemeVariantChanged(object? sender, EventArgs e)

private object? GetValue()
{
if (!(_target.Owner is IThemeStyleable themeStyleable)
if (!(_target.Owner is IStyleable themeStyleable)
|| themeStyleable.ThemeVariant is null
|| !themeStyleable.TryFindResource(_key, themeStyleable.ThemeVariant, out var value))
|| !_target.Owner.TryFindResource(_key, themeStyleable.ThemeVariant, out var value))
{
value = _target.Owner?.FindResource(_key) ?? AvaloniaProperty.UnsetValue;
}
Expand Down
17 changes: 17 additions & 0 deletions src/Avalonia.Base/StyledElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public class StyledElement : Animatable, IDataContextProvider, IStyledElement, I
public static readonly StyledProperty<ControlTheme?> ThemeProperty =
AvaloniaProperty.Register<StyledElement, ControlTheme?>(nameof(Theme));

/// <summary>
/// Defines the <see cref="ThemeVariant"/> property.
/// </summary>
public static readonly StyledProperty<ThemeVariant> ThemeVariantProperty =
AvaloniaProperty.Register<StyledElement, ThemeVariant>(
nameof(ThemeVariant),
inherits: true,
defaultValue: ThemeVariant.Light);

private static readonly ControlTheme s_invalidTheme = new ControlTheme();
private int _initCount;
private string? _name;
Expand Down Expand Up @@ -246,6 +255,10 @@ public ControlTheme? Theme
set => SetValue(ThemeProperty, value);
}

ThemeVariant IStyleable.ThemeVariant => GetValue(ThemeVariantProperty);

public event EventHandler? ThemeVariantChanged;

/// <summary>
/// Gets the styled element's logical children.
/// </summary>
Expand Down Expand Up @@ -659,6 +672,10 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang

InvalidateStyles();
}
else if (change.Property == ThemeVariantProperty)
{
ThemeVariantChanged?.Invoke(this, EventArgs.Empty);
}
}

private static void DataContextNotifying(IAvaloniaObject o, bool updateStarted)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;

using Avalonia.Controls;

namespace Avalonia;
namespace Avalonia.Styling;

/// <summary>
/// Interface for elements that supports dynamic theme.
/// Interface for a host element with a root theme.
/// </summary>
public interface IThemeStyleable : IResourceHost
public interface IApplicationThemeVariantHost : IResourceHost
{
/// <summary>
/// Gets the UI theme that is used by the control (and its child elements) for resource determination.
Expand Down
10 changes: 10 additions & 0 deletions src/Avalonia.Base/Styling/IStyleable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public interface IStyleable : IAvaloniaObject, INamed
/// </summary>
ControlTheme? GetEffectiveTheme();

/// <summary>
/// Gets the UI theme that is used by the control (and its child elements) for resource determination.
/// </summary>
ThemeVariant ThemeVariant { get; }

/// <summary>
/// Raised when the theme is changed on the element or an ancestor of the element.
/// </summary>
event EventHandler? ThemeVariantChanged;

/// <summary>
/// Notifies the element that a style has been applied.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.ComponentModel;

namespace Avalonia;
namespace Avalonia.Styling;

[TypeConverter(typeof(ThemeVariantTypeConverter))]
public class ThemeVariant
public class ThemeVariant : IEquatable<ThemeVariant>
{
public ThemeVariant(object key)
{
Expand All @@ -31,8 +31,17 @@ public override int GetHashCode()

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is ThemeVariant theme && Key.Equals(theme.Key);
}

public bool Equals(ThemeVariant? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return Key.Equals(obj.Key);
}

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.ComponentModel;
using System.Globalization;

namespace Avalonia;
namespace Avalonia.Styling;

public class ThemeVariantTypeConverter : TypeConverter
{
Expand Down
9 changes: 0 additions & 9 deletions src/Avalonia.Base/Themes/IApplicationThemeHost.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/Avalonia.Controls/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Avalonia
/// method.
/// - Tracks the lifetime of the application.
/// </remarks>
public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemplates, IGlobalStyles, IApplicationThemeHost, IApplicationPlatformEvents
public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemplates, IGlobalStyles, IApplicationThemeVariantHost, IApplicationPlatformEvents
{
/// <summary>
/// The application-global data templates.
Expand All @@ -53,9 +53,9 @@ public class Application : AvaloniaObject, IDataContextProvider, IGlobalDataTemp
public static readonly StyledProperty<object?> DataContextProperty =
StyledElement.DataContextProperty.AddOwner<Application>();

/// <inheritdoc cref="ThemeControl.ThemeVariantProperty" />
/// <inheritdoc cref="StyledElement.ThemeVariantProperty" />
public static readonly StyledProperty<ThemeVariant> ThemeVariantProperty =
ThemeControl.ThemeVariantProperty.AddOwner<Application>();
StyledElement.ThemeVariantProperty.AddOwner<Application>();

/// <inheritdoc/>
public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged;
Expand Down Expand Up @@ -87,7 +87,7 @@ public object? DataContext
set { SetValue(DataContextProperty, value); }
}

/// <inheritdoc cref="ThemeControl.ThemeVariant" />
/// <inheritdoc cref="IStyleable.ThemeVariant" />
public ThemeVariant ThemeVariant
{
get => GetValue(ThemeVariantProperty);
Expand Down Expand Up @@ -245,7 +245,7 @@ public virtual void RegisterServices()
.Bind<IAccessKeyHandler>().ToTransient<AccessKeyHandler>()
.Bind<IGlobalDataTemplates>().ToConstant(this)
.Bind<IGlobalStyles>().ToConstant(this)
.Bind<IApplicationThemeHost>().ToConstant(this)
.Bind<IApplicationThemeVariantHost>().ToConstant(this)
.Bind<IFocusManager>().ToConstant(FocusManager)
.Bind<IInputManager>().ToConstant(InputManager)
.Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
Expand Down
9 changes: 1 addition & 8 deletions src/Avalonia.Controls/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ public FlowDirection FlowDirection
get => GetValue(FlowDirectionProperty);
set => SetValue(FlowDirectionProperty, value);
}

ThemeVariant IThemeStyleable.ThemeVariant => GetValue(ThemeControl.ThemeVariantProperty);
public event EventHandler? ThemeVariantChanged;


/// <summary>
/// Occurs when the user has completed a context input gesture, such as a right-click.
/// </summary>
Expand Down Expand Up @@ -551,10 +548,6 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}
}
}
else if (change.Property == ThemeControl.ThemeVariantProperty)
{
ThemeVariantChanged?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions src/Avalonia.Controls/IControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public interface IControl : IVisual,
ILayoutable,
IInputElement,
INamed,
IStyledElement,
IThemeStyleable
IStyledElement
{
new IControl? Parent { get; }
}
Expand Down
Loading

0 comments on commit dd2f821

Please sign in to comment.