Skip to content

Commit

Permalink
Allow forwarding resize events fyne-io#103
Browse files Browse the repository at this point in the history
  • Loading branch information
nagylzs committed Jan 18, 2025
1 parent eddb379 commit 7d947a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions term.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terminal

import (
"github.com/creack/pty"
"image/color"
"io"
"math"
Expand Down Expand Up @@ -86,8 +87,11 @@ type Terminal struct {
printer Printer
cmd *exec.Cmd
readWriterConfigurator ReadWriterConfigurator
onResize ResizeHandler
}

type ResizeHandler = func(size *pty.Winsize)

// Printer is used for spooling print data when its received.
type Printer interface {
Print([]byte)
Expand Down Expand Up @@ -549,3 +553,7 @@ type ReadWriterConfiguratorFunc func(r io.Reader, w io.WriteCloser) (io.Reader,
func (m ReadWriterConfiguratorFunc) SetupReadWriter(r io.Reader, w io.WriteCloser) (io.Reader, io.WriteCloser) {
return m(r, w)
}

func (t *Terminal) SetResizeHandler(h ResizeHandler) {
t.onResize = h
}
12 changes: 9 additions & 3 deletions term_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ import (
)

func (t *Terminal) updatePTYSize() {
if t.pty == nil { // SSH or other direct connection?
if t.pty == nil && t.onResize == nil { // SSH or other direct connection?
return
}
scale := float32(1.0)
c := fyne.CurrentApp().Driver().CanvasForObject(t)
if c != nil {
scale = c.Scale()
}
_ = pty.Setsize(t.pty.(*os.File), &pty.Winsize{
ws := &pty.Winsize{
Rows: uint16(t.config.Rows), Cols: uint16(t.config.Columns),
X: uint16(t.Size().Width * scale), Y: uint16(t.Size().Height * scale)})
X: uint16(t.Size().Width * scale), Y: uint16(t.Size().Height * scale)}
if t.onResize != nil {
t.onResize(ws)
}
if t.pty != nil {
_ = pty.Setsize(t.pty.(*os.File), ws)
}
}

func (t *Terminal) startPTY() (io.WriteCloser, io.Reader, io.Closer, error) {
Expand Down

0 comments on commit 7d947a3

Please sign in to comment.