Skip to content

Commit

Permalink
Feature switch for IVisual scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrozsival committed Feb 6, 2024
1 parent a8681bf commit 6a91b01
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/design/FeatureSwitches.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Certain features of MAUI can be enabled or disabled using feature switches. The
| MSBuild Property Name | AppContext Setting | Description |
|-|-|-|
| MauiXamlRuntimeParsingSupport | Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported | When disabled, all XAML loading at runtime will throw an exception. This will affect usage of APIs such as the `LoadFromXaml` extension method. This feature can be safely turned off when all XAML resources are compiled using XamlC (see [XAML compilation](https://learn.microsoft.com/en-us/dotnet/maui/xaml/xamlc)). This feature is enabled by default for all configurations except for NativeAOT. |
| MauiIVisualScanningSupport | Microsoft.Maui.RuntimeFeature.IsIVisualScanningSupported | When disabled, only visuals manually registered through `[assembly: Visual(...)]` will be available at runtime. |

## MauiXamlRuntimeParsingSupport

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,17 @@
<Target Name="_MauiPrepareForILLink" BeforeTargets="PrepareForILLink">
<PropertyGroup>
<MauiXamlRuntimeParsingSupport Condition="'$(MauiXamlRuntimeParsingSupport)' == '' and '$(PublishAot)' == 'true'">false</MauiXamlRuntimeParsingSupport>
<MauiIVisualScanningSupport Condition="'$(MauiIVisualScanningSupport)' == '' and '$(PublishAot)' == 'true'">false</MauiIVisualScanningSupport>
</PropertyGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported"
Condition="'$(MauiXamlRuntimeParsingSupport)' != ''"
Value="$(MauiXamlRuntimeParsingSupport)"
Trim="true" />
<RuntimeHostConfigurationOption Include="Microsoft.Maui.RuntimeFeature.IsIVisualScanningSupported"
Condition="'$(MauiIVisualScanningSupport)' != ''"
Value="$(MauiIVisualScanningSupport)"
Trim="true" />
</ItemGroup>
</Target>

Expand Down
26 changes: 18 additions & 8 deletions src/Controls/src/Core/Visuals/VisualTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ void InitMappings()
var mappings = new Dictionary<string, IVisual>(StringComparer.OrdinalIgnoreCase);
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

// Check for IVisual Types
foreach (var assembly in assemblies)
Register(assembly, mappings);

if (Internals.Registrar.ExtraAssemblies != null)
foreach (var assembly in Internals.Registrar.ExtraAssemblies)
Register(assembly, mappings);

if (RuntimeFeature.IsIVisualScanningSupported)
{
// Check for IVisual Types
ScanAssemblies(assemblies, mappings);
}

// Check for visual assembly attributes after scanning for IVisual Types
// this will let users replace the default visual names if they want to
Expand All @@ -49,6 +46,19 @@ void InitMappings()
_visualTypeMappings = mappings;
}

[RequiresUnreferencedCode("The IVisual types might be removed by trimming. "
+ "Use the [assembly: Visual(\"key\", typeof(MyIVisual))] attribute to explicitly register the IVisual types instead.",
Url = "TODO")]
static void ScanAssemblies(Assembly[] assemblies, Dictionary<string, IVisual> mappings)
{
foreach (var assembly in assemblies)
Register(assembly, mappings);

if (Internals.Registrar.ExtraAssemblies != null)
foreach (var assembly in Internals.Registrar.ExtraAssemblies)
Register(assembly, mappings);
}

static void RegisterFromAttributes(Assembly assembly, Dictionary<string, IVisual> mappings)
{
object[] attributes = assembly.GetCustomAttributesSafe(typeof(VisualAttribute));
Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/ILLink.Substitutions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<type fullname="Microsoft.Maui.RuntimeFeature">
<method signature="System.Boolean get_IsXamlRuntimeParsingSupported()" body="stub" feature="Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported" value="false" featurevalue="false" />
<method signature="System.Boolean get_IsXamlRuntimeParsingSupported()" body="stub" feature="Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported" value="true" featurevalue="true" />
<method signature="System.Boolean get_IsIVisualScanningSupported()" body="stub" feature="Microsoft.Maui.RuntimeFeature.IsIVisualScanningSupported" value="false" featurevalue="false" />
<method signature="System.Boolean get_IsIVisualScanningSupported()" body="stub" feature="Microsoft.Maui.RuntimeFeature.IsIVisualScanningSupported" value="true" featurevalue="true" />
</type>
</assembly>
</linker>
6 changes: 6 additions & 0 deletions src/Core/src/RuntimeFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ namespace Microsoft.Maui
internal static class RuntimeFeature
{
private const bool IsXamlRuntimeParsingSupportedByDefault = true;
private const bool IsIVisualScanningSupportedByDefault = true;

internal static bool IsXamlRuntimeParsingSupported
=> AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported", out bool isEnabled)
? isEnabled
: IsXamlRuntimeParsingSupportedByDefault;

internal static bool IsIVisualScanningSupported =>
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsIVisualScanningSupported", out bool isSupported)
? isSupported
: IsIVisualScanningSupportedByDefault;
}
}

0 comments on commit 6a91b01

Please sign in to comment.