app,app/internal/wm: introduce app.Window.Run and use it internally

app.Window implements a method for safely running functions against the
underlying native window through the driverFuncs channel. However, the
functions still run in a different goroutine than the one driving the
native event loop, which forces the implementations in package wm to do
complicated synchronization.

A previous change added a mechanism to run functions in the native event
loop thread. The macOS port needed this functionality, but with some
care it can be generalized. That's what this change does through the
new Run method.

The advantage is that the thread switch dance is now confined to
app.Window, with the help of a generic wm.Driver.Wakeup method. All
other Driver methods can then assume they run on their event loop
threads.

Run is exported because it is also needed for programs that use
Windows configured with CustomRenderer to control their own rendering.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-05-18 12:34:58 +01:00
parent c914935169
commit 8611894b4b
14 changed files with 283 additions and 380 deletions
+7 -32
View File
@@ -59,7 +59,6 @@ type window struct {
// placement saves the previous window position when in full screen mode.
placement *windows.WindowPlacement
mu sync.Mutex
animating bool
minmax winConstraints
@@ -67,11 +66,7 @@ type window struct {
opts *Options
}
const (
_WM_REDRAW = windows.WM_USER + iota
_WM_CURSOR
_WM_OPTION
)
const _WM_WAKEUP = windows.WM_USER + iota
type gpuAPI struct {
priority int
@@ -330,14 +325,12 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr
}
case windows.WM_SETCURSOR:
w.cursorIn = (lParam & 0xffff) == windows.HTCLIENT
fallthrough
case _WM_CURSOR:
if w.cursorIn {
windows.SetCursor(w.cursor)
return windows.TRUE
}
case _WM_OPTION:
w.setOptions()
case _WM_WAKEUP:
w.w.Event(WakeupEvent{})
}
return windows.DefWindowProc(hwnd, msg, wParam, lParam)
@@ -422,9 +415,7 @@ func (w *window) loop() error {
msg := new(windows.Msg)
loop:
for {
w.mu.Lock()
anim := w.animating
w.mu.Unlock()
if anim && !windows.PeekMessage(msg, 0, 0, 0, windows.PM_NOREMOVE) {
w.draw(false)
continue
@@ -443,16 +434,11 @@ loop:
}
func (w *window) SetAnimating(anim bool) {
w.mu.Lock()
w.animating = anim
w.mu.Unlock()
if anim {
w.postRedraw()
}
}
func (w *window) postRedraw() {
if err := windows.PostMessage(w.hwnd, _WM_REDRAW, 0, 0); err != nil {
func (w *window) Wakeup() {
if err := windows.PostMessage(w.hwnd, _WM_WAKEUP, 0, 0); err != nil {
panic(err)
}
}
@@ -530,18 +516,7 @@ func (w *window) readClipboard() error {
}
func (w *window) Option(opts *Options) {
w.mu.Lock()
w.opts = opts
w.mu.Unlock()
if err := windows.PostMessage(w.hwnd, _WM_OPTION, 0, 0); err != nil {
panic(err)
}
}
func (w *window) setOptions() {
w.mu.Lock()
opts := w.opts
w.mu.Unlock()
if o := opts.Size; o != nil {
dpi := windows.GetSystemDPI()
cfg := configForDPI(dpi)
@@ -658,8 +633,8 @@ func (w *window) SetCursor(name pointer.CursorName) {
c = resources.cursor
}
w.cursor = c
if err := windows.PostMessage(w.hwnd, _WM_CURSOR, 0, 0); err != nil {
panic(err)
if w.cursorIn {
windows.SetCursor(w.cursor)
}
}