summaryrefslogtreecommitdiff
path: root/treeDraw.go
blob: 802e4988596841eb3f3ba0a8ee33b1ab8f1b8cc8 (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
135
136
137
138
139
140
141
142
143
144
// 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"
	"go.wit.com/widget"
)

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

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

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

	a := tk.gocuiSize.w0
	b := tk.gocuiSize.h0
	c := tk.gocuiSize.w1
	d := tk.gocuiSize.h1

	// this is all terrible. This sets the title. kinda
	if tk.node.WidgetType == widget.Window {
		tk.textResize()
		tk.full.w0 = tk.force.w0
		tk.full.h0 = tk.force.h0

		// for windows, make it the full size
		a = tk.full.w0
		b = tk.full.h0
		c = tk.full.w0 + tk.gocuiSize.Width()
		d = tk.full.h0 + tk.gocuiSize.Height()
	} else {
		if tk.internal {
			// do nothing
		} else {
			tk.textResize() // resize gocui frame to the widget text
		}
		a = tk.gocuiSize.w0
		b = tk.gocuiSize.h0
		c = tk.gocuiSize.w1
		d = tk.gocuiSize.h1

	}

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

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

	// tmp hack to disable buttons on window open
	if tk.node.WidgetType == widget.Button {
		if tk.node.IsEnabled() {
		} else {
			tk.setColorDisable()
		}
	}

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

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

func (w *guiWidget) simpleDrawAt(offsetW, offsetH int) {
	w.setColor(&colorActiveW)
	w.dumpWidget("simpleDrawAt()")
}

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

func (w *guiWidget) Show() {
	if w.isFake {
		// don't display fake widgets
		return
	}

	if w.Hidden() {
		// recursively checks if the parent is hidden
		// never show hidden widgets
		return
	}

	w.drawView()
}