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

View File

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