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.
141 lines
3.2 KiB
Go
141 lines
3.2 KiB
Go
package mygo
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// mainLoop is a FIFO of functions waiting to run on the main thread. The
|
|
// backend drains it (AppHandler.Dispatch) whenever it has been signaled.
|
|
type mainLoop struct {
|
|
mu sync.Mutex
|
|
queue []func()
|
|
closed bool
|
|
}
|
|
|
|
var loop mainLoop
|
|
|
|
// post schedules fn on the main thread. It reports false when the event
|
|
// loop has already shut down and fn will never run.
|
|
func (l *mainLoop) post(fn func()) bool {
|
|
l.mu.Lock()
|
|
if l.closed {
|
|
l.mu.Unlock()
|
|
return false
|
|
}
|
|
l.queue = append(l.queue, fn)
|
|
first := len(l.queue) == 1
|
|
l.mu.Unlock()
|
|
if first {
|
|
backend().Signal()
|
|
}
|
|
return true
|
|
}
|
|
|
|
// drain runs queued functions, one at a time, until the queue is empty.
|
|
// Main thread only. A function running a loop of its own, as a context
|
|
// menu, a modal dialog or await does, runs those queued behind it, which
|
|
// it may be waiting for: the backend is signaled again while some wait.
|
|
func (l *mainLoop) drain() {
|
|
for {
|
|
l.mu.Lock()
|
|
if len(l.queue) == 0 {
|
|
l.mu.Unlock()
|
|
return
|
|
}
|
|
fn := l.queue[0]
|
|
l.queue[0] = nil
|
|
l.queue = l.queue[1:]
|
|
more := len(l.queue) > 0
|
|
l.mu.Unlock()
|
|
if more {
|
|
backend().Signal()
|
|
}
|
|
fn()
|
|
}
|
|
}
|
|
|
|
// stopped reports whether the loop has shut down.
|
|
func (l *mainLoop) stopped() bool {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
return l.closed
|
|
}
|
|
|
|
// shutdown runs whatever is still queued and rejects later posts. The
|
|
// queue is closed in the same critical section that finds it empty, so no
|
|
// post is accepted without running.
|
|
func (l *mainLoop) shutdown() {
|
|
for {
|
|
l.drain()
|
|
l.mu.Lock()
|
|
if len(l.queue) == 0 {
|
|
l.closed = true
|
|
l.mu.Unlock()
|
|
return
|
|
}
|
|
l.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
func isMainThread() bool { return backend().IsMainThread() }
|
|
|
|
// RunOnMain runs fn on the main (UI) thread and waits for it to return.
|
|
// Use it to call native APIs directly, e.g. through Window.NativeHandle,
|
|
// or to read and change the state a window's Content shows without
|
|
// the redraw that Window.Update makes.
|
|
// It runs fn right away when called on the main thread, and drops it after
|
|
// the application has quit.
|
|
func RunOnMain(fn func()) { onMain(fn) }
|
|
|
|
// postMain runs fn asynchronously on the main thread.
|
|
func postMain(fn func()) bool { return loop.post(fn) }
|
|
|
|
// onMain runs fn on the main thread and waits for it to return. It runs fn
|
|
// directly when called on the main thread. After the event loop has shut
|
|
// down fn is dropped.
|
|
func onMain(fn func()) {
|
|
if isMainThread() {
|
|
fn()
|
|
return
|
|
}
|
|
done := make(chan struct{})
|
|
if !loop.post(func() {
|
|
defer close(done)
|
|
fn()
|
|
}) {
|
|
return
|
|
}
|
|
<-done
|
|
}
|
|
|
|
// onMainValue is onMain for functions returning a value.
|
|
func onMainValue[T any](fn func() T) T {
|
|
var v T
|
|
onMain(func() { v = fn() })
|
|
return v
|
|
}
|
|
|
|
// await blocks until a value arrives on ch. On the main thread it keeps
|
|
// processing native events so the UI stays responsive and the value can
|
|
// actually be produced. Whoever sends on ch must call wake afterwards.
|
|
func await[T any](ch <-chan T) T {
|
|
if !isMainThread() {
|
|
return <-ch
|
|
}
|
|
for {
|
|
select {
|
|
case v := <-ch:
|
|
return v
|
|
default:
|
|
}
|
|
backend().Step()
|
|
}
|
|
}
|
|
|
|
// deliver sends v on a buffered channel consumed by await and wakes the main
|
|
// thread in case it is waiting.
|
|
func deliver[T any](ch chan<- T, v T) {
|
|
ch <- v
|
|
backend().Wake()
|
|
}
|