[PTRun]Addressed some disposable + VirtualDesktopHelper cleanup (#29890)

* Addressed some disposable + VirtualDesktopHelper cleanup

* typos

* remove user session id cache
This commit is contained in:
Davide Giacometti
2023-11-23 17:16:12 +01:00
committed by GitHub
parent 9c1fb53d2a
commit df14a5dbb3
2 changed files with 52 additions and 59 deletions

View File

@@ -550,6 +550,7 @@ namespace PowerLauncher.ViewModel
var queryTimer = new System.Diagnostics.Stopwatch(); var queryTimer = new System.Diagnostics.Stopwatch();
queryTimer.Start(); queryTimer.Start();
_updateSource?.Cancel(); _updateSource?.Cancel();
_updateSource?.Dispose();
var currentUpdateSource = new CancellationTokenSource(); var currentUpdateSource = new CancellationTokenSource();
_updateSource = currentUpdateSource; _updateSource = currentUpdateSource;
var currentCancellationToken = _updateSource.Token; var currentCancellationToken = _updateSource.Token;

View File

@@ -6,6 +6,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Common.UI;
using Microsoft.Win32; using Microsoft.Win32;
using Wox.Plugin.Common.VirtualDesktop.Interop; using Wox.Plugin.Common.VirtualDesktop.Interop;
using Wox.Plugin.Common.Win32; using Wox.Plugin.Common.Win32;
@@ -29,7 +30,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
/// <summary> /// <summary>
/// Are we running on Windows 11 /// Are we running on Windows 11
/// </summary> /// </summary>
private static readonly bool _IsWindowsEleven = IsWindowsElevenOrLater(); private readonly bool _isWindowsEleven;
/// <summary> /// <summary>
/// Instance of "Virtual Desktop Manager" /// Instance of "Virtual Desktop Manager"
@@ -46,12 +47,12 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
/// List of all available Virtual Desktop in their real order /// List of all available Virtual Desktop in their real order
/// The order and list in the registry is always up to date /// The order and list in the registry is always up to date
/// </summary> /// </summary>
private List<Guid> availableDesktops = new List<Guid>(); private List<Guid> _availableDesktops = new List<Guid>();
/// <summary> /// <summary>
/// Id of the current visible Desktop. /// Id of the current visible Desktop.
/// </summary> /// </summary>
private Guid currentDesktop; private Guid _currentDesktop;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="VirtualDesktopHelper"/> class. /// Initializes a new instance of the <see cref="VirtualDesktopHelper"/> class.
@@ -69,6 +70,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
return; return;
} }
_isWindowsEleven = OSVersionHelper.IsWindows11();
_desktopListAutoUpdate = desktopListUpdate; _desktopListAutoUpdate = desktopListUpdate;
UpdateDesktopList(); UpdateDesktopList();
} }
@@ -88,46 +90,52 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
/// <remarks>If we can not read from registry, we set the list/guid to empty values.</remarks> /// <remarks>If we can not read from registry, we set the list/guid to empty values.</remarks>
public void UpdateDesktopList() public void UpdateDesktopList()
{ {
// Registry paths
int userSessionId = Process.GetCurrentProcess().SessionId; int userSessionId = Process.GetCurrentProcess().SessionId;
string registrySessionVirtualDesktops = $"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\{userSessionId}\\VirtualDesktops"; string registrySessionVirtualDesktops = $"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\SessionInfo\\{userSessionId}\\VirtualDesktops"; // Windows 10
string registryExplorerVirtualDesktops = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops"; string registryExplorerVirtualDesktops = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops"; // Windows 11
// List of all desktops // List of all desktops
byte[] allDeskValue = (byte[])Registry.CurrentUser.OpenSubKey(registryExplorerVirtualDesktops, false)?.GetValue("VirtualDesktopIDs", null); using RegistryKey virtualDesktopKey = Registry.CurrentUser.OpenSubKey(registryExplorerVirtualDesktops, false);
if (allDeskValue != null) if (virtualDesktopKey != null)
{ {
// We clear only, if we can read from registry. Otherwise we keep the existing values. byte[] allDeskValue = (byte[])virtualDesktopKey.GetValue("VirtualDesktopIDs", null);
availableDesktops.Clear(); if (allDeskValue != null)
// Each guid has a length of 16 elements
int numberOfDesktops = allDeskValue.Length / 16;
for (int i = 0; i < numberOfDesktops; i++)
{ {
byte[] guidArray = new byte[16]; // We clear only, if we can read from registry. Otherwise we keep the existing values.
Array.ConstrainedCopy(allDeskValue, i * 16, guidArray, 0, 16); _availableDesktops.Clear();
availableDesktops.Add(new Guid(guidArray));
// Each guid has a length of 16 elements
int numberOfDesktops = allDeskValue.Length / 16;
for (int i = 0; i < numberOfDesktops; i++)
{
byte[] guidArray = new byte[16];
Array.ConstrainedCopy(allDeskValue, i * 16, guidArray, 0, 16);
_availableDesktops.Add(new Guid(guidArray));
}
}
else
{
Log.Debug("VirtualDesktopHelper.UpdateDesktopList() failed to read the list of existing desktops form registry.", typeof(VirtualDesktopHelper));
} }
}
else
{
Log.Debug("VirtualDesktopHelper.UpdateDesktopList() failed to read the list of existing desktops form registry.", typeof(VirtualDesktopHelper));
} }
// Guid for current desktop // Guid for current desktop
var currentDeskSessionValue = Registry.CurrentUser.OpenSubKey(registrySessionVirtualDesktops, false)?.GetValue("CurrentVirtualDesktop", null); // Windows 10 var virtualDesktopsKeyName = _isWindowsEleven ? registryExplorerVirtualDesktops : registrySessionVirtualDesktops;
var currentDeskExplorerValue = Registry.CurrentUser.OpenSubKey(registryExplorerVirtualDesktops, false)?.GetValue("CurrentVirtualDesktop", null); // Windows 11 using RegistryKey virtualDesktopsKey = Registry.CurrentUser.OpenSubKey(virtualDesktopsKeyName, false);
var currentDeskValue = _IsWindowsEleven ? currentDeskExplorerValue : currentDeskSessionValue; if (virtualDesktopsKey != null)
if (currentDeskValue != null)
{ {
currentDesktop = new Guid((byte[])currentDeskValue); var currentVirtualDesktopValue = virtualDesktopsKey.GetValue("CurrentVirtualDesktop", null);
} if (currentVirtualDesktopValue != null)
else {
{ _currentDesktop = new Guid((byte[])currentVirtualDesktopValue);
// The registry value is missing when the user hasn't switched the desktop at least one time before reading the registry. In this case we can set it to desktop one. }
// We can only set it to desktop one, if we have at least one desktop in the desktops list. Otherwise we keep the existing value. else
Log.Debug("VirtualDesktopHelper.UpdateDesktopList() failed to read the id for the current desktop form registry.", typeof(VirtualDesktopHelper)); {
currentDesktop = availableDesktops.Count >= 1 ? availableDesktops[0] : currentDesktop; // The registry value is missing when the user hasn't switched the desktop at least one time before reading the registry. In this case we can set it to desktop one.
// We can only set it to desktop one, if we have at least one desktop in the desktops list. Otherwise we keep the existing value.
Log.Debug("VirtualDesktopHelper.UpdateDesktopList() failed to read the id for the current desktop form registry.", typeof(VirtualDesktopHelper));
_currentDesktop = _availableDesktops.Count >= 1 ? _availableDesktops[0] : _currentDesktop;
}
} }
} }
@@ -142,7 +150,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
UpdateDesktopList(); UpdateDesktopList();
} }
return availableDesktops; return _availableDesktops;
} }
/// <summary> /// <summary>
@@ -157,7 +165,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
} }
List<VDesktop> list = new List<VDesktop>(); List<VDesktop> list = new List<VDesktop>();
foreach (Guid d in availableDesktops) foreach (Guid d in _availableDesktops)
{ {
list.Add(CreateVDesktopInstance(d)); list.Add(CreateVDesktopInstance(d));
} }
@@ -176,7 +184,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
UpdateDesktopList(); UpdateDesktopList();
} }
return availableDesktops.Count; return _availableDesktops.Count;
} }
/// <summary> /// <summary>
@@ -190,7 +198,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
UpdateDesktopList(); UpdateDesktopList();
} }
return currentDesktop; return _currentDesktop;
} }
/// <summary> /// <summary>
@@ -204,7 +212,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
UpdateDesktopList(); UpdateDesktopList();
} }
return CreateVDesktopInstance(currentDesktop); return CreateVDesktopInstance(_currentDesktop);
} }
/// <summary> /// <summary>
@@ -219,7 +227,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
UpdateDesktopList(); UpdateDesktopList();
} }
return currentDesktop == desktop; return _currentDesktop == desktop;
} }
/// <summary> /// <summary>
@@ -235,7 +243,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
} }
// Adding +1 because index starts with zero and humans start counting with one. // Adding +1 because index starts with zero and humans start counting with one.
return availableDesktops.IndexOf(desktop) + 1; return _availableDesktops.IndexOf(desktop) + 1;
} }
/// <summary> /// <summary>
@@ -255,7 +263,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
var defaultName = string.Format(System.Globalization.CultureInfo.InvariantCulture, Resources.VirtualDesktopHelper_Desktop, GetDesktopNumber(desktop)); var defaultName = string.Format(System.Globalization.CultureInfo.InvariantCulture, Resources.VirtualDesktopHelper_Desktop, GetDesktopNumber(desktop));
string registryPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + desktop.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "}"; string registryPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + desktop.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "}";
RegistryKey deskSubKey = Registry.CurrentUser.OpenSubKey(registryPath, false); using RegistryKey deskSubKey = Registry.CurrentUser.OpenSubKey(registryPath, false);
var desktopName = deskSubKey?.GetValue("Name"); var desktopName = deskSubKey?.GetValue("Name");
return (desktopName != null) ? (string)desktopName : defaultName; return (desktopName != null) ? (string)desktopName : defaultName;
@@ -448,7 +456,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
return false; return false;
} }
Guid newDesktop = availableDesktops[windowDesktopNumber - 1]; Guid newDesktop = _availableDesktops[windowDesktopNumber - 1];
return MoveWindowToDesktop(hWindow, newDesktop); return MoveWindowToDesktop(hWindow, newDesktop);
} }
@@ -479,7 +487,7 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
return false; return false;
} }
Guid newDesktop = availableDesktops[windowDesktopNumber + 1]; Guid newDesktop = _availableDesktops[windowDesktopNumber + 1];
return MoveWindowToDesktop(hWindow, newDesktop); return MoveWindowToDesktop(hWindow, newDesktop);
} }
@@ -511,22 +519,6 @@ namespace Wox.Plugin.Common.VirtualDesktop.Helper
Position = GetDesktopPositionType(desktop), Position = GetDesktopPositionType(desktop),
}; };
} }
/// <summary>
/// Check if we running on Windows 11 or later.
/// </summary>
/// <returns><see langword="True"/> if yes and <see langword="false"/> if no.</returns>
private static bool IsWindowsElevenOrLater()
{
var currentBuildString = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", false)?.GetValue("CurrentBuild", null) ?? uint.MinValue;
uint currentBuild = uint.TryParse(currentBuildString as string, out var build) ? build : uint.MinValue;
var currentBuildNumberString = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", false)?.GetValue("CurrentBuildNumber", null) ?? uint.MinValue;
uint currentBuildNumber = uint.TryParse(currentBuildNumberString as string, out var buildNumber) ? buildNumber : uint.MinValue;
uint currentWindowsBuild = currentBuild != uint.MinValue ? currentBuild : currentBuildNumber;
return currentWindowsBuild >= 22000;
}
} }
/// <summary> /// <summary>