Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (69 sloc) 1.98 KB
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package lifecycler tracks a window's lifecycle state.
// It eliminates sending redundant lifecycle events, ones where the From and To
// stages are equal. For example, moving a window from one part of the screen
// to another should not send multiple events from StageVisible to
// StageVisible, even though the underlying window system's message might only
// hold the new position, and not whether the window was previously visible.
package lifecycler // import ""
import (
// State is a window's lifecycle state.
type State struct {
mu sync.Mutex
stage lifecycle.Stage
dead bool
focused bool
visible bool
func (s *State) SetDead(b bool) {
s.dead = b
func (s *State) SetFocused(b bool) {
s.focused = b
func (s *State) SetVisible(b bool) {
s.visible = b
func (s *State) SendEvent(r Sender, drawContext interface{}) {
from, to := s.stage, lifecycle.StageAlive
// The order of these if's is important. For example, once a window becomes
// StageDead, it should never change stage again.
// Similarly, focused trumps visible. It's hard to imagine a situation
// where a window is focused and not visible on screen, but in that
// unlikely case, StageFocused seems the most appropriate stage.
if s.dead {
to = lifecycle.StageDead
} else if s.focused {
to = lifecycle.StageFocused
} else if s.visible {
to = lifecycle.StageVisible
s.stage = to
if from != to {
From: from,
To: to,
// TODO: does shiny use this at all?
DrawContext: drawContext,
// Sender is who to send the lifecycle event to.
type Sender interface {
Send(event interface{})
You can’t perform that action at this time.