/
clipboard.go
75 lines (64 loc) · 1.57 KB
/
clipboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//go:build !js && !wasm && !test_web_driver
// +build !js,!wasm,!test_web_driver
package glfw
import (
"runtime"
"time"
"fyne.io/fyne/v2"
"github.com/go-gl/glfw/v3.3/glfw"
)
// Declare conformity with Clipboard interface
var _ fyne.Clipboard = (*clipboard)(nil)
// clipboard represents the system clipboard
type clipboard struct {
window *glfw.Window
}
// Content returns the clipboard content
func (c *clipboard) Content() string {
// This retry logic is to work around the "Access Denied" error often thrown in windows PR#1679
if runtime.GOOS != "windows" {
return c.content()
}
for i := 3; i > 0; i-- {
cb := c.content()
if cb != "" {
return cb
}
time.Sleep(50 * time.Millisecond)
}
//can't log retry as it would alos log errors for an empty clipboard
return ""
}
func (c *clipboard) content() string {
content := ""
runOnMain(func() {
content = glfw.GetClipboardString()
})
return content
}
// SetContent sets the clipboard content
func (c *clipboard) SetContent(content string) {
// This retry logic is to work around the "Access Denied" error often thrown in windows PR#1679
if runtime.GOOS != "windows" {
c.setContent(content)
return
}
for i := 3; i > 0; i-- {
c.setContent(content)
if c.content() == content {
return
}
time.Sleep(50 * time.Millisecond)
}
fyne.LogError("GLFW clipboard set failed", nil)
}
func (c *clipboard) setContent(content string) {
runOnMain(func() {
defer func() {
if r := recover(); r != nil {
fyne.LogError("GLFW clipboard error (details above)", nil)
}
}()
glfw.SetClipboardString(content)
})
}