Skip to content

Freeze SolidColorBrush to improve performance#4319

Open
Jack251970 wants to merge 1 commit intodevfrom
FreezeResources1
Open

Freeze SolidColorBrush to improve performance#4319
Jack251970 wants to merge 1 commit intodevfrom
FreezeResources1

Conversation

@Jack251970
Copy link
Member

@Jack251970 Jack251970 commented Mar 4, 2026

Refactor background brush creation to assign, freeze, and reuse SolidColorBrush instances for better WPF performance.

Separated from #4302


Summary by cubic

Freeze SolidColorBrush instances and switch to type-safe Background property checks to improve WPF performance and robustness. This reduces allocations and avoids unnecessary mutable brushes across Theme.cs.

  • Summary of changes
    • Changed: Replaced string-based setter lookups (Property.Name == "Background") with type-safe Control.BackgroundProperty in Theme.SetBlurForWindow.
    • Changed: Centralized background brush creation in SetBlurForWindow, ApplyPreviewBackground, and ColorizeWindow to create a brush, freeze it, then assign it.
    • Added: Freezing of SolidColorBrush instances before applying to setters and MainWindow to reduce overhead and improve rendering performance.
    • Removed: Inline, unfrozen SolidColorBrush allocations in setters and MainWindow assignments; removed reliance on string-based property matching.
    • Memory: Slight reduction in allocations and change-tracking overhead; overall memory footprint neutral to slightly lower.
    • Security: No security-impacting changes.
    • Tests: No new unit tests; behavior verified via UI rendering.

Written for commit 9abf136. Summary will update on new commits.

Refactor background brush creation to assign, freeze, and reuse
SolidColorBrush instances for better WPF performance. Replace
string-based property checks with type-safe Control.BackgroundProperty
comparisons for improved robustness.
Copilot AI review requested due to automatic review settings March 4, 2026 04:49
@prlabeler prlabeler bot added Code Refactor enhancement New feature or request labels Mar 4, 2026
@Jack251970 Jack251970 enabled auto-merge March 4, 2026 04:49
@github-actions github-actions bot added this to the 2.2.0 milestone Mar 4, 2026
@gitstream-cm
Copy link

gitstream-cm bot commented Mar 4, 2026

🥷 Code experts: jjw24

jjw24 has most 👩‍💻 activity in the files.
jjw24 has most 🧠 knowledge in the files.

See details

Flow.Launcher.Core/Resource/Theme.cs

Activity based on git-commit:

jjw24
MAR
FEB
JAN
DEC
NOV
OCT

Knowledge based on git-blame:
jjw24: 99%

✨ Comment /gs review for LinearB AI review. Learn how to automate it here.

@gitstream-cm
Copy link

gitstream-cm bot commented Mar 4, 2026

Be a legend 🏆 by adding a before and after screenshot of the changes you made, especially if they are around UI/UX.

@coderabbitai coderabbitai bot removed the enhancement New feature or request label Mar 4, 2026
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to improve WPF performance and safety by freezing SolidColorBrush instances that are created for theme/window background rendering, reducing mutability and improving rendering efficiency.

Changes:

  • Freeze newly created SolidColorBrush instances before assigning them to Style setters in blur scenarios.
  • Freeze SolidColorBrush used for the preview window background style.
  • Freeze SolidColorBrush assigned to MainWindow.Background when blur is available.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 4, 2026

📝 Walkthrough

Walkthrough

The Theme.cs file was refactored to improve brush immutability and property matching. Property matching logic for Background was updated to compare against Control.BackgroundProperty instead of checking Property.Name. SolidColorBrush instances are now frozen before assignment across multiple code paths including SetBlurForWindow, ApplyPreviewBackground, and ColorizeWindow methods.

Changes

