First Commit
This commit is contained in:
110
bordered_widget.go
Normal file
110
bordered_widget.go
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
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 (
|
||||
h "git.bullercodeworks.com/brian/dhcli/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type BorderedWidget struct {
|
||||
id string
|
||||
style tcell.Style
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
widget Widget
|
||||
border []rune
|
||||
|
||||
title string
|
||||
active bool
|
||||
visible bool
|
||||
|
||||
logger func(string)
|
||||
}
|
||||
|
||||
func (w *BorderedWidget) SetLogger(l func(string)) { w.logger = l }
|
||||
func (w *BorderedWidget) Log(txt string) {
|
||||
if w.logger != nil {
|
||||
w.logger(txt)
|
||||
}
|
||||
}
|
||||
|
||||
func NewBorderedWidget(id string, wd Widget, s tcell.Style) *BorderedWidget {
|
||||
ret := &BorderedWidget{
|
||||
style: s,
|
||||
widget: wd,
|
||||
}
|
||||
ret.Init(id)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *BorderedWidget) Init(id string) {
|
||||
w.id = id
|
||||
w.visible = true
|
||||
w.border = h.BRD_CSIMPLE
|
||||
}
|
||||
|
||||
func (w *BorderedWidget) Id() string { return w.id }
|
||||
func (w *BorderedWidget) HandleResize(ev *tcell.EventResize) {
|
||||
w.widget.HandleResize(ev)
|
||||
}
|
||||
|
||||
func (w *BorderedWidget) HandleKey(ev *tcell.EventKey) bool {
|
||||
return w.HandleKey(ev)
|
||||
}
|
||||
|
||||
func (w *BorderedWidget) HandleTime(ev *tcell.EventTime) {
|
||||
w.HandleTime(ev)
|
||||
}
|
||||
|
||||
func (w *BorderedWidget) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
if len(w.title) > 0 {
|
||||
h.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, w.border, w.style, screen)
|
||||
} else {
|
||||
h.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.border, w.style, screen)
|
||||
}
|
||||
w.widget.SetPos(Coord{X: w.x + 1, Y: w.y + 1})
|
||||
w.widget.Draw(screen)
|
||||
}
|
||||
func (w *BorderedWidget) Active() bool { return w.active }
|
||||
func (w *BorderedWidget) SetActive(a bool) { w.active = a }
|
||||
func (w *BorderedWidget) Visible() bool { return w.visible }
|
||||
func (w *BorderedWidget) SetVisible(a bool) { w.visible = a }
|
||||
func (w *BorderedWidget) Focusable() bool { return true }
|
||||
func (w *BorderedWidget) SetX(x int) { w.x = x }
|
||||
func (w *BorderedWidget) SetY(y int) { w.y = y }
|
||||
func (w *BorderedWidget) GetX() int { return w.x }
|
||||
func (w *BorderedWidget) GetY() int { return w.y }
|
||||
func (w *BorderedWidget) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *BorderedWidget) GetW() int { return w.w }
|
||||
func (w *BorderedWidget) GetH() int { return w.h }
|
||||
func (w *BorderedWidget) SetW(wd int) { w.w = wd }
|
||||
func (w *BorderedWidget) SetH(h int) { w.h = h }
|
||||
func (w *BorderedWidget) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *BorderedWidget) WantW() int { return w.w }
|
||||
func (w *BorderedWidget) WantH() int { return w.h }
|
||||
|
||||
func (w *BorderedWidget) SetBorder(r []rune) { w.border = r }
|
||||
Reference in New Issue
Block a user