So much work
This commit is contained in:
264
linear_layout.go
Normal file
264
linear_layout.go
Normal file
@@ -0,0 +1,264 @@
|
||||
/*
|
||||
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/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// LinearLayout lays out all widgets added one after the other
|
||||
type LinearLayout struct {
|
||||
id string
|
||||
style tcell.Style
|
||||
|
||||
orientation LinearLayoutOrient
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
widgets []Widget
|
||||
|
||||
active bool
|
||||
visible bool
|
||||
tabbable bool
|
||||
disableTab bool
|
||||
|
||||
cursor int
|
||||
}
|
||||
|
||||
type LinearLayoutOrient int
|
||||
|
||||
const (
|
||||
LinLayV = LinearLayoutOrient(iota)
|
||||
LinLayH
|
||||
)
|
||||
|
||||
func NewLinearLayout(id string, s tcell.Style) *LinearLayout {
|
||||
ret := &LinearLayout{}
|
||||
ret.Init(id, s)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *LinearLayout) Init(id string, s tcell.Style) {
|
||||
w.id = id
|
||||
w.style = s
|
||||
w.visible = true
|
||||
}
|
||||
|
||||
func (w *LinearLayout) Id() string { return w.id }
|
||||
func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
|
||||
w.w, w.h = ev.Size()
|
||||
w.updateWidgetLayouts()
|
||||
}
|
||||
|
||||
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.disableTab && ev.Key() == tcell.KeyTab {
|
||||
fndP := -1
|
||||
for i := w.cursor; i < len(w.widgets); i++ {
|
||||
if fndP == -1 {
|
||||
if w.widgets[i].Active() {
|
||||
fndP = i
|
||||
w.widgets[i].SetActive(false)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if w.widgets[i].Focusable() && w.widgets[i].Tabbable() {
|
||||
w.widgets[i].SetActive(true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we're here, we hit the last widget, loop
|
||||
if fndP == -1 { // But didn't even find the active one
|
||||
return false
|
||||
}
|
||||
for i := 0; i < fndP; i++ {
|
||||
if w.widgets[i].Focusable() && w.widgets[i].Tabbable() {
|
||||
w.widgets[i].SetActive(true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
for _, wi := range w.widgets {
|
||||
if wi.HandleKey(ev) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (w *LinearLayout) HandleTime(ev *tcell.EventTime) {
|
||||
for _, wi := range w.widgets {
|
||||
wi.HandleTime(ev)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *LinearLayout) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
p := w.GetPos()
|
||||
for _, wd := range w.widgets {
|
||||
o := wd.GetPos()
|
||||
wd.SetPos(p.Add(o))
|
||||
wd.Draw(screen)
|
||||
wd.SetPos(o)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *LinearLayout) Active() bool { return w.active }
|
||||
func (w *LinearLayout) SetActive(a bool) { w.active = a }
|
||||
func (w *LinearLayout) Visible() bool { return w.visible }
|
||||
func (w *LinearLayout) SetVisible(a bool) { w.visible = a }
|
||||
func (w *LinearLayout) Focusable() bool { return true }
|
||||
func (w *LinearLayout) SetTabbable(b bool) { w.tabbable = b }
|
||||
func (w *LinearLayout) Tabbable() bool { return w.tabbable }
|
||||
func (w *LinearLayout) SetX(x int) { w.x = x }
|
||||
func (w *LinearLayout) SetY(y int) { w.y = y }
|
||||
func (w *LinearLayout) GetX() int { return w.x }
|
||||
func (w *LinearLayout) GetY() int { return w.y }
|
||||
func (w *LinearLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
||||
func (w *LinearLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *LinearLayout) GetW() int { return w.w }
|
||||
func (w *LinearLayout) GetH() int { return w.h }
|
||||
func (w *LinearLayout) SetW(wd int) { w.w = wd }
|
||||
func (w *LinearLayout) SetH(h int) { w.h = h }
|
||||
func (w *LinearLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *LinearLayout) WantW() int {
|
||||
var wantW int
|
||||
for _, wd := range w.widgets {
|
||||
if w.orientation == LinLayV {
|
||||
// Find the highest want of all widgets
|
||||
wantW = h.Max(wd.WantW(), wantW)
|
||||
} else if w.orientation == LinLayH {
|
||||
// Find the sum of all widget widgets wants
|
||||
wantW = wantW + wd.WantW()
|
||||
}
|
||||
}
|
||||
return wantW
|
||||
}
|
||||
|
||||
func (w *LinearLayout) WantH() int {
|
||||
var wantH int
|
||||
for _, wd := range w.widgets {
|
||||
if w.orientation == LinLayV {
|
||||
// Find the sum of all widget widgets wants
|
||||
wantH = wantH + wd.WantH()
|
||||
} else if w.orientation == LinLayH {
|
||||
// Find the highest want of all widgets
|
||||
wantH = h.Max(wd.WantH(), wantH)
|
||||
}
|
||||
}
|
||||
return wantH
|
||||
}
|
||||
|
||||
func (w *LinearLayout) MinW() int {
|
||||
var minW int
|
||||
for _, wd := range w.widgets {
|
||||
if w.orientation == LinLayV {
|
||||
// Find the highest minimum width of all widgets
|
||||
minW = h.Max(wd.MinW(), minW)
|
||||
} else if w.orientation == LinLayH {
|
||||
// Find the sum of all widget minimum widgets
|
||||
minW = minW + wd.MinW()
|
||||
}
|
||||
}
|
||||
return minW
|
||||
}
|
||||
|
||||
func (w *LinearLayout) MinH() int {
|
||||
var minH int
|
||||
for _, wd := range w.widgets {
|
||||
if w.orientation == LinLayV {
|
||||
minH = minH + wd.MinH()
|
||||
} else if w.orientation == LinLayH {
|
||||
minH = h.Max(wd.MinH(), minH)
|
||||
}
|
||||
}
|
||||
return minH
|
||||
}
|
||||
func (w *LinearLayout) Append(n Widget) { w.widgets = append(w.widgets, n) }
|
||||
func (w *LinearLayout) Delete(n Widget) {
|
||||
for i := 0; i < len(w.widgets); i++ {
|
||||
if w.widgets[i] == n {
|
||||
w.DeleteIndex(i)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *LinearLayout) DeleteIndex(idx int) {
|
||||
w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...)
|
||||
}
|
||||
|
||||
func (w *LinearLayout) Insert(n Widget, idx int) {
|
||||
w.widgets = append(w.widgets[:idx], append([]Widget{n}, w.widgets[idx:]...)...)
|
||||
}
|
||||
|
||||
func (w *LinearLayout) updateWidgetLayouts() {
|
||||
for _, wd := range w.widgets {
|
||||
w.updateWidgetPos(wd)
|
||||
w.updateWidgetSize(wd)
|
||||
}
|
||||
}
|
||||
|
||||
// The Layout should have a static Size set at this point that we can use
|
||||
// For now we're centering all views in the Layout (on the cross-axis)
|
||||
//
|
||||
// The position and size of each widget before this should be correct
|
||||
// Find the position and size of the widget before this one
|
||||
func (w *LinearLayout) updateWidgetPos(wd Widget) {
|
||||
prevP, prevS := 0, 0
|
||||
for _, wrk := range w.widgets {
|
||||
if w.orientation == LinLayV {
|
||||
if wrk == wd {
|
||||
wd.SetPos(Coord{X: w.w - (wd.GetW() / 2), Y: prevP + prevS + 1})
|
||||
return
|
||||
}
|
||||
prevP, prevS = wrk.GetY(), wrk.GetH()
|
||||
} else if w.orientation == LinLayH {
|
||||
if wrk == wd {
|
||||
wd.SetPos(Coord{X: prevP + prevS + 1, Y: w.h - (wd.GetH() / 2)})
|
||||
return
|
||||
}
|
||||
prevP, prevS = wrk.GetX(), wrk.GetW()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The Layout should have a static Size set at this point that we can use
|
||||
// For now we're centering all views in the Layout (on the cross-axis)
|
||||
//
|
||||
// The position of this widget should be correct
|
||||
func (w *LinearLayout) updateWidgetSize(wd Widget) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
func (w *LinearLayout) getRelPos(wd Widget) Coord {
|
||||
return Coord{}
|
||||
}
|
||||
|
||||
func (w *LinearLayout) getAbsPos(wd Widget) Coord {
|
||||
rel := w.getRelPos(wd)
|
||||
return rel.Add(Coord{X: w.x, Y: w.y})
|
||||
}
|
||||
Reference in New Issue
Block a user