power

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package power reports the machine's power state (AC or battery, charge, power-saving mode), tells the app when the system sleeps and wakes, and keeps the machine awake while the app plays video or runs a long task.

st, err := power.Get(a)              // st.Source == power.Battery, st.Battery == 0.42
power.OnSuspend(a, pauseSync)
power.OnResume(a, resumeSync)
blk, err := power.KeepDisplayOn(a, "Playing video")
defer blk.Release()

Platforms: macOS uses IOKit (IOPowerSources, IOPMAssertion, sleep/wake notifications) and NSProcessInfo's Low Power Mode; Windows GetSystemPowerStatus, SetThreadExecutionState and WM_POWERBROADCAST; Linux UPower and systemd-logind (Inhibit, PrepareForSleep) over the system D-Bus. A sleep the user asks for (closing the lid, the Sleep command) is never blocked: blocks only stop idle sleep.

On iOS, UIDevice battery monitoring and NSProcessInfo's Low Power Mode; both block kinds disable the idle timer (the screen stays on: an app cannot keep the device awake with the screen off). On Android, the battery broadcast and PowerManager's power-saver mode; KeepDisplayOn sets FLAG_KEEP_SCREEN_ON, KeepAwake holds a partial wake lock when the app declares android.permission.WAKE_LOCK and keeps the screen on otherwise. Neither mobile system tells apps about sleep and wake, so OnSuspend and OnResume never fire there; use the app lifecycle.

Functions are safe from any goroutine once the app runs; handlers run on the main thread. On* may be called before Run. Get returns ErrUnsupported where the backend has no power support (minimal backends) and tango.ErrNotRunning after the app quit; handlers then simply never fire.

Index

Constants

Variables

View Source
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

Functions

func OnChange

func OnChange(a *tango.App, fn func(Status))

OnChange calls fn with the new status whenever the power source, the charge or the power-saving mode changes.

func OnResume

func OnResume(a *tango.App, fn func())

OnResume calls fn after the system woke up.

func OnSuspend

func OnSuspend(a *tango.App, fn func())

OnSuspend calls fn when the system is about to sleep. The system does not wait long: save state quickly.

Types

type Block

type Block struct {
	// contains filtered or unexported fields
}

Block is one active sleep block. Release it when the work is done; the system drops it when the process exits.

func KeepAwake

func KeepAwake(a *tango.App, reason string) (*Block, error)

KeepAwake stops the system from idle-sleeping (the display may still turn off) until the block is released. Use it for long tasks.

func KeepDisplayOn

func KeepDisplayOn(a *tango.App, reason string) (*Block, error)

KeepDisplayOn keeps the display on and the system awake until the block is released. Use it for video playback and presentations.

func (*Block) Active

func (b *Block) Active() bool

Active reports whether the block has not been released.

func (*Block) Release

func (b *Block) Release()

Release ends the block. Releasing twice does nothing.

type Source

type Source = platform.PowerSource

Source is where the machine draws power from.

type Status

type Status = platform.PowerStatus

Status is a snapshot of the power state. Battery is the charge in [0,1], -1 when unknown or there is no battery.

func Get

func Get(a *tango.App) (Status, error)

Get returns the current power state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL