Skip to content

Commit

Permalink
insepector updated to follow cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
maxle5 committed Sep 24, 2023
1 parent 86d7ae7 commit 05db0e5
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 36 deletions.
12 changes: 12 additions & 0 deletions GlazeWM.App.IpcServer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"GlazeWM.App.IpcServer": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:61214;http://localhost:61215"
}
}
}
15 changes: 15 additions & 0 deletions GlazeWM.Domain/GlazeWM.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@
<ItemGroup>
<ProjectReference Include="..\GlazeWM.Infrastructure\GlazeWM.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions GlazeWM.Domain/UserConfigs/CommandParsingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public Command ParseCommand(string commandString, Container subjectContainer)
? new IgnoreWindowCommand(subjectContainer as Window)
: new NoopCommand(),
"binding" => ParseBindingCommand(commandParts),
"inspect" => subjectContainer is Window
? new InspectWindowCommand(subjectContainer as Window)
: new NoopCommand(),
"inspect" => new InspectWindowCommand(subjectContainer as Window),
_ => throw new ArgumentException(null, nameof(commandString)),
};
}
Expand Down
32 changes: 23 additions & 9 deletions GlazeWM.Domain/Windows/CommandHandlers/InspectWindowHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading;
using System.Windows.Forms;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Infrastructure.Bussing;

Expand All @@ -7,18 +9,30 @@ internal class InspectWindowHandler : ICommandHandler<InspectWindowCommand>
{
public CommandResponse Handle(InspectWindowCommand command)
{
var inspector = new Inspector();
inspector.SetTitle(command.WindowToInspect.Title);
inspector.SetClassName(command.WindowToInspect.ClassName);
inspector.SetProcessName(command.WindowToInspect.ProcessName);
inspector.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
// open inspector on a separate thread
var thread = new Thread(() =>
{
using var inspector = new Inspector();
inspector.ShowDialog(GetParentWindow(command.WindowToInspect));
});

// show dialog as child of parent window
var parentWindow = new System.Windows.Forms.NativeWindow();
parentWindow.AssignHandle(command.WindowToInspect.Handle);
inspector.ShowDialog(parentWindow);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

return CommandResponse.Ok;
}

private static NativeWindow GetParentWindow(Window windowToInspect)
{
if (windowToInspect == null)
{
return null;
}

var parentWindow = new NativeWindow();
parentWindow.AssignHandle(windowToInspect.Handle);

return parentWindow;
}
}
}
36 changes: 13 additions & 23 deletions GlazeWM.Domain/Windows/Inspector.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 37 additions & 1 deletion GlazeWM.Domain/Windows/Inspector.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
using System;
using System.Reactive.Linq;
using System.Windows.Forms;
using GlazeWM.Infrastructure.WindowsApi;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;

namespace GlazeWM.Domain.Windows
{
public partial class Inspector : Form
{
public IDisposable EventListener { get; }

public Inspector()
{
InitializeComponent();

MaximizeBox = false;
MinimizeBox = false;
FormBorderStyle = FormBorderStyle.FixedSingle;
StartPosition = FormStartPosition.CenterParent;

var point = new Point();
EventListener = MouseEvents.MouseMoves
.Sample(TimeSpan.FromMilliseconds(50))
.Subscribe((@event) =>
{
if (point.X == @event.Point.X && point.Y == @event.Point.Y)
{
return;
}

point.X = @event.Point.X;
point.Y = @event.Point.Y;
var handle = WindowFromPoint(point);

// get handle details
var processName = WindowService.GetProcessOfHandle(handle)?.ProcessName ?? string.Empty;
if (processName == "GlazeWM")
{
return;
}

var title = WindowService.GetTitleOfHandle(handle) ?? string.Empty;
var className = WindowService.GetClassNameOfHandle(handle) ?? string.Empty;

// update the inspector info
titleValue.Text = title;
classNameValue.Text = className;
processNameValue.Text = processName;
});
}
}
}
16 changes: 16 additions & 0 deletions GlazeWM.Infrastructure/WindowsApi/Point.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace GlazeWM.Infrastructure.WindowsApi
Expand All @@ -7,5 +8,20 @@ public struct Point
{
public int X;
public int Y;

public static bool operator ==(Point obj1, Point obj2)
{
return obj1.Equals(obj2);
}

public static bool operator !=(Point obj1, Point obj2)
{
return !(obj1 == obj2);
}

public override bool Equals([NotNullWhen(true)] object obj)
{
return obj is Point other && other.X == X && other.Y == Y;
}
}
}

0 comments on commit 05db0e5

Please sign in to comment.