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

"Sticky window" or "Pin window" Feature #12

Open
wants to merge 6 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
12 changes: 11 additions & 1 deletion source/SylphyHorn/Interop/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@ public static class NativeMethods

[DllImport("Dwmapi.dll")]
public static extern void DwmGetColorizationColor([Out] out int pcrColorization, [Out] out bool pfOpaqueBlend);
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern int OpenInputDesktop(int dwFlags, bool fInherit, int dwDesiredAccess);

[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, StringBuilder text, int count);
}
}
50 changes: 48 additions & 2 deletions source/SylphyHorn/Models/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using MetroRadiance;
using Microsoft.Win32;
using SylphyHorn.Interop;
using VDMHelperCLR.Common;

namespace SylphyHorn.Models
{
Expand Down Expand Up @@ -63,9 +64,54 @@ public static IntPtr GetForegroundWindowEx()
var howner = NativeMethods.GetWindow(hwnd, 4 /* GW_OWNER */);
return howner == IntPtr.Zero ? hwnd : howner;
}
}

internal static class VisualHelper
public static bool IsWindow(IntPtr hWnd)
{
return NativeMethods.IsWindow(hWnd);
}

public static int GetWindowThreadProcessId(IntPtr hWnd)
{
int processId = 0;
NativeMethods.GetWindowThreadProcessId(hWnd, out processId);
return processId;
}

public static string GetWindowText(int hWnd)
{
const int bufferSize = 256;
var windowText = new StringBuilder(bufferSize);
if (NativeMethods.GetWindowText(hWnd, windowText, bufferSize) > 0)
{
return windowText.ToString();
}

return string.Empty;
}
}

/*
* The VDMHelper need to be a singleton or else it will create multiple instance of the process VDMHelper32.exe
* This can cause failure when moving window.
*/
internal class VDMHelper
{
private static IVdmHelper instancehelper;
public static IVdmHelper helper
{
get
{
if (instancehelper == null)
{
instancehelper = VdmHelperFactory.CreateInstance();
helper.Init();
}
return instancehelper;
}
}
}

internal static class VisualHelper
{
public static void InvokeOnUIDispatcher(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
{
Expand Down
25 changes: 15 additions & 10 deletions source/SylphyHorn/Models/HookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public HookService()
this.detector.Pressed += this.KeyHookOnPressed;
this.detector.Start();

this.helper = VdmHelperFactory.CreateInstance();
this.helper.Init();
}
this.helper = VDMHelper.helper;
}

public IDisposable Suspend()
{
Expand All @@ -47,14 +46,14 @@ private void KeyHookOnPressed(object sender, ShortcutKeyPressedEventArgs args)
if (ShortcutSettings.MoveLeft.Value != null &&
ShortcutSettings.MoveLeft.Value == args.ShortcutKey)
{
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToLeft());
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToLeft()?.MoveSticky());
args.Handled = true;
}

if (ShortcutSettings.MoveLeftAndSwitch.Value != null &&
ShortcutSettings.MoveLeftAndSwitch.Value == args.ShortcutKey)
{
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToLeft()?.Switch());
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToLeft()?.MoveSticky().Switch());
args.Handled = true;
}

Expand All @@ -68,7 +67,7 @@ private void KeyHookOnPressed(object sender, ShortcutKeyPressedEventArgs args)
if (ShortcutSettings.MoveRightAndSwitch.Value != null &&
ShortcutSettings.MoveRightAndSwitch.Value == args.ShortcutKey)
{
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToRight()?.Switch());
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToRight()?.MoveSticky().Switch());
args.Handled = true;
}

Expand All @@ -82,7 +81,7 @@ private void KeyHookOnPressed(object sender, ShortcutKeyPressedEventArgs args)
if (ShortcutSettings.MoveNewAndSwitch.Value != null &&
ShortcutSettings.MoveNewAndSwitch.Value == args.ShortcutKey)
{
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToNew()?.Switch());
VisualHelper.InvokeOnUIDispatcher(() => this.MoveToNew()?.MoveSticky().Switch());
args.Handled = true;
}

