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