207 lines
5.8 KiB
Go
207 lines
5.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"
|
|
)
|
|
|
|
type Alert struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
x, y int
|
|
w, h int
|
|
active bool
|
|
visible bool
|
|
tabbable bool
|
|
|
|
layout *LinearLayout
|
|
title string
|
|
message *Text
|
|
btnOk, btnCancel *Button
|
|
|
|
keyMap KeyMap
|
|
}
|
|
|
|
var _ Widget = (*Alert)(nil)
|
|
|
|
func NewAlert(id string, style tcell.Style) *Alert {
|
|
ret := &Alert{}
|
|
ret.Init(id, style)
|
|
return ret
|
|
}
|
|
|
|
func (w *Alert) Init(id string, style tcell.Style) {
|
|
w.id = id
|
|
w.style = style
|
|
|
|
w.layout = NewLinearLayout("alertlayout", 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.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)
|
|
w.btnOk.SetLabel("Ok")
|
|
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,
|
|
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.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 w.keyMap.Handle(ev)
|
|
}
|
|
func (w *Alert) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *Alert) 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.layout.DrawOffset(w.GetPos(), screen)
|
|
}
|
|
|
|
func (w *Alert) DrawOffset(c Coord, screen tcell.Screen) {
|
|
p := w.GetPos()
|
|
w.SetPos(p.Add(c))
|
|
w.Draw(screen)
|
|
w.SetPos(p)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|