ui/app,apps/gophers,apps/hello: revert NewWindow to CreateWindow

It turns out we already support multiple windows on Android: when
the activity is recreated.

This reverts commit f21b5eb1df.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-04-02 19:26:23 +02:00
parent 6899f96532
commit ed2590e30e
9 changed files with 90 additions and 80 deletions
+15 -10
View File
@@ -121,6 +121,14 @@ func main() {
fmt.Println("The quota for anonymous GitHub API access is very low. Specify a token with -token to avoid quota errors.") fmt.Println("The quota for anonymous GitHub API access is very low. Specify a token with -token to avoid quota errors.")
fmt.Println("See https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line.") fmt.Println("See https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line.")
} }
err := app.CreateWindow(&app.WindowOptions{
Width: ui.Dp(400),
Height: ui.Dp(800),
Title: "Gophers",
})
if err != nil {
log.Fatal(err)
}
app.Main() app.Main()
} }
@@ -141,16 +149,13 @@ func init() {
fonts.italic = mustLoadFont(goitalic.TTF) fonts.italic = mustLoadFont(goitalic.TTF)
fonts.mono = mustLoadFont(gomono.TTF) fonts.mono = mustLoadFont(gomono.TTF)
go func() { go func() {
w, err := app.NewWindow(&app.WindowOptions{ for w := range app.Windows() {
Width: ui.Dp(400), w := w
Height: ui.Dp(800), go func() {
Title: "Gophers", if err := newApp(w).run(); err != nil {
}) log.Fatal(err)
if err != nil { }
log.Fatal(err) }()
}
if err := newApp(w).run(); err != nil {
log.Fatal(err)
} }
}() }()
} }
+25 -23
View File
@@ -20,14 +20,24 @@ import (
) )
func main() { func main() {
// Never called on mobile, blocks forever on err := app.CreateWindow(nil)
// desktop. if err != nil {
log.Fatal(err)
}
app.Main() app.Main()
} }
// On iOS and Android main will never be called, so // On iOS and Android main will never be called, so
// setting up the window must run in an init function. // setting up the window must run in an init function.
func init() { func init() {
go func() {
for w := range app.Windows() {
go loop(w)
}
}()
}
func loop(w *app.Window) {
regular, err := sfnt.Parse(goregular.TTF) regular, err := sfnt.Parse(goregular.TTF)
if err != nil { if err != nil {
panic("failed to load font") panic("failed to load font")
@@ -35,26 +45,18 @@ func init() {
var faces measure.Faces var faces measure.Faces
black := &image.Uniform{color.Black} black := &image.Uniform{color.Black}
face := faces.For(regular, ui.Dp(50)) face := faces.For(regular, ui.Dp(50))
// On iOS and Android app.NewWindow blocks, waiting for w.IsAlive() {
// for the platform to create a window. e := <-w.Events()
go func() { switch e := e.(type) {
w, err := app.NewWindow(nil) case app.Draw:
if err != nil { faces.Cfg = e.Config
log.Fatal(err) cs := layout.ExactConstraints(w.Size())
root, _ := (text.Label{Src: black, Face: face, Text: "Hello, World!"}).Layout(cs)
w.Draw(root)
faces.Frame()
} }
for w.IsAlive() { }
e := <-w.Events() if w.Err() != nil {
switch e := e.(type) { log.Fatal(err)
case app.Draw: }
faces.Cfg = e.Config
cs := layout.ExactConstraints(w.Size())
root, _ := (text.Label{Src: black, Face: face, Text: "Hello, World!"}).Layout(cs)
w.Draw(root)
faces.Frame()
}
}
if w.Err() != nil {
log.Fatal(err)
}
}()
} }
+9 -4
View File
@@ -59,12 +59,13 @@ const (
// Set it with the go tool linker flag -X. // Set it with the go tool linker flag -X.
var extraArgs string var extraArgs string
// NewWindow creates a new window for a set of window var windows = make(chan *Window)
// CreateWindow creates a new window for a set of window
// options. The options are hints; the platform is free to // options. The options are hints; the platform is free to
// ignore or adjust them. // ignore or adjust them.
// If the current program is running on iOS and Android, // CreateWindow is not supported on iOS and Android.
// NewWindow the window previously created by the platform. func CreateWindow(opts *WindowOptions) error {
func NewWindow(opts *WindowOptions) (*Window, error) {
if opts == nil { if opts == nil {
opts = &WindowOptions{ opts = &WindowOptions{
Width: ui.Dp(800), Width: ui.Dp(800),
@@ -78,6 +79,10 @@ func NewWindow(opts *WindowOptions) (*Window, error) {
return createWindow(opts) return createWindow(opts)
} }
func Windows() <-chan *Window {
return windows
}
func (l Stage) String() string { func (l Stage) String() string {
switch l { switch l {
case StageDead: case StageDead:
+2 -4
View File
@@ -55,8 +55,6 @@ type window struct {
var theJVM *C.JavaVM var theJVM *C.JavaVM
var windows = make(chan *Window)
var views = make(map[C.jlong]*window) var views = make(map[C.jlong]*window)
func jniGetMethodID(env *C.JNIEnv, class C.jclass, method, sig string) C.jmethodID { func jniGetMethodID(env *C.JNIEnv, class C.jclass, method, sig string) C.jmethodID {
@@ -381,6 +379,6 @@ func Main() {
panic("unreachable") panic("unreachable")
} }
func createWindow(opts *WindowOptions) (*Window, error) { func createWindow(opts *WindowOptions) error {
return <-windows, nil return errors.New("createWindow not supported")
} }
+2 -4
View File
@@ -42,8 +42,6 @@ var layerFactory func() uintptr
var views = make(map[C.CFTypeRef]*window) var views = make(map[C.CFTypeRef]*window)
var windows = make(chan *Window)
func init() { func init() {
// Darwin requires UI operations happen on the main thread only. // Darwin requires UI operations happen on the main thread only.
runtime.LockOSThread() runtime.LockOSThread()
@@ -235,8 +233,8 @@ func (w *window) setTextInput(s key.TextInputState) {
} }
} }
func createWindow(opts *WindowOptions) (*Window, error) { func createWindow(opts *WindowOptions) error {
return <-windows, nil panic("unsupported")
} }
func Main() { func Main() {
+20 -15
View File
@@ -15,6 +15,7 @@ import (
"errors" "errors"
"image" "image"
"runtime" "runtime"
"sync"
"time" "time"
"unsafe" "unsafe"
@@ -35,15 +36,13 @@ type window struct {
stage Stage stage Stage
} }
type windowError struct { // Only support one main window for now.
window *Window var singleWindow struct {
err error mu sync.Mutex
hasOpts bool
opts *WindowOptions
} }
var windowOpts = make(chan *WindowOptions)
var windows = make(chan windowError)
var viewFactory func() uintptr var viewFactory func() uintptr
var views = make(map[C.CFTypeRef]*window) var views = make(map[C.CFTypeRef]*window)
@@ -163,6 +162,7 @@ func gio_onTerminate(view C.CFTypeRef) {
w := views[view] w := views[view]
delete(views, view) delete(views, view)
w.setStage(StageDead) w.setStage(StageDead)
close(windows)
} }
//export gio_onHide //export gio_onHide
@@ -185,23 +185,28 @@ func gio_onCreate(view C.CFTypeRef) {
ow := newWindow(w) ow := newWindow(w)
w.w = ow w.w = ow
views[view] = w views[view] = w
windows <- windowError{window: ow} windows <- ow
} }
func createWindow(opts *WindowOptions) (*Window, error) { func createWindow(opts *WindowOptions) error {
windowOpts <- opts singleWindow.mu.Lock()
werr := <-windows defer singleWindow.mu.Unlock()
return werr.window, werr.err if singleWindow.hasOpts {
panic("only one window supported")
}
singleWindow.opts = opts
singleWindow.hasOpts = true
return nil
} }
func Main() { func Main() {
view := C.CFTypeRef(viewFactory()) view := C.CFTypeRef(viewFactory())
if view == 0 { if view == 0 {
windows <- windowError{err: errors.New("CreateWindow: failed to create view")} // TODO: return this error from CreateWindow.
return panic(errors.New("CreateWindow: failed to create view"))
} }
cfg := getConfig() cfg := getConfig()
opts := <-windowOpts opts := singleWindow.opts
w := cfg.Pixels(opts.Width) w := cfg.Pixels(opts.Width)
h := cfg.Pixels(opts.Height) h := cfg.Pixels(opts.Height)
title := C.CString(opts.Title) title := C.CString(opts.Title)
+6 -4
View File
@@ -141,28 +141,30 @@ func Main() {
<-mainDone <-mainDone
} }
func createWindow(opts *WindowOptions) (*Window, error) { func createWindow(opts *WindowOptions) error {
connMu.Lock() connMu.Lock()
defer connMu.Unlock() defer connMu.Unlock()
if len(winMap) > 0 { if len(winMap) > 0 {
panic("multiple windows are not supported") panic("multiple windows are not supported")
} }
if err := waylandConnect(); err != nil { if err := waylandConnect(); err != nil {
return nil, err return err
} }
w, err := createNativeWindow(opts) w, err := createNativeWindow(opts)
if err != nil { if err != nil {
conn.destroy() conn.destroy()
return nil, err return err
} }
go func() { go func() {
windows <- w.w
w.setStage(StageVisible) w.setStage(StageVisible)
w.loop() w.loop()
w.destroy() w.destroy()
conn.destroy() conn.destroy()
close(windows)
close(mainDone) close(mainDone)
}() }()
return w.w, nil return nil
} }
func createNativeWindow(opts *WindowOptions) (*window, error) { func createNativeWindow(opts *WindowOptions) (*window, error) {
+7 -10
View File
@@ -157,37 +157,34 @@ func Main() {
<-mainDone <-mainDone
} }
func createWindow(opts *WindowOptions) (*Window, error) { func createWindow(opts *WindowOptions) error {
onceMu.Lock() onceMu.Lock()
defer onceMu.Unlock() defer onceMu.Unlock()
if len(winMap) > 0 { if len(winMap) > 0 {
panic("multiple windows are not supported") panic("multiple windows are not supported")
} }
type windowError struct { cerr := make(chan error)
window *Window
err error
}
cerr := make(chan windowError)
go func() { go func() {
// Call win32 API from a single OS thread. // Call win32 API from a single OS thread.
runtime.LockOSThread() runtime.LockOSThread()
w, err := createNativeWindow(opts) w, err := createNativeWindow(opts)
if err != nil { if err != nil {
cerr <- windowError{err: err} cerr <- err
return return
} }
defer w.destroy() defer w.destroy()
cerr <- windowError{w.w, nil} cerr <- nil
windows <- w.w
showWindow(w.hwnd, _SW_SHOWDEFAULT) showWindow(w.hwnd, _SW_SHOWDEFAULT)
setForegroundWindow(w.hwnd) setForegroundWindow(w.hwnd)
setFocus(w.hwnd) setFocus(w.hwnd)
if err := w.loop(); err != nil { if err := w.loop(); err != nil {
panic(err) panic(err)
} }
close(windows)
close(mainDone) close(mainDone)
}() }()
werr := <-cerr return <-cerr
return werr.window, werr.err
} }
func createNativeWindow(opts *WindowOptions) (*window, error) { func createNativeWindow(opts *WindowOptions) (*window, error) {
+4 -6
View File
@@ -36,7 +36,6 @@ type Window struct {
mu sync.Mutex mu sync.Mutex
stage Stage stage Stage
size image.Point size image.Point
skipAcks int
syncGPU bool syncGPU bool
animating bool animating bool
hasNextFrame bool hasNextFrame bool
@@ -208,9 +207,11 @@ func (w *Window) event(e Event) {
case key.Event: case key.Event:
needRedraw = true needRedraw = true
case ChangeStage: case ChangeStage:
needAck = true
w.stage = e.Stage w.stage = e.Stage
w.syncGPU = true if w.stage > StageDead {
needAck = true
w.syncGPU = true
}
case Draw: case Draw:
if e.Size == (image.Point{}) { if e.Size == (image.Point{}) {
panic(errors.New("internal error: zero-sized Draw")) panic(errors.New("internal error: zero-sized Draw"))
@@ -223,9 +224,6 @@ func (w *Window) event(e Event) {
w.syncGPU = e.sync w.syncGPU = e.sync
w.size = e.Size w.size = e.Size
} }
if !needAck {
w.skipAcks++
}
stage := w.stage stage := w.stage
w.mu.Unlock() w.mu.Unlock()
if needRedraw { if needRedraw {