Skip to content

Commit b763385

Browse files
CopilotPureWeen
andauthored
Disable AppThemeBinding source generation for .NET 10 (dotnet#33219)
- [x] Apply changes from PR dotnet#33101 to AppThemeBinding.cs (make class public for NET11+, make AppThemeResource public) - [x] Apply changes from PR dotnet#33101 to KnownMarkups.cs (replace helper methods with object initializers) - [x] Apply changes from PR dotnet#33101 to XamlGenerator.cs (remove AppThemeBindingHelpers generation) - [x] Apply changes from PR dotnet#33101 to test file (wrap with NET11_0_OR_GREATER) - [x] Apply changes from PR dotnet#33107 to KnownMarkups.cs (wrap method with NET11_0_OR_GREATER) - [x] Apply changes from PR dotnet#33107 to NodeSGExtensions.cs (wrap dictionary entry with NET11_0_OR_GREATER) - [x] Verify changes compile successfully - [x] Run relevant tests to ensure functionality (123 SourceGen tests passed, 34 AppTheme XAML tests passed) - [x] Code review completed (no issues found) - [x] Security scan completed (no vulnerabilities found) - [x] Remove unrelated HybridWebView.js.map file (per review feedback) <!-- START COPILOT CODING AGENT SUFFIX --> <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > Please apply the changes from dotnet#33101 and dotnet#33107 to the main branch </details> <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/dotnet/maui/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: PureWeen <[email protected]>
1 parent a14b855 commit b763385

File tree

5 files changed

+25
-222
lines changed

5 files changed

+25
-222
lines changed

src/Controls/src/Core/AppThemeBinding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
namespace Microsoft.Maui.Controls
88
{
99
#if NET11_0_OR_GREATER
10-
public // TODO: add to PublicAPI.Unshipped.txt once we start targeting net11
10+
public
1111
#endif
1212
class AppThemeBinding : BindingBase
1313
{
14-
internal const string AppThemeResource = "__MAUI_ApplicationTheme__";
14+
public const string AppThemeResource = "__MAUI_ApplicationTheme__";
1515
class AppThemeProxy : Element
1616
{
1717
public AppThemeProxy(Element parent, AppThemeBinding binding)

src/Controls/src/SourceGen/KnownMarkups.cs

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ internal static bool ProvideValueForReferenceExtension(ElementNode markupNode, I
674674
/// Provides value for AppThemeBindingExtension by generating an AppThemeBinding instance
675675
/// with Light, Dark, and Default properties set based on the markup extension's properties.
676676
/// </summary>
677+
#if NET11_0_OR_GREATER
677678
internal static bool ProvideValueForAppThemeBindingExtension(ElementNode node, IndentedTextWriter writer, SourceGenContext context, NodeSGExtensions.GetNodeValueDelegate? getNodeValue, out ITypeSymbol? returnType, out string value)
678679
{
679680
returnType = context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.AppThemeBinding")!;
@@ -757,61 +758,29 @@ internal static bool ProvideValueForAppThemeBindingExtension(ElementNode node, I
757758
// At least one value must be set
758759
if (lightValue is null && darkValue is null && defaultValue is null)
759760
{
760-
context.ReportDiagnostic(Diagnostic.Create(Descriptors.XamlParserError,
761-
LocationHelpers.LocationCreate(context.ProjectItem.RelativePath!, (IXmlLineInfo)node, ""),
761+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.XamlParserError,
762+
LocationHelpers.LocationCreate(context.ProjectItem.RelativePath!, (IXmlLineInfo)node, ""),
762763
"AppThemeBindingExtension requires a non-null value to be specified for at least one theme or Default"));
763764
value = string.Empty;
764765
return false;
765766
}
766767

767-
// Determine which helper method to call based on which values are set
768-
string helperMethodName;
769-
var args = new List<string>();
770-
771-
if (lightValue is not null && darkValue is not null && defaultValue is not null)
772-
{
773-
helperMethodName = "CreateAppThemeBindingLightDarkDefault";
774-
args.Add(lightValue);
775-
args.Add(darkValue);
776-
args.Add(defaultValue);
777-
}
778-
else if (lightValue is not null && darkValue is not null)
779-
{
780-
helperMethodName = "CreateAppThemeBindingLightDark";
781-
args.Add(lightValue);
782-
args.Add(darkValue);
783-
}
784-
else if (lightValue is not null && defaultValue is not null)
785-
{
786-
helperMethodName = "CreateAppThemeBindingLightDefault";
787-
args.Add(lightValue);
788-
args.Add(defaultValue);
789-
}
790-
else if (darkValue is not null && defaultValue is not null)
791-
{
792-
helperMethodName = "CreateAppThemeBindingDarkDefault";
793-
args.Add(darkValue);
794-
args.Add(defaultValue);
795-
}
796-
else if (lightValue is not null)
797-
{
798-
helperMethodName = "CreateAppThemeBindingLight";
799-
args.Add(lightValue);
800-
}
801-
else if (darkValue is not null)
802-
{
803-
helperMethodName = "CreateAppThemeBindingDark";
804-
args.Add(darkValue);
805-
}
806-
else // defaultValue is not null
807-
{
808-
helperMethodName = "CreateAppThemeBindingDefault";
809-
args.Add(defaultValue!);
810-
}
768+
// Build the AppThemeBinding initialization expression
769+
var parts = new List<string>();
770+
771+
if (lightValue is not null)
772+
parts.Add($"Light = {lightValue}");
773+
774+
if (darkValue is not null)
775+
parts.Add($"Dark = {darkValue}");
776+
777+
if (defaultValue is not null)
778+
parts.Add($"Default = {defaultValue}");
811779

812-
value = $"global::Microsoft.Maui.Controls.XamlSourceGen.AppThemeBindingHelpers.{helperMethodName}({string.Join(", ", args)})";
780+
value = $"new global::Microsoft.Maui.Controls.AppThemeBinding {{ {string.Join(", ", parts)} }}";
813781
return true;
814782
}
783+
#endif
815784

816785
//all of this could/should be better, but is already slightly better than XamlC
817786
internal static bool ProvideValueForStaticResourceExtension(ElementNode node, IndentedTextWriter writer, SourceGenContext context, NodeSGExtensions.GetNodeValueDelegate? getNodeValue, out ITypeSymbol? returnType, out string value)

src/Controls/src/SourceGen/NodeSGExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public static Dictionary<ITypeSymbol, ProvideValueDelegate> GetKnownLateMarkupEx
109109
{context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.TemplateBindingExtension")!, KnownMarkups.ProvideValueForTemplateBindingExtension},
110110
{context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.ReferenceExtension")!, KnownMarkups.ProvideValueForReferenceExtension},
111111
{context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.StaticResourceExtension")!, KnownMarkups.ProvideValueForStaticResourceExtension},
112+
#if NET11_0_OR_GREATER
112113
{context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.AppThemeBindingExtension")!, KnownMarkups.ProvideValueForAppThemeBindingExtension},
114+
#endif
113115
};
114116

115117
public static bool TryGetPropertyName(this INode node, INode parentNode, out XmlName name)

src/Controls/src/SourceGen/XamlGenerator.cs

Lines changed: 0 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -246,176 +246,6 @@ public void Initialize(IncrementalGeneratorInitializationContext initContext)
246246
, Encoding.UTF8));
247247
});
248248

249-
// Generate AppThemeBinding helper methods for .NET 10 compatibility
250-
initContext.RegisterPostInitializationOutput(static context =>
251-
{
252-
context.AddSource("AppThemeBindingHelpers.g.cs", SourceText.From(
253-
$$"""
254-
{{AutoGeneratedHeaderText}}
255-
#nullable enable
256-
257-
namespace Microsoft.Maui.Controls.XamlSourceGen;
258-
259-
internal static class AppThemeBindingHelpers
260-
{
261-
#if NET11_0_OR_GREATER
262-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
263-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLight(object? light)
264-
{
265-
return new global::Microsoft.Maui.Controls.AppThemeBinding
266-
{
267-
Light = light,
268-
};
269-
}
270-
271-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
272-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingDark(object? dark)
273-
{
274-
return new global::Microsoft.Maui.Controls.AppThemeBinding
275-
{
276-
Dark = dark,
277-
};
278-
}
279-
280-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
281-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingDefault(object? defaultValue)
282-
{
283-
return new global::Microsoft.Maui.Controls.AppThemeBinding
284-
{
285-
Default = defaultValue,
286-
};
287-
}
288-
289-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
290-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLightDark(object? light, object? dark)
291-
{
292-
return new global::Microsoft.Maui.Controls.AppThemeBinding
293-
{
294-
Light = light,
295-
Dark = dark,
296-
};
297-
}
298-
299-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
300-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLightDefault(object? light, object? defaultValue)
301-
{
302-
return new global::Microsoft.Maui.Controls.AppThemeBinding
303-
{
304-
Light = light,
305-
Default = defaultValue,
306-
};
307-
}
308-
309-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
310-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingDarkDefault(object? dark, object? defaultValue)
311-
{
312-
return new global::Microsoft.Maui.Controls.AppThemeBinding
313-
{
314-
Dark = dark,
315-
Default = defaultValue,
316-
};
317-
}
318-
319-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
320-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLightDarkDefault(object? light, object? dark, object? defaultValue)
321-
{
322-
return new global::Microsoft.Maui.Controls.AppThemeBinding
323-
{
324-
Light = light,
325-
Dark = dark,
326-
Default = defaultValue,
327-
};
328-
}
329-
#else
330-
// Shared UnsafeAccessor methods for accessing internal AppThemeBinding members
331-
[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Constructor)]
332-
[return: global::System.Runtime.CompilerServices.UnsafeAccessorType("Microsoft.Maui.Controls.AppThemeBinding, Microsoft.Maui.Controls")]
333-
private static extern object AppThemeBindingCtor();
334-
335-
[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_Light")]
336-
private static extern void AppThemeBinding_SetLight(
337-
[global::System.Runtime.CompilerServices.UnsafeAccessorType("Microsoft.Maui.Controls.AppThemeBinding, Microsoft.Maui.Controls")]
338-
object instance,
339-
object? light);
340-
341-
[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_Dark")]
342-
private static extern void AppThemeBinding_SetDark(
343-
[global::System.Runtime.CompilerServices.UnsafeAccessorType("Microsoft.Maui.Controls.AppThemeBinding, Microsoft.Maui.Controls")]
344-
object instance,
345-
object? dark);
346-
347-
[global::System.Runtime.CompilerServices.UnsafeAccessor(global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method, Name = "set_Default")]
348-
private static extern void AppThemeBinding_SetDefault(
349-
[global::System.Runtime.CompilerServices.UnsafeAccessorType("Microsoft.Maui.Controls.AppThemeBinding, Microsoft.Maui.Controls")]
350-
object instance,
351-
object? defaultValue);
352-
353-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
354-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLight(object? light)
355-
{
356-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
357-
AppThemeBinding_SetLight(binding, light);
358-
return binding;
359-
}
360-
361-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
362-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingDark(object? dark)
363-
{
364-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
365-
AppThemeBinding_SetDark(binding, dark);
366-
return binding;
367-
}
368-
369-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
370-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingDefault(object? defaultValue)
371-
{
372-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
373-
AppThemeBinding_SetDefault(binding, defaultValue);
374-
return binding;
375-
}
376-
377-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
378-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLightDark(object? light, object? dark)
379-
{
380-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
381-
AppThemeBinding_SetLight(binding, light);
382-
AppThemeBinding_SetDark(binding, dark);
383-
return binding;
384-
}
385-
386-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
387-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLightDefault(object? light, object? defaultValue)
388-
{
389-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
390-
AppThemeBinding_SetLight(binding, light);
391-
AppThemeBinding_SetDefault(binding, defaultValue);
392-
return binding;
393-
}
394-
395-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
396-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingDarkDefault(object? dark, object? defaultValue)
397-
{
398-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
399-
AppThemeBinding_SetDark(binding, dark);
400-
AppThemeBinding_SetDefault(binding, defaultValue);
401-
return binding;
402-
}
403-
404-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
405-
internal static global::Microsoft.Maui.Controls.BindingBase CreateAppThemeBindingLightDarkDefault(object? light, object? dark, object? defaultValue)
406-
{
407-
var binding = (global::Microsoft.Maui.Controls.BindingBase)AppThemeBindingCtor();
408-
AppThemeBinding_SetLight(binding, light);
409-
AppThemeBinding_SetDark(binding, dark);
410-
AppThemeBinding_SetDefault(binding, defaultValue);
411-
return binding;
412-
}
413-
#endif
414-
}
415-
"""
416-
, Encoding.UTF8));
417-
});
418-
419249
// Register the global xmlns definitions. create equivalent XmlnsDefintion for the global ones, so most of the 1sr and 3rd party tooling should keep working
420250
initContext.RegisterSourceOutput(xmlnsDefinitionsProvider, static (sourceProductionContext, xmlnsCache) =>
421251
{

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/AppThemeBinding.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Xunit;
55

66
namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
7+
#if NET11_0_OR_GREATER
78

89
public class AppThemeBinding : SourceGenXamlInitializeComponentTestBase
910
{
@@ -99,7 +100,7 @@ private partial void InitializeComponent()
99100
#line 6 "{{testXamlFilePath}}"
100101
appThemeBindingExtension.Dark = "Black";
101102
#line default
102-
var appThemeBinding = global::Microsoft.Maui.Controls.XamlSourceGen.AppThemeBindingHelpers.CreateAppThemeBindingLightDark(global::Microsoft.Maui.Graphics.Colors.White, global::Microsoft.Maui.Graphics.Colors.Black);
103+
var appThemeBinding = new global::Microsoft.Maui.Controls.AppThemeBinding { Light = global::Microsoft.Maui.Graphics.Colors.White, Dark = global::Microsoft.Maui.Graphics.Colors.Black };
103104
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(appThemeBinding!) == null)
104105
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(appThemeBinding!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 2);
105106
__root.SetBinding(global::Microsoft.Maui.Controls.VisualElement.BackgroundColorProperty, appThemeBinding);
@@ -205,7 +206,7 @@ private partial void InitializeComponent()
205206
#line 6 "{{testXamlFilePath}}"
206207
appThemeBindingExtension.Dark = "Black";
207208
#line default
208-
var appThemeBinding = global::Microsoft.Maui.Controls.XamlSourceGen.AppThemeBindingHelpers.CreateAppThemeBindingDarkDefault(global::Microsoft.Maui.Graphics.Colors.Black, global::Microsoft.Maui.Graphics.Colors.Gray);
209+
var appThemeBinding = new global::Microsoft.Maui.Controls.AppThemeBinding { Dark = global::Microsoft.Maui.Graphics.Colors.Black, Default = global::Microsoft.Maui.Graphics.Colors.Gray };
209210
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(appThemeBinding!) == null)
210211
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(appThemeBinding!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 2);
211212
__root.SetBinding(global::Microsoft.Maui.Controls.VisualElement.BackgroundColorProperty, appThemeBinding);
@@ -314,7 +315,7 @@ private partial void InitializeComponent()
314315
#line 6 "{{testXamlFilePath}}"
315316
appThemeBindingExtension.Default = "Gray";
316317
#line default
317-
var appThemeBinding = global::Microsoft.Maui.Controls.XamlSourceGen.AppThemeBindingHelpers.CreateAppThemeBindingLightDarkDefault(global::Microsoft.Maui.Graphics.Colors.White, global::Microsoft.Maui.Graphics.Colors.Black, global::Microsoft.Maui.Graphics.Colors.Gray);
318+
var appThemeBinding = new global::Microsoft.Maui.Controls.AppThemeBinding { Light = global::Microsoft.Maui.Graphics.Colors.White, Dark = global::Microsoft.Maui.Graphics.Colors.Black, Default = global::Microsoft.Maui.Graphics.Colors.Gray };
318319
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(appThemeBinding!) == null)
319320
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(appThemeBinding!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 2);
320321
__root.SetBinding(global::Microsoft.Maui.Controls.VisualElement.BackgroundColorProperty, appThemeBinding);
@@ -328,3 +329,4 @@ private partial void InitializeComponent()
328329
Assert.Equal(expected, generated, ignoreLineEndingDifferences: true);
329330
}
330331
}
332+
#endif

0 commit comments

Comments
 (0)