Primitive widths/heights should not become negative. Also fixed a

TextView issue where purging led to panics. Fixes #306
This commit is contained in:
Oliver 2019-07-09 14:01:41 +01:00
parent c3d1d4bcf9
commit fc28d646d0
2 changed files with 25 additions and 6 deletions

18
box.go
View File

@ -88,7 +88,8 @@ func (b *Box) GetRect() (int, int, int, int) {
} }
// GetInnerRect returns the position of the inner rectangle (x, y, width, // GetInnerRect returns the position of the inner rectangle (x, y, width,
// height), without the border and without any padding. // height), without the border and without any padding. Width and height values
// will clamp to 0 and thus never be negative.
func (b *Box) GetInnerRect() (int, int, int, int) { func (b *Box) GetInnerRect() (int, int, int, int) {
if b.innerX >= 0 { if b.innerX >= 0 {
return b.innerX, b.innerY, b.innerWidth, b.innerHeight return b.innerX, b.innerY, b.innerWidth, b.innerHeight
@ -100,10 +101,17 @@ func (b *Box) GetInnerRect() (int, int, int, int) {
width -= 2 width -= 2
height -= 2 height -= 2
} }
return x + b.paddingLeft, x, y, width, height = x+b.paddingLeft,
y+b.paddingTop, y+b.paddingTop,
width-b.paddingLeft-b.paddingRight, width-b.paddingLeft-b.paddingRight,
height-b.paddingTop-b.paddingBottom height-b.paddingTop-b.paddingBottom
if width < 0 {
width = 0
}
if height < 0 {
height = 0
}
return x, y, width, height
} }
// SetRect sets a new position of the primitive. Note that this has no effect // SetRect sets a new position of the primitive. Note that this has no effect
@ -318,6 +326,12 @@ func (b *Box) Draw(screen tcell.Screen) {
b.innerHeight += b.innerY b.innerHeight += b.innerY
b.innerY = 0 b.innerY = 0
} }
if b.innerWidth < 0 {
b.innerWidth = 0
}
if b.innerHeight < 0 {
b.innerHeight = 0
}
} }
// Focus is called when this primitive receives focus. // Focus is called when this primitive receives focus.

View File

@ -939,8 +939,13 @@ func (t *TextView) Draw(screen tcell.Screen) {
// If this view is not scrollable, we'll purge the buffer of lines that have // If this view is not scrollable, we'll purge the buffer of lines that have
// scrolled out of view. // scrolled out of view.
if !t.scrollable && t.lineOffset > 0 { if !t.scrollable && t.lineOffset > 0 {
if t.lineOffset <= len(t.index) {
t.buffer = nil
} else {
t.buffer = t.buffer[t.index[t.lineOffset].Line:] t.buffer = t.buffer[t.index[t.lineOffset].Line:]
}
t.index = nil t.index = nil
t.lineOffset = 0
} }
} }