From 4536932dde13b270d1e28cdf254b93e29242a4fc Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 10 Nov 2022 01:00:47 -0500 Subject: [PATCH] Resize canvas when the window is resized --- backends/x/x.go | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/backends/x/x.go b/backends/x/x.go index aa7f8f7..07f0378 100644 --- a/backends/x/x.go +++ b/backends/x/x.go @@ -1,7 +1,6 @@ package x import "image" -import "image/color" // import "golang.org/x/image/font" // import "golang.org/x/image/font/basicfont" @@ -91,11 +90,52 @@ func (backend *Backend) handleXEvent (event xgb.Event) { backend.metrics.windowHeight = newHeight if sizeChanged { - // TODO: resize and rebind canvas + // compress events + configureEvent = + backend.compressConfigureNotify(configureEvent) + + // resize and rebind canvas + backend.canvas.Destroy() + backend.canvas = xgraphics.New ( + backend.connection, + image.Rect ( + 0, 0, + backend.metrics.windowWidth, + backend.metrics.windowHeight)) + // FIXME (?): this doesn't work. if it were to work, it + // would possibly be a cleaner way to resize the canvas. + // backend.canvas.Scale ( + // backend.metrics.windowWidth, + // backend.metrics.windowHeight) + backend.bindCanvas() } } } +func (backend *Backend) compressConfigureNotify ( + firstEvent xproto.ConfigureNotifyEvent, +) ( + lastEvent xproto.ConfigureNotifyEvent, +) { + backend.connection.Sync() + xevent.Read(backend.connection, false) + lastEvent = firstEvent + + for index, untypedEvent := range xevent.Peek(backend.connection) { + if untypedEvent.Err != nil { continue } + + typedEvent, ok := untypedEvent.Event.(xproto.ConfigureNotifyEvent) + if !ok { continue } + + lastEvent = typedEvent + defer func (index int) { + xevent.DequeueAt(backend.connection, index) + } (index) + } + + return +} + func (backend *Backend) shutDown () { backend.channel <- stone.EventQuit { } } @@ -160,10 +200,6 @@ func factory (application *stone.Application) (output stone.Backend, err error) 0, 0, backend.metrics.windowWidth, backend.metrics.windowHeight)) - - for i := 8; i < 64; i ++ { - backend.canvas.Set(8, i, color.RGBA { R: 0xFF, A: 0xFF }) - } backend.bindCanvas() // attatch graceful close handler