Cohort / File(s) Summary
Brush Freezing & Property Matching
Flow.Launcher.Core/Resource/Theme.cs
Updated Background property matching from name-based comparison to Control.BackgroundProperty comparison. Added Brush.Freeze() calls to SolidColorBrush instances before assignment in SetBlurForWindow (Mica/MicaAlt and Acrylic backdrops), ApplyPreviewBackground preview brush construction, and ColorizeWindow main window background assignment.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Fluent Window #3271 — Directly modifies Theme.cs background brush handling and SetBlurForWindow method with related backdrop configuration changes.

Suggested reviewers

  • jjw24
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Freeze SolidColorBrush to improve performance' directly summarizes the main change: freezing brush instances for performance improvements, which is the primary focus of the changeset.
Description check ✅ Passed The pull request description accurately describes the changes: refactoring background brush creation to freeze and reuse SolidColorBrush instances for WPF performance improvements, which matches the actual changeset modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch FreezeResources1

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
Flow.Launcher.Core/Resource/Theme.cs (1)

676-686: Cache constant frozen brushes instead of recreating them on each refresh.

Line 677, Line 684, and Line 926 still allocate new SolidColorBrush instances for fixed colors. Reusing class-level frozen singletons would better match the PR’s reuse/perf goal and remove repeated allocations.

♻️ Suggested refactor
+        private static readonly SolidColorBrush MicaTransparentBrush = CreateFrozenBrush(Color.FromArgb(1, 0, 0, 0));
+        private static readonly SolidColorBrush TransparentBrush = CreateFrozenBrush(Colors.Transparent);
+
+        private static SolidColorBrush CreateFrozenBrush(Color color)
+        {
+            var brush = new SolidColorBrush(color);
+            brush.Freeze();
+            return brush;
+        }

@@
-                    var brush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
-                    brush.Freeze();
-                    windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush));
+                    windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, MicaTransparentBrush));
@@
-                    var brush = new SolidColorBrush(Colors.Transparent);
-                    brush.Freeze();
-                    windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, brush));
+                    windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, TransparentBrush));
@@
-                    var backgroundBrush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
-                    backgroundBrush.Freeze();
-                    mainWindow.Background = backgroundBrush;
+                    mainWindow.Background = MicaTransparentBrush;

Also applies to: 926-934

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Flow.Launcher.Core/Resource/Theme.cs` around lines 676 - 686, Create and
reuse class-level frozen SolidColorBrush singletons for the fixed colors instead
of allocating new instances each refresh: add readonly static fields (e.g.,
FrozenAlmostTransparent = new SolidColorBrush(Color.FromArgb(1,0,0,0)).Freeze()
and FrozenTransparent = new SolidColorBrush(Colors.Transparent).Freeze()) and
replace the local allocations where windowBorderStyle.Setters.Add(new
Setter(Border.BackgroundProperty, brush)) is used (the blocks around the current
SolidColorBrush creations and the similar code in the region referenced at
926-934) to reference these frozen fields; ensure the fields are frozen once at
initialization and used everywhere the same constant brushes are needed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@Flow.Launcher.Core/Resource/Theme.cs`:
- Around line 676-686: Create and reuse class-level frozen SolidColorBrush
singletons for the fixed colors instead of allocating new instances each
refresh: add readonly static fields (e.g., FrozenAlmostTransparent = new
SolidColorBrush(Color.FromArgb(1,0,0,0)).Freeze() and FrozenTransparent = new
SolidColorBrush(Colors.Transparent).Freeze()) and replace the local allocations
where windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty,
brush)) is used (the blocks around the current SolidColorBrush creations and the
similar code in the region referenced at 926-934) to reference these frozen
fields; ensure the fields are frozen once at initialization and used everywhere
the same constant brushes are needed.

ℹ️ Review info
Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ea83e89b-ddb2-4ede-a1ae-202bbb0172b9

📥 Commits

Reviewing files that changed from the base of the PR and between dda9000 and 9abf136.

📒 Files selected for processing (1)
  • Flow.Launcher.Core/Resource/Theme.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants