gocui/treeDraw.go

255 lines
5.0 KiB
Go

// 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"
)
// don't draw widgets that are too far down the window
func (tk *guiWidget) doNotDraw() bool {
var check bool
switch tk.WidgetType() {
case widget.Button:
check = true
case widget.Label:
check = true
default:
}
if !check {
return false
}
win := tk.findParentWindow()
if win == nil {
// don't draw anything if you can't find the parent window
return true
}
h := tk.gocuiSize.h0 - win.gocuiSize.h0
if h > 20 {
return true
}
return false
}
// page widgets in the window
func (tk *guiWidget) pageWidget() *rectType {
r := new(rectType)
var check bool
switch tk.WidgetType() {
case widget.Button:
check = true
case widget.Label:
check = true
default:
}
if !check {
return nil
}
win := tk.findParentWindow()
if win == nil {
// don't draw anything if you can't find the parent window
return nil
}
r.w0 = tk.gocuiSize.w0
r.h0 = tk.gocuiSize.h0
r.w1 = tk.gocuiSize.w1
r.h1 = tk.gocuiSize.h1
// r.h0 = tk.gocuiSize.h0 - win.gocuiSize.h0
if r.h0 > 20 {
return r
}
return r
}
// 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.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.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
/*
// testing code for paging large windows
if tk.doNotDraw() {
return
}
if tk.window.pager != 0 {
if r := tk.pageWidget(); r == nil {
// if nil, draw whatever it is anyway
} else {
if r.Width() == 0 && r.Height() == 0 {
// don't draw empty stuff
return
}
a = r.w0
b = r.h0
c = r.w1
d = r.h1
}
}
if tk.WidgetType() == widget.Window || tk.WidgetType() == widget.Flag {
if tk.window.pager != 0 {
if tk.gocuiSize.Height() > 40 {
tk.window.large = true
tk.gocuiSize.h1 = tk.gocuiSize.h0 + 40
d = tk.gocuiSize.h1
}
}
}
*/
// this is all terrible. This sets the title. kinda
if tk.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.WidgetType() == widget.Button {
if tk.IsEnabled() {
} else {
tk.setColorDisable()
}
}
switch tk.WidgetType() {
case widget.Button:
if tk.IsEnabled() {
if tk.isWindowDense() && tk.isInGrid() {
tk.setColorButtonDense()
} else {
tk.setColorButton()
}
} else {
tk.setColorDisable()
}
default:
}
// 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
}
if me.debug {
w.dumpWidget("drawView()")
}
w.drawView()
}