summaryrefslogtreecommitdiff
path: root/textbox.go
blob: 73a8ed6d1b3bfef93ae8e0cd697b9f379e0d81df (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
// 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
}

func initTextbox() {
	if me.textbox.tk == nil {
		// should only happen once
		me.textbox.tk = makeNewFlagWidget(me.textbox.wId)
		// me.textbox.tk.dumpWidget("init() textbox")
	}
}

func (callertk *guiWidget) prepTextbox() {
	initTextbox()
	if me.textbox.tk == nil {
		log.Log(GOCUI, "prepTextbox() Is Broken")
		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)
	// me.textbox.tk.dumpWidget("after sizes")

	me.textbox.callerTK = callertk

	// showTextbox(callertk.String())
}

func showTextbox(callers string) {
	// tk := me.textbox.tk
	// me.textbox.tk.dumpWidget("after sizes")

	//	me.textbox.tk.Show() // actually makes the gocui view. TODO: redo this

	if me.textbox.tk.v == nil {
		log.Info("wtf went wrong")
		return
	}

	me.textbox.tk.setColorModal()
	me.textbox.tk.v.Clear()
	cur := strings.TrimSpace(callers)
	// log.Info("setting textbox string to:", cur)
	me.textbox.tk.v.WriteString(cur)

	me.textbox.tk.v.Editable = true
	me.textbox.tk.v.Wrap = true

	r := me.textbox.tk.gocuiSize
	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()")
}

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
	var newtext string
	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()
	// log.Info("textbox closed with text:", newtext, me.textbox.callerTK.cuiName)

	if me.notify.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)

	win := me.textbox.callerTK.findParentWindow()
	if win != nil {
		// win.dumpWidget("redraw this!!!")
		tk := me.textbox.callerTK
		// me.textbox.callerTK.dumpWidget("resize this!!!")
		me.textbox.callerTK.Size()
		me.textbox.callerTK.placeWidgets(tk.gocuiSize.w0-4, tk.gocuiSize.h0-4)
		// tk.dumpWidget("resize:" + tk.String())
		win.makeWindowActive()
	}
}