Skip to content

Commit f43dc3c

Browse files
committed
Fix issue with cursor not chaning in openGL mode.
1 parent ee4be0c commit f43dc3c

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

src/fidget/common.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type
161161
pixelScale*: float32
162162
wheelDelta*: float32
163163
cursorStyle*: MouseCursorStyle ## Sets the mouse cursor icon
164+
prevCursorStyle*: MouseCursorStyle
164165

165166
Keyboard* = ref object
166167
state*: KeyState

src/fidget/htmlbackend.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ proc draw*(index: int, node: Node, parent: Node) =
362362
draw(i, n, node)
363363

364364
var startTime: float
365-
var prevMouseCursorStyle: MouseCursorStyle
366365

367366
proc drawStart() =
368367
startTime = dom.window.performance.now()
@@ -443,8 +442,8 @@ proc drawFinish() =
443442
# echo perf.numLowLevelCalls
444443
445444
# Only set mouse style when it changes.
446-
if prevMouseCursorStyle != mouse.cursorStyle:
447-
prevMouseCursorStyle = mouse.cursorStyle
445+
if mouse.prevCursorStyle != mouse.cursorStyle:
446+
mouse.prevCursorStyle = mouse.cursorStyle
448447
case mouse.cursorStyle:
449448
of Default:
450449
rootDomNode.style.cursor = "default"

src/fidget/opengl/base.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ var
4141
frameCount*, tickCount*: int
4242
lastDraw, lastTick: int64
4343

44+
var
45+
cursorDefault*: CursorHandle
46+
cursorPointer*: CursorHandle
47+
cursorGrab*: CursorHandle
48+
cursorNSResize*: CursorHandle
49+
50+
proc setCursor*(cursor: CursorHandle) =
51+
echo "set cursor"
52+
window.setCursor(cursor)
53+
4454
proc updateWindowSize() =
4555
requestedFrame = true
4656

@@ -335,6 +345,11 @@ proc start*(openglVersion: (int, int), msaa: MSAA, mainLoopMode: MainLoopMode) =
335345

336346
window.makeContextCurrent()
337347

348+
cursorDefault = createStandardCursor(ARROW_CURSOR)
349+
cursorPointer = createStandardCursor(HAND_CURSOR)
350+
cursorGrab = createStandardCursor(HAND_CURSOR)
351+
cursorNSResize = createStandardCursor(HRESIZE_CURSOR)
352+
338353
when not defined(emscripten):
339354
swapInterval(1)
340355
# Load OpenGL

src/fidget/openglbackend.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ proc setupFidget(
306306
ctx.saveTransform()
307307
ctx.scale(ctx.pixelScale)
308308

309+
mouse.cursorStyle = Default
310+
309311
setupRoot()
310312
root.box.x = float 0
311313
root.box.y = float 0
@@ -330,6 +332,20 @@ proc setupFidget(
330332
ctx.restoreTransform()
331333
ctx.endFrame()
332334

335+
# Only set mouse style when it changes.
336+
if mouse.prevCursorStyle != mouse.cursorStyle:
337+
mouse.prevCursorStyle = mouse.cursorStyle
338+
echo mouse.cursorStyle
339+
case mouse.cursorStyle:
340+
of Default:
341+
setCursor(cursorDefault)
342+
of Pointer:
343+
setCursor(cursorPointer)
344+
of Grab:
345+
setCursor(cursorGrab)
346+
of NSResize:
347+
setCursor(cursorNSResize)
348+
333349
when defined(testOneFrame):
334350
## This is used for test only
335351
## Take a screen shot of the first frame and exit.

tests/cursorstyle/cursorstyle.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Test the mouse cursor changing when you hover over an element.
2+
3+
import fidget
4+
5+
proc drawMain() =
6+
rectangle "example":
7+
box 100, 100, 100, 100
8+
fill "#FF0000"
9+
onHover:
10+
fill "#00FF00"
11+
mouse.cursorStyle = Pointer
12+
13+
startFidget(drawMain, w = 400, h = 400)

tests/cursorstyle/screenshot.png

135 Bytes
Loading

tests/run.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ proc main(
146146
runList.add "tests/textalignexpand"
147147
runList.add "tests/textalignfixed"
148148
runList.add "tests/textandinputs"
149+
runList.add "tests/cursorstyle"
149150

150151
runList.add "examples/areas"
151152
runList.add "examples/demo"

0 commit comments

Comments
 (0)