simonkrauter / NiGui

Cross-platform desktop GUI toolkit written in Nim
MIT License
724 stars 49 forks source link

Obtaining cursor position #77

Closed ghost closed 6 months ago

ghost commented 4 years ago

Hi, I was wondering if it's possible to obtain the position of the cursor from a clickevent. Is there something like a cursor field? The x and y fields respectively only seem to return the position of the widget.

simonkrauter commented 4 years ago

Since commit https://github.com/trustable-code/NiGui/commit/3cd60917d261942dff845b3fd7c6fe81ccb82c48 there are methods to retrieve the mouse cusor position relative the window or the widget.

Example program:

import nigui

app.init()

var window = newWindow()

var mainContainer = newContainer()
window.add(mainContainer)

var button = newButton()
mainContainer.add(button)
button.width = 300
button.height = 150
button.x = 150
button.y = 150

window.show()

proc timerProc(event: TimerEvent) =
  var text = ""

  block:
    var (x, y) = window.mousePosition()
    text.add($x & ", " & $y)

  text.add("  |  ")

  block:
    var (x, y) = button.mousePosition()
    text.add($x & ", " & $y)

  button.text = text

let timer = startRepeatingTimer(50, timerProc)

app.run()
simonkrauter commented 6 months ago

Looks like resolved to me.