Files
Commilitia-Drop/desktop/platform/clipboard_darwin.go
T
admin f21fa5b5e8 cdrop — 跨 OS 剪贴板与文件传输服务
Commilitia Drop:自托管的跨设备剪贴板同步与点对点文件传输。

- 后端 Go(chi / SQLite WAL / SSE Hub / WebRTC signaling + 状态机 / Relay ring buffer),编译进单个 distroless 镜像(前端 go:embed)。
- 前端 React + TanStack Router + Zustand,自实现 SSE + WebRTC P2P,NAT 受阻时回退服务端中继;聚珍(Juzhen)CJK 综合排版。
- 桌面端 Wails v2(macOS / Windows),瘦客户端复用 web。
- 鉴权 OIDC PKCE(自建 Casdoor 等),refresh_token 信封加密存系统密钥库;iOS Shortcut 用 HS256 scoped token。

架构文档与变更记录见 docs 分支(PROJECT_BRIEF / FRONTEND_DESIGN / CHANGELOG)。

本次为公开发布初始提交:完整开发历史(含部署细节)留存于私有归档,公开仓库自此干净起步。
2026-06-15 21:38:28 +08:00

81 lines
1.9 KiB
Go

//go:build darwin
package platform
/*
#cgo darwin CFLAGS: -x objective-c -fobjc-arc
#cgo darwin LDFLAGS: -framework Cocoa
#include <stdlib.h>
long cdropPasteboardChangeCount(void);
char *cdropPasteboardReadText(int *sensitive);
void cdropPasteboardWriteText(const char *text);
*/
import "C"
import (
"context"
"time"
"unsafe"
)
// pasteboardPollInterval is how often the watcher samples the change counter.
// 400ms keeps copies feeling instant without busy-polling.
const pasteboardPollInterval = 400 * time.Millisecond
// darwinClipboard is the macOS Source: it polls NSPasteboard's changeCount for
// local copies and writes plain text back. NSPasteboard read/write off the main
// thread is the same approach the common clipboard libraries take.
type darwinClipboard struct {
poll time.Duration
}
// NewClipboardSource returns the platform clipboard Source (macOS).
func NewClipboardSource() Source {
return &darwinClipboard{poll: pasteboardPollInterval}
}
func (d *darwinClipboard) Watch(ctx context.Context) <-chan ClipboardEvent {
ch := make(chan ClipboardEvent)
go func() {
defer close(ch)
last := C.cdropPasteboardChangeCount()
ticker := time.NewTicker(d.poll)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
cc := C.cdropPasteboardChangeCount()
if cc == last {
continue
}
last = cc
var sensitive C.int
cstr := C.cdropPasteboardReadText(&sensitive)
if cstr == nil {
continue // non-text payload (image / file) — ignore
}
text := C.GoString(cstr)
C.free(unsafe.Pointer(cstr))
select {
case ch <- ClipboardEvent{Text: text, Sensitive: sensitive != 0}:
case <-ctx.Done():
return
}
}
}
}()
return ch
}
func (d *darwinClipboard) Write(text string) error {
c := C.CString(text)
defer C.free(unsafe.Pointer(c))
C.cdropPasteboardWriteText(c)
return nil
}