fixed FlowLayout.RemoveViews

This commit is contained in:
Timon Ringwald 2022-04-03 17:07:15 +02:00
parent a20d361871
commit 9f3213e45c
4 changed files with 14 additions and 5 deletions

View File

@ -62,8 +62,8 @@ func TestScrollView(t *testing.T) {
}
screen.MouseClicked = func(event *tui.MouseEvent) (consumed bool) {
//textViews[0].(*views.TextView).Text = fmt.Sprintf("mouse position: %d | %d", event.X, event.Y)
//textViews[1].(*views.TextView).Text = fmt.Sprintf("mouse button: %d", event.Button)
textViews[0].(*views.TextView).Text = fmt.Sprintf("mouse position: %d | %d", event.X, event.Y)
textViews[1].(*views.TextView).Text = fmt.Sprintf("mouse button: %d", event.Button)
if event.Button == tui.MouseWheelUp {
scrollView.Scroll(-1, 0)

View File

@ -29,6 +29,7 @@ func (g *CoordLayout) Views() []tui.View {
func (g *CoordLayout) SetView(v tui.View, x, y, width, height int) {
g.views[v] = tui.Dimension{Point: tui.Point{X: x, Y: y}, Size: tui.Size{Width: width, Height: height}}
}
func (g *CoordLayout) Draw(buf *tui.ViewBuffer) {
for v, d := range g.views {
v.Draw(buf.Sub(d.X, d.Y, d.Width, d.Height))

View File

@ -39,15 +39,21 @@ func (g *FlowLayout) InsertView(v tui.View, index int) {
g.views = append(g.views[:index], append([]tui.View{v}, g.views[index:]...)...)
}
func (g *FlowLayout) RemoveView(v tui.View) {
func (g *FlowLayout) removeView(v tui.View) {
for index, view := range g.Views() {
if view == v {
g.views = append(g.views[:index], g.views[index:]...)
if v == view {
g.views = append(g.views[:index], g.views[index+1:]...)
return
}
}
}
func (g *FlowLayout) RemoveViews(v ...tui.View) {
for _, view := range v {
g.removeView(view)
}
}
func (g *FlowLayout) Draw(buf *tui.ViewBuffer) {
g.ViewTmpl.Draw(buf)

View File

@ -29,6 +29,8 @@ func NewScrollView(view tui.View) *ScrollView {
}
func (v *ScrollView) Draw(buf *tui.ViewBuffer) {
v.ViewTmpl.Draw(buf)
w, h := v.View().Layout()
if v.buf == nil || v.buf.Width() != w || v.buf.Height() != h {