github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/event/lifecycle/lifecycle.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package lifecycle defines an event for an app's lifecycle. 6 // 7 // The app lifecycle consists of moving back and forth between an ordered 8 // sequence of stages. For example, being at a stage greater than or equal to 9 // StageVisible means that the app is visible on the screen. 10 // 11 // A lifecycle event is a change from one stage to another, which crosses every 12 // intermediate stage. For example, changing from StageAlive to StageFocused 13 // implicitly crosses StageVisible. 14 // 15 // Crosses can be in a positive or negative direction. A positive crossing of 16 // StageFocused means that the app has gained the focus. A negative crossing 17 // means it has lost the focus. 18 // 19 // See the golang.org/x/mobile/app package for details on the event model. 20 package lifecycle // import "golang.org/x/mobile/event/lifecycle" 21 22 import ( 23 "fmt" 24 ) 25 26 // Cross is whether a lifecycle stage was crossed. 27 type Cross uint32 28 29 func (c Cross) String() string { 30 switch c { 31 case CrossOn: 32 return "on" 33 case CrossOff: 34 return "off" 35 } 36 return "none" 37 } 38 39 const ( 40 CrossNone Cross = 0 41 CrossOn Cross = 1 42 CrossOff Cross = 2 43 ) 44 45 // Event is a lifecycle change from an old stage to a new stage. 46 type Event struct { 47 From, To Stage 48 49 // DrawContext is the state used for painting, if any is valid. 50 // 51 // For OpenGL apps, a non-nil DrawContext is a gl.Context. 52 // 53 // TODO: make this an App method if we move away from an event channel? 54 DrawContext interface{} 55 } 56 57 // Crosses returns whether the transition from From to To crosses the stage s: 58 // - It returns CrossOn if it does, and the lifecycle change is positive. 59 // - It returns CrossOff if it does, and the lifecycle change is negative. 60 // - Otherwise, it returns CrossNone. 61 // See the documentation for Stage for more discussion of positive and negative 62 // crosses. 63 func (e Event) Crosses(s Stage) Cross { 64 switch { 65 case e.From < s && e.To >= s: 66 return CrossOn 67 case e.From >= s && e.To < s: 68 return CrossOff 69 } 70 return CrossNone 71 } 72 73 // Stage is a stage in the app's lifecycle. The values are ordered, so that a 74 // lifecycle change from stage From to stage To implicitly crosses every stage 75 // in the range (min, max], exclusive on the low end and inclusive on the high 76 // end, where min is the minimum of From and To, and max is the maximum. 77 // 78 // The documentation for individual stages talk about positive and negative 79 // crosses. A positive lifecycle change is one where its From stage is less 80 // than its To stage. Similarly, a negative lifecycle change is one where From 81 // is greater than To. Thus, a positive lifecycle change crosses every stage in 82 // the range (From, To] in increasing order, and a negative lifecycle change 83 // crosses every stage in the range (To, From] in decreasing order. 84 type Stage uint32 85 86 // TODO: how does iOS map to these stages? What do cross-platform mobile 87 // abstractions do? 88 89 const ( 90 // StageDead is the zero stage. No lifecycle change crosses this stage, 91 // but: 92 // - A positive change from this stage is the very first lifecycle change. 93 // - A negative change to this stage is the very last lifecycle change. 94 StageDead Stage = iota 95 96 // StageAlive means that the app is alive. 97 // - A positive cross means that the app has been created. 98 // - A negative cross means that the app is being destroyed. 99 // Each cross, either from or to StageDead, will occur only once. 100 // On Android, these correspond to onCreate and onDestroy. 101 StageAlive 102 103 // StageVisible means that the app window is visible. 104 // - A positive cross means that the app window has become visible. 105 // - A negative cross means that the app window has become invisible. 106 // On Android, these correspond to onStart and onStop. 107 // On Desktop, an app window can become invisible if e.g. it is minimized, 108 // unmapped, or not on a visible workspace. 109 StageVisible 110 111 // StageFocused means that the app window has the focus. 112 // - A positive cross means that the app window has gained the focus. 113 // - A negative cross means that the app window has lost the focus. 114 // On Android, these correspond to onResume and onFreeze. 115 StageFocused 116 ) 117 118 func (s Stage) String() string { 119 switch s { 120 case StageDead: 121 return "StageDead" 122 case StageAlive: 123 return "StageAlive" 124 case StageVisible: 125 return "StageVisible" 126 case StageFocused: 127 return "StageFocused" 128 default: 129 return fmt.Sprintf("lifecycle.Stage(%d)", s) 130 } 131 }