So much work
This commit is contained in:
145
alert.go
145
alert.go
@@ -33,14 +33,18 @@ type Alert struct {
|
||||
id string
|
||||
style tcell.Style
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
active bool
|
||||
visible bool
|
||||
x, y int
|
||||
w, h int
|
||||
active bool
|
||||
visible bool
|
||||
tabbable bool
|
||||
|
||||
layout *AbsoluteLayout
|
||||
title string
|
||||
message *Text
|
||||
btnOk, btnCancel *Button
|
||||
|
||||
keyMap KeyMap
|
||||
}
|
||||
|
||||
var _ Widget = (*Alert)(nil)
|
||||
@@ -54,23 +58,66 @@ func NewAlert(id string, style tcell.Style) *Alert {
|
||||
func (w *Alert) Init(id string, style tcell.Style) {
|
||||
w.id = id
|
||||
w.style = style
|
||||
|
||||
w.layout = NewAbsoluteLayout("alertlayout", tcell.StyleDefault)
|
||||
|
||||
w.message = NewText(fmt.Sprintf("%s-text", id), style)
|
||||
w.layout.AddAnchored(w.message, Coord{X: 0, Y: 0}, AnchorC)
|
||||
|
||||
w.btnOk = NewButton(fmt.Sprintf("%s-select", id), style)
|
||||
w.btnOk.SetLabel("Ok")
|
||||
w.btnOk.SetActive(true)
|
||||
w.layout.AddAnchored(w.btnOk, Coord{X: -2, Y: 0}, AnchorBR)
|
||||
|
||||
w.btnCancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
|
||||
w.btnCancel.SetLabel("Cancel")
|
||||
w.layout.AddAnchored(w.btnCancel, Coord{X: 2, Y: 0}, AnchorBL)
|
||||
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
|
||||
tcell.KeyTab: w.SelectNext,
|
||||
tcell.KeyRight: w.SelectNext,
|
||||
tcell.KeyDown: w.SelectNext,
|
||||
tcell.KeyLeft: w.SelectNext,
|
||||
tcell.KeyUp: w.SelectNext,
|
||||
tcell.KeyEnter: w.Do,
|
||||
})
|
||||
w.tabbable = true
|
||||
}
|
||||
func (w *Alert) Id() string { return w.id }
|
||||
func (w *Alert) HandleResize(ev *tcell.EventResize) {
|
||||
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})
|
||||
w.w, w.h = ev.Size()
|
||||
// Trim space for the borders and pass on the size to the layout
|
||||
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 {
|
||||
if !w.active {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
return w.keyMap.Handle(ev)
|
||||
}
|
||||
func (w *Alert) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *Alert) Draw(screen tcell.Screen) {
|
||||
@@ -81,36 +128,68 @@ func (w *Alert) Draw(screen tcell.Screen) {
|
||||
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.message.Draw(screen)
|
||||
w.btnOk.Draw(screen)
|
||||
w.btnCancel.Draw(screen)
|
||||
}
|
||||
func (w *Alert) Active() bool { return w.active }
|
||||
func (w *Alert) SetActive(a bool) { w.active = a }
|
||||
func (w *Alert) Visible() bool { return w.visible }
|
||||
func (w *Alert) SetVisible(a bool) { w.visible = a }
|
||||
func (w *Alert) SetX(x int) { w.x = x }
|
||||
func (w *Alert) SetY(y int) { w.y = y }
|
||||
func (w *Alert) GetX() int { return w.x }
|
||||
func (w *Alert) GetY() int { return w.y }
|
||||
func (w *Alert) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *Alert) SetW(x int) { w.w = x }
|
||||
func (w *Alert) SetH(y int) { w.h = y }
|
||||
func (w *Alert) GetW() int { return w.w }
|
||||
func (w *Alert) GetH() int { return w.y }
|
||||
func (w *Alert) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *Alert) Focusable() bool { return true }
|
||||
func (w *Alert) WantW() int {
|
||||
return w.btnOk.WantW() + w.btnCancel.WantW() + 4
|
||||
}
|
||||
|
||||
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, wh.BRD_SIMPLE, w.style, screen)
|
||||
w.layout.Draw(screen)
|
||||
// w.message.Draw(screen)
|
||||
// w.btnOk.Draw(screen)
|
||||
// w.btnCancel.Draw(screen)
|
||||
}
|
||||
func (w *Alert) Active() bool { return w.active }
|
||||
func (w *Alert) SetActive(a bool) { w.active = a }
|
||||
func (w *Alert) Visible() bool { return w.visible }
|
||||
func (w *Alert) SetVisible(a bool) { w.visible = a }
|
||||
func (w *Alert) SetX(x int) { w.x = x }
|
||||
func (w *Alert) SetY(y int) { w.y = y }
|
||||
func (w *Alert) GetX() int { return w.x }
|
||||
func (w *Alert) GetY() int { return w.y }
|
||||
func (w *Alert) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
||||
func (w *Alert) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *Alert) SetW(x int) { w.w = x }
|
||||
func (w *Alert) SetH(y int) { w.h = y }
|
||||
func (w *Alert) GetW() int { return w.w }
|
||||
func (w *Alert) GetH() int { return w.y }
|
||||
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) WantH() int {
|
||||
msg := len(strings.Split(wh.WrapText(w.message.GetText(), w.WantW()), "\n"))
|
||||
return 2 + w.btnOk.WantH() + msg
|
||||
}
|
||||
|
||||
func (w *Alert) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *Alert) SetMessage(msg string) {
|
||||
w.message.SetText(msg)
|
||||
// Borders + Buttons
|
||||
func (w *Alert) MinW() int {
|
||||
return 2 + w.message.MinW() + w.btnOk.MinW() + w.btnCancel.MinW()
|
||||
}
|
||||
|
||||
// Borders + Buttons + 2 lines for message
|
||||
func (w *Alert) MinH() int {
|
||||
return 2 + w.message.MinH() + w.btnOk.MinH()
|
||||
}
|
||||
|
||||
func (w *Alert) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *Alert) SetMessage(msg string) { w.message.SetText(msg) }
|
||||
|
||||
func (w *Alert) SetOkPressed(b func() bool) { w.btnOk.SetOnPressed(b) }
|
||||
func (w *Alert) SetCancelPressed(b func() bool) { w.btnCancel.SetOnPressed(b) }
|
||||
func (w *Alert) SelectNext(ev *tcell.EventKey) bool {
|
||||
if w.btnOk.Active() {
|
||||
w.btnOk.SetActive(false)
|
||||
w.btnCancel.SetActive(true)
|
||||
} else {
|
||||
w.btnOk.SetActive(true)
|
||||
w.btnCancel.SetActive(false)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *Alert) Do(ev *tcell.EventKey) bool {
|
||||
if w.btnOk.Active() {
|
||||
return w.btnOk.HandleKey(ev)
|
||||
} else if w.btnCancel.Active() {
|
||||
return w.btnCancel.HandleKey(ev)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user