// 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 if tk.node.WidgetType == widget.Window { if !tk.resize { tk.resize = true tk.textResize() // resize window only once } else { // for windows, make it the full size a = tk.full.w0 b = tk.full.h0 c = tk.full.w1 d = tk.full.h1 } } else { tk.textResize() // resize everything except windows 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 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 tk.v.Wrap = true tk.v.Frame = tk.frame tk.v.Clear() fmt.Fprint(tk.v, tk.labelN) // 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.active = false // w.dumpWidget(fmt.Sprintf("DrawAt(%d,%d)", offsetW, offsetH)) } func (w *guiWidget) simpleDrawAt(offsetW, offsetH int) { w.setColor(&colorActiveW) w.active = false w.dumpWidget("simpleDrawAt()") } var toggle bool = true 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) } } func (w *guiWidget) Show() { // don't display fake widgets if w.isFake { return } // deprecate this // if this isn't in the binary tree // it's some internal widget so always display those if w.node == nil { w.drawView() return } // deprecate this // always show window titles if w.node.WidgetType == widget.Window { w.drawView() return } /* // if the widget is not in the current displayed windo if !w.IsCurrent() { // log.Log(GOCUI, "Show() w.IsCurrent == false. NOT drawing", w.cuiName, w.String()) return } */ // this comes from the application developer // finally, check if the widget State is hidden or not if w.node.Hidden() { // log.Log(GOCUI, "Show() w.node.Hidden() = false. not drawing", w.cuiName, w.String()) // don't display hidden widgets return } // log.Log(GOCUI, "Show() doing w.drawView()", w.cuiName, w.String()) w.drawView() }