Skip to content

Commit

Permalink
Add virtual desktop rename notification support
Browse files Browse the repository at this point in the history
  • Loading branch information
mntone committed Jul 30, 2020
1 parent 5edd471 commit dc1dc87
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 21 deletions.
1 change: 1 addition & 0 deletions samples/VirtualDesktop.Showcase/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private static async void InitializeComObjects()
}

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

private void CreateNew(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Runtime.InteropServices;

namespace WindowsDesktop.Interop
{
[ComImport]
[Guid("00000000-0000-0000-0000-000000000000") /* replace at runtime */]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IVirtualDesktopNotification2
{
void VirtualDesktopCreated(IVirtualDesktop pDesktop);

void VirtualDesktopDestroyBegin(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);

void VirtualDesktopDestroyFailed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);

void VirtualDesktopDestroyed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);

void ViewVirtualDesktopChanged(IntPtr pView);

void CurrentVirtualDesktopChanged(IVirtualDesktop pDesktopOld, IVirtualDesktop pDesktopNew);

void VirtualDesktopRenamed(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string name);
}

public class VirtualDesktopNotificationListener2 : VirtualDesktopNotification, IVirtualDesktopNotification, IVirtualDesktopNotification2
{
public void VirtualDesktopCreated(IVirtualDesktop pDesktop)
{
this.VirtualDesktopCreatedCore(pDesktop);
}

public void VirtualDesktopDestroyBegin(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback)
{
this.VirtualDesktopDestroyBeginCore(pDesktopDestroyed, pDesktopFallback);
}

public void VirtualDesktopDestroyFailed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback)
{
this.VirtualDesktopDestroyFailedCore(pDesktopDestroyed, pDesktopFallback);
}

public void VirtualDesktopDestroyed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback)
{
this.VirtualDesktopDestroyedCore(pDesktopDestroyed, pDesktopFallback);
}

public void ViewVirtualDesktopChanged(IntPtr pView)
{
this.ViewVirtualDesktopChangedCore(pView);
}

public void CurrentVirtualDesktopChanged(IVirtualDesktop pDesktopOld, IVirtualDesktop pDesktopNew)
{
this.CurrentVirtualDesktopChangedCore(pDesktopOld, pDesktopNew);
}

