summaryrefslogtreecommitdiff
path: root/toolkit/gocui/gocui.go
blob: d2877d7be83b28fa17f0306aba9509596a5d00ea (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
// 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"
	"github.com/awesome-gocui/gocui"
)

// This initializes the gocui package
// it runs SetManagerFunc which passes every input
// event (keyboard, mouse, etc) to the function "gocuiEvent()"
func gocuiMain() {
	g, err := gocui.NewGui(gocui.OutputNormal, true)
	if err != nil {
		panic(err)
	}
	defer g.Close()

	me.baseGui = g

	g.Cursor = true
	g.Mouse = true

	// this sets the function that is run on every event. For example:
	// When you click the mouse, move the mouse, or press a key on the keyboard
	// This is equivalent to xev or similar to cat /dev/input on linux
	g.SetManagerFunc(gocuiEvent)

	if err := defaultKeybindings(g); err != nil {
		panic(err)
	}

	if err := g.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) {
		panic(err)
	}
}

// Thanks to the gocui developers -- your package kicks ass
// This function is called on every event. It is a callback function from the gocui package
// which has an excellent implementation. While gocui handles things like text highlighting
// and the layout of the text areas -- also things like handling SIGWINCH and lots of really
// complicated console handling, it sends events here in a clean way.
// This is equivalent to the linux command xev (apt install x11-utils)
func gocuiEvent(g *gocui.Gui) error {
	maxX, maxY := g.Size()
	mx, my := g.MousePosition()
	log(logVerbose, "handleEvent() START", maxX, maxY, mx, my, msgMouseDown)
	if _, err := g.View("msg"); msgMouseDown && err == nil {
		moveMsg(g)
	}
	if widgetView, _ := g.View("msg"); widgetView == nil {
		log(logNow, "handleEvent() create output widget now", maxX, maxY, mx, my)
		makeOutputWidget(g, "this is a create before a mouse click")
		if (me.logStdout != nil) {
			// setOutput(me.logStdout)
		}
	} else {
		log(logInfo, "output widget already exists", maxX, maxY, mx, my)
	}
	mouseMove(g)
	log(logVerbose, "handleEvent() END  ", maxX, maxY, mx, my, msgMouseDown)
	return nil
}

func dragOutputWindow() {
}

// turns off the frame on the global window
func setFrame(b bool) {
	// TODO: figure out what this might be useful for
	// what is this do? I made it just 2 lines for now. Is this useful for something?
	v := SetView("global", 5, 10, 5, 10, 0) // x0, x1, y1, y2, overlap
	if (v == nil) {
		log(logError, "setFrame() global failed")
	}
	v.Frame = b
}

func quit(g *gocui.Gui, v *gocui.View) error {
	return gocui.ErrQuit
}

func SetView(name string, x0, y0, x1, y1 int, overlaps byte) *gocui.View {
	if (me.baseGui == nil) {
		log(logError, "SetView() ERROR: me.baseGui == nil")
		return nil
	}

	v, err := me.baseGui.SetView(name, x0, y0, x1, y1, overlaps)
	if err != nil {
		if !errors.Is(err, gocui.ErrUnknownView) {
			log(logError, "SetView() global failed on name =", name)
		}
		return nil
	}
	return v
}