clipboard.go 1.6 KB

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