Files
openchamber-android-native/third_party/mygo/internal/platform/heldkeys.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

93 lines
3.0 KiB
Go

package platform
// HeldKeys keeps the keys whose presses a backend sent the content and
// whose releases it has not, for a backend whose system keeps some
// releases from the window: those an input method takes, those a menu's
// loop takes, and, in a Mac's browsers, those let go of while Command is
// held. The backend lets go of them itself (Lift, LiftAll), so that the
// content never holds a key the user let go of (KeyReleased).
type HeldKeys struct {
held []heldKey
// gone are the keys Lift and LiftAll let go of, whose own releases may
// still come, which Track then keeps from the content.
gone []heldKey
}
type heldKey struct {
key, physical Key
code KeyCode
}
// same reports whether k is the key at physical, or key where either
// position is unknown.
func (k heldKey) same(key, physical Key) bool {
if k.physical != KeyUnknown && physical != KeyUnknown {
return k.physical == physical
}
return key != KeyUnknown && k.key == key
}
func indexKey(keys []heldKey, key, physical Key) int {
for i, k := range keys {
if k.same(key, physical) {
return i
}
}
return -1
}
// Track notes ev, an event the backend is about to send the content, and
// reports whether to send it: false for the release of a key Lift or
// LiftAll let go of already.
func (h *HeldKeys) Track(ev SurfaceEvent) bool {
switch ev.Kind {
case KeyPressed:
if i := indexKey(h.gone, ev.Key, ev.PhysicalKey); i >= 0 {
h.gone = append(h.gone[:i], h.gone[i+1:]...)
}
if indexKey(h.held, ev.Key, ev.PhysicalKey) < 0 && (ev.Key != KeyUnknown || ev.PhysicalKey != KeyUnknown) {
h.held = append(h.held, heldKey{ev.Key, ev.PhysicalKey, ev.Code})
}
case KeyReleased:
if i := indexKey(h.held, ev.Key, ev.PhysicalKey); i >= 0 {
h.held = append(h.held[:i], h.held[i+1:]...)
return true
}
if i := indexKey(h.gone, ev.Key, ev.PhysicalKey); i >= 0 {
h.gone = append(h.gone[:i], h.gone[i+1:]...)
return false
}
case SurfaceBlur:
// The content lets go of every key itself.
h.held, h.gone = h.held[:0], h.gone[:0]
}
return true
}
// Lift sends the release of the key held at physical (or key, where its
// position is unknown), whose own release the system keeps from the
// window, as an input method takes it, through send, which tracks it. It
// does nothing when no such key is held.
func (h *HeldKeys) Lift(key, physical Key, mods Modifiers, send func(SurfaceEvent) bool) {
i := indexKey(h.held, key, physical)
if i < 0 {
return
}
k := h.held[i]
send(SurfaceEvent{Kind: KeyReleased, Key: k.key, PhysicalKey: k.physical, Code: k.code, Mods: mods})
// send tracked the release, unless the window closed.
if i := indexKey(h.held, k.key, k.physical); i >= 0 {
h.held = append(h.held[:i], h.held[i+1:]...)
}
h.gone = append(h.gone, k)
}
// LiftAll sends the releases of every key held through send, as a menu's
// loop takes the keyboard.
func (h *HeldKeys) LiftAll(mods Modifiers, send func(SurfaceEvent) bool) {
for len(h.held) > 0 {
k := h.held[0]
h.Lift(k.key, k.physical, mods, send)
}
}