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

View File

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