Skip to content

Commit

Permalink
Merge pull request #15 from widavies/revamp
Browse files Browse the repository at this point in the history
Revamp
  • Loading branch information
widavies authored Apr 18, 2023
2 parents 7f552fe + 96451bd commit 63f773b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
3 changes: 3 additions & 0 deletions WinJump/Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace WinJump.Core;

/// <summary>
/// Handles loading the configuration file
/// </summary>
internal sealed class Config {
public static readonly string LOCATION = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Expand Down
10 changes: 10 additions & 0 deletions WinJump/Core/VirtualDesktopDefinitions/IVirtualDesktopAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

namespace WinJump.Core.VirtualDesktopDefinitions;

/// <summary>
/// This interface represents the essential functions that must be provided by a virtual desktop API for WinJump
/// to function. When reverse engineering the Windows virtual desktop API, you must provide definitions
/// for these functions.
/// </summary>
public interface IVirtualDesktopAPI : IDisposable {

/// <summary>
Expand All @@ -22,6 +27,11 @@ public interface IVirtualDesktopAPI : IDisposable {
/// <param name="index">0-indexed desktop number. If it is invalid it will be ignored.</param>
void JumpToDesktop(int index);

/// <summary>
/// Creates the appropriate virtual desktop API definition for the current Windows version.
/// </summary>
/// <returns>A virtual desktop API for the installed Windows version</returns>
/// <exception cref="Exception">If the particular Windows version is unsupported</exception>
public static IVirtualDesktopAPI Create() {
string? releaseId = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion",
"CurrentBuildNumber", "")?.ToString();
Expand Down
50 changes: 40 additions & 10 deletions WinJump/Core/WinJumpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace WinJump.Core;

public delegate void DesktopChanged(bool lightMode, uint desktopNum);

// Main controller for WinJump core functionality

/// <summary>
/// Ties everything together.
///
Expand Down Expand Up @@ -47,7 +45,6 @@ public WinJumpManager(DesktopChanged desktopChanged) {
_keyboardHook.RegisterHotKey(shortcut.ModifierKeys, shortcut.Keys));
},
() => {
// Check if the shortcuts are already registered
return config.ToggleGroups.Select(t => t.Shortcut).All(shortcut =>
_keyboardHook.RegisterHotKey(shortcut.ModifierKeys, shortcut.Keys));
}
Expand All @@ -58,11 +55,14 @@ public WinJumpManager(DesktopChanged desktopChanged) {
// and restart it after we've registered the shortcuts
_explorerMonitor.Kill();
if(!registerShortcuts.All(x => x.Invoke())) {
// Still didn't work, exit out
// Still didn't work, exit out (make sure to restart explorer before doing so)
_explorerMonitor.EnsureExplorerIsAlive();
Application.Current.Shutdown();
return;
}
}

// Add handler for hotkey press events
_keyboardHook.KeyPressed += (_, args) => {
Shortcut pressed = new Shortcut {
ModifierKeys = args.Modifier,
Expand Down Expand Up @@ -98,6 +98,7 @@ public WinJumpManager(DesktopChanged desktopChanged) {
desktopChanged(_lightMode, desktop);
});

// This particular event fires immediately on initial register
_explorerMonitor.OnColorSchemeChanged += lightMode => {
_lightMode = lightMode;
desktopChanged.Invoke(lightMode, (uint) _thread.GetCurrentDesktop());
Expand All @@ -109,17 +110,14 @@ public WinJumpManager(DesktopChanged desktopChanged) {
} catch(Exception) {
// ignored
}
string? currentExecutablePath = Process.GetCurrentProcess().MainModule?.FileName;
if(currentExecutablePath == null) return;
Process.Start(currentExecutablePath);
Application.Current.Shutdown();
};

// Set the desktop right away
desktopChanged.Invoke(_lightMode, (uint) _thread.GetCurrentDesktop());
}

public void Dispose() {
Expand All @@ -140,6 +138,15 @@ internal sealed class STAThread : IDisposable {
private readonly ManualResetEvent mre;
private readonly IVirtualDesktopAPI api = IVirtualDesktopAPI.Create();

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern IntPtr GetTopWindow();

[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();

public STAThread(Action<uint> DesktopChanged) {
using(mre = new ManualResetEvent(false)) {
var thread = new Thread(() => {
Expand Down Expand Up @@ -178,11 +185,27 @@ public void JumpTo(uint index, uint fallback) {
}
api.JumpToDesktop((int) index);
// Hackish way to fix kind of annoying problem where
// focus doesn't always come along with a desktop change
IntPtr t = GetTopWindow();
if(t != IntPtr.Zero) {
SetForegroundWindow(t);
}
});
}

public void JumpTo(uint index) {
WrapCall(() => api.JumpToDesktop((int) index));
WrapCall(() => {
api.JumpToDesktop((int) index);
// Hackish way to fix kind of annoying problem where
// focus doesn't always come along with a desktop change
IntPtr t = GetTopWindow();
if(t != IntPtr.Zero) {
SetForegroundWindow(t);
}
});
}

// desktops must be 0-indexed
Expand All @@ -195,6 +218,13 @@ public void JumpToNext(int[] desktops) {
int next = desktops[(index + 1) % desktops.Length];
api.JumpToDesktop(next);
// Hackish way to fix kind of annoying problem where
// focus doesn't always come along with a desktop change
IntPtr t = GetTopWindow();
if(t != IntPtr.Zero) {
SetForegroundWindow(t);
}
});
}

Expand Down
6 changes: 3 additions & 3 deletions WinJump/WinJump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<Version>2.0.0</Version>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Version>2.0.1</Version>
<AssemblyVersion>2.0.1</AssemblyVersion>
<FileVersion>2.0.1</FileVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand Down

0 comments on commit 63f773b

Please sign in to comment.