erm/app/darktile/gui/resize.go

49 lines
1.1 KiB
Go

package gui
import (
"image"
"math"
"github.com/hajimehoshi/ebiten/v2"
)
// Layout provides the terminal gui size in pixels. Required to implement the ebiten interface.
func (g *GUI) Layout(outsideWidth, outsideHeight int) (int, int) {
panic("should not call this")
}
// Layout provides the terminal gui size in pixels. Required to implement the ebiten interface.
func (g *GUI) LayoutF(outsideWidth, outsideHeight float64) (float64, float64) {
scale := ebiten.Monitor().DeviceScaleFactor()
sw, sh := math.Ceil(scale*outsideWidth), math.Ceil(scale*outsideHeight)
w, h := int(sw), int(sh)
if g.size.X != w || g.size.Y != h {
g.size = image.Point{
X: int(w),
Y: h,
}
g.resize(w, h)
}
return sw, sh
}
func (g *GUI) resize(w, h int) {
if g.fontManager.CharSize().X == 0 || g.fontManager.CharSize().Y == 0 || g.terminal == nil {
return
}
cols := uint16(w / g.fontManager.CharSize().X)
rows := uint16(h / g.fontManager.CharSize().Y)
g.terminal.Lock()
defer g.terminal.Unlock()
if g.terminal.IsRunning() {
g.log.Info("terminal resizing", "w", w, "h", h, "rows", rows, "cols", cols)
_ = g.terminal.SetSize(rows, cols)
}
}