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

fix mac initial click to unfocused window mouse pos #63

Merged
merged 2 commits into from
Feb 7, 2022
Merged
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
3 changes: 2 additions & 1 deletion src/windy/platforms/macos/macdefs.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import opengl, std/typetraits, objc
import opengl, objc
export objc

{.passL: "-framework Cocoa".}
Expand Down Expand Up @@ -210,6 +210,7 @@ objc:
proc setStyleMask*(self: NSWindow, _: NSWindowStyleMask)
proc toggleFullscreen*(self: NSWindow, _: ID)
proc invalidateCursorRectsForView*(self: NSWindow, _: NSView)
proc mouseLocationOutsideOfEventStream*(self: NSWindow): NSPoint
proc convertRectToBacking*(self: NSView, _: NSRect): NSRect
proc window*(self: NSView): NSWindow
proc bounds*(self: NSView): NSRect
Expand Down
33 changes: 18 additions & 15 deletions src/windy/platforms/macos/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ proc `cursor=`*(window: Window, cursor: Cursor) =
autoreleasepool:
window.inner.invalidateCursorRectsForView(window.inner.contentView)

proc handleMouseMove(window: Window, location: NSPoint) =
let
x = round(location.x)
y = round(window.inner.contentView.bounds.size.height - location.y)

window.state.mousePrevPos = window.state.mousePos
window.state.mousePos = (vec2(x, y) * window.contentScale).ivec2
window.state.perFrame.mouseDelta +=
window.state.mousePos - window.state.mousePrevPos

if window.onMouseMove != nil:
window.onMouseMove()

proc handleButtonPress(window: Window, button: Button) =
handleButtonPressTemplate()

Expand Down Expand Up @@ -276,8 +289,10 @@ proc windowDidBecomeKey(
notification: NSNotification
): ID {.cdecl.} =
let window = windows.forNSWindow(self.NSWindow)
if window != nil and window.onFocusChange != nil:
window.onFocusChange()
if window != nil:
if window.onFocusChange != nil:
window.onFocusChange()
handleMouseMove(window, window.inner.mouseLocationOutsideOfEventStream)

proc windowDidResignKey(
self: ID,
Expand Down Expand Up @@ -349,19 +364,7 @@ proc mouseMoved(self: ID, cmd: SEL, event: NSEvent): ID {.cdecl.} =
let window = windows.forNSWindow(self.NSView.window)
if window == nil:
return

let
locationInWindow = event.locationInWindow
x = round(locationInWindow.x)
y = round(self.NSView.bounds.size.height - locationInWindow.y)

window.state.mousePrevPos = window.state.mousePos
window.state.mousePos = (vec2(x, y) * window.contentScale).ivec2
window.state.perFrame.mouseDelta +=
window.state.mousePos - window.state.mousePrevPos

if window.onMouseMove != nil:
window.onMouseMove()
handleMouseMove(window, event.locationInWindow)

proc mouseDragged(self: ID, cmd: SEL, event: NSEvent): ID {.cdecl.} =
mouseMoved(self, cmd, event)
Expand Down