Skip to content

Commit

Permalink
Merge pull request #50 from guzba/master
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
treeform authored Jan 16, 2022
2 parents 61bb956 + 6b360e0 commit 4252e43
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 59 deletions.
8 changes: 0 additions & 8 deletions examples/monitors.nim

This file was deleted.

5 changes: 4 additions & 1 deletion examples/property_changes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ while not window.closeRequested:

pollEvents()

# Initial window open size
doAssert window.size == ivec2(1280, 800)

# Window will block changing size.
window.size = ivec2(300, 400)
doAssert window.size == ivec2(300, 400)
Expand Down Expand Up @@ -72,5 +75,5 @@ while not window.closeRequested:
window.fullscreen = false
doAssert not window.fullscreen

echo "SUCESS!"
echo "SUCCESS!"
quit()
8 changes: 8 additions & 0 deletions examples/screens.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import windy

when defined(windows) or defined(macosx):
# Screens API only currently supported on Windows and macOS

let screens = getScreens()
for screen in screens:
echo screen
2 changes: 1 addition & 1 deletion src/windy/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import unicode, pixie
type
WindyError* = object of ValueError

Monitor* = object
Screen* = object
left*, right*, top*, bottom*: int
primary*: bool

Expand Down
3 changes: 1 addition & 2 deletions src/windy/internal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type
icon*: Image
cursor*: Cursor
closeRequested*, closed*: bool
mousePos*: IVec2
mousePos*, mousePrevPos*: IVec2
buttonDown*, buttonToggle*: set[Button]
perFrame*: PerFrame

Expand All @@ -23,7 +23,6 @@ type
imeCompositionString*: string

PerFrame* = object
mousePrevPos*: IVec2
mouseDelta*: IVec2
scrollDelta*: Vec2
buttonPressed*, buttonReleased*: set[Button]
Expand Down
64 changes: 18 additions & 46 deletions src/windy/platforms/win32/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,20 @@ proc registerWindowClass(windowClassName: string, wndProc: WNDPROC) =
if RegisterClassExW(wc.addr) == 0:
raise newException(WindyError, "Error registering window class")

proc createWindow(windowClassName, title: string, size: IVec2): HWND =
proc createWindow(windowClassName, title: string): HWND =
let
wideWindowClassName = windowClassName.wstr()
wideTitle = title.wstr()

var size = size
if size != ivec2(CW_USEDEFAULT, CW_USEDEFAULT):
# Adjust the window creation size for window styles (border, etc)
var rect = Rect(top: 0, left: 0, right: size.x, bottom: size.y)
discard AdjustWindowRectExForDpi(
rect.addr,
decoratedWindowStyle,
0,
WS_EX_APPWINDOW,
defaultScreenDpi
)
size.x = rect.right - rect.left
size.y = rect.bottom - rect.top

