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

63 lines
1.8 KiB
Go

//go:build darwin && !ios
package darwin
import (
"sync"
"github.com/ebitengine/purego"
"github.com/ebitengine/purego/objc"
)
var macClipboardWatch struct {
once sync.Once
timer id
change int
notify func()
newTimer func(id, objc.SEL, float64, id, objc.SEL, id, bool) id
}
// AppKit has no public pasteboard-change notification. Poll only its change
// count while asked, never its types/data (which can invoke lazy providers).
func (clipboard) WatchChanges(changed func()) (func(), error) {
w := &macClipboardWatch
withPool(func() {
w.once.Do(func() {
purego.RegisterFunc(&w.newTimer, msgSendAddr)
classDef("MyGoClipboardObserver", "NSObject", nil, []objc.MethodDef{
method("clipboardTick:", func(_ id, _ objc.SEL, timer id) {
if timer != w.timer || w.notify == nil {
return
}
withPool(func() {
change := sendInt(pasteboard(), "changeCount")
if change != w.change {
w.change = change
w.notify()
}
})
}),
})
})
w.change = sendInt(pasteboard(), "changeCount")
w.notify = changed
observer := alloc("MyGoClipboardObserver")
w.timer = retain(w.newTimer(class("NSTimer"), sel("timerWithTimeInterval:target:selector:userInfo:repeats:"), .25, observer, sel("clipboardTick:"), 0, true))
release(observer) // NSTimer retains its target until invalidated.
// A tenth of the interval, as Apple asks of repeating timers, lets
// the system fire it with its other wakeups: nobody notices a
// change 25 ms later, and the app idles in fewer of them.
msgSetFloat(w.timer, sel("setTolerance:"), .025)
send(send(class("NSRunLoop"), "mainRunLoop"), "addTimer:forMode:", uintptr(w.timer), kCFRunLoopCommonModes)
})
timer := w.timer
return func() {
if w.timer != timer {
return
}
w.timer, w.notify = 0, nil
send(timer, "invalidate")
release(timer)
}, nil
}