public void VirtualDesktopRenamed(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string name)
{
this.VirtualDesktopRenamedCore(pDesktop, name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

namespace WindowsDesktop.Interop
{
[ComInterfaceWrapper]
[ComInterfaceWrapper(2)]
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public abstract class VirtualDesktopNotification
{
internal static VirtualDesktopNotification CreateInstance(ComInterfaceAssembly assembly)
{
var type = assembly.GetType("VirtualDesktopNotificationListener");
var instance = (VirtualDesktopNotification)Activator.CreateInstance(type);

return instance;
var type2 = assembly.GetType("VirtualDesktopNotificationListener2");
if (type2 != null) {
var instance = (VirtualDesktopNotification)Activator.CreateInstance(type2);
return instance;
}
else
{
var type = assembly.GetType("VirtualDesktopNotificationListener");
var instance = (VirtualDesktopNotification)Activator.CreateInstance(type);
return instance;
}
}

protected VirtualDesktop GetDesktop(object comObject)
Expand Down Expand Up @@ -49,5 +56,10 @@ protected void CurrentVirtualDesktopChangedCore(object pDesktopOld, object pDesk
{
VirtualDesktop.EventRaiser.RaiseCurrentChanged(this, VirtualDesktopCache.GetOrCreate(pDesktopOld), VirtualDesktopCache.GetOrCreate(pDesktopNew));
}

protected void VirtualDesktopRenamedCore(object pDesktop, string name)
{
VirtualDesktop.EventRaiser.RaiseRenamed(this, VirtualDesktopCache.GetOrCreate(pDesktop), name);
}
}
}
33 changes: 17 additions & 16 deletions source/VirtualDesktop/VirtualDesktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using WindowsDesktop.Interop;
using JetBrains.Annotations;

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

private string _name = null;

/// <summary>
/// Gets the name for the virtual desktop.
/// </summary>
public string Name
{
get
{
if (this.ComVersion >= 2)
{
var name = this.Invoke<string>(Args(), "GetName");
if (!string.IsNullOrEmpty(name))
{
return name;
}
}

var desktops = GetDesktops();
var index = Array.IndexOf(desktops, this) + 1;
return $"Desktop {index}";
}
get => this._name;
set
{
if (this.ComVersion < 2) throw new PlatformNotSupportedException("This Windows 10 version is not supported.");
Expand All @@ -55,6 +42,7 @@ internal VirtualDesktop(ComInterfaceAssembly assembly, Guid id, object comObject
: base(assembly, comObject, latestVersion: 2)
{
this.Id = id;
this.UpdateName();
}

/// <summary>
Expand Down Expand Up @@ -115,5 +103,18 @@ public VirtualDesktop GetRight()
return null;
}
}

private void UpdateName()
{
if (this.ComVersion >= 2)
{
this._name = this.Invoke<string>(Args(), "GetName");
}
}

private void SetNameToCache(string name)
{
this._name = name;
}
}
}
2 changes: 2 additions & 0 deletions source/VirtualDesktop/VirtualDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopManagerInternal.cs" />
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopManagerInternal2.cs" />
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopNotification.cs" />
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopNotification2.cs" />
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopNotificationService.cs" />
<Compile Remove="Interop\%28interfaces%29\IVirtualDesktopPinnedApps.cs" />
</ItemGroup>
Expand All @@ -48,6 +49,7 @@
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopManagerInternal.cs" />
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopManagerInternal2.cs" />
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopNotification.cs" />
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopNotification2.cs" />
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopNotificationService.cs" />
<EmbeddedResource Include="Interop\(interfaces)\IVirtualDesktopPinnedApps.cs" />
<None Include="..\..\LICENSE">
Expand Down
14 changes: 14 additions & 0 deletions source/VirtualDesktop/VirtualDesktop.static.notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ partial class VirtualDesktop
/// Occurs when the current virtual desktop is changed.
/// </summary>
public static event EventHandler<VirtualDesktopChangedEventArgs> CurrentChanged;

/// <summary>
/// Occurs when a virtual desktop is renamed.
/// </summary>
public static event EventHandler<VirtualDesktopRenamedEventArgs> Renamed;

internal static class EventRaiser
{
Expand Down Expand Up @@ -65,6 +70,15 @@ public static void RaiseCurrentChanged(object sender, VirtualDesktop pDesktopOld
var args = new VirtualDesktopChangedEventArgs(pDesktopOld, pDesktopNew);
CurrentChanged?.Invoke(sender, args);
}

public static void RaiseRenamed(object sender, VirtualDesktop pDesktop, string name)
{
var oldName = pDesktop.Name;
pDesktop.SetNameToCache(name);

var args = new VirtualDesktopRenamedEventArgs(pDesktop, oldName, name);
Renamed?.Invoke(sender, args);
}
}
}
}
32 changes: 32 additions & 0 deletions source/VirtualDesktop/VirtualDesktopRenamedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace WindowsDesktop
{
/// <summary>
/// Provides data for the <see cref="VirtualDesktop.Renamed" /> event.
/// </summary>
public class VirtualDesktopRenamedEventArgs : EventArgs
{
/// <summary>
/// Gets the virtual desktop that was renamed.
/// </summary>
public VirtualDesktop Source { get; }

/// <summary>
/// Gets the old name of the virtual desktop.
/// </summary>
public string OldName { get; }

/// <summary>
/// Gets the new name of the virtual desktop.
/// </summary>
public string NewName { get; }

public VirtualDesktopRenamedEventArgs(VirtualDesktop source, string oldName, string newName)
{
this.Source = source;
this.OldName = oldName;
this.NewName = newName;
}
}
}
1 change: 1 addition & 0 deletions source/VirtualDesktop/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<string>IVirtualDesktopManagerInternal,{F31574D6-B682-4CDC-BD56-1827860ABEC6} </string>
<string>IVirtualDesktopManagerInternal2,{0F3A72B0-4566-487E-9A33-4ED302F6D6CE} </string>
<string>IVirtualDesktopNotification,{C179334C-4295-40D3-BEA1-C654D965605A} </string>
<string>IVirtualDesktopNotification2,{1BA7CF30-3591-43FA-ABFA-4AAF7ABEEDB7} </string>
<string>IVirtualDesktopNotificationService,{0CD45E71-D927-4F15-8B0A-8FEF525337BF} </string>
<string>IVirtualDesktopPinnedApps,{4CE81583-1E4C-4632-A621-07A53543148F} </string>
</ArrayOfString>
Expand Down

0 comments on commit dc1dc87

Please sign in to comment.