Skip to content

Commit

Permalink
version 2.0 (draft)
Browse files Browse the repository at this point in the history
- Major changes in management for com objects.
- Add support moving the window of another process.
  • Loading branch information
Grabacr07 committed Aug 1, 2016
1 parent 9d84dd6 commit 634d886
Show file tree
Hide file tree
Showing 17 changed files with 497 additions and 252 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -7,8 +7,7 @@ VirtualDesktop is C# wrapper for [IVirtualDesktopManager](https://msdn.microsoft

* Switch, add, and remove a Virtual Desktop.
* Move the window in the same process to any Virtual Desktop.
* **[CANNOT]** Move the window of another process to any Virtual Desktop.
(alternate method: [https://github.com/tmyt/VDMHelper](https://github.com/tmyt/VDMHelper))
* Move the window of another process to any Virtual Desktop (Support in version 2.0 or later).


## Installation
Expand Down
12 changes: 10 additions & 2 deletions samples/VirtualDesktop.Showcase/MainWindow.xaml.cs
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using WindowsDesktop;
using WindowsDesktop.Interop;

namespace VirtualDesktopShowcase
{
Expand All @@ -14,6 +13,11 @@ partial class MainWindow
public MainWindow()
{
this.InitializeComponent();

foreach (var id in VirtualDesktop.GetDesktops().Select(x => x.Id))
{
System.Diagnostics.Debug.WriteLine(id);
}
}

private void CreateNew(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -61,5 +65,9 @@ private void Pin(object sender, RoutedEventArgs e)
{
this.TogglePin();
}


[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
}
}
16 changes: 7 additions & 9 deletions samples/VirtualDesktop.Showcase/Properties/AssemblyInfo.cs
@@ -1,17 +1,15 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

[assembly: AssemblyTitle("VirtualDesktop.Showcase")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VirtualDesktop.Showcase")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyCompany("grabacr.net")]
[assembly: AssemblyProduct("VirtualDesktop")]
[assembly: AssemblyDescription("C# wrapper for IVirtualDesktopManager on Windows 10.")]
[assembly: AssemblyCopyright("Copyright © 2015 Manato KAMEYA")]

[assembly: ComVisible(false)]
[assembly: Guid("5B4544B8-3EF0-4E9F-8D60-DD605AD99725")]
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

[assembly: AssemblyVersion("1.0.0.0")]
4 changes: 2 additions & 2 deletions source/VirtualDesktop.WPF/Properties/AssemblyInfo.cs
Expand Up @@ -10,5 +10,5 @@
[assembly: ComVisible(false)]
[assembly: Guid("9dd597c6-065a-4764-a96c-1b18c4eded78")]

[assembly: AssemblyVersion("1.0.3")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]
2 changes: 1 addition & 1 deletion source/VirtualDesktop.WPF/VirtualDesktop.WPF.nuspec
Expand Up @@ -13,7 +13,7 @@
<language>en-US</language>
<tags>Windows Windows10 Desktop VirtualDesktop WPF</tags>
<dependencies>
<dependency id="VirtualDesktop" version="1.0.3" />
<dependency id="VirtualDesktop" version="2.0.0" />
</dependencies>
</metadata>
</package>
410 changes: 293 additions & 117 deletions source/VirtualDesktop.sln.DotSettings

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions source/VirtualDesktop/Internal/Disposable.cs
@@ -0,0 +1,31 @@
using System;

namespace WindowsDesktop.Internal
{
public class Disposable
{
public static IDisposable Create(Action dispose)
{
return new AnonymousDisposable(dispose);
}

private class AnonymousDisposable : IDisposable
{
private bool _isDisposed;
private readonly Action _dispose;

public AnonymousDisposable(Action dispose)
{
this._dispose = dispose;
}

public void Dispose()
{
if (this._isDisposed) return;

this._isDisposed = true;
this._dispose();
}
}
}
}
92 changes: 92 additions & 0 deletions source/VirtualDesktop/Interop/ComObjects.cs
@@ -0,0 +1,92 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace WindowsDesktop.Interop
{
public static class ComObjects
{
private static IDisposable _listener;
private static readonly ConcurrentDictionary<Guid, IVirtualDesktop> _virtualDesktops = new ConcurrentDictionary<Guid, IVirtualDesktop>();

internal static IVirtualDesktopManager VirtualDesktopManager { get; private set; }
internal static VirtualDesktopManagerInternal VirtualDesktopManagerInternal { get; private set; }
internal static IVirtualDesktopNotificationService VirtualDesktopNotificationService { get; private set; }
internal static IVirtualDesktopPinnedApps VirtualDesktopPinnedApps { get; private set; }
internal static IApplicationViewCollection ApplicationViewCollection { get; private set; }

internal static void Initialize()
{
VirtualDesktopManager = GetVirtualDesktopManager();
VirtualDesktopManagerInternal = VirtualDesktopManagerInternal.GetInstance();
VirtualDesktopNotificationService = GetVirtualDesktopNotificationService();
VirtualDesktopPinnedApps = GetVirtualDesktopPinnedApps();
ApplicationViewCollection = GetApplicationViewCollection();

_virtualDesktops.Clear();
_listener = VirtualDesktop.RegisterListener();
}

internal static void Register(IVirtualDesktop vd)
{
_virtualDesktops.AddOrUpdate(vd.GetID(), vd, (guid, desktop) => vd);
}

internal static IVirtualDesktop GetVirtualDesktop(Guid id)
{
return _virtualDesktops.GetOrAdd(id, x => VirtualDesktopManagerInternal.FindDesktop(ref x));
}

internal static void Terminate()
{
_listener?.Dispose();
}


#region public methods

public static IVirtualDesktopManager GetVirtualDesktopManager()
{
var vdmType = Type.GetTypeFromCLSID(CLSID.VirtualDesktopManager);
var instance = Activator.CreateInstance(vdmType);

return (IVirtualDesktopManager)instance;
}

public static IVirtualDesktopNotificationService GetVirtualDesktopNotificationService()
{
var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
var shell = (IServiceProvider)Activator.CreateInstance(shellType);

object ppvObject;
shell.QueryService(CLSID.VirtualDesktopNotificationService, typeof(IVirtualDesktopNotificationService).GUID, out ppvObject);

return (IVirtualDesktopNotificationService)ppvObject;
}

public static IVirtualDesktopPinnedApps GetVirtualDesktopPinnedApps()
{
var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
var shell = (IServiceProvider)Activator.CreateInstance(shellType);

object ppvObject;
shell.QueryService(CLSID.VirtualDesktopPinnedApps, typeof(IVirtualDesktopPinnedApps).GUID, out ppvObject);

return (IVirtualDesktopPinnedApps)ppvObject;
}

public static IApplicationViewCollection GetApplicationViewCollection()
{
var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
var shell = (IServiceProvider)Activator.CreateInstance(shellType);

object ppvObject;
shell.QueryService(typeof(IApplicationViewCollection).GUID, typeof(IApplicationViewCollection).GUID, out ppvObject);

return (IApplicationViewCollection)ppvObject;
}

#endregion
}
}
12 changes: 6 additions & 6 deletions source/VirtualDesktop/Interop/IVirtualDesktopManagerInternal.cs
Expand Up @@ -10,9 +10,9 @@ internal interface IVirtualDesktopManagerInternal10130
{
int GetCount();

void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop);

bool CanViewMoveDesktops(object pView);
bool CanViewMoveDesktops(IntPtr pView);

IVirtualDesktop GetCurrentDesktop();

Expand All @@ -36,9 +36,9 @@ internal interface IVirtualDesktopManagerInternal10240
{
int GetCount();

void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop);

bool CanViewMoveDesktops(object pView);
bool CanViewMoveDesktops(IntPtr pView);

IVirtualDesktop GetCurrentDesktop();

Expand All @@ -62,9 +62,9 @@ internal interface IVirtualDesktopManagerInternal14328
{
int GetCount();

void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop);

bool CanViewMoveDesktops(object pView);
bool CanViewMoveDesktops(IntPtr pView);

IVirtualDesktop GetCurrentDesktop();

Expand Down
48 changes: 0 additions & 48 deletions source/VirtualDesktop/Interop/VirtualDesktopInteropHelper.cs

This file was deleted.

Expand Up @@ -57,7 +57,7 @@ public int GetCount()
}


public void MoveViewToDesktop(object pView, IVirtualDesktop desktop)
public void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop)
{
if (this._manager14328 != null)
{
Expand All @@ -80,7 +80,7 @@ public void MoveViewToDesktop(object pView, IVirtualDesktop desktop)
throw new NotSupportedException();
}

public bool CanViewMoveDesktops(object pView)
public bool CanViewMoveDesktops(IntPtr pView)
{
if (this._manager14328 != null)
{
Expand Down
4 changes: 2 additions & 2 deletions source/VirtualDesktop/Properties/AssemblyInfo.cs
Expand Up @@ -10,5 +10,5 @@
[assembly: ComVisible(false)]
[assembly: Guid("ab848ecd-76aa-41c0-b63d-86a8591b25aa")]

[assembly: AssemblyVersion("1.0.3")]
[assembly: AssemblyInformationalVersion("1.0.3")]
[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]
16 changes: 8 additions & 8 deletions source/VirtualDesktop/VirtualDesktop.cs
Expand Up @@ -20,11 +20,11 @@ public partial class VirtualDesktop
public Guid Id { get; }

[EditorBrowsable(EditorBrowsableState.Never)]
public IVirtualDesktop ComObject { get; }
public IVirtualDesktop ComObject => ComObjects.GetVirtualDesktop(this.Id);

private VirtualDesktop(IVirtualDesktop comObject)
{
this.ComObject = comObject;
ComObjects.Register(comObject);
this.Id = comObject.GetID();
}

Expand All @@ -34,7 +34,7 @@ private VirtualDesktop(IVirtualDesktop comObject)
/// </summary>
public void Switch()
{
ComInternal.SwitchDesktop(this.ComObject);
ComObjects.VirtualDesktopManagerInternal.SwitchDesktop(this.ComObject);
}

/// <summary>
Expand All @@ -52,7 +52,7 @@ public void Remove(VirtualDesktop fallbackDesktop)
{
if (fallbackDesktop == null) throw new ArgumentNullException(nameof(fallbackDesktop));

ComInternal.RemoveDesktop(this.ComObject, fallbackDesktop.ComObject);
ComObjects.VirtualDesktopManagerInternal.RemoveDesktop(this.ComObject, fallbackDesktop.ComObject);
}

/// <summary>
Expand All @@ -63,13 +63,13 @@ public VirtualDesktop GetLeft()
IVirtualDesktop desktop;
try
{
desktop = ComInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.LeftDirection);
desktop = ComObjects.VirtualDesktopManagerInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.LeftDirection);
}
catch (COMException ex) when (ex.Match(HResult.TYPE_E_OUTOFBOUNDS))
{
return null;
}
var wrapper = wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));
var wrapper = _wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));

return wrapper;
}
Expand All @@ -82,13 +82,13 @@ public VirtualDesktop GetRight()
IVirtualDesktop desktop;
try
{
desktop = ComInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.RightDirection);
desktop = ComObjects.VirtualDesktopManagerInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.RightDirection);
}
catch (COMException ex) when (ex.Match(HResult.TYPE_E_OUTOFBOUNDS))
{
return null;
}
var wrapper = wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));
var wrapper = _wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));

return wrapper;
}
Expand Down

0 comments on commit 634d886

Please sign in to comment.