~eliasnaur/gio-patches

3 2

Mouse Cursor Position Label Example

Details
Message ID
<CAG2iXeOgd3XzrNJ5TtqGDFBz46DpHic6B2N-22==JCLOJmoUOA@mail.gmail.com>
DKIM signature
pass
Download raw message
Hi All,

I'd like to add a simple example that just displays the mouse cursors'
current position and tracks if the pointer is hovering over the
window.

I only started learning the framework today, and found a few surprises
that took me longer to figure out despite the other examples in
outlay/fan and game of life. Anyways contributing this back in case it
helps someone out there.

Patch attached. I don't have a sr.ht account so I'm not sure the right
way to do this.
Also feel free to disregard or do with this as you please if you feel
there's sufficient coverage already.

PS: This is my 3rd attempt trying to get the mailer filter to accept
my patch so i will just give up after. Apologies.

PPS: Here's the patch since attachments and html are not allowed. IDK
how to email a commit LOL

   Subject: [PATCH] Add simple example to show mouse pointer coordinates
---
Index: coordinates/main.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/coordinates/main.go b/coordinates/main.go
new file mode 100644
--- /dev/null (revision 78f25a88b771070652556a0e2420c73461602125)
+++ b/coordinates/main.go (revision 78f25a88b771070652556a0e2420c73461602125)
@@ -0,0 +1,102 @@
+package main
+
+import (
+ "fmt"
+ "gioui.org/io/event"
+ "gioui.org/op/clip"
+ "image"
+ "log"
+ "os"
+
+ "gioui.org/app"
+ "gioui.org/f32"
+ "gioui.org/io/pointer"
+ "gioui.org/layout"
+ "gioui.org/op"
+ "gioui.org/unit"
+ "gioui.org/widget/material"
+)
+
+func main() {
+ // Create a new window.
+ go func() {
+ w := new(app.Window)
+ w.Option(app.Size(800, 600))
+ if err := loop(w); err != nil {
+ log.Fatal(err)
+ }
+ os.Exit(0)
+ }()
+
+ app.Main()
+}
+
+func loop(w *app.Window) error {
+ var ops op.Ops
+
+ // Initialize the mouse position.
+ var mousePos f32.Point
+ mousePresent := false
+
+ // Create a material theme.
+ th := material.NewTheme()
+
+ for {
+
+ switch e := w.Event().(type) {
+ case app.DestroyEvent:
+ return e.Err
+ case app.FrameEvent:
+ log.Printf("Processing window FrameEvent.")
+
+ gtx := app.NewContext(&ops, e)
+
+ // Register for pointer move events over the entire window.
+ r := image.Rectangle{Max: image.Point{X: gtx.Constraints.Max.X, Y:
gtx.Constraints.Max.Y}}
+ area := clip.Rect(r).Push(&ops)
+ event.Op(&ops, &mousePos)
+ area.Pop()
+
+ latestPos := mousePos
+ latestPresent := mousePresent
+ for {
+ ev, ok := gtx.Event(pointer.Filter{
+ Target: &mousePos,
+ Kinds:  pointer.Move | pointer.Enter | pointer.Leave,
+ })
+ if !ok {
+ log.Printf("No matching pointer events.")
+ break
+ }
+ switch ev := ev.(type) {
+ case pointer.Event:
+ switch ev.Kind {
+ case pointer.Enter:
+ latestPresent = true
+ case pointer.Leave:
+ latestPresent = false
+ }
+ log.Printf("processed move %v", image.Pt(int(ev.Position.X),
int(ev.Position.Y)))
+ latestPos = ev.Position
+ }
+ }
+ if latestPos != mousePos || latestPresent != mousePresent {
+ mousePos = latestPos
+ mousePresent = latestPresent
+ gtx.Execute(op.InvalidateCmd{})
+ }
+
+ // Display the mouse coordinates.
+ layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
+ coords := "Mouse is outside window"
+ if mousePresent {
+ coords = fmt.Sprintf("Mouse Position: (%.2f, %.2f)", mousePos.X, mousePos.Y)
+ }
+ lbl := material.Label(th, unit.Sp(24), coords)
+ return lbl.Layout(gtx)
+ })
+
+ e.Frame(gtx.Ops)
+ }
+ }
+}
Details
Message ID
<CAFcc3FQ5jRWOytFO7rf_ATPLBDnP4VUBaMsS9rhnekmCMQ8OCQ@mail.gmail.com>
In-Reply-To
<CAG2iXeOgd3XzrNJ5TtqGDFBz46DpHic6B2N-22==JCLOJmoUOA@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Thanks Alec for the effort to submit an example! In the future, give
https://git-send-email.io/ a look for some guidance on how to set up
the email patching. It can be a pain to do the first time, but it's
easy after that.

 Could you reply stating that you agree to the DCO (see
https://gioui.org/doc/contribute#style) and that your example is
licensed under the dual Unlicense/MIT like the rest of the examples? I
can merge your patch if we have that.

Cheers,
Chris
Details
Message ID
<CAG2iXeOyw81y9xFaU=VxHDhWqvz8DkpLPGy25KO=Fr5cmBv4ng@mail.gmail.com>
In-Reply-To
<CAFcc3FQ5jRWOytFO7rf_ATPLBDnP4VUBaMsS9rhnekmCMQ8OCQ@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Thanks Chris!

I'll check out the guidance for any future contributions I make,
appreciate the feedback.

I agree with the DCO, am the sole author of that snippet and am free
to license it with Unlicense/MIT.

Thanks!

On Wed, Sep 18, 2024 at 3:11 AM Chris Waldon
<christopher.waldon.dev@gmail.com> wrote:
>
> Thanks Alec for the effort to submit an example! In the future, give
> https://git-send-email.io/ a look for some guidance on how to set up
> the email patching. It can be a pain to do the first time, but it's
> easy after that.
>
>  Could you reply stating that you agree to the DCO (see
> https://gioui.org/doc/contribute#style) and that your example is
> licensed under the dual Unlicense/MIT like the rest of the examples? I
> can merge your patch if we have that.
>
> Cheers,
> Chris
Details
Message ID
<CAFcc3FS02CouGnCDpc1PoGyu-kAzBuAAbAZ2Eft0GDLDtnnuxw@mail.gmail.com>
In-Reply-To
<CAG2iXeOyw81y9xFaU=VxHDhWqvz8DkpLPGy25KO=Fr5cmBv4ng@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Merged, thanks!

To git.sr.ht:~eliasnaur/gio-example
   c1f4ec2..715969b  main -> main
Reply to thread Export thread (mbox)