Skip to content

Commit

Permalink
Merge pull request #53 from guzba/master
Browse files Browse the repository at this point in the history
mac fullscreen, custom cursor
  • Loading branch information
treeform authored Jan 19, 2022
2 parents 25017d7 + ce6aea4 commit 2227894
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 123 deletions.
13 changes: 11 additions & 2 deletions examples/custom_cursor.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pixie, windy

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

let window = newWindow("Windy Cursor", ivec2(1280, 800))
window.makeContextCurrent()
Expand All @@ -18,5 +18,14 @@ when defined(windows):

echo window.cursor

window.onButtonPress = proc(button: Button) =
if button == MouseLeft:
echo "Toggling cursor"
case window.cursor.kind:
of DefaultCursor:
window.cursor = Cursor(kind: CustomCursor, image: cursor)
else:
window.cursor = Cursor(kind: DefaultCursor)

while not window.closeRequested:
pollEvents()
27 changes: 27 additions & 0 deletions examples/highdpi.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import opengl, windy

let window = newWindow("Windy Retina", ivec2(1280, 800))

# Windy uses physical pixels for window size units.
# This means this 1280 x 800 pixel window will look smaller than expected
# on high-dpi screens like a Macbook Retina display.

# To address this, scale the window size by the window's content scale.
window.size = (window.size.vec2 * window.contentScale).ivec2

# On a Retina display with a content scale of 2.0, this window will be resized
# to 2560 x 1600 physical pixels. This resized window will have approximately
# the same size-on-screen as a 1280 x 800 physical pixel window would with a
# content scale of 1.0.

window.makeContextCurrent()
loadExtensions()

proc display() =
glClear(GL_COLOR_BUFFER_BIT)
# Your OpenGL display code here
window.swapBuffers()

while not window.closeRequested:
display()
pollEvents()
43 changes: 43 additions & 0 deletions src/windy/platforms/macos/macdefs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ type
NSOpenGLContext* = distinct NSObject
NSTrackingArea* = distinct NSObject
NSResponder* = distinct NSObject
NSImage* = distinct NSObject
NSCursor* = distinct NSObject

const
NSNotFound* = int.high
Expand All @@ -112,6 +114,7 @@ const
NSWindowStyleMaskClosable* = (1 shl 1).NSWindowStyleMask
NSWindowStyleMaskMiniaturizable* = (1 shl 2).NSWindowStyleMask
NSWindowStyleMaskResizable* = (1 shl 3).NSWindowStyleMask
NSWindowStyleMaskFullScreen* = (1 shl 14).NSWindowStyleMask
NSBackingStoreBuffered* = 2.NSBackingStoreType
NSApplicationActivationPolicyRegular* = 0.NSApplicationActivationPolicy
NSApplicationPresentationDefault* = 0.NSApplicationPresentationOptions
Expand Down Expand Up @@ -158,6 +161,9 @@ proc NSMakeSize*(w, h: float64): NSSize =
proc NSMakeRange*(loc, len: uint): NSRange =
NSRange(location: loc, length: len)

proc NSMakePoint*(x, y: float): NSPoint =
NSPoint(x: x, y: y)

proc getClass*(t: typedesc): Class =
objc_getClass(t.name.cstring)

Expand Down Expand Up @@ -708,6 +714,20 @@ proc setStyleMask*(window: NSWindow, styleMask: NSWindowStyleMask) =
styleMask
)

proc toggleFullscreen*(window: NSWindow, sender: ID) =
discard objc_msgSend(
window.ID,
sel_registerName("toggleFullScreen:".cstring),
sender
)

proc invalidateCursorRectsForView*(window: NSWindow, view: NSView) =
discard objc_msgSend(
window.ID,
sel_registerName("invalidateCursorRectsForView:".cstring),
view.ID
)

proc convertRectToBacking*(view: NSView, rect: NSRect): NSRect =
objc_msgSend_stret(
result.addr,
Expand Down Expand Up @@ -743,6 +763,14 @@ proc addTrackingArea*(view: NSView, trackingArea: NSTrackingArea) =
trackingArea
)

proc addCursorRect*(view: NSview, rect: NSRect, cursor: NSCursor) =
discard objc_msgSend(
view.ID,
sel_registerName("addCursorRect:cursor:".cstring),
rect,
cursor
)

proc initWithAttributes*(
pixelFormat: NSOpenGLPixelFormat,
attribs: ptr NSOpenGLPixelFormatAttribute
Expand Down Expand Up @@ -826,3 +854,18 @@ proc interpretKeyEvents*(responder: NSResponder, events: NSArray) =
sel_registerName("interpretKeyEvents:".cstring),
events
)

proc initWithData*(image: NSImage, data: NSData) =
discard objc_msgSend(
image.ID,
sel_registerName("initWithData:".cstring),
data
)

proc initWithImage*(cursor: NSCursor, image: NSImage, hotspot: NSPoint) =
discard objc_msgSend(
cursor.ID,
sel_registerName("initWithImage:hotSpot:".cstring),
image,
hotspot
)
Loading

0 comments on commit 2227894

Please sign in to comment.