Files
openchamber-android-native/third_party/mygo/power.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

117 lines
3.7 KiB
Go

package mygo
import (
"sync"
"time"
"github.com/egoist/mygo/internal/platform"
)
// PowerModule reports power and session changes and keeps the computer
// awake. Use the Power singleton.
type PowerModule struct {
mu sync.Mutex
watching bool
onSuspend, onResume, onLockScreen, onUnlockScreen listeners[func()]
}
// Power reports power and session changes and keeps the computer awake.
var Power = &PowerModule{}
// OnSuspend is called when the system is about to sleep.
func (p *PowerModule) OnSuspend(fn func()) (off func()) { return p.add(&p.onSuspend, fn) }
// OnResume is called when the system woke up.
func (p *PowerModule) OnResume(fn func()) (off func()) { return p.add(&p.onResume, fn) }
// OnLockScreen is called when the screen locks (on Linux, when the screen
// saver starts, which usually locks it).
func (p *PowerModule) OnLockScreen(fn func()) (off func()) { return p.add(&p.onLockScreen, fn) }
// OnUnlockScreen is called when the screen unlocks.
func (p *PowerModule) OnUnlockScreen(fn func()) (off func()) { return p.add(&p.onUnlockScreen, fn) }
// add registers a listener and starts watching the system once.
func (p *PowerModule) add(l *listeners[func()], fn func()) func() {
off := l.add(fn, false)
p.mu.Lock()
start := !p.watching
p.watching = true
p.mu.Unlock()
if start {
App.WhenReady(func() { backend().Power().Watch() })
}
return off
}
func (p *PowerModule) event(event string) {
switch event {
case "suspend":
fire(&p.onSuspend)
case "resume":
fire(&p.onResume)
case "lock-screen":
fire(&p.onLockScreen)
case "unlock-screen":
fire(&p.onUnlockScreen)
}
}
// KeepAwake keeps the computer from sleeping while idle, and with display
// the display from turning off, e.g. during a long task or while a video
// plays, until release is called:
//
// release := mygo.Power.KeepAwake("Exporting the video", false)
// defer release()
//
// The reason shows where the system lists what keeps it awake.
func (p *PowerModule) KeepAwake(reason string, display bool) (release func()) {
needsApp("Power.KeepAwake")
var r func()
onMain(func() { r = backend().Power().KeepAwake(display, reason) })
if r == nil {
return func() {}
}
var once sync.Once
return func() { once.Do(func() { onMain(r) }) }
}
// KeepRunning keeps the app at full speed while none of its windows shows,
// for an app that serves the network, streams or syncs out of sight, until
// release is called, and leaves the computer free to sleep when idle:
//
// release := mygo.Power.KeepRunning("Serving the phones")
// defer release()
//
// macOS otherwise puts an app whose windows are hidden, minimized or
// covered, or behind the locked screen, into App Nap within a minute: it
// lowers the app's priority, coalesces its timers and throttles its I/O.
// KeepAwake keeps App Nap off too, but the computer awake as well, which
// an app that stays in the menu bar all day should not do. Other systems
// do nothing here.
func (p *PowerModule) KeepRunning(reason string) (release func()) {
needsApp("Power.KeepRunning")
var r func()
onMain(func() { r = backend().Power().KeepRunning(reason) })
if r == nil {
return func() {}
}
var once sync.Once
return func() { once.Do(func() { onMain(r) }) }
}
// IsOnBattery reports whether the computer runs on battery power.
func (p *PowerModule) IsOnBattery() bool {
return onMainValue(func() bool { return backend().Power().OnBattery() })
}
// IdleTime returns how long the user has not used the keyboard or mouse,
// or 0 when the system does not tell (some Linux desktops).
func (p *PowerModule) IdleTime() time.Duration {
needsApp("Power.IdleTime")
return onMainValue(func() time.Duration { return backend().Power().IdleTime() })
}
var _ platform.Power // the backends implement it