Skip to content

Improve handling of not found exception from GetViewForHwnd() and re-add TogglePin functions #14

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

Open
wants to merge 5 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
23 changes: 18 additions & 5 deletions source/VirtualDesktop/ApplicationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,29 @@ public static class ApplicationHelper
{
internal static IApplicationView GetApplicationView(this IntPtr hWnd)
{
IApplicationView view;
ComObjects.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);

return view;
try
{
IApplicationView view;
ComObjects.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
return view;
}
catch (System.Runtime.InteropServices.COMException ex) when (ex.Match(HResult.TYPE_E_ELEMENTNOTFOUND))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might consider not using C#6.0 specific features yet for more broad compilation support of the library itself

{
return null;
}
}

public static string GetAppId(IntPtr hWnd)
{
var view = hWnd.GetApplicationView();

if (view == null)
{
throw new ArgumentException(nameof(hWnd));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should message out why its an argument exception, and probably not use C#6.0 for more broad compilation possibility:
throw new ArgumentException("hWnd specified not found","hWnd");

}

string appId;
hWnd.GetApplicationView().GetAppUserModelId(out appId);
view.GetAppUserModelId(out appId);

return appId;
}
Expand Down
54 changes: 53 additions & 1 deletion source/VirtualDesktop/VirtualDesktop.static.pin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ public static bool IsPinnedWindow(IntPtr hWnd)
{
VirtualDesktopHelper.ThrowIfNotSupported();

return ComObjects.VirtualDesktopPinnedApps.IsViewPinned(hWnd.GetApplicationView());
var view = hWnd.GetApplicationView();

if (view == null)
{
throw new ArgumentException(nameof(hWnd));
}

return ComObjects.VirtualDesktopPinnedApps.IsViewPinned(view);
}

public static void PinWindow(IntPtr hWnd)
Expand All @@ -20,6 +27,11 @@ public static void PinWindow(IntPtr hWnd)

var view = hWnd.GetApplicationView();

if (view == null)
{
throw new ArgumentException(nameof(hWnd));
}

if (!ComObjects.VirtualDesktopPinnedApps.IsViewPinned(view))
{
ComObjects.VirtualDesktopPinnedApps.PinView(view);
Expand All @@ -32,10 +44,36 @@ public static void UnpinWindow(IntPtr hWnd)

var view = hWnd.GetApplicationView();

if (view == null)
{
throw new ArgumentException(nameof(hWnd));
}

if (ComObjects.VirtualDesktopPinnedApps.IsViewPinned(view))
{
ComObjects.VirtualDesktopPinnedApps.UnpinView(view);
}
}

public static void TogglePinWindow(IntPtr hWnd)
{
VirtualDesktopHelper.ThrowIfNotSupported();

var view = hWnd.GetApplicationView();

if (view == null)
{
throw new ArgumentException(nameof(hWnd));
}

if (ComObjects.VirtualDesktopPinnedApps.IsViewPinned(view))
{
ComObjects.VirtualDesktopPinnedApps.UnpinView(view);
}
else
{
ComObjects.VirtualDesktopPinnedApps.PinView(view);
}
}

public static bool IsPinnedApplication(string appId)
Expand Down Expand Up @@ -64,5 +102,19 @@ public static void UnpinApplication(string appId)
ComObjects.VirtualDesktopPinnedApps.UnpinAppID(appId);
}
}

public static void TogglePinApplication(string appId)
{
VirtualDesktopHelper.ThrowIfNotSupported();

if (ComObjects.VirtualDesktopPinnedApps.IsAppIdPinned(appId))
{
ComObjects.VirtualDesktopPinnedApps.UnpinAppID(appId);
}
else
{
ComObjects.VirtualDesktopPinnedApps.PinAppID(appId);
}
}
}
}
13 changes: 10 additions & 3 deletions source/VirtualDesktop/VirtualDesktopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ public static void MoveToDesktop(IntPtr hWnd, VirtualDesktop virtualDesktop)
}
else
{
IApplicationView view;
ComObjects.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
ComObjects.VirtualDesktopManagerInternal.MoveViewToDesktop(view, virtualDesktop.ComObject);
try
{
IApplicationView view;
ComObjects.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
ComObjects.VirtualDesktopManagerInternal.MoveViewToDesktop(view, virtualDesktop.ComObject);
}
catch (System.Runtime.InteropServices.COMException ex) when (ex.Match(HResult.TYPE_E_ELEMENTNOTFOUND))
{
throw new ArgumentException(nameof(hWnd));
}
}
}
}
Expand Down