158 lines
4.8 KiB
Go
158 lines
4.8 KiB
Go
/*
|
|
Copyright © Brian Buller <brian@bullercodeworks.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package widgets
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
// TODO: Rebuild with LinearLayouts
|
|
type Prompt struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
x, y int
|
|
w, h int
|
|
active bool
|
|
visible bool
|
|
focusable bool
|
|
|
|
title string
|
|
message *Text
|
|
field *Field
|
|
btnOk, btnCancel *Button
|
|
onOk func(string) bool
|
|
|
|
keyMap KeyMap
|
|
}
|
|
|
|
var _ Widget = (*Prompt)(nil)
|
|
|
|
func NewPrompt(id string, style tcell.Style) *Prompt {
|
|
ret := &Prompt{}
|
|
ret.Init(id, style)
|
|
return ret
|
|
}
|
|
|
|
func (w *Prompt) Init(id string, style tcell.Style) {
|
|
w.id = id
|
|
w.style = style
|
|
w.message = NewText(fmt.Sprintf("%s-text", id), style)
|
|
w.field = NewField(fmt.Sprintf("%s-field", id), style)
|
|
w.btnOk = NewButton(fmt.Sprintf("%s-select", id), style)
|
|
w.btnOk.SetLabel("Ok")
|
|
w.btnCancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
|
|
w.btnCancel.SetLabel("Cancel")
|
|
w.focusable = true
|
|
w.keyMap = BlankKeyMap()
|
|
}
|
|
func (w *Prompt) Id() string { return w.id }
|
|
func (w *Prompt) HandleResize(ev *tcell.EventResize) {
|
|
w.w, w.h = ev.Size()
|
|
|
|
// Prompt is shrink-wrapped by default
|
|
w.w = wh.Min(w.w, w.WantW())
|
|
w.h = wh.Min(w.h, w.WantH())
|
|
|
|
w.message.SetPos(Coord{X: w.x + 1, Y: w.y + 1})
|
|
w.field.SetPos(Coord{X: w.x + 1, Y: WidgetBottom(w.message)})
|
|
w.btnOk.SetPos(Coord{X: w.x + w.w - w.btnOk.WantW(), Y: w.y + w.h - 1})
|
|
w.btnCancel.SetPos(Coord{X: w.x + 1, Y: w.y + w.h - 1})
|
|
}
|
|
|
|
func (w *Prompt) SetKeyMap(km KeyMap) { w.keyMap = km }
|
|
func (w *Prompt) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
|
|
func (w *Prompt) RemoveFromKeyMap(km KeyMap) {
|
|
for k := range km.Keys {
|
|
w.keyMap.Remove(k)
|
|
}
|
|
for r := range km.Runes {
|
|
w.keyMap.RemoveRune(r)
|
|
}
|
|
}
|
|
|
|
func (w *Prompt) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
return w.keyMap.Handle(ev)
|
|
}
|
|
func (w *Prompt) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *Prompt) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
dS := w.style
|
|
if !w.active {
|
|
dS = dS.Dim(true)
|
|
}
|
|
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, wh.BRD_SIMPLE, w.style, screen)
|
|
w.GetPos().DrawOffset(w.message, screen)
|
|
w.GetPos().DrawOffset(w.btnOk, screen)
|
|
w.GetPos().DrawOffset(w.btnCancel, screen)
|
|
}
|
|
|
|
func (w *Prompt) Active() bool { return w.active }
|
|
func (w *Prompt) SetActive(a bool) { w.active = a }
|
|
func (w *Prompt) Visible() bool { return w.visible }
|
|
func (w *Prompt) SetVisible(a bool) { w.visible = a }
|
|
func (w *Prompt) SetX(x int) { w.x = x }
|
|
func (w *Prompt) SetY(y int) { w.y = y }
|
|
func (w *Prompt) GetX() int { return w.x }
|
|
func (w *Prompt) GetY() int { return w.y }
|
|
func (w *Prompt) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *Prompt) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *Prompt) SetW(x int) { w.w = x }
|
|
func (w *Prompt) SetH(y int) { w.h = y }
|
|
func (w *Prompt) GetW() int { return w.w }
|
|
func (w *Prompt) GetH() int { return w.y }
|
|
func (w *Prompt) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
|
func (w *Prompt) Focusable() bool { return w.focusable }
|
|
func (w *Prompt) SetFocusable(b bool) { w.focusable = b }
|
|
func (w *Prompt) WantW() int {
|
|
return w.btnOk.WantW() + w.btnCancel.WantW() + 4
|
|
}
|
|
|
|
func (w *Prompt) WantH() int {
|
|
msg := len(strings.Split(wh.WrapText(w.message.GetText(), w.WantW()), "\n"))
|
|
return 2 + w.field.WantH() + w.btnOk.WantH() + msg
|
|
}
|
|
|
|
func (w *Prompt) SetTitle(ttl string) { w.title = ttl }
|
|
func (w *Prompt) SetMessage(msg string) {
|
|
w.message.SetText(msg)
|
|
}
|
|
|
|
func (w *Prompt) MinW() int {
|
|
return 2 + w.field.MinW() + w.btnOk.MinW() + w.btnCancel.MinW() + w.message.MinW()
|
|
}
|
|
|
|
func (w *Prompt) MinH() int {
|
|
return 2 + w.field.MinH() + w.btnOk.MinH() + w.message.MinH()
|
|
}
|
|
|
|
func (w *Prompt) SetOnOk(f func(string) bool) { w.onOk = f }
|