// Copyright 2017-2025 WIT.COM Inc. All rights reserved. // Use of this source code is governed by the GPL 3.0 package main import ( "errors" "fmt" "github.com/awesome-gocui/gocui" "go.wit.com/log" ) // event triggers when you push down on a mouse button func msgDown(g *gocui.Gui, v *gocui.View) error { initialMouseX, initialMouseY = g.MousePosition() w := initialMouseX h := initialMouseY for _, tk := range findByXY(w, h) { tk.dumpWidget("mouseDown()") } // debugging output // log.Log(GOCUI, "msgDown() X,Y", initialMouseX, initialMouseY) // vx, vy, _, _, err := g.ViewPosition("msg") if err == nil { xOffset = initialMouseX - vx yOffset = initialMouseY - vy msgMouseDown = true } return nil } func mouseUp(g *gocui.Gui, v *gocui.View) error { w, h := g.MousePosition() // useful to debug everything that is being clicked on /* for _, tk := range findByXY(w, h) { tk.dumpWidget("mouseUp()") } */ dropdownUnclicked(w, h) if msgMouseDown { msgMouseDown = false if me.movingMsg { me.movingMsg = false return nil } else { g.DeleteView("msg") } } else if globalMouseDown { globalMouseDown = false g.DeleteView("globalDown") } return nil } // func isMouseInMsg // this is where you have to figure out what // widget was underneath so you can active // the right response for the toolkit user's app func mouseDown(g *gocui.Gui, v *gocui.View) error { mx, my := g.MousePosition() var found bool = false for _, tk := range findByXY(mx, my) { tk.dumpWidget("mouseDown()") found = true } if !found { log.Log(GOCUI, fmt.Sprintf("mouseDown() found nothing at (%d,%d)", mx, my)) } vx0, vy0, vx1, vy1, err := g.ViewPosition("msg") if err == nil { if mx >= vx0 && mx <= vx1 && my >= vy0 && my <= vy1 { return msgDown(g, v) } } globalMouseDown = true maxX, _ := g.Size() // why was this here? // findUnderMouse() // TODO: USE THIS TO MAKE TEMPORARY HELP / INSTRUCTION DIALOGS // this message will pop up when you click on the magic thing // figure out how this works and make it generically useful. msg := fmt.Sprintf("This is -222 widget demo. %d,%d", mx, my) // dropdownClicked(mx, my) x := mx - len(msg)/2 if x < 0 { x = 0 } else if x+len(msg)+1 > maxX-1 { x = maxX - 1 - len(msg) - 1 } log.Log(GOCUI, "mouseDown() about to write out message to 'globalDown' view. msg =", msg) if v, err := g.SetView("globalDown", x, my-1, x+len(msg)+1, my+1, 0); err != nil { if !errors.Is(err, gocui.ErrUnknownView) { return err } v.WriteString(msg) } return nil }