github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/linux/clipboard.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package linux
     5  
     6  /*
     7  #cgo linux pkg-config: gtk+-3.0 webkit2gtk-4.0
     8  
     9  #include "gtk/gtk.h"
    10  #include "webkit2/webkit2.h"
    11  
    12  static gchar* GetClipboardText() {
    13  	GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    14  	return gtk_clipboard_wait_for_text(clip);
    15  }
    16  
    17  static void SetClipboardText(gchar* text) {
    18  	GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    19  	gtk_clipboard_set_text(clip, text, -1);
    20  
    21  	clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
    22  	gtk_clipboard_set_text(clip, text, -1);
    23  }
    24  */
    25  import "C"
    26  import "sync"
    27  
    28  func (f *Frontend) ClipboardGetText() (string, error) {
    29  	var text string
    30  	var wg sync.WaitGroup
    31  	wg.Add(1)
    32  	invokeOnMainThread(func() {
    33  		ctxt := C.GetClipboardText()
    34  		defer C.g_free(C.gpointer(ctxt))
    35  		text = C.GoString(ctxt)
    36  		wg.Done()
    37  	})
    38  	wg.Wait()
    39  	return text, nil
    40  }
    41  
    42  func (f *Frontend) ClipboardSetText(text string) error {
    43  	invokeOnMainThread(func() {
    44  		ctxt := (*C.gchar)(C.CString(text))
    45  		defer C.g_free(C.gpointer(ctxt))
    46  		C.SetClipboardText(ctxt)
    47  	})
    48  	return nil
    49  }