Permalink
Browse files

Move pin/unpin methods to VirtualDesktop class.

  • Loading branch information...
1 parent 397656c commit ab252fab06e82204d608eb80674628839ecf8c16 @Grabacr07 committed Aug 6, 2016
@@ -8,49 +8,52 @@
SizeToContent="WidthAndHeight"
SnapsToDevicePixels="True"
FontSize="20">
- <StackPanel Margin="8">
- <Panel.Resources>
- <Style TargetType="{x:Type Button}">
- <Setter Property="Padding"
- Value="20,4" />
- <Setter Property="HorizontalContentAlignment"
- Value="Left" />
- <Setter Property="Margin"
- Value="8" />
- <Setter Property="BorderThickness"
- Value="0.99" />
- </Style>
- </Panel.Resources>
+ <ScrollViewer HorizontalScrollBarVisibility="Disabled"
+ VerticalScrollBarVisibility="Auto">
<StackPanel Margin="8">
- <RadioButton x:Name="ThisWindowMenu"
- IsChecked="True"
- VerticalContentAlignment="Center">
- <TextBlock Text="This window" />
- </RadioButton>
- <Border Height="4" />
- <RadioButton VerticalContentAlignment="Center">
- <TextBlock>
- <Run>Foreground window</Run>
- <LineBreak />
- <Run FontSize="14">(seconds later)</Run>
- </TextBlock>
- </RadioButton>
+ <Panel.Resources>
+ <Style TargetType="{x:Type Button}">
+ <Setter Property="Padding"
+ Value="20,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"
+ IsChecked="True"
+ VerticalContentAlignment="Center">
+ <TextBlock Text="This window" />
+ </RadioButton>
+ <Border Height="4" />
+ <RadioButton VerticalContentAlignment="Center">
+ <TextBlock>
+ <Run>Foreground window</Run>
+ <LineBreak />
+ <Run FontSize="14">(seconds later)</Run>
+ </TextBlock>
+ </RadioButton>
+ </StackPanel>
+ <Button Content="Create new"
+ Click="CreateNew" />
+ <Button Content="Create new and move"
+ Click="CreateNewAndMove" />
+ <Button Content="Switch left"
+ Click="SwitchLeft" />
+ <Button Content="Switch left and move"
+ Click="SwitchLeftAndMove" />
+ <Button Content="Switch right"
+ Click="SwitchRight" />
+ <Button Content="Switch right and move"
+ Click="SwitchRightAndMove" />
+ <Button Content="Pin/Unpin"
+ Click="Pin" />
+ <Button Content="Pin/Unpin (App)"
+ Click="PinApp" />
</StackPanel>
- <Button Content="Create new"
- Click="CreateNew" />
- <Button Content="Create new and move"
- Click="CreateNewAndMove" />
- <Button Content="Switch left"
- Click="SwitchLeft" />
- <Button Content="Switch left and move"
- Click="SwitchLeftAndMove" />
- <Button Content="Switch right"
- Click="SwitchRight" />
- <Button Content="Switch right and move"
- Click="SwitchRightAndMove" />
- <Button Content="Pin/Unpin"
- Click="Pin" />
- <Button Content="Pin/Unpin (App)"
- Click="PinApp" />
- </StackPanel>
+ </ScrollViewer>
</Window>
@@ -99,7 +99,8 @@ private void SwitchRight(object sender, RoutedEventArgs e)
else
{
await Task.Delay(_delay);
- VirtualDesktopHelper.TogglePinWindow(GetForegroundWindow());
+ var handle = GetForegroundWindow();
+ (VirtualDesktop.IsPinnedWindow(handle) ? VirtualDesktop.UnpinWindow : (Action<IntPtr>)VirtualDesktop.PinWindow)(handle);
}
}
@@ -112,7 +113,8 @@ private void SwitchRight(object sender, RoutedEventArgs e)
else
{
await Task.Delay(_delay);
- VirtualDesktopHelper.TogglePinApplication(GetForegroundWindow());
+ var appId = ApplicationHelper.GetAppId(GetForegroundWindow());
+ (VirtualDesktop.IsPinnedApplication(appId) ? VirtualDesktop.UnpinApplication : (Action<string>)VirtualDesktop.PinApplication)(appId);
}
}
@@ -9,22 +9,31 @@ public static class ApplicationExtensions
{
public static bool IsPinned(this Application app)
{
- return VirtualDesktopHelper.IsPinnedApplication(app.GetWindowHandle());
+ return VirtualDesktop.IsPinnedApplication(ApplicationHelper.GetAppId(app.GetWindowHandle()));
}
public static void Pin(this Application app)
{
- VirtualDesktopHelper.PinApplication(app.GetWindowHandle());
+ VirtualDesktop.PinApplication(ApplicationHelper.GetAppId(app.GetWindowHandle()));
}
public static void Unpin(this Application app)
{
- VirtualDesktopHelper.UnpinApplication(app.GetWindowHandle());
+ VirtualDesktop.UnpinApplication(ApplicationHelper.GetAppId(app.GetWindowHandle()));
}
public static void TogglePin(this Application app)
{
- VirtualDesktopHelper.TogglePinApplication(app.GetWindowHandle());
+ var appId = ApplicationHelper.GetAppId(app.GetWindowHandle());
+
+ if (VirtualDesktop.IsPinnedApplication(appId))
+ {
+ VirtualDesktop.UnpinApplication(appId);
+ }
+ else
+ {
+ VirtualDesktop.PinApplication(appId);
+ }
}
private static IntPtr GetWindowHandle(this Application app)
@@ -11,4 +11,4 @@
[assembly: Guid("9dd597c6-065a-4764-a96c-1b18c4eded78")]
[assembly: AssemblyVersion("2.0.0")]
-[assembly: AssemblyInformationalVersion("2.0.0-beta2")]
+[assembly: AssemblyInformationalVersion("2.0.0-beta3")]
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
- <metadata>
- <id>$id$</id>
- <title>$title$</title>
- <version>$version$</version>
- <authors>$author$</authors>
- <owners>$author$</owners>
- <licenseUrl>https://github.com/Grabacr07/VirtualDesktop/blob/master/LICENSE</licenseUrl>
- <projectUrl>https://github.com/Grabacr07/VirtualDesktop</projectUrl>
- <requireLicenseAcceptance>false</requireLicenseAcceptance>
- <description>$description$</description>
- <language>en-US</language>
- <tags>Windows Windows10 Desktop VirtualDesktop WPF</tags>
- <dependencies>
- <dependency id="VirtualDesktop" version="2.0.0-beta2" />
- </dependencies>
- </metadata>
+ <metadata>
+ <id>$id$</id>
+ <title>$title$</title>
+ <version>$version$</version>
+ <authors>$author$</authors>
+ <owners>$author$</owners>
+ <licenseUrl>https://github.com/Grabacr07/VirtualDesktop/blob/master/LICENSE</licenseUrl>
+ <projectUrl>https://github.com/Grabacr07/VirtualDesktop</projectUrl>
+ <requireLicenseAcceptance>false</requireLicenseAcceptance>
+ <description>$description$</description>
+ <language>en-US</language>
+ <tags>Windows Windows10 Desktop VirtualDesktop WPF</tags>
+ <dependencies>
+ <dependency id="VirtualDesktop" version="2.0.0-beta3" />
+ </dependencies>
+ </metadata>
</package>
@@ -42,22 +42,31 @@ public static void SwitchAndMove(this VirtualDesktop virtualDesktop, Window wind
public static bool IsPinned(this Window window)
{
- return VirtualDesktopHelper.IsPinnedWindow(window.GetHandle());
+ return VirtualDesktop.IsPinnedWindow(window.GetHandle());
}
public static void Pin(this Window window)
{
- VirtualDesktopHelper.PinWindow(window.GetHandle());
+ VirtualDesktop.PinWindow(window.GetHandle());
}
public static void Unpin(this Window window)
{
- VirtualDesktopHelper.UnpinWindow(window.GetHandle());
+ VirtualDesktop.UnpinWindow(window.GetHandle());
}
public static void TogglePin(this Window window)
{
- VirtualDesktopHelper.TogglePinWindow(window.GetHandle());
+ var handle = window.GetHandle();
+
+ if (VirtualDesktop.IsPinnedWindow(handle))
+ {
+ VirtualDesktop.UnpinWindow(handle);
+ }
+ else
+ {
+ VirtualDesktop.PinWindow(handle);
+ }
}
internal static IntPtr GetHandle(this Visual window)
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using WindowsDesktop.Interop;
+
+namespace WindowsDesktop
+{
+ public static class ApplicationHelper
+ {
+ internal static IApplicationView GetApplicationView(this IntPtr hWnd)
+ {
+ IApplicationView view;
+ ComObjects.ApplicationViewCollection.GetViewForHwnd(hWnd, out view);
+
+ return view;
+ }
+
+ public static string GetAppId(IntPtr hWnd)
+ {
+ string appId;
+ hWnd.GetApplicationView().GetAppUserModelId(out appId);
+
+ return appId;
+ }
+ }
+}
@@ -11,4 +11,4 @@
[assembly: Guid("ab848ecd-76aa-41c0-b63d-86a8591b25aa")]
[assembly: AssemblyVersion("2.0.0")]
-[assembly: AssemblyInformationalVersion("2.0.0-beta2")]
+[assembly: AssemblyInformationalVersion("2.0.0-beta3")]
@@ -82,6 +82,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="ApplicationHelper.cs" />
<Compile Include="Internal\Disposable.cs" />
<Compile Include="Internal\RawWindow.cs" />
<Compile Include="Internal\TransparentWindow.cs" />
@@ -101,6 +102,7 @@
<Compile Include="Interop\NativeMethods.cs" />
<Compile Include="Interop\VirtualDesktopManagerInternal.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="VirtualDesktop.static.pin.cs" />
<Compile Include="VirtualDesktop.static.cs" />
<Compile Include="VirtualDesktop.cs" />
<Compile Include="VirtualDesktopChangedEventArgs.cs" />
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
- <metadata>
- <id>$id$</id>
- <title>$title$</title>
- <version>$version$</version>
- <authors>$author$</authors>
- <owners>$author$</owners>
- <licenseUrl>https://github.com/Grabacr07/VirtualDesktop/blob/master/LICENSE</licenseUrl>
- <projectUrl>https://github.com/Grabacr07/VirtualDesktop</projectUrl>
- <requireLicenseAcceptance>false</requireLicenseAcceptance>
- <description>$description$ You need to include the app manifest in your project so as to target Windows 10.</description>
- <language>en-US</language>
- <tags>Windows Windows10 Desktop VirtualDesktop</tags>
- </metadata>
+ <metadata>
+ <id>$id$</id>
+ <title>$title$</title>
+ <version>$version$</version>
+ <authors>$author$</authors>
+ <owners>$author$</owners>
+ <licenseUrl>https://github.com/Grabacr07/VirtualDesktop/blob/master/LICENSE</licenseUrl>
+ <projectUrl>https://github.com/Grabacr07/VirtualDesktop</projectUrl>
+ <requireLicenseAcceptance>false</requireLicenseAcceptance>
+ <description>$description$ You need to include the app manifest in your project so as to target Windows 10.</description>
+ <language>en-US</language>
+ <tags>Windows Windows10 Desktop VirtualDesktop</tags>
+ </metadata>
</package>
@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using WindowsDesktop.Interop;
+
+namespace WindowsDesktop
+{
+ partial class VirtualDesktop
+ {
+ public static bool IsPinnedWindow(IntPtr hWnd)
+ {
+ VirtualDesktopHelper.ThrowIfNotSupported();
+
+ return ComObjects.VirtualDesktopPinnedApps.IsViewPinned(hWnd.GetApplicationView());
+ }
+
+ public static void PinWindow(IntPtr hWnd)
+ {
+ VirtualDesktopHelper.ThrowIfNotSupported();
+
+ var view = hWnd.GetApplicationView();
+
+ if (!ComObjects.VirtualDesktopPinnedApps.IsViewPinned(view))
+ {
+ ComObjects.VirtualDesktopPinnedApps.PinView(view);
+ }
+ }
+
+ public static void UnpinWindow(IntPtr hWnd)
+ {
+ VirtualDesktopHelper.ThrowIfNotSupported();
+
+ var view = hWnd.GetApplicationView();
+
+ if (ComObjects.VirtualDesktopPinnedApps.IsViewPinned(view))
+ {
+ ComObjects.VirtualDesktopPinnedApps.UnpinView(view);
+ }
+ }
+
+ public static bool IsPinnedApplication(string appId)
+ {
+ VirtualDesktopHelper.ThrowIfNotSupported();
+
+ return ComObjects.VirtualDesktopPinnedApps.IsAppIdPinned(appId);
+ }
+
+ public static void PinApplication(string appId)
+ {
+ VirtualDesktopHelper.ThrowIfNotSupported();
+
+ if (!ComObjects.VirtualDesktopPinnedApps.IsAppIdPinned(appId))
+ {
+ ComObjects.VirtualDesktopPinnedApps.PinAppID(appId);
+ }
+ }
+
+ public static void UnpinApplication(string appId)
+ {
+ VirtualDesktopHelper.ThrowIfNotSupported();
+
+ if (ComObjects.VirtualDesktopPinnedApps.IsAppIdPinned(appId))
+ {
+ ComObjects.VirtualDesktopPinnedApps.UnpinAppID(appId);
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit ab252fa

Please sign in to comment.