summaryrefslogtreecommitdiff
path: root/treeDraw.go
blob: 6a171e1337b495ee14bbd4fbff0133565eefc64a (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
// 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"
	"strconv"

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

var toggle bool = true

func (w *guiWidget) DrawAt(offsetW, offsetH int) {
	w.setColor(&colorActiveW)
	w.placeWidgets(offsetW, offsetH) // compute the sizes & places for each widget
	w.active = false
	w.dumpWidget("DrawAt()")
}

func (w *guiWidget) toggleTree() {
	if toggle {
		w.drawTree(toggle)
		toggle = false
	} else {
		w.hideWidgets()
		toggle = true
	}
}

// display the widgets in the binary tree
func (w *guiWidget) drawTree(draw bool) {
	if w == nil {
		return
	}
	w.dumpWidget("in drawTree()")
	if draw {
		// w.textResize()
		w.Show()
	} else {
		w.Hide()
	}

	for _, child := range w.children {
		child.drawTree(draw)
	}
}

// display's the text of the widget in gocui
// deletes the old view if it exists and recreates it
func (w *guiWidget) drawView() {
	var err error
	log.Log(INFO, "drawView() START", w.node.WidgetType, w.String())
	if me.baseGui == nil {
		log.Log(ERROR, "drawView() ERROR: me.baseGui == nil", w)
		return
	}

	if w.cuiName == "" {
		log.Log(ERROR, "drawView() w.cuiName was not set for widget", w)
		w.cuiName = strconv.Itoa(w.node.WidgetId) + " TK"
	}
	log.Log(INFO, "drawView() labelN =", w.labelN)

	// this deletes the button from gocui
	me.baseGui.DeleteView(w.cuiName)
	w.v = nil

	w.textResize()
	a := w.gocuiSize.w0
	b := w.gocuiSize.h0
	c := w.gocuiSize.w1
	d := w.gocuiSize.h1

	w.v, err = me.baseGui.SetView(w.cuiName, a, b, c, d, 0)
	if err == nil {
		w.dumpWidget("drawView() err")
		log.Log(ERROR, "drawView() internal plugin error err = nil")
		return
	}
	if !errors.Is(err, gocui.ErrUnknownView) {
		w.dumpWidget("drawView() err")
		log.Log(ERROR, "drawView() internal plugin error error.IS()", err)
		return
	}

	// this sets up the keybinding for the name of the window
	// does this really need to be done? I think we probably already
	// know everything about where all the widgets are so we could bypass
	// the gocui package and just handle all the mouse events internally here (?)
	// for now, the w.v.Name is a string "1", "2", "3", etc from the widgetId

	// set the binding for this gocui view now that it has been created
	// gocui handles overlaps of views so it will run on the view that is clicked on
	// me.baseGui.SetKeybinding(w.v.Name(), gocui.MouseLeft, gocui.ModNone, click)

	// this actually sends the text to display to gocui
	w.v.Wrap = true
	w.v.Frame = w.frame
	w.v.Clear()
	fmt.Fprint(w.v, w.labelN)

	// if you don't do this here, it will be black & white only
	if w.color != nil {
		w.v.FrameColor = w.color.frame
		w.v.FgColor = w.color.fg
		w.v.BgColor = w.color.bg
		w.v.SelFgColor = w.color.selFg
		w.v.SelBgColor = w.color.selBg
	}
	log.Log(INFO, "drawView() END")
}