ui/app: replace WindowOptions with WindowOption

Thanks to Larry Clapp for noticing the opportunity for improvement.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-08-21 10:27:48 +02:00
parent e140f2a1c0
commit bff8b64e5a
8 changed files with 58 additions and 28 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ type windowRendezvous struct {
type windowAndOptions struct { type windowAndOptions struct {
window *Window window *Window
opts *WindowOptions opts *windowOptions
} }
const ( const (
+1 -1
View File
@@ -422,7 +422,7 @@ func (w *window) showTextInput(show bool) {
func main() { func main() {
} }
func createWindow(window *Window, opts *WindowOptions) error { func createWindow(window *Window, opts *windowOptions) error {
mainWindow.in <- windowAndOptions{window, opts} mainWindow.in <- windowAndOptions{window, opts}
return <-mainWindow.errs return <-mainWindow.errs
} }
+1 -1
View File
@@ -246,7 +246,7 @@ func (w *window) showTextInput(show bool) {
} }
} }
func createWindow(win *Window, opts *WindowOptions) error { func createWindow(win *Window, opts *windowOptions) error {
mainWindow.in <- windowAndOptions{win, opts} mainWindow.in <- windowAndOptions{win, opts}
return <-mainWindow.errs return <-mainWindow.errs
} }
+1 -1
View File
@@ -31,7 +31,7 @@ type window struct {
var mainDone = make(chan struct{}) var mainDone = make(chan struct{})
func createWindow(win *Window, opts *WindowOptions) error { func createWindow(win *Window, opts *windowOptions) error {
doc := js.Global().Get("document") doc := js.Global().Get("document")
cont := getContainer(doc) cont := getContainer(doc)
cnv := createCanvas(doc) cnv := createCanvas(doc)
+1 -1
View File
@@ -266,7 +266,7 @@ func gio_onCreate(view C.CFTypeRef) {
}) })
} }
func createWindow(win *Window, opts *WindowOptions) error { func createWindow(win *Window, opts *windowOptions) error {
mainWindow.in <- windowAndOptions{win, opts} mainWindow.in <- windowAndOptions{win, opts}
return <-mainWindow.errs return <-mainWindow.errs
} }
+2 -2
View File
@@ -156,7 +156,7 @@ func main() {
<-mainDone <-mainDone
} }
func createWindow(window *Window, opts *WindowOptions) error { func createWindow(window *Window, opts *windowOptions) error {
connMu.Lock() connMu.Lock()
defer connMu.Unlock() defer connMu.Unlock()
if len(winMap) > 0 { if len(winMap) > 0 {
@@ -182,7 +182,7 @@ func createWindow(window *Window, opts *WindowOptions) error {
return nil return nil
} }
func createNativeWindow(opts *WindowOptions) (*window, error) { func createNativeWindow(opts *windowOptions) (*window, error) {
pipe := make([]int, 2) pipe := make([]int, 2)
if err := syscall.Pipe2(pipe, syscall.O_NONBLOCK|syscall.O_CLOEXEC); err != nil { if err := syscall.Pipe2(pipe, syscall.O_NONBLOCK|syscall.O_CLOEXEC); err != nil {
return nil, fmt.Errorf("createNativeWindow: failed to create pipe: %v", err) return nil, fmt.Errorf("createNativeWindow: failed to create pipe: %v", err)
+2 -2
View File
@@ -161,7 +161,7 @@ func main() {
<-mainDone <-mainDone
} }
func createWindow(window *Window, opts *WindowOptions) error { func createWindow(window *Window, opts *windowOptions) error {
onceMu.Lock() onceMu.Lock()
defer onceMu.Unlock() defer onceMu.Unlock()
if len(winMap) > 0 { if len(winMap) > 0 {
@@ -191,7 +191,7 @@ func createWindow(window *Window, opts *WindowOptions) error {
return <-cerr return <-cerr
} }
func createNativeWindow(opts *WindowOptions) (*window, error) { func createNativeWindow(opts *windowOptions) (*window, error) {
setProcessDPIAware() setProcessDPIAware()
screenDC, err := getDC(0) screenDC, err := getDC(0)
if err != nil { if err != nil {
+49 -19
View File
@@ -15,11 +15,12 @@ import (
"gioui.org/ui/system" "gioui.org/ui/system"
) )
// WindowOptions specifies a set of window properties // WindowOption configures a Window.
// for creating new Windows. type WindowOption struct{
type WindowOptions struct { apply func(opts *windowOptions)
// Width and Height of the Window. Use the zero value }
// to choose a default size.
type windowOptions struct {
Width, Height ui.Value Width, Height ui.Value
Title string Title string
} }
@@ -83,20 +84,15 @@ var ackEvent input.Event
// platform. // platform.
// //
// BUG: Calling NewWindow more than once is not yet supported. // BUG: Calling NewWindow more than once is not yet supported.
func NewWindow(opts *WindowOptions) *Window { func NewWindow(options ...WindowOption) *Window {
if opts == nil { opts := &windowOptions{
opts = &WindowOptions{ Width: ui.Dp(800),
Title: "Gio", Height: ui.Dp(600),
} Title: "Gio",
} }
if opts.Width.V < 0 || opts.Height.V < 0 {
panic("window width and height must be larger than or equal to 0") for _, o := range options {
} o.apply(opts)
if opts.Width.V == 0 {
opts.Width = ui.Dp(800)
}
if opts.Height.V == 0 {
opts.Height = ui.Dp(600)
} }
w := &Window{ w := &Window{
@@ -224,7 +220,7 @@ func (w *Window) destroy(err error) {
} }
} }
func (w *Window) run(opts *WindowOptions) { func (w *Window) run(opts *windowOptions) {
defer close(w.in) defer close(w.in)
defer close(w.out) defer close(w.out)
if err := createWindow(w, opts); err != nil { if err := createWindow(w, opts); err != nil {
@@ -332,4 +328,38 @@ func (q *Queue) Next(k input.Key) (input.Event, bool) {
return q.q.Next(k) return q.q.Next(k)
} }
// WithTitle returns an option that sets the window title.
func WithTitle(t string) WindowOption {
return WindowOption{
apply: func(opts *windowOptions) {
opts.Title = t
},
}
}
// WithWidth returns an option that sets the window width.
func WithWidth(w ui.Value) WindowOption {
if w.V<= 0 {
panic("width must be larger than or equal to 0")
}
return WindowOption{
apply: func(opts *windowOptions) {
opts.Width= w
},
}
}
// WithHeight returns an option that sets the window height.
func WithHeight(h ui.Value) WindowOption {
if h.V<= 0 {
panic("height must be larger than or equal to 0")
}
return WindowOption{
apply: func(opts *windowOptions) {
opts.Height = h
},
}
}
func (_ driverEvent) ImplementsEvent() {} func (_ driverEvent) ImplementsEvent() {}