64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package keepassgo
|
|
|
|
import (
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
|
|
gioclipboard "gioui.org/io/clipboard"
|
|
"gioui.org/layout"
|
|
|
|
appclipboard "git.julianfamily.org/keepassgo/clipboard"
|
|
)
|
|
|
|
type clipboardCommandWriter struct {
|
|
mu sync.Mutex
|
|
pending []string
|
|
invalidate func()
|
|
}
|
|
|
|
func newPlatformClipboardWriter(goos string, invalidate func()) appclipboard.Writer {
|
|
if strings.EqualFold(goos, "android") {
|
|
return &clipboardCommandWriter{invalidate: invalidate}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func processClipboardWrites(gtx layout.Context, writer appclipboard.Writer) {
|
|
commandWriter, ok := writer.(*clipboardCommandWriter)
|
|
if !ok {
|
|
return
|
|
}
|
|
commandWriter.Process(gtx)
|
|
}
|
|
|
|
func (w *clipboardCommandWriter) WriteText(text string) error {
|
|
w.mu.Lock()
|
|
w.pending = append(w.pending, text)
|
|
w.mu.Unlock()
|
|
if w.invalidate != nil {
|
|
w.invalidate()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *clipboardCommandWriter) Process(gtx layout.Context) {
|
|
for _, text := range w.drain() {
|
|
gtx.Execute(gioclipboard.WriteCmd{
|
|
Type: "application/text",
|
|
Data: io.NopCloser(strings.NewReader(text)),
|
|
})
|
|
}
|
|
}
|
|
|
|
func (w *clipboardCommandWriter) drain() []string {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if len(w.pending) == 0 {
|
|
return nil
|
|
}
|
|
pending := append([]string(nil), w.pending...)
|
|
w.pending = nil
|
|
return pending
|
|
}
|