Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the latest Windows 10 build #57

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions samples/VirtualDesktop.Showcase/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<Setter Property="BorderThickness"
Value="0.99" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Padding"
Value="18,4" />
<Setter Property="HorizontalContentAlignment"
Value="Left" />
<Setter Property="Margin"
Value="8" />
<Setter Property="BorderThickness"
Value="0.99" />
</Style>
</Panel.Resources>
<StackPanel Margin="8">
<RadioButton x:Name="ThisWindowMenu"
Expand Down Expand Up @@ -56,6 +66,21 @@
Click="PinApp" />
<Button Content="Remove"
Click="Remove" />
<Button Content="Get name"
Click="GetName" />
<TextBox x:Name="NameTextBlock"
Margin="8,8,8,0"
Text="Desktop name" />
<Button Grid.Column="1"
Content="Set name"
Click="SetName"
Margin="8,0,8,8" />
<Button Content="Get wallpaper path"
Click="GetWallpaperPath" />
<Button Content="Move to previous"
Click="MovePrevious" />
<Button Content="Move to next"
Click="MoveNext" />
</StackPanel>
</ScrollViewer>
</Window>
75 changes: 75 additions & 0 deletions samples/VirtualDesktop.Showcase/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private static async void InitializeComObjects()
}

VirtualDesktop.CurrentChanged += (sender, args) => System.Diagnostics.Debug.WriteLine($"Desktop changed: {args.NewDesktop.Id}");
VirtualDesktop.Moved += (sender, args) => System.Diagnostics.Debug.WriteLine($"Desktop moved: {args.OldIndex} -> {args.NewIndex} ({args.Source.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 Expand Up @@ -141,6 +143,79 @@ private async void Remove(object sender, RoutedEventArgs e)
}
}

private async void GetName(object sender, RoutedEventArgs e)
{
if (this.ThisWindowMenu.IsChecked ?? false)
{
var name = this.GetCurrentDesktop().Name;
MessageBox.Show(name, "Current desktop name");
}
else
{
await Task.Delay(_delay);
var name = this.GetCurrentDesktop().Name;
MessageBox.Show(name, "Current desktop name");
}
}

