From 4441a3e13ef5bac4be842228ed14f48ff21c4da6 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 12 May 2019 14:47:38 +0200 Subject: [PATCH] Revert "ui/app,apps: unexport ChangeStage and Stage" I found a convincing use case: stopping asynchronous activities while paused. A follow up change will rename the stages and add an example. This reverts commit f9840b0963d16028521fef6911f993194d484b0b. --- apps/gophers/main.go | 1 + ui/app/app.go | 30 +++++++++++++++--------------- ui/app/os_android.go | 20 ++++++++++---------- ui/app/os_ios.go | 8 ++++---- ui/app/os_js.go | 2 +- ui/app/os_macos.go | 14 +++++++------- ui/app/os_windows.go | 16 ++++++++-------- ui/app/window.go | 28 ++++++++++++++++------------ 8 files changed, 62 insertions(+), 57 deletions(-) diff --git a/apps/gophers/main.go b/apps/gophers/main.go index a93f311e..f0805be5 100644 --- a/apps/gophers/main.go +++ b/apps/gophers/main.go @@ -185,6 +185,7 @@ func (a *App) run() error { } } } + case app.ChangeStage: case *app.Command: switch e.Type { case app.CommandBack: diff --git a/ui/app/app.go b/ui/app/app.go index 2e0756db..7342976e 100644 --- a/ui/app/app.go +++ b/ui/app/app.go @@ -23,8 +23,8 @@ type Draw struct { sync bool } -type changeStage struct { - stage stage +type ChangeStage struct { + Stage Stage } // Command is a system event. @@ -34,7 +34,7 @@ type Command struct { Cancel bool } -type stage uint8 +type Stage uint8 type CommandType uint8 type Input interface { @@ -42,9 +42,9 @@ type Input interface { } const ( - stageDead stage = iota - stageInvisible - stageVisible + StageDead Stage = iota + StageInvisible + StageVisible ) const ( @@ -97,21 +97,21 @@ func Windows() <-chan *Window { return windows } -func (l stage) String() string { +func (l Stage) String() string { switch l { - case stageDead: - return "stageDead" - case stageInvisible: - return "stageInvisible" - case stageVisible: - return "stageVisible" + case StageDead: + return "StageDead" + case StageInvisible: + return "StageInvisible" + case StageVisible: + return "StageVisible" default: - panic("unexpected stage value") + panic("unexpected Stage value") } } func (_ Draw) ImplementsEvent() {} -func (_ changeStage) ImplementsEvent() {} +func (_ ChangeStage) ImplementsEvent() {} func (_ *Command) ImplementsEvent() {} func init() { diff --git a/ui/app/os_android.go b/ui/app/os_android.go index 8cab176f..afa883b7 100644 --- a/ui/app/os_android.go +++ b/ui/app/os_android.go @@ -38,7 +38,7 @@ type window struct { dpi int fontScale float32 - stage stage + stage Stage started bool mu sync.Mutex @@ -96,7 +96,7 @@ func onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong { views[handle] = w w.loadConfig(env, class) windows <- ow - w.setStage(stageInvisible) + w.setStage(StageInvisible) return handle } @@ -104,7 +104,7 @@ func onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong { func onDestroyView(env *C.JNIEnv, class C.jclass, handle C.jlong) { w := views[handle] delete(views, handle) - w.setStage(stageDead) + w.setStage(StageDead) C.gio_jni_DeleteGlobalRef(env, w.view) w.view = 0 } @@ -113,7 +113,7 @@ func onDestroyView(env *C.JNIEnv, class C.jclass, handle C.jlong) { func onStopView(env *C.JNIEnv, class C.jclass, handle C.jlong) { w := views[handle] w.started = false - w.setStage(stageInvisible) + w.setStage(StageInvisible) } //export onStartView @@ -131,7 +131,7 @@ func onSurfaceDestroyed(env *C.JNIEnv, class C.jclass, handle C.jlong) { w.mu.Lock() w.win = nil w.mu.Unlock() - w.setStage(stageInvisible) + w.setStage(StageInvisible) } //export onSurfaceChanged @@ -155,7 +155,7 @@ func onLowMemory() { func onConfigurationChanged(env *C.JNIEnv, class C.jclass, view C.jlong) { w := views[view] w.loadConfig(env, class) - if w.stage >= stageVisible { + if w.stage >= StageVisible { w.draw(true) } } @@ -166,7 +166,7 @@ func onFrameCallback(env *C.JNIEnv, class C.jclass, view C.jlong, nanos C.jlong) if !exist { return } - if w.stage < stageVisible { + if w.stage < StageVisible { return } w.mu.Lock() @@ -197,16 +197,16 @@ func (w *window) setVisible() { if width == 0 || height == 0 { return } - w.setStage(stageVisible) + w.setStage(StageVisible) w.draw(true) } -func (w *window) setStage(stage stage) { +func (w *window) setStage(stage Stage) { if stage == w.stage { return } w.stage = stage - w.event(changeStage{stage}) + w.event(ChangeStage{stage}) } func (w *window) display() unsafe.Pointer { diff --git a/ui/app/os_ios.go b/ui/app/os_ios.go index 2747fc11..6a3f40f5 100644 --- a/ui/app/os_ios.go +++ b/ui/app/os_ios.go @@ -59,7 +59,7 @@ func onCreate(view C.CFTypeRef) { C.gio_addLayerToView(view, w.layer) views[view] = w windows <- ow - w.w.event(changeStage{stageInvisible}) + w.w.event(ChangeStage{StageInvisible}) } //export onDraw @@ -72,7 +72,7 @@ func onDraw(view C.CFTypeRef, dpi, sdpi, width, height C.CGFloat, sync C.int) { w.visible.Store(true) C.gio_updateView(view, w.layer) if !wasVisible { - w.w.event(changeStage{stageVisible}) + w.w.event(ChangeStage{StageVisible}) } isSync := false if sync != 0 { @@ -96,14 +96,14 @@ func onDraw(view C.CFTypeRef, dpi, sdpi, width, height C.CGFloat, sync C.int) { func onStop(view C.CFTypeRef) { w := views[view] w.visible.Store(false) - w.w.event(changeStage{stageInvisible}) + w.w.event(ChangeStage{StageInvisible}) } //export onDestroy func onDestroy(view C.CFTypeRef) { w := views[view] delete(views, view) - w.w.event(changeStage{stageDead}) + w.w.event(ChangeStage{StageDead}) C.gio_removeLayer(w.layer) C.CFRelease(w.layer) w.layer = 0 diff --git a/ui/app/os_js.go b/ui/app/os_js.go index 684e0705..fc32dfda 100644 --- a/ui/app/os_js.go +++ b/ui/app/os_js.go @@ -56,7 +56,7 @@ func createWindow(opts *WindowOptions) error { w.w = newWindow(w) go func() { windows <- w.w - w.w.event(changeStage{stageVisible}) + w.w.event(ChangeStage{StageVisible}) w.draw(true) select {} w.cleanup() diff --git a/ui/app/os_macos.go b/ui/app/os_macos.go index c290e82f..63960ef3 100644 --- a/ui/app/os_macos.go +++ b/ui/app/os_macos.go @@ -33,7 +33,7 @@ func init() { type window struct { view C.CFTypeRef w *Window - stage stage + stage Stage } // Only support one main window for now. @@ -61,12 +61,12 @@ func (w *window) setAnimating(anim bool) { C.gio_setAnimating(w.view, animb) } -func (w *window) setStage(stage stage) { +func (w *window) setStage(stage Stage) { if stage == w.stage { return } w.stage = stage - w.w.event(changeStage{stage}) + w.w.event(ChangeStage{stage}) } //export gio_onFrameCallback @@ -134,7 +134,7 @@ func (w *window) draw(sync bool) { } cfg := getConfig() cfg.Now = time.Now() - w.setStage(stageVisible) + w.setStage(StageVisible) w.w.event(Draw{ Size: image.Point{ X: width, @@ -161,20 +161,20 @@ func getConfig() ui.Config { func gio_onTerminate(view C.CFTypeRef) { w := views[view] delete(views, view) - w.setStage(stageDead) + w.setStage(StageDead) close(windows) } //export gio_onHide func gio_onHide(view C.CFTypeRef) { w := views[view] - w.setStage(stageInvisible) + w.setStage(StageInvisible) } //export gio_onShow func gio_onShow(view C.CFTypeRef) { w := views[view] - w.setStage(stageVisible) + w.setStage(StageVisible) } //export gio_onCreate diff --git a/ui/app/os_windows.go b/ui/app/os_windows.go index bd78ff69..7ce3a5f2 100644 --- a/ui/app/os_windows.go +++ b/ui/app/os_windows.go @@ -60,7 +60,7 @@ type window struct { w *Window width int height int - stage stage + stage Stage mu sync.Mutex animating bool @@ -239,7 +239,7 @@ func createNativeWindow(opts *WindowOptions) (*window, error) { } w := &window{ hwnd: hwnd, - stage: stageInvisible, + stage: StageInvisible, } winMap[hwnd] = w w.hdc, err = getDC(hwnd) @@ -310,7 +310,7 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr w.scrollEvent(wParam, lParam) case _WM_DESTROY: delete(winMap, hwnd) - w.setStage(stageDead) + w.setStage(StageDead) case _WM_REDRAW: w.mu.Lock() anim := w.animating @@ -324,9 +324,9 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr case _WM_SIZE: switch wParam { case _SIZE_MINIMIZED: - w.setStage(stageInvisible) + w.setStage(StageInvisible) case _SIZE_MAXIMIZED, _SIZE_RESTORED: - w.setStage(stageVisible) + w.setStage(StageVisible) w.draw(true) } } @@ -359,7 +359,7 @@ func (w *window) scrollEvent(wParam, lParam uintptr) { // Adapted from https://blogs.msdn.microsoft.com/oldnewthing/20060126-00/?p=32513/ func (w *window) loop() error { loop: - for w.stage > stageDead { + for w.stage > StageDead { var msg msg // Since posted messages are always returned before system messages, // but we want our WM_REDRAW to always come last, just like WM_PAINT. @@ -398,9 +398,9 @@ func (w *window) postRedraw() { } } -func (w *window) setStage(s stage) { +func (w *window) setStage(s Stage) { w.stage = s - w.w.event(changeStage{s}) + w.w.event(ChangeStage{s}) } func (w *window) draw(sync bool) { diff --git a/ui/app/window.go b/ui/app/window.go index 50dd626b..001ffc85 100644 --- a/ui/app/window.go +++ b/ui/app/window.go @@ -35,7 +35,7 @@ type Window struct { events chan Event mu sync.Mutex - stage stage + stage Stage size image.Point syncGPU bool animating bool @@ -62,7 +62,7 @@ func newWindow(nw *window) *Window { w := &Window{ driver: nw, events: make(chan Event), - stage: stageInvisible, + stage: StageInvisible, } return w } @@ -103,7 +103,7 @@ func (w *Window) Draw(root *ui.Ops) { w.hasNextFrame = false w.syncGPU = false w.mu.Unlock() - if stage < stageVisible { + if stage < StageVisible { return } if w.gpu != nil { @@ -176,7 +176,7 @@ func (w *Window) updateAnimation() { w.mu.Lock() defer w.mu.Unlock() animate := false - if w.stage >= stageVisible && w.hasNextFrame { + if w.stage >= StageVisible && w.hasNextFrame { if dt := time.Until(w.nextFrame); dt <= 0 { animate = true } else { @@ -208,10 +208,14 @@ func (w *Window) Size() image.Point { return w.size } -func (w *Window) IsAlive() bool { +func (w *Window) Stage() Stage { w.mu.Lock() defer w.mu.Unlock() - return w.stage != stageDead && w.err == nil + return w.stage +} + +func (w *Window) IsAlive() bool { + return w.Stage() != StageDead && w.err == nil } func (w *Window) contextDriver() interface{} { @@ -230,9 +234,9 @@ func (w *Window) event(e Event) { case *Command: needAck = true needRedraw = true - case changeStage: - w.stage = e.stage - if w.stage > stageDead { + case ChangeStage: + w.stage = e.Stage + if w.stage > StageDead { needAck = true w.syncGPU = true } @@ -240,7 +244,7 @@ func (w *Window) event(e Event) { if e.Size == (image.Point{}) { panic(errors.New("internal error: zero-sized Draw")) } - if w.stage < stageVisible { + if w.stage < StageVisible { // No drawing if not visible. break } @@ -266,14 +270,14 @@ func (w *Window) event(e Event) { w.syncGPU = false w.mu.Unlock() switch { - case stage < stageVisible: + case stage < StageVisible: w.gpu.Release() w.gpu = nil case sync: w.gpu.Refresh() } } - if stage == stageDead { + if stage == StageDead { close(w.events) } }