mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 09:25:38 +00:00
ui/app,apps/gophers,apps/hello: replace CreateWindow with NewWindow
Gio doesn't support multiple windows anyway, so get rid of the app.Windows channel and use NewWindow call for the mobile platforms as well. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -2,6 +2,8 @@ module gioui.org/apps
|
|||||||
|
|
||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
|
replace gioui.org/ui => ../ui
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gioui.org/ui v0.0.0-20190331090026-ca5204fcb8b3
|
gioui.org/ui v0.0.0-20190331090026-ca5204fcb8b3
|
||||||
github.com/google/go-github/v24 v24.0.1
|
github.com/google/go-github/v24 v24.0.1
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
gioui.org/ui v0.0.0-20190331090026-ca5204fcb8b3 h1:+iSMCchOrM75QcNH8czjwCXVbwYLofiPeKHl/8Nxxck=
|
|
||||||
gioui.org/ui v0.0.0-20190331090026-ca5204fcb8b3/go.mod h1:FBjAd/bO8PFxAPY49SiMXkF7Mj677k+KYut4glYlhnM=
|
|
||||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
|||||||
+10
-15
@@ -119,14 +119,6 @@ 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,13 +139,16 @@ 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() {
|
||||||
for w := range app.Windows() {
|
w, err := app.NewWindow(app.WindowOptions{
|
||||||
w := w
|
Width: ui.Dp(400),
|
||||||
go func() {
|
Height: ui.Dp(800),
|
||||||
if err := newApp(w).run(); err != nil {
|
Title: "Gophers",
|
||||||
log.Fatal(err)
|
})
|
||||||
}
|
if err != nil {
|
||||||
}()
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := newApp(w).run(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-30
@@ -18,26 +18,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := app.CreateWindow(app.WindowOptions{
|
|
||||||
Width: ui.Dp(400),
|
|
||||||
Height: ui.Dp(800),
|
|
||||||
Title: "Hello World",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
app.Main()
|
app.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
@@ -45,19 +29,29 @@ func loop(w *app.Window) {
|
|||||||
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))
|
||||||
for w.IsAlive() {
|
go func() {
|
||||||
e := <-w.Events()
|
w, err := app.NewWindow(app.WindowOptions{
|
||||||
switch e := e.(type) {
|
Width: ui.Dp(400),
|
||||||
case app.Draw:
|
Height: ui.Dp(800),
|
||||||
faces.Cfg = e.Config
|
Title: "Hello World",
|
||||||
cs := layout.ExactConstraints(w.Size())
|
})
|
||||||
root, _ := (text.Label{Src: black, Face: face, Text: "Hello, World!"}).Layout(cs)
|
if err != nil {
|
||||||
w.Draw(root)
|
log.Fatal(err)
|
||||||
faces.Frame()
|
|
||||||
}
|
}
|
||||||
w.Ack()
|
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()
|
||||||
|
}
|
||||||
|
w.Ack()
|
||||||
|
}
|
||||||
|
if w.Err() != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-7
@@ -59,19 +59,18 @@ 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
|
||||||
|
|
||||||
var windows = make(chan *Window)
|
// NewWindow creates a new window for a set of window
|
||||||
|
// options. The options are hints; the platform is free to
|
||||||
func CreateWindow(opts WindowOptions) error {
|
// ignore or adjust them.
|
||||||
|
// If the current program is running on iOS and Android,
|
||||||
|
// NewWindow the window previously created by the platform.
|
||||||
|
func NewWindow(opts WindowOptions) (*Window, error) {
|
||||||
if opts.Width.V <= 0 || opts.Height.V <= 0 {
|
if opts.Width.V <= 0 || opts.Height.V <= 0 {
|
||||||
panic("window width and height must be larger than 0")
|
panic("window width and height must be larger than 0")
|
||||||
}
|
}
|
||||||
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:
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ 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 {
|
||||||
@@ -379,6 +381,6 @@ func Main() {
|
|||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWindow(opts WindowOptions) error {
|
func createWindow(opts WindowOptions) (*Window, error) {
|
||||||
return errors.New("createWindow not supported")
|
return <- windows, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -22,10 +22,10 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gioui.org/ui"
|
||||||
"gioui.org/ui/f32"
|
"gioui.org/ui/f32"
|
||||||
"gioui.org/ui/key"
|
"gioui.org/ui/key"
|
||||||
"gioui.org/ui/pointer"
|
"gioui.org/ui/pointer"
|
||||||
"gioui.org/ui"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type window struct {
|
type window struct {
|
||||||
@@ -42,6 +42,8 @@ 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()
|
||||||
@@ -233,8 +235,8 @@ func (w *window) setTextInput(s key.TextInputState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWindow(opts WindowOptions) error {
|
func createWindow(opts WindowOptions) (*Window, error) {
|
||||||
panic("unsupported")
|
return <-windows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
|
|||||||
+15
-20
@@ -15,7 +15,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"image"
|
"image"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
@@ -36,13 +35,15 @@ type window struct {
|
|||||||
stage Stage
|
stage Stage
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only support one main window for now.
|
type windowError struct {
|
||||||
var singleWindow struct {
|
window *Window
|
||||||
mu sync.Mutex
|
err error
|
||||||
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)
|
||||||
@@ -162,7 +163,6 @@ 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,28 +185,23 @@ 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 <- ow
|
windows <- windowError{window: ow}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWindow(opts WindowOptions) error {
|
func createWindow(opts WindowOptions) (*Window, error) {
|
||||||
singleWindow.mu.Lock()
|
windowOpts <- opts
|
||||||
defer singleWindow.mu.Unlock()
|
werr := <-windows
|
||||||
if singleWindow.hasOpts {
|
return werr.window, werr.err
|
||||||
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 {
|
||||||
// TODO: return this error from CreateWindow.
|
windows <- windowError{err: errors.New("CreateWindow: failed to create view")}
|
||||||
panic(errors.New("CreateWindow: failed to create view"))
|
return
|
||||||
}
|
}
|
||||||
cfg := getConfig()
|
cfg := getConfig()
|
||||||
opts := singleWindow.opts
|
opts := <-windowOpts
|
||||||
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)
|
||||||
|
|||||||
@@ -141,30 +141,28 @@ func Main() {
|
|||||||
<-mainDone
|
<-mainDone
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWindow(opts WindowOptions) error {
|
func createWindow(opts WindowOptions) (*Window, 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 err
|
return nil, err
|
||||||
}
|
}
|
||||||
w, err := createNativeWindow(opts)
|
w, err := createNativeWindow(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.destroy()
|
conn.destroy()
|
||||||
return err
|
return nil, 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 nil
|
return w.w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNativeWindow(opts WindowOptions) (*window, error) {
|
func createNativeWindow(opts WindowOptions) (*window, error) {
|
||||||
|
|||||||
+11
-8
@@ -13,10 +13,10 @@ import (
|
|||||||
|
|
||||||
syscall "golang.org/x/sys/windows"
|
syscall "golang.org/x/sys/windows"
|
||||||
|
|
||||||
|
"gioui.org/ui"
|
||||||
"gioui.org/ui/f32"
|
"gioui.org/ui/f32"
|
||||||
"gioui.org/ui/key"
|
"gioui.org/ui/key"
|
||||||
"gioui.org/ui/pointer"
|
"gioui.org/ui/pointer"
|
||||||
"gioui.org/ui"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var winMap = make(map[syscall.Handle]*window)
|
var winMap = make(map[syscall.Handle]*window)
|
||||||
@@ -157,34 +157,37 @@ func Main() {
|
|||||||
<-mainDone
|
<-mainDone
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWindow(opts WindowOptions) error {
|
func createWindow(opts WindowOptions) (*Window, 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")
|
||||||
}
|
}
|
||||||
cerr := make(chan error, 1)
|
type windowError struct {
|
||||||
|
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 <- err
|
cerr <- windowError{err: err}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer w.destroy()
|
defer w.destroy()
|
||||||
cerr <- nil
|
cerr <- windowError{w.w, 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)
|
||||||
}()
|
}()
|
||||||
return <-cerr
|
werr := <-cerr
|
||||||
|
return werr.window, werr.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNativeWindow(opts WindowOptions) (*window, error) {
|
func createNativeWindow(opts WindowOptions) (*window, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user