private async void SetName(object sender, RoutedEventArgs e)
{
if (this.ThisWindowMenu.IsChecked ?? false)
{
try
{
this.GetCurrentDesktop().Name = this.NameTextBlock.Text;
}
catch (PlatformNotSupportedException ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
else
{
await Task.Delay(_delay);
try
{
this.GetCurrentDesktop().Name = this.NameTextBlock.Text;
}
catch (PlatformNotSupportedException ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
}

private async void GetWallpaperPath(object sender, RoutedEventArgs e)
{
if (this.ThisWindowMenu.IsChecked ?? false)
{
var name = this.GetCurrentDesktop().WallpaperPath;
MessageBox.Show(name, "Current wallpaper path");
}
else
{
await Task.Delay(_delay);
var name = this.GetCurrentDesktop().WallpaperPath;
MessageBox.Show(name, "Current wallpaper path");
}
}

private void MovePrevious(object sender, RoutedEventArgs e)
{
var desktop = this.GetCurrentDesktop();
if (desktop == null) return;

desktop.Move(desktop.Index - 1);
}

private void MoveNext(object sender, RoutedEventArgs e)
{
var desktop = this.GetCurrentDesktop();
if (desktop == null) return;

desktop.Move(desktop.Index + 1);
}


[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace WindowsDesktop.Interop
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IVirtualDesktop
{
bool IsViewVisible(object pView);
[return: MarshalAs(UnmanagedType.Bool)]
bool IsViewVisible(IApplicationView pView);

Guid GetID();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;

namespace WindowsDesktop.Interop
{
[ComImport]
[Guid("00000000-0000-0000-0000-000000000000") /* replace at runtime */]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IVirtualDesktop2
{
[return: MarshalAs(UnmanagedType.Bool)]
bool IsViewVisible(IApplicationView pView);

Guid GetID();

[return: MarshalAs(UnmanagedType.HString)]
string GetName();
}

// see also:
// https://github.com/MScholtes/VirtualDesktop/blob/f7c0018069f5500bce3b170a53fb71edee44ebec/VirtualDesktop.cs#L156-L173

public class VirtualDesktopCacheImpl2 : IVirtualDesktopCache
{
private readonly ConcurrentDictionary<Guid, VirtualDesktop> _wrappers = new ConcurrentDictionary<Guid, VirtualDesktop>();

public Func<Guid, object, VirtualDesktop> Factory { get; set; }

public VirtualDesktop GetOrCreate(object comObject)
{
if (comObject is IVirtualDesktop2)
{
return this._wrappers.GetOrAdd(((IVirtualDesktop)comObject).GetID(), id => this.Factory(id, comObject));
}

throw new ArgumentException();
}

public void Clear()
{
this._wrappers.Clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ internal interface IVirtualDesktopManagerInternal
{
int GetCount();

void MoveViewToDesktop(IApplicationView pView, IVirtualDesktop desktop);
void MoveViewToDesktop(IApplicationView pView, IVirtualDesktop pDesktop);

[return: MarshalAs(UnmanagedType.Bool)]
bool CanViewMoveDesktops(IApplicationView pView);

IVirtualDesktop GetCurrentDesktop();
Expand All @@ -20,12 +21,12 @@ internal interface IVirtualDesktopManagerInternal

IVirtualDesktop GetAdjacentDesktop(IVirtualDesktop pDesktopReference, AdjacentDesktop uDirection);

void SwitchDesktop(IVirtualDesktop desktop);
void SwitchDesktop(IVirtualDesktop pDesktop);

IVirtualDesktop CreateDesktopW();

void RemoveDesktop(IVirtualDesktop pRemove, IVirtualDesktop pFallbackDesktop);

IVirtualDesktop FindDesktop(ref Guid desktopId);
IVirtualDesktop FindDesktop([In, MarshalAs(UnmanagedType.LPStruct)] Guid desktopId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Runtime.InteropServices;

namespace WindowsDesktop.Interop
{
[ComImport]
[Guid("00000000-0000-0000-0000-000000000000") /* replace at runtime */]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IVirtualDesktopManagerInternal2
{
int GetCount();

void MoveViewToDesktop(IApplicationView pView, IVirtualDesktop pDesktop);

[return: MarshalAs(UnmanagedType.Bool)]
bool CanViewMoveDesktops(IApplicationView pView);

IVirtualDesktop GetCurrentDesktop();

IObjectArray GetDesktops();

IVirtualDesktop GetAdjacentDesktop(IVirtualDesktop pDesktopReference, AdjacentDesktop uDirection);

void SwitchDesktop(IVirtualDesktop pDesktop);

IVirtualDesktop CreateDesktopW();

void RemoveDesktop(IVirtualDesktop pRemove, IVirtualDesktop pFallbackDesktop);

IVirtualDesktop FindDesktop([In, MarshalAs(UnmanagedType.LPStruct)] Guid desktopId);

void Unknown1(IVirtualDesktop pDesktop, out IObjectArray pUnknown1, out IObjectArray pUnknown2);

void SetName(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string chName);
}

// see also:
// https://github.com/MScholtes/VirtualDesktop/blob/f7c0018069f5500bce3b170a53fb71edee44ebec/VirtualDesktop.cs#L193-L211
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Runtime.InteropServices;

namespace WindowsDesktop.Interop
{
[ComImport]
[Guid("00000000-0000-0000-0000-000000000000") /* replace at runtime */]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IVirtualDesktopManagerInternal3
{
int GetCount();

void MoveViewToDesktop(IApplicationView pView, IVirtualDesktop pDesktop);

[return: MarshalAs(UnmanagedType.Bool)]
bool CanViewMoveDesktops(IApplicationView pView);

IVirtualDesktop GetCurrentDesktop();

IObjectArray GetDesktops();

IVirtualDesktop GetAdjacentDesktop(IVirtualDesktop pDesktopReference, AdjacentDesktop uDirection);

void SwitchDesktop(IVirtualDesktop pDesktop);

IVirtualDesktop CreateDesktopW();

void RemoveDesktop(IVirtualDesktop pRemove, IVirtualDesktop pFallbackDesktop);

IVirtualDesktop FindDesktop([In, MarshalAs(UnmanagedType.LPStruct)] Guid desktopId);

void Unknown1(IVirtualDesktop pDesktop, out IObjectArray pUnknown1, out IObjectArray pUnknown2);

void SetName(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string chName);

void Unknown2(IApplicationView pUnknown0, IApplicationView pUnknown1);
}

// see also:
// https://github.com/MScholtes/VirtualDesktop/blob/f7c0018069f5500bce3b170a53fb71edee44ebec/VirtualDesktop.cs#L193-L211
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IVirtualDesktopNotification

void VirtualDesktopDestroyed(IVirtualDesktop pDesktopDestroyed, IVirtualDesktop pDesktopFallback);

void ViewVirtualDesktopChanged(IntPtr pView);
void ViewVirtualDesktopChanged(IApplicationView pView);

void CurrentVirtualDesktopChanged(IVirtualDesktop pDesktopOld, IVirtualDesktop pDesktopNew);
}
Expand All @@ -43,7 +43,7 @@ public void VirtualDesktopDestroyed(IVirtualDesktop pDesktopDestroyed, IVirtualD
this.VirtualDesktopDestroyedCore(pDesktopDestroyed, pDesktopFallback);
}

public void ViewVirtualDesktopChanged(IntPtr pView)
public void ViewVirtualDesktopChanged(IApplicationView pView)
{
this.ViewVirtualDesktopChangedCore(pView);
}
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(IApplicationView pView);

void CurrentVirtualDesktopChanged(IVirtualDesktop pDesktopOld, IVirtualDesktop pDesktopNew);

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

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(IApplicationView pView)
{
this.ViewVirtualDesktopChangedCore(pView);
}

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

public void VirtualDesktopRenamed(IVirtualDesktop pDesktop, [MarshalAs(UnmanagedType.HString)] string chName)
{
this.VirtualDesktopRenamedCore(pDesktop, chName);
}
}
}
Loading