Skip to content

Commit

Permalink
Handle display resolution changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lesderid committed Jan 11, 2021
1 parent 075f767 commit 10c6d69
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
26 changes: 17 additions & 9 deletions src/keynavish/commands.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ private void redrawWindow()

private void start()
{
import core.sys.windows.windows : MoveWindow;

resetGrid();
MoveWindow(windowHandle, 0, 0, grid.rect.width, grid.rect.height, false);

showWindow();
}

private void end()
{
hideWindow();
resetGrid();
}

private void toggleStart()
Expand Down Expand Up @@ -140,6 +144,8 @@ private void move(Direction direction, string arg)

if (!active) return;

auto resolution = deviceResolution;

auto value = getCutMoveValue(direction, arg != null ? arg : "1");

Grid newGrid = grid;
Expand All @@ -157,10 +163,10 @@ private void move(Direction direction, string arg)
case down:
newGrid.rect.top += value;
newGrid.rect.bottom += value;
if (newGrid.rect.bottom > screenHeight)
if (newGrid.rect.bottom > resolution.height)
{
newGrid.rect.top -= (newGrid.rect.bottom - screenHeight);
newGrid.rect.bottom = screenHeight;
newGrid.rect.top -= (newGrid.rect.bottom - resolution.height);
newGrid.rect.bottom = resolution.height;
}
break;
case left:
Expand All @@ -175,10 +181,10 @@ private void move(Direction direction, string arg)
case right:
newGrid.rect.left += value;
newGrid.rect.right += value;
if (newGrid.rect.right > screenWidth)
if (newGrid.rect.right > resolution.width)
{
newGrid.rect.left -= (newGrid.rect.right - screenWidth);
newGrid.rect.right = screenWidth;
newGrid.rect.left -= (newGrid.rect.right - resolution.width);
newGrid.rect.right = resolution.width;
}
break;
}
Expand All @@ -195,13 +201,15 @@ private void warp()

if (!active) return;

auto resolution = deviceResolution;

auto middleX = grid.rect.left + grid.rect.width / 2;
auto middleY = grid.rect.top + grid.rect.height / 2;

INPUT input;
input.type = INPUT_MOUSE;
input.mi.dx = middleX * 65536 / screenWidth;
input.mi.dy = middleY * 65536 / screenHeight;
input.mi.dx = middleX * 65536 / resolution.width;
input.mi.dy = middleY * 65536 / resolution.height;
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | draggingFlag;
SendInput(1, &input, INPUT.sizeof);
}
Expand Down
20 changes: 17 additions & 3 deletions src/keynavish/grid.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module keynavish.grid;

import core.sys.windows.windows : RECT, HDC, HPEN;
import std.container : SList;
import std.typecons : Tuple;
import keynavish;

struct Grid
Expand All @@ -16,8 +17,19 @@ private SList!Grid gridStack;

HPEN pen;

int screenWidth;
int screenHeight;
@property
Tuple!(int, "width", int, "height") deviceResolution()
{
import core.sys.windows.windows : GetDC, GetDeviceCaps, HORZRES, VERTRES;

auto rootDeviceContext = GetDC(null);

auto resolution = typeof(return)();
resolution.width = GetDeviceCaps(rootDeviceContext, HORZRES);
resolution.height = GetDeviceCaps(rootDeviceContext, VERTRES);

return resolution;
}

const(Grid) grid()
{
Expand All @@ -42,7 +54,9 @@ void tryPopGrid()

void resetGrid()
{
grid_.rect = RECT(0, 0, screenWidth, screenHeight);
auto resolution = deviceResolution;

grid_.rect = RECT(0, 0, resolution.width, resolution.height);
grid_.rows = 2;
grid_.columns = 2;
gridStack = typeof(gridStack)();
Expand Down
4 changes: 0 additions & 4 deletions src/keynavish/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ static this()
registerKeyboardHook();

pen = CreatePen(PS_SOLID, penWidth, penColour);

auto rootDeviceContext = GetDC(null);
screenWidth = GetDeviceCaps(rootDeviceContext, HORZRES);
screenHeight = GetDeviceCaps(rootDeviceContext, VERTRES);
}

extern(C)
Expand Down
6 changes: 4 additions & 2 deletions src/keynavish/window.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ void registerWindowClass()

void createWindow()
{
auto resolution = deviceResolution;

windowHandle = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE | WS_EX_TRANSPARENT,
windowClassName.ptr,
programName.ptr,
WS_POPUP,
0,
0,
screenWidth,
screenHeight,
resolution.width,
resolution.height,
null,
null,
GetModuleHandle(null),
Expand Down

0 comments on commit 10c6d69

Please sign in to comment.