summaryrefslogtreecommitdiff
path: root/eventMouse.go
blob: 9821bd1cd2609dc1da36d82c57f6125dbdbb4669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

// NOTE: our code is under the GPL, not BSD

// note by [email protected] in 2025: this is one of the coolest
// things ever what this does. I've tried to improve
// it while I've been working on making a gocui
// GO plugin so it can be generalized as a useful
// console interface. Well done everyone that has
// contributed to this gocui project !!!

// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"errors"
	"fmt"

	"github.com/awesome-gocui/gocui"

	"go.wit.com/log"
)

// this function uses the mouse position to highlight & unhighlight things
// this is run every time the user moves the mouse over the terminal window
func mouseMove(g *gocui.Gui) {
	mx, my := g.MousePosition()
	for _, view := range g.Views() {
		view.Highlight = false
	}
	if v, err := g.ViewByPosition(mx, my); err == nil {
		v.Highlight = true
	}
}

// I think this lets me drag the debugging window
func msgDown(g *gocui.Gui, v *gocui.View) error {
	initialMouseX, initialMouseY = g.MousePosition()

	w := initialMouseX
	h := initialMouseY

	for _, tk := range findByXY(w, h) {
		log.Log(GOCUI, fmt.Sprintf("findByXY() msgDown() %s wId=%d cuiName=%s at (%d,%d)", tk.WidgetType, tk.node.WidgetId, tk.cuiName, w, h))
	}

	// 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) {
		log.Log(GOCUI, fmt.Sprintf("findByXY() mouseUp() %s wId=%d cuiName=%s at (%d,%d)", tk.WidgetType, tk.node.WidgetId, tk.cuiName, w, h))
	}

	dropdownUnclicked(w, h)

	if msgMouseDown {
		msgMouseDown = false
		if movingMsg {
			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) {
		log.Log(GOCUI, fmt.Sprintf("findByXY() mouseDown() %s wId=%d cuiName=%s at (%d,%d)", tk.WidgetType, tk.node.WidgetId, tk.cuiName, mx, my))
		found = true
	}
	if !found {
		log.Log(GOCUI, fmt.Sprintf("findByXY() 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()

	findUnderMouse()

	msg := fmt.Sprintf("mouseDown() Mouse really down at: %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
}