[net11.0] Trimmable StyleSheets#33160
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
This PR introduces a trimming optimization for CSS-related code in .NET MAUI applications. The feature allows CSS stylesheets code to be trimmed away when not used, reducing app size. The main approach involves moving assembly-level StylePropertyAttribute declarations from AssemblyInfo.cs into a local array in Registrar.cs, which enables better trimming behavior. A new runtime feature flag IsCssEnabled controls whether CSS functionality is available at runtime, with MSBuild logic automatically disabling it in trimmed builds when no CSS files are present.
Key Changes
- Adds
IsCssEnabledruntime feature flag with default value oftrue - Moves 100+ assembly-level StyleProperty attributes into a local array to enable trimming
- Adds runtime checks that throw
NotSupportedExceptionwhen CSS is disabled but CSS APIs are called
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/Core/src/RuntimeFeature.cs |
Adds IsCssEnabled feature flag with default value of true and feature switch definition for .NET 9+ |
src/Controls/src/Core/VisualElement/VisualElement_StyleSheet.cs |
Adds runtime check in GetProperty to throw helpful error when CSS is disabled |
src/Controls/src/Core/StyleSheets/Style.cs |
Adds runtime checks in Parse and Apply methods to prevent CSS usage when disabled |
src/Controls/src/Core/Registrar.cs |
Converts 100+ assembly-level attributes to local array and adds early return when CSS is disabled |
src/Controls/src/Core/Properties/AssemblyInfo.cs |
Removes all assembly-level StylePropertyAttribute declarations and unused using statements |
src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets |
Sets MauiCssEnabled=false in trimmed builds when no @(MauiCss) items exist, and configures runtime option |
| new StylePropertyAttribute("width", typeof(VisualElement), nameof(VisualElement.WidthRequestProperty)), | ||
| new StylePropertyAttribute("letter-spacing", typeof(ITextElement), nameof(TextElement.CharacterSpacingProperty)) { Inherited = true }, | ||
| #pragma warning disable CS0618 // Type or member is obsolete | ||
| new StylePropertyAttribute("line-height", typeof(ILineHeightElement), nameof(LineHeightElement.LineHeightProperty)) { Inherited = true }, |
There was a problem hiding this comment.
The type reference for ILineHeightElement in this attribute is incorrect. Since this code is in the Microsoft.Maui.Controls.Internals namespace, typeof(ILineHeightElement) will resolve to the internal obsolete Microsoft.Maui.Controls.Internals.ILineHeightElement instead of the public Microsoft.Maui.Controls.ILineHeightElement that was used in the original assembly attribute. This needs to be changed to use the fully qualified type name or add a using alias to ensure the correct type is referenced.
| new StylePropertyAttribute("line-height", typeof(ILineHeightElement), nameof(LineHeightElement.LineHeightProperty)) { Inherited = true }, | |
| new StylePropertyAttribute("line-height", typeof(Microsoft.Maui.Controls.ILineHeightElement), nameof(LineHeightElement.LineHeightProperty)) { Inherited = true }, |
|
/azp run maui-pr-devicetests,maui-pr-uitests |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Follow-up reminder: Documentation update neededAfter this PR is merged, we need to document the The feature switch documentation should explain:
The in-repo documentation has been updated in |
3c28640 to
db2f075
Compare
|
/azp run maui-pr-uitests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run maui-pr-devicetests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/rebase |
db2f075 to
74375c4
Compare
|
/azp run maui-pr-devicetests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
22761a2 to
77c461a
Compare
|
/rebase |
77c461a to
7ded7f1
Compare
|
@copilot this PR needs a proper rebase with conflict resolution. open a PR which addresses this issue. make sure it is up to date with the base net11.0 branch. |
|
@simonrozsival I've opened a new pull request, #34010, to work on those changes. Once the pull request is ready, I'll request review from you. |
7ded7f1 to
66d1243
Compare
…hanges These changes belong in separate PRs: - #33611 (CSS StyleSheet trimming) - #33561 (EventTrigger trimming) - #33160 (HybridWebView trimming) Reverted files to their main branch state, keeping only the [ElementHandler] attribute addition on HybridWebView. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
66d1243 to
564c244
Compare
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33160Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33160" |
564c244 to
afbda13
Compare
…ed on @(MauiCss) item count
- Add MauiCssEnabled=true to TestCases.HostApp.csproj since some UI tests use CSS functionality - Add unit tests for CSS-disabled scenarios in StyleTests.cs
- Update NotSupportedException messages to explain why CSS is disabled and how to enable it - Add MauiCssEnabled documentation to docs/design/FeatureSwitches.md - Added follow-up reminder comment to PR for dotnet/maui-docs update
typeof(ILineHeightElement) was resolving to the obsolete Microsoft.Maui.Controls.Internals.ILineHeightElement. Use fully qualified Microsoft.Maui.Controls.ILineHeightElement and remove the CS0618 pragma suppression. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- IsCssEnabled: use FeatureSwitchPrefix + nameof() to match other feature switches in RuntimeFeature.cs - FeatureSwitches.md: clarify that CSS auto-disable only applies to optimized builds (PublishAot=true or TrimMode=full) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
96755c5 to
fa6afda
Compare
|
@rmarinho @jfversluis could you please have a look at this PR and let me know what you think about it? could we merge this into net11.0 soon? |
Description of Change
This PR will allow trimming CSS-related code and default styles when there are no CSS stylesheets used in the MAUI app.
@(MauiCss)item is added, the CSS feature is enabled@(MauiCss)but devs want to useStyle.Parseto parse styles at runtime, they must enable$(MauiCssEnabled)=trueMost of the changes in this PR are around moving assembly-level attributes into a local list. This will allow us to shave of some unnecessary code from most apps:
Related to #29952