Skip to content

Commit ab028d6

Browse files
committed
Add virtual desktop rename notification support
1 parent 5edd471 commit ab028d6

File tree

8 files changed

+143
-21
lines changed

8 files changed

+143
-21
lines changed

samples/VirtualDesktop.Showcase/MainWindow.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ private static async void InitializeComObjects()
3030
}
3131

3232
VirtualDesktop.CurrentChanged += (sender, args) => System.Diagnostics.Debug.WriteLine($"Desktop changed: {args.NewDesktop.Id}");
33+
VirtualDesktop.Renamed += (sender, args) => System.Diagnostics.Debug.WriteLine($"Desktop renamed: {args.OldName} -> {args.NewName} ({args.Source.Id})");
3334
}
3435

3536
private void CreateNew(object sender, RoutedEventArgs e)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace WindowsDesktop.Interop
5+
{
6+
[ComImport]
7+
[Guid("00000000-0000-0000-0000-000000000000") /* replace at runtime */]
8+
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
9+
public interface IVirtualDesktopNotification2
10+
{
11+
void VirtualDesktopCreated(IVirtualDesktop pDesktop);
12+
13+
void VirtualDesktopDestroyBegin(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);
14+
15+
void VirtualDesktopDestroyFailed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);
16+
17+
void VirtualDesktopDestroyed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);
18+
19+
void ViewVirtualDesktopChanged(IntPtr pView);
20+
21+
void CurrentVirtualDesktopChanged(IVirtualDesktop pDesktopOld, IVirtualDesktop pDesktopNew);
22+
23+
void VirtualDesktopRenamed(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string name);
24+
}
25+
26+
public class VirtualDesktopNotificationListener2 : VirtualDesktopNotification, IVirtualDesktopNotification, IVirtualDesktopNotification2
27+
{
28+
public void VirtualDesktopCreated(IVirtualDesktop pDesktop)
29+
{
30+
this.VirtualDesktopCreatedCore(pDesktop);
31+
}
32+
33+
public void VirtualDesktopDestroyBegin(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback)
34+
{
35+
this.VirtualDesktopDestroyBeginCore(pDesktopDestroyed, pDesktopFallback);
36+
}
37+
38+
public void VirtualDesktopDestroyFailed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback)
39+
{
40+
this.VirtualDesktopDestroyFailedCore(pDesktopDestroyed, pDesktopFallback);
41+
}
42+
43+
public void VirtualDesktopDestroyed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback)
44+
{
45+
this.VirtualDesktopDestroyedCore(pDesktopDestroyed, pDesktopFallback);
46+
}
47+
48+
public void ViewVirtualDesktopChanged(IntPtr pView)
49+
{
50+
this.ViewVirtualDesktopChangedCore(pView);
51+
}
52+
53+
public void CurrentVirtualDesktopChanged(IVirtualDesktop pDesktopOld, IVirtualDesktop pDesktopNew)
54+
{
55+
this.CurrentVirtualDesktopChangedCore(pDesktopOld, pDesktopNew);
56+
}
57+
58+
public void VirtualDesktopRenamed(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string name)
59+
{
60+
this.VirtualDesktopRenamedCore(pDesktop, name);
61+
}
62+
}
63+
}

source/VirtualDesktop/Interop/(wrappers)/VirtualDesktopNotification.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55

66
namespace WindowsDesktop.Interop
77
{
8-
[ComInterfaceWrapper]
8+
[ComInterfaceWrapper(2)]
99
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
1010
public abstract class VirtualDesktopNotification
1111
{
1212
internal static VirtualDesktopNotification CreateInstance(ComInterfaceAssembly assembly)
1313
{
14-
var type = assembly.GetType("VirtualDesktopNotificationListener");
15-
var instance = (VirtualDesktopNotification)Activator.CreateInstance(type);
16-
17-
return instance;
14+
var type2 = assembly.GetType("VirtualDesktopNotificationListener2");
15+
if (type2 != null) {
16+
var instance = (VirtualDesktopNotification)Activator.CreateInstance(type2);
17+
return instance;
18+
}
19+
else
20+
{
21+
var type = assembly.GetType("VirtualDesktopNotificationListener");
22+
var instance = (VirtualDesktopNotification)Activator.CreateInstance(type);
23+
return instance;
24+
}
1825
}
1926

2027
protected VirtualDesktop GetDesktop(object comObject)
@@ -49,5 +56,10 @@ protected void CurrentVirtualDesktopChangedCore(object pDesktopOld, object pDesk
4956
{
5057
VirtualDesktop.EventRaiser.RaiseCurrentChanged(this, VirtualDesktopCache.GetOrCreate(pDesktopOld), VirtualDesktopCache.GetOrCreate(pDesktopNew));
5158
}
59+
60+
protected void VirtualDesktopRenamedCore(object pDesktop, string name)
61+
{
62+
VirtualDesktop.EventRaiser.RaiseRenamed(this, VirtualDesktopCache.GetOrCreate(pDesktop), name);
63+
}
5264
}
5365
}

source/VirtualDesktop/VirtualDesktop.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Diagnostics;
44
using System.Linq;
55
using System.Runtime.InteropServices;
6-
using System.Text;
76
using WindowsDesktop.Interop;
87
using JetBrains.Annotations;
98

@@ -22,26 +21,14 @@ public partial class VirtualDesktop : ComInterfaceWrapperBase
2221
/// </summary>
2322
public Guid Id { get; }
2423

24+
private string _name = null;
25+
2526
/// <summary>
2627
/// Gets the name for the virtual desktop.
2728
/// </summary>
2829
public string Name
2930
{
30-
get
31-
{
32-
if (this.ComVersion >= 2)
33-
{
34-
var name = this.Invoke<string>(Args(), "GetName");
35-
if (!string.IsNullOrEmpty(name))
36-
{
37-
return name;
38-
}
39-
}
40-
41-
var desktops = GetDesktops();
42-
var index = Array.IndexOf(desktops, this) + 1;
43-
return $"Desktop {index}";
44-
}
31+
get => this._name;
4532
set
4633
{
4734
if (this.ComVersion < 2) throw new PlatformNotSupportedException("This Windows 10 version is not supported.");
@@ -55,6 +42,11 @@ internal VirtualDesktop(ComInterfaceAssembly assembly, Guid id, object comObject
5542
: base(assembly, comObject, latestVersion: 2)
5643
{
5744
this.Id = id;
45+
46+
if (this.ComVersion >= 2)
47+
{
48+
this._name = this.Invoke<string>(Args(), "GetName");
49+
}
5850
}
5951

6052
/// <summary>
@@ -115,5 +107,10 @@ public VirtualDesktop GetRight()
115107
return null;
116108
}
117109
}
110+
111+
private void SetNameToCache(string name)
112+
{
113+
this._name = name;
114+
}
118115
}
119116
}

