summaryrefslogtreecommitdiff
path: root/eventMouseMove.go
blob: b9b34fd790192af977c6280010e1af536110b3c8 (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

// 2025 note by jcarr:
// this is one of the coolest things ever worked with.
// Personally, I've been working on making a gocui GO plugin
// so I can use it as a generalized console GUI toolkit.
//
// Well done everyone that has contributed to this gocui project !!!
// I am in your debt. Happy hacking & peace.

package main

import (
	"fmt"

	"github.com/awesome-gocui/gocui"
	log "go.wit.com/log"
	"go.wit.com/widget"
)

// 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()

	w := mx
	h := my

	if me.supermouse {
		for _, tk := range findByXY(w, h) {
			tk.dumpWidget("mouseMove()")
		}
	}

	if msgMouseDown {
		log.Info("msgMouseDown == true")
		// plugin will segfault if you don't keep this inside a check for msgMouseDown
		// don't move this code out of here
		for _, tk := range findByXY(w, h) {
			if tk.node.WidgetType == widget.Stdout {
				tk.moveNew(g)
				return
			}
			if tk.node.WidgetType == widget.Label {
				tk.moveNew(g)
				return
			}
		}
	}

	if createStdout(g) {
		return
	}

	for _, view := range g.Views() {
		view.Highlight = false
	}
	if v, err := g.ViewByPosition(mx, my); err == nil {
		v.Highlight = true
	}
}

// this is how the window gets dragged around
func (tk *guiWidget) moveNew(g *gocui.Gui) {
	mx, my := g.MousePosition()
	tk.dumpWidget("moveNew() on " + tk.cuiName)
	g.SetView("msg", mx-xOffset, my-yOffset, mx-xOffset+outputW, my-yOffset+outputH+me.FramePadH, 0)
	me.startOutputW = mx - xOffset
	me.startOutputH = my - yOffset
	g.SetViewOnBottom("msg")
}

func createStdout(g *gocui.Gui) bool {
	if widgetView, _ := g.View("msg"); widgetView == nil {
		makeOutputWidget(g, "this is a create before a mouse click")
		if me.logStdout != nil {
			msg := fmt.Sprintf("test out gocuiEvent() %d\n", me.ecount)
			me.logStdout.Write([]byte(msg))
			log.Log(NOW, "logStdout test out")
		}
		return true
	}
	return false
}