Skip to content

power ​

go
import "github.com/tangoui/tango/sys/power"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

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.

常量 ​

go
const (
	Unknown = platform.PowerSourceUnknown
	AC      = platform.PowerSourceAC
	Battery = platform.PowerSourceBattery
)

变量 ​

go
var ErrUnsupported = platform.ErrUnsupported

ErrUnsupported is platform.ErrUnsupported.

函数 ​

func OnChange ​

go
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 ​

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

OnResume calls fn after the system woke up.

func OnSuspend ​

go
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.

类型 ​

Block ​

go
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 ​

go
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 ​

go
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 ​

go
func (b *Block) Active() bool

Active reports whether the block has not been released.

func (*Block) Release ​

go
func (b *Block) Release()

Release ends the block. Releasing twice does nothing.

Source ​

go
type Source = platform.PowerSource

Source is where the machine draws power from.

Status ​

go
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 ​

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

Get returns the current power state.

以 MIT 许可发布