Work
This commit is contained in:
116
alert.go
Normal file
116
alert.go
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
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
|
||||
|
||||
title string
|
||||
message *Text
|
||||
btnOk, btnCancel *Button
|
||||
}
|
||||
|
||||
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.message = NewText(fmt.Sprintf("%s-text", 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")
|
||||
}
|
||||
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})
|
||||
}
|
||||
|
||||
func (w *Alert) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
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.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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user