source/VirtualDesktop/VirtualDesktop.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopManagerInternal.cs" />
3535
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopManagerInternal2.cs" />
3636
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopNotification.cs" />
37+
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopNotification2.cs" />
3738
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopNotificationService.cs" />
3839
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopPinnedApps.cs" />
3940
</ItemGroup>
@@ -48,6 +49,7 @@
4849
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopManagerInternal.cs" />
4950
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopManagerInternal2.cs" />
5051
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopNotification.cs" />
52+
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopNotification2.cs" />
5153
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopNotificationService.cs" />
5254
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopPinnedApps.cs" />
5355
<None Include="..\..\LICENSE">

source/VirtualDesktop/VirtualDesktop.static.notification.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ partial class VirtualDesktop
2929
/// Occurs when the current virtual desktop is changed.
3030
/// </summary>
3131
public static event EventHandler<VirtualDesktopChangedEventArgs> CurrentChanged;
32+
33+
/// <summary>
34+
/// Occurs when a virtual desktop is renamed.
35+
/// </summary>
36+
public static event EventHandler<VirtualDesktopRenamedEventArgs> Renamed;
3237

3338
internal static class EventRaiser
3439
{
@@ -65,6 +70,15 @@ public static void RaiseCurrentChanged(object sender, VirtualDesktop pDesktopOld
6570
var args = new VirtualDesktopChangedEventArgs(pDesktopOld, pDesktopNew);
6671
CurrentChanged?.Invoke(sender, args);
6772
}
73+
74+
public static void RaiseRenamed(object sender, VirtualDesktop pDesktop, string name)
75+
{
76+
var oldName = pDesktop.Name;
77+
pDesktop.SetNameToCache(name);
78+
79+
var args = new VirtualDesktopRenamedEventArgs(pDesktop, oldName, name);
80+
Renamed?.Invoke(sender, args);
81+
}
6882
}
6983
}
7084
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace WindowsDesktop
4+
{
5+
/// <summary>
6+
/// Provides data for the <see cref="VirtualDesktop.Renamed" /> event.
7+
/// </summary>
8+
public class VirtualDesktopRenamedEventArgs : EventArgs
9+
{
10+
/// <summary>
11+
/// Gets the virtual desktop that was renamed.
12+
/// </summary>
13+
public VirtualDesktop Source { get; }
14+
15+
/// <summary>
16+
/// Gets the old name of the virtual desktop.
17+
/// </summary>
18+
public string OldName { get; }
19+
20+
/// <summary>
21+
/// Gets the new name of the virtual desktop.
22+
/// </summary>
23+
public string NewName { get; }
24+
25+
public VirtualDesktopRenamedEventArgs(VirtualDesktop source, string oldName, string newName)
26+
{
27+
this.Source = source;
28+
this.OldName = oldName;
29+
this.NewName = newName;
30+
}
31+
}
32+
}

source/VirtualDesktop/app.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string>IVirtualDesktopManagerInternal,{F31574D6-B682-4CDC-BD56-1827860ABEC6} </string>
2222
<string>IVirtualDesktopManagerInternal2,{0F3A72B0-4566-487E-9A33-4ED302F6D6CE} </string>
2323
<string>IVirtualDesktopNotification,{C179334C-4295-40D3-BEA1-C654D965605A} </string>
24+
<string>IVirtualDesktopNotification2,{1BA7CF30-3591-43FA-ABFA-4AAF7ABEEDB7} </string>
2425
<string>IVirtualDesktopNotificationService,{0CD45E71-D927-4F15-8B0A-8FEF525337BF} </string>
2526
<string>IVirtualDesktopPinnedApps,{4CE81583-1E4C-4632-A621-07A53543148F} </string>
2627
</ArrayOfString>

0 commit comments

Comments
 (0)