Skip to content

[Repo Assist] Refactor: extract ApplyDisplayAffinity helper to eliminate repetition #66

Description

@github-actions

🤖 This PR was created by Repo Assist, an automated AI assistant.

Problem

ApplyExcludeFromCapture repeated the same 7-line pattern four times:

var hwnd = new WindowInteropHelper(window).Handle;
if (hwnd != IntPtr.Zero)
{
    var result = SetWindowDisplayAffinity(hwnd, affinity);
    if (!result)
    {
        var error = Marshal.GetLastWin32Error();
        System.Diagnostics.Debug.WriteLine($"...");
    }
}

This appeared for: the main window, the control window, each additional monitor window, and again inside CreateMonitorWindow's Loaded lambda.

Fix

Extract a private ApplyDisplayAffinity(Window window, uint affinity) helper and use it everywhere. ApplyExcludeFromCapture now reads cleanly:

private void ApplyExcludeFromCapture()
{
    uint affinity = settings.ExcludeFromCapture ? WDA_EXCLUDEFROMCAPTURE : WDA_NONE;
    ApplyDisplayAffinity(this, affinity);
    if (controlWindow != null) ApplyDisplayAffinity(controlWindow, affinity);
    foreach (var ctx in additionalMonitorWindows) ApplyDisplayAffinity(ctx.Window, affinity);
}

No Behaviour Change

Purely a structural refactoring — the same HWND fetch, null-guard, SetWindowDisplayAffinity call, and debug-log on failure are preserved in the helper.

Test Status

dotnet build /p:EnableWindowsTargeting=true — 0 errors, 0 warnings

Note

🔒 Integrity filter blocked 2 items

The following items were blocked because they don't meet the GitHub integrity level.

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Repo Assist · ● 2.7M ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@cbb46ab386962aa371045839fc9998ee4e97ca64

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch repo-assist/improve-display-affinity-helper-2026-06-19-eda98cfefb8e7083.

Click here to create the pull request

To fix the permissions issue, go to SettingsActionsGeneral and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (105 of 105 lines)
From c34664b22b8b0c1fdafe7929ffb35a82eb5565c7 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Fri, 19 Jun 2026 01:49:48 +0000
Subject: [PATCH] refactor: extract ApplyDisplayAffinity helper to reduce
 duplication

ApplyExcludeFromCapture repeated the same 7-line HWND-fetch + affinity-set
+ error-log block three times (main window, control window, each additional
monitor window).  CreateMonitorWindow had a fourth copy in its Loaded lambda.

Extract a private ApplyDisplayAffinity(Window, uint) helper and use it in
all four call sites.  No behaviour change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 WindowsEdgeLight/MainWindow.xaml.cs | 60 ++++++++---------------------
 1 file changed, 15 insertions(+), 45 deletions(-)

diff --git a/WindowsEdgeLight/MainWindow.xaml.cs b/WindowsEdgeLight/MainWindow.xaml.cs
index c24a3c7..0181b12 100644
--- a/WindowsEdgeLight/MainWindow.xaml.cs
+++ b/WindowsEdgeLight/MainWindow.xaml.cs
@@ -725,50 +725,25 @@ Version {version}";
         ApplyExcludeFromCapture();
     }
 
-    private void ApplyExcludeFromCapture()
+    private void ApplyDisplayAffinity(Window window, uint affinity)
     {
-        var hwnd = new WindowInteropHelper(this).Handle;
-        if (hwnd != IntPtr.Zero)
-        {
-            var result = SetWindowDisplayAffinity(hwnd, settings.ExcludeFromCapture ? WDA_EXCLUDEFROMCAPTURE : WDA_NONE);
-            if (!result)
-            {
-                var error = Marshal.GetLastWin32Error();
-                System.Diagnostics.Debug.WriteLine($"Failed to set display affinity for main window. Error: {error}");
-            }
-        }
-        
-        // Apply to control window
-        if (controlWindow != null)
-        {
-            var controlHwnd = new WindowInteropHelper(controlWindow).Handle;
-            if (controlHwnd != IntPtr.Zero)
-            {
-                var result = SetWindowDisplayAffinity(controlHwnd, settin
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions