power
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.
常量
const (
Unknown = platform.PowerSourceUnknown
AC = platform.PowerSourceAC
Battery = platform.PowerSourceBattery
)变量
var ErrUnsupported = platform.ErrUnsupportedErrUnsupported is platform.ErrUnsupported.
函数
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.
类型
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() boolActive reports whether the block has not been released.
func (*Block) Release
func (b *Block) Release()Release ends the block. Releasing twice does nothing.
Source
type Source = platform.PowerSourceSource is where the machine draws power from.
Status
type Status = platform.PowerStatusStatus 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.