Skip to content

Commit

Permalink
feat(styles): Allow native style usage to be overridden per control
Browse files Browse the repository at this point in the history
Add a configuration hook to allow individual control types to opt in/out of using native styling.

This configuration hook is intended to more easily support the most common usage pattern amongst published Uno apps: the UWP styling is used by default for most controls, but the native style is used for a few controls which are optimized for the native style (eg Frame, CommandBar).
  • Loading branch information
davidjohnoliver committed Jun 3, 2020
1 parent d107c70 commit b0f8f86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Uno.UI/FeatureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ public static class Style
/// a native counterpart. (e.g. Button, Slider, ComboBox, ...)
/// </summary>
public static bool UseUWPDefaultStyles { get; set; } = true;

/// <summary>
/// Override the native styles usage per control type.
/// </summary>
/// <remarks>
/// Usage: 'UseUWPDefaultStylesOverride[typeof(Frame)] = false;' will result in the native style always being the default for Frame, irrespective
/// of the value of <see cref="UseUWPDefaultStyles"/>. This is useful when an app uses the UWP default look for most controls but the native
/// appearance/comportment for a few particular controls, or vice versa.
/// </remarks>
public static IDictionary<Type, bool> UseUWPDefaultStylesOverride { get; } = new Dictionary<Type, bool>();
}

public static class TextBlock
Expand Down
12 changes: 11 additions & 1 deletion src/Uno.UI/UI/Xaml/Style/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static void RegisterDefaultStyleForType(Type type, StyleProviderHandler s
/// <summary>
/// Returns the default Style for given type.
/// </summary>
internal static Style GetDefaultStyleForType(Type type) => GetDefaultStyleForType(type, Uno.UI.FeatureConfiguration.Style.UseUWPDefaultStyles);
internal static Style GetDefaultStyleForType(Type type) => GetDefaultStyleForType(type, ShouldUseUWPDefaultStyle(type));

private static Style GetDefaultStyleForType(Type type, bool useUWPDefaultStyles)
{
Expand Down Expand Up @@ -189,5 +189,15 @@ private static Style GetDefaultStyleForType(Type type, bool useUWPDefaultStyles)

return style;
}

private static bool ShouldUseUWPDefaultStyle(Type type)
{
if (type != null && FeatureConfiguration.Style.UseUWPDefaultStylesOverride.TryGetValue(type, out var value))
{
return value;
}

return FeatureConfiguration.Style.UseUWPDefaultStyles;
}
}
}

0 comments on commit b0f8f86

Please sign in to comment.