Skip to content

Commit

Permalink
Merge pull request #68 from AvaloniaUtils/reworkColorsPicking
Browse files Browse the repository at this point in the history
Replace MultiDynamicResourceExtension with ResourceProvider for Background lookup
  • Loading branch information
SKProCH authored Sep 30, 2024
2 parents 61f074f + cf8660f commit e55d78c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 102 deletions.
7 changes: 2 additions & 5 deletions DialogHost.Avalonia/DialogHost.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="DialogMargin" Value="8" />
<Setter Property="OverlayBackground"
Value="{utilities:MultiDynamicResource DefaultBrush=Black, ResourceKeys=SystemControlBackgroundBaseHighBrush}" />
<Setter Property="Background"
Value="{utilities:MultiDynamicResource DefaultBrush=Black, ResourceKeys=SystemControlBackgroundAltHighBrush;MaterialDesignPaper;MaterialPaperBrush}">
</Setter>
<Setter Property="OverlayBackground" Value="{DynamicResource DialogHostOverlayBackgroundMixinBrush}" />
<Setter Property="Background" Value="{DynamicResource DialogHostBackgroundMixinBrush}" />
<Setter Property="dialogHostAvalonia:DialogHostStyle.ClipToBounds" Value="False" />
<Setter Property="dialogHostAvalonia:DialogHostStyle.CornerRadius" Value="2" />
<Setter Property="Template">
Expand Down
58 changes: 56 additions & 2 deletions DialogHost.Avalonia/DialogHostStyles.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Avalonia.Controls;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Styling;

namespace DialogHostAvalonia {
Expand All @@ -19,7 +22,7 @@ namespace DialogHostAvalonia {
/// &lt;/Application&gt;
/// </code>
/// </remarks>
public class DialogHostStyles : Styles {
public class DialogHostStyles : Styles, IResourceNode {
/// <inheritdoc />
public DialogHostStyles() {
AvaloniaXamlLoader.Load(this);
Expand All @@ -29,5 +32,56 @@ public DialogHostStyles() {
public DialogHostStyles(IResourceHost owner) : base(owner) {
AvaloniaXamlLoader.Load(this);
}

/// <summary>
/// Lists of resource keys which will be used as defaults for DialogHost.Background property
/// </summary>
public static IReadOnlyList<string> BackgroundColorKeys { get; } = ["SystemControlPageBackgroundChromeLowBrush"];

/// <summary>
/// Lists of resource keys which will be used as defaults for DialogHost.OverlayBackground property
/// </summary>
public static IReadOnlyList<string> OverlayBackgroundColorKeys { get; } =
["SystemControlBackgroundAltHighBrush", "MaterialDesignPaper", "MaterialPaperBrush"];

bool IResourceNode.HasResources => true;

/// <summary>
/// Tries to find a resource within the object.
/// </summary>
/// <param name="key">The resource key.</param>
/// <param name="theme">Theme used to select theme dictionary.</param>
/// <param name="value">
/// When this method returns, contains the value associated with the specified key,
/// if the key is found; otherwise, null.
/// </param>
/// <returns>
/// True if the resource if found, otherwise false.
/// </returns>
bool IResourceNode.TryGetResource(object key, ThemeVariant? theme, out object? value) {
if (key is "DialogHostBackgroundMixinBrush") {
foreach (var colorKey in BackgroundColorKeys) {
if (Application.Current!.TryGetResource(colorKey, theme, out value)) {
return true;
}
}

value = Brushes.Black;
return true;
}

if (key is "DialogHostOverlayBackgroundMixinBrush") {
foreach (var colorKey in OverlayBackgroundColorKeys) {
if (Application.Current!.TryGetResource(colorKey, theme, out value)) {
return true;
}
}

value = Brushes.Black;
return true;
}

return base.TryGetResource(key, theme, out value);
}
}
}
95 changes: 0 additions & 95 deletions DialogHost.Avalonia/Utilities/MultiDynamicResourceExtension.cs

This file was deleted.

0 comments on commit e55d78c

Please sign in to comment.