Files
openchamber-android-native/third_party/mygo/ui/actions.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

152 lines
4.2 KiB
Go

package ui
type actionKind uint8
const (
actionClick actionKind = iota
actionChange
actionSubmit
actionShortcut
actionWindowShortcut
)
type action struct {
element Element
handle *Handle
window *Context
kind actionKind
mods Modifiers
key Key
fn func()
ready bool
}
func (e Element) on(kind actionKind, mods Modifiers, key Key, fn func()) Element {
if n := e.node(); n != nil && fn != nil {
if kind == actionClick {
n.flags |= flagClickable
}
n.c.rt.actions = append(n.c.rt.actions, action{element: e, kind: kind, mods: mods, key: key, fn: fn})
}
return e
}
// OnClick runs after construction and bound-value input.
func (e Element) OnClick(fn func()) Element { return e.on(actionClick, 0, 0, fn) }
// OnChange runs after input changes the bound value, before rebuilding the
// view. The callback can commit a derived value local to this build pass.
func (e Element) OnChange(fn func()) Element { return e.on(actionChange, 0, 0, fn) }
// OnSubmit runs after bound-value input handles a submission gesture,
// before rebuilding the view.
func (e Element) OnSubmit(fn func()) Element { return e.on(actionSubmit, 0, 0, fn) }
// OnShortcut declares a key handler within the element's focus subtree.
func (e Element) OnShortcut(mods Modifiers, key Key, fn func()) Element {
return e.on(actionShortcut, mods, key, fn)
}
// OnShortcut declares a handler in the current context's parent scope:
// the window's, as OnWindowShortcut, at the root, else the parent
// element's focus subtree, as Element.OnShortcut.
func (c *Context) OnShortcut(mods Modifiers, key Key, fn func()) {
if raw := c.build(); raw != nil && fn != nil {
if raw.parent != raw.root {
wrapElement(raw.parent).OnShortcut(mods, key, fn)
return
}
c.OnWindowShortcut(mods, key, fn)
}
}
// OnWindowShortcut declares a handler of the window's shortcut mods+key,
// which runs when c.Shortcut would report it, wherever c is in the tree:
// a component built deep in the view hears its keys while the focus is
// anywhere else, or nowhere, as one built at the root does. Elements
// around the focus that take the key, and overlays, still come first;
// one built in a dialog fires while the dialog shows, and one built
// behind it waits, as Shortcut does.
func (c *Context) OnWindowShortcut(mods Modifiers, key Key, fn func()) {
if raw := c.build(); raw != nil && fn != nil && raw.insideModal() && !raw.inert {
raw.rt.actions = append(raw.rt.actions, action{window: c, kind: actionWindowShortcut, mods: mods, key: key, fn: fn})
}
}
func (rt *engine) actionNode(a action) *node {
if a.handle != nil {
b := a.handle.binding(rt, false)
if b == nil || b.closed {
return nil
}
return b.element.lookup()
}
return a.element.nodeFor(rt)
}
func (rt *engine) runNoticeActions() {
// Select every observer before marking notices delivered, so several
// callbacks on one element all run once for the same input.
for i := range rt.actions {
a := &rt.actions[i]
a.ready = false
if rt.closing {
return
}
if a.kind != actionChange && a.kind != actionSubmit {
continue
}
n := rt.actionNode(*a)
if n == nil || n.disabled() {
continue
}
a.ready = a.kind == actionChange && n.st.changed && !n.st.changeDelivered ||
a.kind == actionSubmit && n.st.submitted && !n.st.submitDelivered
}
for _, a := range rt.actions {
if !a.ready {
continue
}
n := rt.actionNode(a)
if a.kind == actionChange {
n.st.changeDelivered = true
} else {
n.st.submitDelivered = true
}
}
for _, a := range rt.actions {
if rt.closing {
return
}
if a.ready {
rt.consumed = true
a.fn()
}
}
}
func (rt *engine) runActions() {
for _, a := range rt.actions {
if rt.closing {
return
}
if a.kind == actionChange || a.kind == actionSubmit {
continue
}
handled := false
if a.kind == actionWindowShortcut {
// Whether the dialog on top lets it through was decided as it
// was declared, where it was built (OnWindowShortcut).
handled = rt.shortcut(0, a.mods, a.key)
} else if n := rt.actionNode(a); n != nil && !n.disabled() {
switch a.kind {
case actionClick:
handled = n.Clicked()
case actionShortcut:
handled = n.Shortcut(a.mods, a.key)
}
}
if handled {
rt.consumed = true
a.fn()
}
}
}