uidriver/glfw: Reduce (*thread).Call

This commit is contained in:
Hajime Hoshi 2020-10-17 04:21:07 +09:00
parent 1db7bed2a7
commit 7762f5dcec
2 changed files with 197 additions and 202 deletions

View File

@ -441,12 +441,11 @@ func (u *UserInterface) SetFullscreen(fullscreen bool) {
return
}
var w, h int
_ = u.t.Call(func() error {
w, h = u.windowWidth, u.windowHeight
w, h := u.windowWidth, u.windowHeight
u.setWindowSize(w, h, fullscreen)
return nil
})
u.setWindowSize(w, h, fullscreen)
}
func (u *UserInterface) IsFocused() bool {
@ -600,7 +599,15 @@ func (u *UserInterface) Run(uicontext driver.UIContext) error {
}()
defer close(ch)
if err := u.run(); err != nil {
_ = u.t.Call(func() error {
if err := u.init(); err != nil {
ch <- err
}
return nil
})
if err := u.loop(); err != nil {
ch <- err
}
}()
@ -664,8 +671,7 @@ func (u *UserInterface) createWindow() error {
return nil
}
func (u *UserInterface) run() error {
if err := u.t.Call(func() error {
func (u *UserInterface) init() error {
if u.Graphics().IsGL() {
glfw.WindowHint(glfw.ClientAPI, glfw.OpenGLAPI)
glfw.WindowHint(glfw.ContextVersionMajor, 2)
@ -717,13 +723,9 @@ func (u *UserInterface) run() error {
if i := u.getInitIconImages(); i != nil {
u.window.SetIcon(i)
}
return nil
}); err != nil {
return err
}
setPosition := func() {
u.iwindow.SetPosition(u.getInitWindowPosition())
u.iwindow.setPosition(u.getInitWindowPosition())
}
setSize := func() {
ww, wh := u.getInitWindowSize()
@ -733,8 +735,8 @@ func (u *UserInterface) run() error {
}
// Set the window size and the window position in this order on Linux or other UNIX using X (#1118),
// but this should be inverted on Windows. This is very tricky, but there is no obvious way to solve this.
// This doesn't matter on macOS.
// but this should be inverted on Windows. This is very tricky, but there is no obvious way to solve
// this. This doesn't matter on macOS.
if runtime.GOOS == "windows" {
setPosition()
setSize()
@ -745,37 +747,26 @@ func (u *UserInterface) run() error {
// Maximizing a window requires a proper size and position. Call Maximize here (#1117).
if u.isInitWindowMaximized() {
_ = u.t.Call(func() error {
u.window.Maximize()
return nil
})
}
_ = u.t.Call(func() error {
u.title = u.getInitTitle()
u.window.SetTitle(u.title)
u.window.Show()
return nil
})
var w uintptr
_ = u.t.Call(func() error {
w = u.nativeWindow()
return nil
})
if g, ok := u.Graphics().(interface{ SetWindow(uintptr) }); ok {
g.SetWindow(w)
g.SetWindow(u.nativeWindow())
}
return u.loop()
return nil
}
func (u *UserInterface) updateSize() {
var w, h int
_ = u.t.Call(func() error {
w, h = u.windowWidth, u.windowHeight
w, h := u.windowWidth, u.windowHeight
u.setWindowSize(w, h, u.isFullscreen())
return nil
})
u.setWindowSize(w, h, u.isFullscreen())
sizeChanged := false
_ = u.t.Call(func() error {
@ -823,12 +814,11 @@ func (u *UserInterface) update() error {
}
if u.isInitFullscreen() {
var w, h int
_ = u.t.Call(func() error {
w, h = u.window.GetSize()
w, h := u.window.GetSize()
u.setWindowSize(w, h, true)
return nil
})
u.setWindowSize(w, h, true)
u.setInitFullscreen(false)
}
@ -867,14 +857,13 @@ func (u *UserInterface) update() error {
}
// Update the screen size when the window is resizable.
var w, h int
_ = u.t.Call(func() error {
w, h = u.reqWidth, u.reqHeight
return nil
})
if w != 0 || h != 0 {
if w, h := u.reqWidth, u.reqHeight; w != 0 || h != 0 {
u.setWindowSize(w, h, u.isFullscreen())
}
return nil
})
_ = u.t.Call(func() error {
u.reqWidth = 0
u.reqHeight = 0
@ -936,12 +925,10 @@ func (u *UserInterface) swapBuffers() {
}
}
// setWindowSize must be called from the main thread.
func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) {
windowRecreated := false
_ = u.t.Call(func() error {
if u.windowWidth == width && u.windowHeight == height && u.isFullscreen() == fullscreen && u.lastDeviceScaleFactor == u.deviceScaleFactor() {
return nil
return
}
if width < 1 {
@ -957,6 +944,8 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) {
// swap buffers here before SetSize is called.
u.swapBuffers()
var windowRecreated bool
if fullscreen {
if u.origPosX == invalidPos || u.origPosY == invalidPos {
u.origPosX, u.origPosY = u.window.GetPos()
@ -1053,8 +1042,6 @@ func (u *UserInterface) setWindowSize(width, height int, fullscreen bool) {
u.windowHeight = height
u.toChangeSize = true
return nil
})
if windowRecreated {
if g, ok := u.Graphics().(interface{ SetWindow(uintptr) }); ok {

View File

@ -205,20 +205,25 @@ func (w *window) SetPosition(x, y int) {
return
}
_ = w.ui.t.Call(func() error {
w.setPosition(x, y)
return nil
})
}
// setPosition must be called from the main thread
func (w *window) setPosition(x, y int) {
defer func() {
w.setPositionCalled = true
}()
mx, my := currentMonitor(w.ui.window).GetPos()
xf := w.ui.toGLFWPixel(float64(x))
yf := w.ui.toGLFWPixel(float64(y))
x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf))
if w.ui.isFullscreen() {
if x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf)); w.ui.isFullscreen() {
w.ui.origPosX, w.ui.origPosY = x, y
} else {
w.ui.window.SetPos(x, y)
}
return nil
})
}
func (w *window) Size() (int, int) {
@ -237,7 +242,10 @@ func (w *window) SetSize(width, height int) {
}
ww := int(w.ui.toGLFWPixel(float64(width)))
wh := int(w.ui.toGLFWPixel(float64(height)))
_ = w.ui.t.Call(func() error {
w.ui.setWindowSize(ww, wh, w.ui.isFullscreen())
return nil
})
}
func (w *window) SetIcon(iconImages []image.Image) {