Repository navigation
feat: add Display Profiles tab to switch resolution and refresh rate - #1079
Merged
Merged
Conversation
Implements Display Profiles (replaces its WIP placeholder in Gaming & Profiles). Closes #328. - DisplayProfileService: user32 display APIs (EnumDisplayDevicesW / EnumDisplaySettingsW / ChangeDisplaySettingsExW) to enumerate displays, list supported resolution+refresh modes, and apply one. Validates with CDS_TEST before applying; applies for the session (reverts on reboot); no admin. Classic [DllImport] with explicit ...W EntryPoint because DEVMODE/DISPLAY_DEVICE have non-blittable inline buffers (SYSLIB1051). - DisplayProfileViewModel: per-display mode list + apply, with a 15-second in-tab auto-revert (DispatcherTimer) that restores the previous mode unless the user confirms "Keep" — a bad mode can never strand the user on a blank screen. - DisplayMode/DisplayDevice models hold the pure display formatting, unit-tested. - Marked PREVIEW via the badge mechanism. Uses only Windows display APIs (no NVIDIA/AMD SDK), so no vendor conflict. HDR + color profile are noted as a future follow-up (separate CCD API).
| try | ||
| { | ||
| var dd = new NativeMethods.DISPLAY_DEVICE { cb = (uint)Marshal.SizeOf<NativeMethods.DISPLAY_DEVICE>() }; | ||
| for (uint i = 0; NativeMethods.EnumDisplayDevices(null, i, ref dd, 0); i++) |
| string friendly = dd.DeviceString; | ||
|
|
||
| var mon = new NativeMethods.DISPLAY_DEVICE { cb = (uint)Marshal.SizeOf<NativeMethods.DISPLAY_DEVICE>() }; | ||
| if (NativeMethods.EnumDisplayDevices(adapter, 0, ref mon, 0) && !string.IsNullOrWhiteSpace(mon.DeviceString)) |
| try | ||
| { | ||
| var dm = new NativeMethods.DEVMODE { dmSize = (ushort)Marshal.SizeOf<NativeMethods.DEVMODE>() }; | ||
| if (NativeMethods.EnumDisplaySettings(deviceName, NativeMethods.EnumCurrentSettings, ref dm)) |
| try | ||
| { | ||
| var dm = new NativeMethods.DEVMODE { dmSize = (ushort)Marshal.SizeOf<NativeMethods.DEVMODE>() }; | ||
| for (int i = 0; NativeMethods.EnumDisplaySettings(deviceName, i, ref dm); i++) |
| try | ||
| { | ||
| var dm = new NativeMethods.DEVMODE { dmSize = (ushort)Marshal.SizeOf<NativeMethods.DEVMODE>() }; | ||
| if (!NativeMethods.EnumDisplaySettings(deviceName, NativeMethods.EnumCurrentSettings, ref dm)) |
| dm.dmDisplayFrequency = (uint)refreshHz; | ||
| dm.dmFields = NativeMethods.DM_PELSWIDTH | NativeMethods.DM_PELSHEIGHT | NativeMethods.DM_DISPLAYFREQUENCY; | ||
|
|
||
| int test = NativeMethods.ChangeDisplaySettingsEx(deviceName, ref dm, IntPtr.Zero, NativeMethods.CDS_TEST, IntPtr.Zero); |
| return false; | ||
| } | ||
|
|
||
| int apply = NativeMethods.ChangeDisplaySettingsEx(deviceName, ref dm, IntPtr.Zero, 0, IntPtr.Zero); |
|
|
||
| [DllImport("user32.dll", EntryPoint = "EnumDisplayDevicesW", CharSet = CharSet.Unicode, SetLastError = true)] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool EnumDisplayDevices(string? lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags); |
|
|
||
| [DllImport("user32.dll", EntryPoint = "EnumDisplaySettingsW", CharSet = CharSet.Unicode, SetLastError = true)] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode); |
| public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode); | ||
|
|
||
| [DllImport("user32.dll", EntryPoint = "ChangeDisplaySettingsExW", CharSet = CharSet.Unicode)] | ||
| public static extern int ChangeDisplaySettingsEx(string lpszDeviceName, ref DEVMODE lpDevMode, IntPtr hwnd, uint dwflags, IntPtr lParam); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Implements Display Profiles (Gaming & Profiles), replacing its WIP placeholder. Closes #328.
Quick-switch resolution + refresh rate per monitor — e.g. 165 Hz for gaming, 60 Hz for work — from the list of modes the display actually supports.
How
DisplayProfileService—user32display APIs:EnumDisplayDevicesW(list adapters),EnumDisplaySettingsW(current + supported modes),ChangeDisplaySettingsExW(apply). Validates each mode withCDS_TESTbefore applying. Applies for the session only (dwflags=0) so a reboot reverts; no admin required. Uses only Windows APIs — no NVIDIA/AMD SDK, so no vendor-tool conflict.[DllImport]with explicitEntryPoint="...W"+CharSet.Unicode(not[LibraryImport]):DEVMODE/DISPLAY_DEVICEhave inlineByValTStrbuffers (non-blittable) → SYSLIB1051.dmSizeis set fromMarshal.SizeOfbefore every call; the DEVMODE union is sized correctly for x64.DispatcherTimer): the user must click Keep to retain the change, otherwise it restores the previous mode. A bad mode (e.g. unsupported refresh) can blank the panel, so a modal the user can't see would be useless — the countdown bar + session-only apply + reboot-reverts give three layers of safety.Preview
Marked PREVIEW (sidebar pill + banner).
Tests
DisplayModeTests— resolution/refresh formatting, value equality, primary-display tagging.NavLeaf_DisplayProfiles_IsImplementedAndMarkedPreviewpins the WIP→real-view swap + preview flag.Verification
...WEntryPoints.Future follow-up (out of scope)
HDR toggle + color profile use a different API family (CCD
DisplayConfigSetDeviceInfo) and are noted for a later pass.