Really figuring some things out
This commit is contained in:
57
alert.go
57
alert.go
@@ -23,7 +23,6 @@ package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
@@ -45,6 +44,7 @@ type Alert struct {
|
||||
btnOk, btnCancel *Button
|
||||
|
||||
keyMap KeyMap
|
||||
logger func(string, ...any)
|
||||
}
|
||||
|
||||
var _ Widget = (*Alert)(nil)
|
||||
@@ -59,17 +59,17 @@ func (w *Alert) Init(id string, style tcell.Style) {
|
||||
w.id = id
|
||||
w.style = style
|
||||
|
||||
w.layout = NewLinearLayout("alertlayout", tcell.StyleDefault)
|
||||
w.layout = NewLinearLayout(fmt.Sprintf("%s-layout", id), tcell.StyleDefault)
|
||||
|
||||
w.message = NewText(fmt.Sprintf("%s-text", id), style)
|
||||
w.layout.Add(w.message)
|
||||
|
||||
btnLayout := NewLinearLayout("alertbtn-layout", tcell.StyleDefault)
|
||||
btnLayout.SetOrientation(LinLayH)
|
||||
w.layout.Add(btnLayout)
|
||||
|
||||
w.btnCancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
|
||||
w.btnCancel.SetLabel("Cancel")
|
||||
w.layout.Add(w.btnCancel)
|
||||
btnLayout.Add(w.btnCancel)
|
||||
|
||||
w.btnOk = NewButton(fmt.Sprintf("%s-select", id), style)
|
||||
@@ -77,8 +77,6 @@ func (w *Alert) Init(id string, style tcell.Style) {
|
||||
w.btnOk.SetActive(true)
|
||||
btnLayout.Add(w.btnOk)
|
||||
|
||||
w.layout.Add(btnLayout)
|
||||
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
|
||||
tcell.KeyTab: w.SelectNext,
|
||||
tcell.KeyRight: w.SelectNext,
|
||||
@@ -92,31 +90,13 @@ func (w *Alert) Init(id string, style tcell.Style) {
|
||||
func (w *Alert) Id() string { return w.id }
|
||||
func (w *Alert) HandleResize(ev *tcell.EventResize) {
|
||||
w.w, w.h = ev.Size()
|
||||
|
||||
w.w = wh.Min(w.w, w.WantW())
|
||||
w.h = wh.Min(w.h, w.WantH())
|
||||
|
||||
// Trim space for the borders and pass on the size to the layout
|
||||
w.layout.SetPos(Coord{X: 1, Y: 1})
|
||||
w.layout.HandleResize(tcell.NewEventResize(w.w-2, w.h-2))
|
||||
/*
|
||||
w.message.HandleResize(ev)
|
||||
w.message.SetPos(Coord{X: w.x + 1, Y: w.y + 1})
|
||||
msgWantH := w.message.WantH()
|
||||
if msgWantH > w.h {
|
||||
// TODO message won't fit in alert window
|
||||
}
|
||||
w.message.SetSize(Coord{X: w.w - 2, Y: msgWantH})
|
||||
|
||||
w.btnCancel.HandleResize(ev)
|
||||
w.btnCancel.SetPos(Coord{
|
||||
X: w.x + 2,
|
||||
Y: w.y + w.h - 3,
|
||||
})
|
||||
w.btnCancel.SetSize(Coord{X: 10, Y: 3})
|
||||
|
||||
w.btnOk.HandleResize(ev)
|
||||
w.btnOk.SetPos(Coord{
|
||||
X: w.x + w.w - 12,
|
||||
Y: w.y + w.h - 3,
|
||||
})
|
||||
w.btnOk.SetSize(Coord{X: 10, Y: 3})
|
||||
*/
|
||||
}
|
||||
|
||||
func (w *Alert) HandleKey(ev *tcell.EventKey) bool {
|
||||
@@ -164,15 +144,17 @@ func (w *Alert) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *Alert) Focusable() bool { return true }
|
||||
func (w *Alert) SetTabbable(b bool) { w.tabbable = b }
|
||||
func (w *Alert) Tabbable() bool { return w.tabbable }
|
||||
func (w *Alert) WantW() int { return w.btnOk.WantW() + w.btnCancel.WantW() + 4 }
|
||||
func (w *Alert) WantW() int {
|
||||
return 4 + wh.Max(w.message.WantW(), (w.btnOk.WantW()+w.btnCancel.WantW()))
|
||||
}
|
||||
|
||||
func (w *Alert) WantH() int {
|
||||
msg := len(strings.Split(wh.WrapText(w.message.GetText(), w.WantW()), "\n"))
|
||||
return 2 + w.btnOk.WantH() + msg
|
||||
return 4 + w.btnOk.WantH() + w.message.WantH()
|
||||
}
|
||||
|
||||
// Borders + Buttons
|
||||
func (w *Alert) MinW() int {
|
||||
return 2 + w.message.MinW() + w.btnOk.MinW() + w.btnCancel.MinW()
|
||||
return 2 + wh.Max(w.message.MinW(), (w.btnOk.MinW()+w.btnCancel.MinW()))
|
||||
}
|
||||
|
||||
// Borders + Buttons + 2 lines for message
|
||||
@@ -204,3 +186,14 @@ func (w *Alert) Do(ev *tcell.EventKey) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (w *Alert) SetLogger(l func(string, ...any)) {
|
||||
w.logger = l
|
||||
w.layout.SetLogger(l)
|
||||
}
|
||||
|
||||
func (w *Alert) Log(txt string, args ...any) {
|
||||
if w.logger != nil {
|
||||
w.logger(txt, args...)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user