gocui/textbox.go

136 lines
3.2 KiB
Go
Raw Normal View History

2025-02-08 17:19:41 -06:00
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
// simulates a dropdown menu in gocui
import (
"strings"
"github.com/awesome-gocui/gocui"
log "go.wit.com/log"
)
func (tk *guiWidget) forceSizes(r *rectType) {
tk.gocuiSize.w0 = r.w0
tk.gocuiSize.w1 = r.w1
tk.gocuiSize.h0 = r.h0
tk.gocuiSize.h1 = r.h1
tk.full.w0 = r.w0
tk.full.w1 = r.w1
tk.full.h0 = r.h0
tk.full.h1 = r.h1
tk.force.w0 = r.w0
tk.force.w1 = r.w1
tk.force.h0 = r.h0
tk.force.h1 = r.h1
}
2025-02-11 13:22:23 -06:00
func initTextbox() {
2025-02-08 17:19:41 -06:00
if me.textbox.tk == nil {
// should only happen once
me.textbox.tk = makeNewFlagWidget(me.textbox.wId)
2025-02-09 04:19:32 -06:00
// me.textbox.tk.dumpWidget("init() textbox")
2025-02-08 17:19:41 -06:00
}
2025-02-11 13:22:23 -06:00
}
func (callertk *guiWidget) prepTextbox() {
initTextbox()
2025-02-08 17:19:41 -06:00
if me.textbox.tk == nil {
2025-02-11 13:22:23 -06:00
log.Log(GOCUI, "prepTextbox() Is Broken")
2025-02-08 17:19:41 -06:00
return
}
r := new(rectType)
// startW, startH := tk.Position()
r.w0 = callertk.gocuiSize.w0 + 4
r.h0 = callertk.gocuiSize.h0 + 3
r.w1 = r.w0 + 24
r.h1 = r.h0 + 2
me.textbox.tk.forceSizes(r)
2025-02-09 04:03:48 -06:00
// me.textbox.tk.dumpWidget("after sizes")
2025-02-08 17:19:41 -06:00
me.textbox.callerTK = callertk
2025-02-11 13:22:23 -06:00
// showTextbox(callertk.String())
}
2025-02-11 13:22:23 -06:00
func showTextbox(callers string) {
// tk := me.textbox.tk
// me.textbox.tk.dumpWidget("after sizes")
2025-02-11 13:22:23 -06:00
// me.textbox.tk.Show() // actually makes the gocui view. TODO: redo this
2025-02-08 17:19:41 -06:00
if me.textbox.tk.v == nil {
log.Info("wtf went wrong")
return
}
me.textbox.tk.setColorModal()
2025-02-09 04:03:48 -06:00
me.textbox.tk.v.Clear()
2025-02-11 13:22:23 -06:00
cur := strings.TrimSpace(callers)
2025-02-09 04:19:32 -06:00
// log.Info("setting textbox string to:", cur)
2025-02-09 04:03:48 -06:00
me.textbox.tk.v.WriteString(cur)
2025-02-08 17:19:41 -06:00
me.textbox.tk.v.Editable = true
me.textbox.tk.v.Wrap = true
r := me.textbox.tk.gocuiSize
2025-02-08 17:19:41 -06:00
me.baseGui.SetView(me.textbox.tk.cuiName, r.w0, r.h0, r.w1, r.h1, 0)
me.baseGui.SetCurrentView(me.textbox.tk.v.Name())
// bind the enter key to a function so we can close the textbox
me.baseGui.SetKeybinding(me.textbox.tk.v.Name(), gocui.KeyEnter, gocui.ModNone, theCloseTheTextbox)
me.textbox.active = true
// me.textbox.dumpWidget("showTextbox()")
2025-02-08 17:19:41 -06:00
}
func theCloseTheTextbox(g *gocui.Gui, v *gocui.View) error {
textboxClosed()
return nil
}
// updates the text and sends an event back to the application
func textboxClosed() {
// get the text the user entered
2025-02-09 04:03:48 -06:00
var newtext string
2025-02-08 17:19:41 -06:00
if me.textbox.tk.v == nil {
newtext = ""
} else {
newtext = me.textbox.tk.v.ViewBuffer()
}
newtext = strings.TrimSpace(newtext)
me.textbox.active = false
me.textbox.tk.Hide()
2025-02-09 04:19:32 -06:00
// log.Info("textbox closed with text:", newtext, me.textbox.callerTK.cuiName)
2025-02-08 17:19:41 -06:00
if me.clock.tk.v != nil {
me.baseGui.SetCurrentView("help")
} else {
me.baseGui.SetCurrentView("msg")
}
// change the text of the caller widget
me.textbox.callerTK.SetText(newtext)
me.textbox.callerTK.node.SetCurrentS(newtext)
// send an event from the plugin with the new string
me.myTree.SendUserEvent(me.textbox.callerTK.node)
2025-02-08 18:03:11 -06:00
win := me.textbox.callerTK.findParentWindow()
if win != nil {
2025-02-09 04:03:48 -06:00
// win.dumpWidget("redraw this!!!")
2025-02-08 18:43:48 -06:00
tk := me.textbox.callerTK
2025-02-09 04:03:48 -06:00
// me.textbox.callerTK.dumpWidget("resize this!!!")
2025-02-08 18:43:48 -06:00
me.textbox.callerTK.Size()
me.textbox.callerTK.placeWidgets(tk.gocuiSize.w0-4, tk.gocuiSize.h0-4)
2025-02-09 04:03:48 -06:00
// tk.dumpWidget("resize:" + tk.String())
2025-02-09 12:21:43 -06:00
win.makeWindowActive()
2025-02-08 18:03:11 -06:00
}
2025-02-08 17:19:41 -06:00
}