Proper resizing

This commit is contained in:
Sasha Koshka 2023-04-08 14:31:45 -04:00
parent 76d37df913
commit ce7584ceee
2 changed files with 26 additions and 6 deletions

View File

@ -43,6 +43,8 @@ type Grid struct {
cursor image.Point
ignorePush bool
face font.Face
config config.Wrapped
theme theme.Wrapped
@ -82,7 +84,9 @@ func (element *Grid) Set (point image.Point, cell Cell) {
}
func (element *Grid) Push () {
element.drawAndPush()
if !element.ignorePush {
element.drawAndPush()
}
}
func (element *Grid) OnResize (callback func ()) {
@ -145,7 +149,7 @@ func (element *Grid) alloc () bool {
if width == oldWidth && height == oldHeight { return false }
oldCells := element.cells
heightLarger := height < oldHeight
heightLarger := height > oldHeight
element.stride = width
element.cells = make([]gridCell, width * height)
@ -153,22 +157,27 @@ func (element *Grid) alloc () bool {
// TODO: attempt to wrap text?
if heightLarger {
for index := range element.cells[oldHeight * width:] {
element.cells[index].initColor()
}}
newRowsStart := oldHeight * width
for index := range element.cells[newRowsStart:] {
element.cells[index + newRowsStart].initColor()
}
}
commonHeight := height
if heightLarger { commonHeight = oldHeight }
for index := range element.cells[:commonHeight * width] {
x := index % width
if x < oldWidth {
element.cells[index] = oldCells[x + index / oldWidth]
row := index / width
element.cells[index] = oldCells[x + row * oldWidth]
} else {
element.cells[index].initColor()
}
}
element.ignorePush = true
if element.onResize != nil { element.onResize() }
element.ignorePush = false
return true
}

View File

@ -1,5 +1,7 @@
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/piss/ansi"
type Terminal struct {
@ -9,6 +11,7 @@ type Terminal struct {
func NewTerminal () (element *Terminal) {
element = &Terminal { Grid: NewGrid() }
element.Grid.OnResize(element.handleResize)
return
}
@ -17,3 +20,11 @@ func (element *Terminal) Write (buffer []byte) (wrote int, err error) {
element.Push()
return
}
func (element *Terminal) handleResize () {
element.Set(image.Pt(0, 0), Cell {
Rune: 'X',
Background: tomo.ColorRed,
Foreground: tomo.ColorBlack,
})
}