Expand All @@ -91,7 +90,7 @@ private void KeyHookOnPressed(object sender, ShortcutKeyPressedEventArgs args)
{
if (GeneralSettings.OverrideOSDefaultKeyCombination)
{
VisualHelper.InvokeOnUIDispatcher(() => PrepareSwitchToLeft()?.Switch());
VisualHelper.InvokeOnUIDispatcher(() => PrepareSwitchToLeft()?.MoveSticky().Switch());
args.Handled = true;
}
}
Expand All @@ -101,11 +100,17 @@ private void KeyHookOnPressed(object sender, ShortcutKeyPressedEventArgs args)
{
if (GeneralSettings.OverrideOSDefaultKeyCombination)
{
VisualHelper.InvokeOnUIDispatcher(() => PrepareSwitchToRight()?.Switch());
VisualHelper.InvokeOnUIDispatcher(() => PrepareSwitchToRight()?.MoveSticky().Switch());
args.Handled = true;
}
}
}

if (ShortcutSettings.PinWindows.Value != null &&
ShortcutSettings.PinWindows.Value == args.ShortcutKey)
{
VisualHelper.InvokeOnUIDispatcher(() => StickyWindowsManager.Instance.ToggleStickyWindow());
}
}

private static VirtualDesktop PrepareSwitchToLeft()
{
Expand Down
40 changes: 29 additions & 11 deletions source/SylphyHorn/Models/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class NotificationService : IDisposable
public NotificationService()
{
VirtualDesktop.CurrentChanged += this.VirtualDesktopOnCurrentChanged;
}
StickyWindowsManager.ToggleStickyWindowEvent += this.ShowToggleStickyWindow;
}

private void VirtualDesktopOnCurrentChanged(object sender, VirtualDesktopChangedEventArgs e)
{
Expand All @@ -29,22 +30,38 @@ private void VirtualDesktopOnCurrentChanged(object sender, VirtualDesktopChanged
var newIndex = Array.IndexOf(desktops, e.NewDesktop) + 1;

this.currentNotificationWindow?.Dispose();
this.currentNotificationWindow = ShowWindow(newIndex);

var vmodel = new NotificationWindowViewModel
{
Title = ProductInfo.Title,
Header = "Virtual Desktop Switched",
Body = "Current Desktop: Desktop " + newIndex,
};
this.currentNotificationWindow = ShowWindow(vmodel);
});
}

private static IDisposable ShowWindow(int index)

private void ShowToggleStickyWindow(object sender, string body)
{
this.currentNotificationWindow?.Dispose();

var vmodel = new NotificationWindowViewModel
{
Title = ProductInfo.Title,
Header = "Window Pin Toggled",
Body = body
};
this.currentNotificationWindow = ShowWindow(vmodel);
}

private static IDisposable ShowWindow(NotificationWindowViewModel vm)
{
var vmodel = new NotificationWindowViewModel
{
Title = ProductInfo.Title,
Header = "Virtual Desktop Switched",
Body = "Current Desktop: Desktop " + index,
};

var source = new CancellationTokenSource();
var window = new NotificationWindow
{
DataContext = vmodel,
DataContext = vm,
};
window.Show();

Expand All @@ -57,6 +74,7 @@ private static IDisposable ShowWindow(int index)
public void Dispose()
{
VirtualDesktop.CurrentChanged -= this.VirtualDesktopOnCurrentChanged;
}
StickyWindowsManager.ToggleStickyWindowEvent -= this.ShowToggleStickyWindow;
}
}
}
3 changes: 3 additions & 0 deletions source/SylphyHorn/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public static class ShortcutSettings
public static SerializableProperty<ShortcutKey?> SwitchToRight { get; }
= new SerializableProperty<ShortcutKey?>(GetKey(), Providers.Local, new ShortcutKey(Key.Right, Key.LeftCtrl, Key.LWin));

public static SerializableProperty<ShortcutKey?> PinWindows { get; }
= new SerializableProperty<ShortcutKey?>(GetKey(), Providers.Local, new ShortcutKey(Key.P, Key.LeftCtrl, Key.LeftAlt, Key.LWin));


private static string GetKey([CallerMemberName] string caller = "")
{
Expand Down
Loading