result = CreateWindowExW(
WS_EX_APPWINDOW,
cast[ptr WCHAR](wideWindowClassName[0].unsafeAddr),
cast[ptr WCHAR](wideTitle[0].unsafeAddr),
decoratedWindowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
size.x,
size.y,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
GetModuleHandleW(nil),
Expand All @@ -168,8 +154,6 @@ proc createWindow(windowClassName, title: string, size: IVec2): HWND =
if result == 0:
raise newException(WindyError, "Creating native window failed")

discard SetPropW(result, cast[ptr WCHAR](windowPropKey[0].addr), 1)

proc destroy(window: Window) =
window.onCloseRequest = nil
window.onMove = nil
Expand Down Expand Up @@ -306,9 +290,6 @@ proc minimized*(window: Window): bool =
proc maximized*(window: Window): bool =
IsZoomed(window.hWnd) != 0

proc framebufferSize*(window: Window): IVec2 =
window.size

proc contentScale*(window: Window): float32 =
let dpi = GetDpiForWindow(window.hWnd)
result = dpi.float32 / defaultScreenDpi
Expand Down Expand Up @@ -541,11 +522,7 @@ proc loadOpenGL() =
registerWindowClass(dummyWindowClassName, dummyWndProc)

let
hWnd = createWindow(
dummyWindowClassName,
dummyWindowClassName,
ivec2(CW_USEDEFAULT, CW_USEDEFAULT)
)
hWnd = createWindow(dummyWindowClassName, dummyWindowClassName)
hdc = getDC(hWnd)

var pfd: PIXELFORMATDESCRIPTOR
Expand Down Expand Up @@ -644,11 +621,7 @@ proc createHelperWindow(): HWND =

registerWindowClass(helperWindowClassName, helperWndProc)

result = createWindow(
helperWindowClassName,
helperWindowClassName,
ivec2(CW_USEDEFAULT, CW_USEDEFAULT)
)
result = createWindow(helperWindowClassName,helperWindowClassName)

proc handleButtonPress(window: Window, button: Button) =
handleButtonPressTemplate()
Expand Down Expand Up @@ -705,13 +678,13 @@ proc wndProc(
)
return 0
of WM_MOUSEMOVE:
window.state.perFrame.mousePrevPos = window.state.mousePos
window.state.mousePrevPos = window.state.mousePos
var pos: POINT
discard GetCursorPos(pos.addr)
discard ScreenToClient(window.hWnd, pos.addr)
window.state.mousePos = ivec2(pos.x, pos.y)
window.state.perFrame.mouseDelta =
window.state.mousePos - window.state.perFrame.mousePrevPos
window.state.perFrame.mouseDelta +=
window.state.mousePos - window.state.mousePrevPos
if window.onMouseMove != nil:
window.onMouseMove()
if not window.trackMouseEventRegistered:
Expand Down Expand Up @@ -927,11 +900,10 @@ proc newWindow*(

result = Window()
result.title = title
result.hWnd = createWindow(
windowClassName,
title,
size
)
result.hWnd = createWindow(windowClassName, title)
result.size = size

discard SetPropW(result.hWnd, cast[ptr WCHAR](windowPropKey[0].addr), 1)

try:
result.hdc = getDC(result.hWnd)
Expand Down Expand Up @@ -1033,7 +1005,7 @@ proc mousePos*(window: Window): IVec2 =
window.state.mousePos

proc mousePrevPos*(window: Window): IVec2 =
window.state.perFrame.mousePrevPos
window.state.mousePrevPos

proc mouseDelta*(window: Window): IVec2 =
window.state.perFrame.mouseDelta
Expand Down Expand Up @@ -1192,11 +1164,11 @@ proc hideTrayIcon*() =
discard DestroyIcon(trayIconHandle)
trayIconHandle = 0

proc getMonitors*(): seq[Monitor] =
## Queries and returns the currently connected monitors.
proc getScreens*(): seq[Screen] =
## Queries and returns the currently connected screens.

type Holder = ref object
monitors: seq[Monitor]
screens: seq[Screen]

var h = Holder()

Expand All @@ -1211,7 +1183,7 @@ proc getMonitors*(): seq[Monitor] =

discard GetMonitorInfoW(hMonitor, mi.addr)

cast[ptr Holder](extra).monitors.add(Monitor(
cast[ptr Holder](extra).screens.add(Screen(
left: screenCoords.left,
right: screenCoords.right,
top: screenCoords.top,
Expand All @@ -1223,4 +1195,4 @@ proc getMonitors*(): seq[Monitor] =

discard EnumDisplayMonitors(0, nil, callback, cast[LPARAM](h.addr))

h.monitors
h.screens
1 change: 0 additions & 1 deletion windy.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ license = "MIT"
srcDir = "src"

requires "nim >= 1.4.8"
requires "vmath >= 1.1.0"
requires "opengl >= 1.2.6"
requires "pixie >= 3.1.2"

0 comments on commit 4252e43

Please sign in to comment.