Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move clipboard handling away from window #19

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions browser_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,30 +964,10 @@ func DefaultWindowHints() {
}

func (w *Window) SetClipboardString(str string) {
// Set the clipboard content from the input str
js.Global().Get("navigator").Get("clipboard").Call("writeText", str)
SetClipboardString(str)
}
func (w *Window) GetClipboardString() (string, error) {
// Get the clipboard object.
clipboard := js.Global().Get("navigator").Get("clipboard")
clipboardChan := make(chan js.Value)
// Call the `readText()` function and send the result to the channel
clipboard.Call("readText").Call("then", js.FuncOf(func(this js.Value, p []js.Value) interface{} {
clipboardContent := p[0]
clipboardChan <- clipboardContent
return nil
})).Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
clipboardChan <- js.ValueOf(nil)
return nil
}))
// Get the js.Value of the clipboard text from the channel
result := <-clipboardChan
if result.Truthy() {
// Convert the value to a string and return the value
text := result.String()
return text, nil
}
return "", errors.New("Failed to get clipboard text")
func (w *Window) GetClipboardString() string {
return GetClipboardString()
}

func (w *Window) SetTitle(title string) {
Expand Down
19 changes: 19 additions & 0 deletions clipboard_glfw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !wasm

package glfw

import "github.com/go-gl/glfw/v3.3/glfw"

// GetClipboardString returns the contents of the system clipboard, if it contains or is convertible to a UTF-8 encoded string.
//
// This function may only be called from the main thread.
func GetClipboardString() string {
return glfw.GetClipboardString()
}

// SetClipboardString sets the system clipboard to the specified UTF-8 encoded string.
//
// This function may only be called from the main thread.
func SetClipboardString(text string) {
glfw.SetClipboardString(text)
}
38 changes: 38 additions & 0 deletions clipboard_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build wasm

package glfw

import (
"syscall/js"
)

// GetClipboardString returns the contents of the system clipboard, if it contains or is convertible to a UTF-8 encoded string.
//
// This function may only be called from the main thread.
func GetClipboardString() string {
text := make(chan string)

clipboard := js.Global().Get("navigator").Get("clipboard")
clipboard.Call("readText").Call("then", js.FuncOf(func(this js.Value, p []js.Value) interface{} {
content := p[0]
if !content.Truthy() {
text <- ""
return nil
}

text <- content.String()
return nil
})).Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
text <- ""
return nil
}))

return <-text
}

// SetClipboardString sets the system clipboard to the specified UTF-8 encoded string.
//
// This function may only be called from the main thread.
func SetClipboardString(str string) {
js.Global().Get("navigator").Get("clipboard").Call("writeText", str)
}
Loading