-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspaceManager.cs
More file actions
177 lines (155 loc) · 5.92 KB
/
WorkspaceManager.cs
File metadata and controls
177 lines (155 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using VirtualDesktop; // MScholtes namespace
namespace bws
{
public class WorkspaceItem
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public bool IsCurrent { get; set; }
}
public static class WorkspaceManager
{
private static List<Guid> _mruDesktops = new();
private static string? GetDesktopNameFromRegistry(Guid desktopId)
{
try
{
using var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
$@"Software\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops\Desktops\{desktopId:B}");
if (key != null)
{
var name = key.GetValue("Name") as string;
if (!string.IsNullOrWhiteSpace(name))
{
return name;
}
}
}
catch {}
return null;
}
public static Guid? GetDesktopIdForWindow(IntPtr hWnd)
{
try
{
var d = Desktop.FromWindow(hWnd);
if (d != null) return d.Id;
}
catch {}
return null;
}
public static void Initialize()
{
try
{
// Disable native Windows 11 slide animation
Desktop.SetAnimation(false);
Desktop? currentDesk = null;
try { currentDesk = Desktop.Current; }
catch { try { currentDesk = Desktop.FromWindow(Win32Interop.GetForegroundWindow()); } catch {} }
if (currentDesk == null) currentDesk = Desktop.FromIndex(0); // Fallback
_mruDesktops.Add(currentDesk.Id);
for (int i = 0; i < Desktop.Count; i++)
{
var d = Desktop.FromIndex(i);
if (d != null && d.Id != currentDesk.Id)
{
_mruDesktops.Add(d.Id);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"WorkspaceManager failed to initialize: {ex.Message}");
}
}
public static void Shutdown()
{
// No events to unhook in MScholtes implementation
}
public static List<WorkspaceItem> GetWorkspacesInMruOrder()
{
var result = new List<WorkspaceItem>();
try
{
Desktop? currentDesk = null;
try { currentDesk = Desktop.Current; }
catch { try { currentDesk = Desktop.FromWindow(Win32Interop.GetForegroundWindow()); } catch {} }
if (currentDesk == null) currentDesk = Desktop.FromIndex(0);
// Just-In-Time MRU update: Always ensure Current is at the front
if (currentDesk != null)
{
_mruDesktops.Remove(currentDesk.Id);
_mruDesktops.Insert(0, currentDesk.Id);
}
// Collect all known live desktops
var liveIds = new HashSet<Guid>();
for (int i = 0; i < Desktop.Count; i++)
{
var d = Desktop.FromIndex(i);
if (d != null)
{
liveIds.Add(d.Id);
if (!_mruDesktops.Contains(d.Id))
{
_mruDesktops.Add(d.Id);
}
}
}
// Remove deleted desktops
_mruDesktops.RemoveAll(id => !liveIds.Contains(id));
// Process in MRU order
for (int i = 0; i < _mruDesktops.Count; i++)
{
var id = _mruDesktops[i];
Desktop? desktop = null;
int index = -1;
for (int j = 0; j < Desktop.Count; j++)
{
var d = Desktop.FromIndex(j);
if (d != null && d.Id == id)
{
desktop = d;
index = j;
break;
}
}
if (desktop != null && currentDesk != null)
{
result.Add(new WorkspaceItem
{
Id = desktop.Id,
Name = GetDesktopNameFromRegistry(desktop.Id) ?? desktop.Name ?? $"Desktop {index + 1}",
IsCurrent = desktop.Id == currentDesk.Id
});
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to get workspaces: {ex.Message}");
}
return result;
}
public static void SwitchToWorkspace(Guid id)
{
try
{
// Use direct COM SwitchDesktop instead of MakeVisible.
// MakeVisible manipulates the taskbar focus (AttachThreadInput + SetForegroundWindow + SW_MINIMIZE)
// which causes pinned/all-desktop windows to steal focus from the intended target.
// Direct SwitchDesktop avoids this and lets the caller set focus immediately after.
var desktopId = id;
var ivd = DesktopManager.VirtualDesktopManagerInternal.FindDesktop(ref desktopId);
DesktopManager.VirtualDesktopManagerInternal.SwitchDesktop(ivd);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to switch workspace: {ex.Message}");
}
}
}
}