Use var when evident

This commit is contained in:
Jiří Polášek
2026-02-12 18:08:26 +01:00
parent 2663f90ed2
commit 6c9a0a0711
4 changed files with 19 additions and 19 deletions

View File

@@ -78,7 +78,7 @@ internal sealed class OpenWindows
lock (_enumWindowsLock) lock (_enumWindowsLock)
{ {
windows.Clear(); windows.Clear();
EnumWindowsProc callbackptr = new EnumWindowsProc(WindowEnumerationCallBack); var callbackptr = new EnumWindowsProc(WindowEnumerationCallBack);
_ = NativeMethods.EnumWindows(callbackptr, tokenHandleParam); _ = NativeMethods.EnumWindows(callbackptr, tokenHandleParam);
} }
} }
@@ -109,7 +109,7 @@ internal sealed class OpenWindows
return false; return false;
} }
Window newWindow = new Window(hwnd); var newWindow = new Window(hwnd);
if (newWindow.IsWindow && newWindow.Visible && newWindow.IsOwner && if (newWindow.IsWindow && newWindow.Visible && newWindow.IsOwner &&
(!newWindow.IsToolWindow || newWindow.IsAppWindow) && !newWindow.TaskListDeleted && (!newWindow.IsToolWindow || newWindow.IsAppWindow) && !newWindow.TaskListDeleted &&

View File

@@ -51,7 +51,7 @@ internal sealed class Window
var sizeOfTitle = NativeMethods.GetWindowTextLength(hwnd); var sizeOfTitle = NativeMethods.GetWindowTextLength(hwnd);
if (sizeOfTitle++ > 0) if (sizeOfTitle++ > 0)
{ {
StringBuilder titleBuffer = new StringBuilder(sizeOfTitle); var titleBuffer = new StringBuilder(sizeOfTitle);
var numCharactersWritten = NativeMethods.GetWindowText(hwnd, titleBuffer, sizeOfTitle); var numCharactersWritten = NativeMethods.GetWindowText(hwnd, titleBuffer, sizeOfTitle);
if (numCharactersWritten == 0) if (numCharactersWritten == 0)
{ {
@@ -260,7 +260,7 @@ internal sealed class Window
/// <returns>The state (minimized, maximized, etc..) of the window</returns> /// <returns>The state (minimized, maximized, etc..) of the window</returns>
internal WindowSizeState GetWindowSizeState() internal WindowSizeState GetWindowSizeState()
{ {
NativeMethods.GetWindowPlacement(Hwnd, out WINDOWPLACEMENT placement); NativeMethods.GetWindowPlacement(Hwnd, out var placement);
switch (placement.ShowCmd) switch (placement.ShowCmd)
{ {
@@ -332,7 +332,7 @@ internal sealed class Window
/// <returns>Class name</returns> /// <returns>Class name</returns>
private static string GetWindowClassName(IntPtr hwnd) private static string GetWindowClassName(IntPtr hwnd)
{ {
StringBuilder windowClassName = new StringBuilder(300); var windowClassName = new StringBuilder(300);
var numCharactersWritten = NativeMethods.GetClassName(hwnd, windowClassName, windowClassName.MaxCapacity); var numCharactersWritten = NativeMethods.GetClassName(hwnd, windowClassName, windowClassName.MaxCapacity);
if (numCharactersWritten == 0) if (numCharactersWritten == 0)
@@ -384,7 +384,7 @@ internal sealed class Window
{ {
new Task(() => new Task(() =>
{ {
EnumWindowsProc callbackptr = new EnumWindowsProc((IntPtr hwnd, IntPtr lParam) => var callbackptr = new EnumWindowsProc((IntPtr hwnd, IntPtr lParam) =>
{ {
// Every uwp app main window has at least three child windows. Only the one we are interested in has a class starting with "Windows.UI.Core." and is assigned to the real app process. // Every uwp app main window has at least three child windows. Only the one we are interested in has a class starting with "Windows.UI.Core." and is assigned to the real app process.
// (The other ones have a class name that begins with the string "ApplicationFrame".) // (The other ones have a class name that begins with the string "ApplicationFrame".)

View File

@@ -197,7 +197,7 @@ internal sealed class WindowProcess
internal static string GetProcessNameFromProcessID(uint pid) internal static string GetProcessNameFromProcessID(uint pid)
{ {
var processHandle = NativeMethods.OpenProcess(ProcessAccessFlags.QueryLimitedInformation, true, (int)pid); var processHandle = NativeMethods.OpenProcess(ProcessAccessFlags.QueryLimitedInformation, true, (int)pid);
StringBuilder processName = new StringBuilder(MaximumFileNameLength); var processName = new StringBuilder(MaximumFileNameLength);
if (NativeMethods.GetProcessImageFileName(processHandle, processName, MaximumFileNameLength) != 0) if (NativeMethods.GetProcessImageFileName(processHandle, processName, MaximumFileNameLength) != 0)
{ {

View File

@@ -97,7 +97,7 @@ public class VirtualDesktopHelper
var registryExplorerVirtualDesktops = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops"; // Windows 11 var registryExplorerVirtualDesktops = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops"; // Windows 11
// List of all desktops // List of all desktops
using RegistryKey? virtualDesktopKey = Registry.CurrentUser.OpenSubKey(registryExplorerVirtualDesktops, false); using var virtualDesktopKey = Registry.CurrentUser.OpenSubKey(registryExplorerVirtualDesktops, false);
if (virtualDesktopKey is not null) if (virtualDesktopKey is not null)
{ {
var allDeskValue = (byte[]?)virtualDesktopKey.GetValue("VirtualDesktopIDs", null) ?? Array.Empty<byte>(); var allDeskValue = (byte[]?)virtualDesktopKey.GetValue("VirtualDesktopIDs", null) ?? Array.Empty<byte>();
@@ -123,7 +123,7 @@ public class VirtualDesktopHelper
// Guid for current desktop // Guid for current desktop
var virtualDesktopsKeyName = _isWindowsEleven ? registryExplorerVirtualDesktops : registrySessionVirtualDesktops; var virtualDesktopsKeyName = _isWindowsEleven ? registryExplorerVirtualDesktops : registrySessionVirtualDesktops;
using RegistryKey? virtualDesktopsKey = Registry.CurrentUser.OpenSubKey(virtualDesktopsKeyName, false); using var virtualDesktopsKey = Registry.CurrentUser.OpenSubKey(virtualDesktopsKeyName, false);
if (virtualDesktopsKey is not null) if (virtualDesktopsKey is not null)
{ {
var currentVirtualDesktopValue = virtualDesktopsKey.GetValue("CurrentVirtualDesktop", null); var currentVirtualDesktopValue = virtualDesktopsKey.GetValue("CurrentVirtualDesktop", null);
@@ -166,8 +166,8 @@ public class VirtualDesktopHelper
UpdateDesktopList(); UpdateDesktopList();
} }
List<VDesktop> list = new List<VDesktop>(); var list = new List<VDesktop>();
foreach (Guid d in _availableDesktops) foreach (var d in _availableDesktops)
{ {
list.Add(CreateVDesktopInstance(d)); list.Add(CreateVDesktopInstance(d));
} }
@@ -265,7 +265,7 @@ public class VirtualDesktopHelper
var defaultName = string.Format(System.Globalization.CultureInfo.InvariantCulture, VirtualDesktopHelperDesktop, GetDesktopNumber(desktop)); var defaultName = string.Format(System.Globalization.CultureInfo.InvariantCulture, VirtualDesktopHelperDesktop, GetDesktopNumber(desktop));
var registryPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + desktop.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "}"; var registryPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + desktop.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "}";
using RegistryKey? deskSubKey = Registry.CurrentUser.OpenSubKey(registryPath, false); using var deskSubKey = Registry.CurrentUser.OpenSubKey(registryPath, false);
var desktopName = deskSubKey?.GetValue("Name"); var desktopName = deskSubKey?.GetValue("Name");
return (desktopName is not null) ? (string)desktopName : defaultName; return (desktopName is not null) ? (string)desktopName : defaultName;
@@ -336,7 +336,7 @@ public class VirtualDesktopHelper
return CreateVDesktopInstance(Guid.Empty); return CreateVDesktopInstance(Guid.Empty);
} }
var hr = _virtualDesktopManager.GetWindowDesktopId(hWindow, out Guid desktopId); var hr = _virtualDesktopManager.GetWindowDesktopId(hWindow, out var desktopId);
return (hr != (int)HRESULT.S_OK || desktopId == Guid.Empty) ? VDesktop.Empty : CreateVDesktopInstance(desktopId, hWindow); return (hr != (int)HRESULT.S_OK || desktopId == Guid.Empty) ? VDesktop.Empty : CreateVDesktopInstance(desktopId, hWindow);
} }
@@ -355,7 +355,7 @@ public class VirtualDesktopHelper
} }
_ = _virtualDesktopManager.IsWindowOnCurrentVirtualDesktop(hWindow, out var isOnCurrentDesktop); _ = _virtualDesktopManager.IsWindowOnCurrentVirtualDesktop(hWindow, out var isOnCurrentDesktop);
Guid windowDesktopId = desktop ?? Guid.Empty; // Prepare variable in case we have no input parameter for desktop var windowDesktopId = desktop ?? Guid.Empty; // Prepare variable in case we have no input parameter for desktop
var hResult = desktop is null ? GetWindowDesktopId(hWindow, out windowDesktopId) : 0; var hResult = desktop is null ? GetWindowDesktopId(hWindow, out windowDesktopId) : 0;
if (hResult != (int)HRESULT.S_OK) if (hResult != (int)HRESULT.S_OK)
@@ -438,7 +438,7 @@ public class VirtualDesktopHelper
/// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns> /// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns>
public bool MoveWindowOneDesktopLeft(IntPtr hWindow) public bool MoveWindowOneDesktopLeft(IntPtr hWindow)
{ {
var hr = GetWindowDesktopId(hWindow, out Guid windowDesktop); var hr = GetWindowDesktopId(hWindow, out var windowDesktop);
if (hr != (int)HRESULT.S_OK) if (hr != (int)HRESULT.S_OK)
{ {
ExtensionHost.LogMessage(new LogMessage() { Message = $"VirtualDesktopHelper.MoveWindowOneDesktopLeft() failed when moving the window ({hWindow}) one desktop left: Can't get current desktop of the window." }); ExtensionHost.LogMessage(new LogMessage() { Message = $"VirtualDesktopHelper.MoveWindowOneDesktopLeft() failed when moving the window ({hWindow}) one desktop left: Can't get current desktop of the window." });
@@ -458,7 +458,7 @@ public class VirtualDesktopHelper
return false; return false;
} }
Guid newDesktop = _availableDesktops[windowDesktopNumber - 1]; var newDesktop = _availableDesktops[windowDesktopNumber - 1];
return MoveWindowToDesktop(hWindow, ref newDesktop); return MoveWindowToDesktop(hWindow, ref newDesktop);
} }
@@ -469,7 +469,7 @@ public class VirtualDesktopHelper
/// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns> /// <returns><see langword="True"/> on success and <see langword="false"/> on failure.</returns>
public bool MoveWindowOneDesktopRight(IntPtr hWindow) public bool MoveWindowOneDesktopRight(IntPtr hWindow)
{ {
var hr = GetWindowDesktopId(hWindow, out Guid windowDesktop); var hr = GetWindowDesktopId(hWindow, out var windowDesktop);
if (hr != (int)HRESULT.S_OK) if (hr != (int)HRESULT.S_OK)
{ {
ExtensionHost.LogMessage(new LogMessage() { Message = $"VirtualDesktopHelper.MoveWindowOneDesktopRight() failed when moving the window ({hWindow}) one desktop right: Can't get current desktop of the window." }); ExtensionHost.LogMessage(new LogMessage() { Message = $"VirtualDesktopHelper.MoveWindowOneDesktopRight() failed when moving the window ({hWindow}) one desktop right: Can't get current desktop of the window." });
@@ -489,7 +489,7 @@ public class VirtualDesktopHelper
return false; return false;
} }
Guid newDesktop = _availableDesktops[windowDesktopNumber + 1]; var newDesktop = _availableDesktops[windowDesktopNumber + 1];
return MoveWindowToDesktop(hWindow, ref newDesktop); return MoveWindowToDesktop(hWindow, ref newDesktop);
} }
@@ -507,7 +507,7 @@ public class VirtualDesktopHelper
} }
// Can be only detected if method is invoked with window handle parameter. // Can be only detected if method is invoked with window handle parameter.
VirtualDesktopAssignmentType desktopType = (hWindow != default) ? GetWindowDesktopAssignmentType(hWindow, desktop) : VirtualDesktopAssignmentType.Unknown; var desktopType = (hWindow != default) ? GetWindowDesktopAssignmentType(hWindow, desktop) : VirtualDesktopAssignmentType.Unknown;
var isAllDesktops = (hWindow != default) && desktopType == VirtualDesktopAssignmentType.AllDesktops; var isAllDesktops = (hWindow != default) && desktopType == VirtualDesktopAssignmentType.AllDesktops;
var isDesktopVisible = (hWindow != default) ? (isAllDesktops || desktopType == VirtualDesktopAssignmentType.CurrentDesktop) : IsDesktopVisible(desktop); var isDesktopVisible = (hWindow != default) ? (isAllDesktops || desktopType == VirtualDesktopAssignmentType.CurrentDesktop) : IsDesktopVisible(desktop);