ui/app,apps: unexport ChangeStage and Stage

I'm not convinced the API is right. For exmaple, an event that
notifies a program when to save its state is both smaller and
might be sufficient.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-05-11 13:50:18 +02:00
parent 314c79a725
commit f9840b0963
8 changed files with 57 additions and 62 deletions
-1
View File
@@ -185,7 +185,6 @@ func (a *App) run() error {
}
}
}
case app.ChangeStage:
case *app.Command:
switch e.Type {
case app.CommandBack:
+15 -15
View File
@@ -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() {
+10 -10
View File
@@ -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 {
+4 -4
View File
@@ -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
+1 -1
View File
@@ -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()
+7 -7
View File
@@ -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
+8 -8
View File
@@ -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) {
+12 -16
View File
@@ -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,14 +208,10 @@ func (w *Window) Size() image.Point {
return w.size
}
func (w *Window) Stage() Stage {
func (w *Window) IsAlive() bool {
w.mu.Lock()
defer w.mu.Unlock()
return w.stage
}
func (w *Window) IsAlive() bool {
return w.Stage() != StageDead && w.err == nil
return w.stage != stageDead && w.err == nil
}
func (w *Window) contextDriver() interface{} {
@@ -234,9 +230,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
}
@@ -244,7 +240,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
}
@@ -270,14 +266,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)
}
}