mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-11 22:49:29 +08:00
[linux] support clipboard
This commit is contained in:
parent
aea0db5919
commit
9d615463f4
@ -16,9 +16,11 @@ func newClipboard() *Clipboard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Clipboard) SetText(text string) bool {
|
func (c *Clipboard) SetText(text string) bool {
|
||||||
return c.impl.setText(text)
|
return InvokeSyncWithResult(func() bool {
|
||||||
|
return c.impl.setText(text)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Clipboard) Text() (string, bool) {
|
func (c *Clipboard) Text() (string, bool) {
|
||||||
return c.impl.text()
|
return InvokeSyncWithResultAndOther(c.impl.text)
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,29 @@
|
|||||||
|
|
||||||
package application
|
package application
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo linux pkg-config: gtk+-3.0 webkit2gtk-4.0
|
||||||
|
|
||||||
|
#include "gtk/gtk.h"
|
||||||
|
#include "webkit2/webkit2.h"
|
||||||
|
|
||||||
|
static gchar* getClipboardText() {
|
||||||
|
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
|
||||||
|
return gtk_clipboard_wait_for_text(clip);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setClipboardText(gchar* text) {
|
||||||
|
GtkClipboard *clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
|
||||||
|
gtk_clipboard_set_text(clip, text, -1);
|
||||||
|
|
||||||
|
clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
|
||||||
|
gtk_clipboard_set_text(clip, text, -1);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
var clipboardLock sync.RWMutex
|
var clipboardLock sync.RWMutex
|
||||||
@ -13,19 +34,18 @@ type linuxClipboard struct{}
|
|||||||
func (m linuxClipboard) setText(text string) bool {
|
func (m linuxClipboard) setText(text string) bool {
|
||||||
clipboardLock.Lock()
|
clipboardLock.Lock()
|
||||||
defer clipboardLock.Unlock()
|
defer clipboardLock.Unlock()
|
||||||
// cText := C.CString(text)
|
cText := C.CString(text)
|
||||||
// success := C.setClipboardText(cText)
|
C.setClipboardText(cText)
|
||||||
// C.free(unsafe.Pointer(cText))
|
C.free(unsafe.Pointer(cText))
|
||||||
success := false
|
return true
|
||||||
return bool(success)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m linuxClipboard) text() (string, bool) {
|
func (m linuxClipboard) text() (string, bool) {
|
||||||
clipboardLock.RLock()
|
clipboardLock.RLock()
|
||||||
defer clipboardLock.RUnlock()
|
defer clipboardLock.RUnlock()
|
||||||
// clipboardText := C.getClipboardText()
|
clipboardText := C.getClipboardText()
|
||||||
// result := C.GoString(clipboardText)
|
result := C.GoString(clipboardText)
|
||||||
return "", false
|
return result, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func newClipboardImpl() *linuxClipboard {
|
func newClipboardImpl() *linuxClipboard {
|
||||||
|
@ -119,7 +119,7 @@ static void* gtkFileChooserDialogNew(char* title, GtkWindow* window, GtkFileChoo
|
|||||||
GTK_RESPONSE_CANCEL,
|
GTK_RESPONSE_CANCEL,
|
||||||
acceptLabel,
|
acceptLabel,
|
||||||
GTK_RESPONSE_ACCEPT,
|
GTK_RESPONSE_ACCEPT,
|
||||||
0);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct Screen {
|
typedef struct Screen {
|
||||||
|
@ -67,6 +67,18 @@ func InvokeSyncWithResultAndError[T any](fn func() (T, error)) (res T, err error
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InvokeSyncWithResultAndOther[T any, U any](fn func() (T, U)) (res T, other U) {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
globalApplication.dispatchOnMainThread(func() {
|
||||||
|
defer processPanicHandlerRecover()
|
||||||
|
res, other = fn()
|
||||||
|
wg.Done()
|
||||||
|
})
|
||||||
|
wg.Wait()
|
||||||
|
return res, other
|
||||||
|
}
|
||||||
|
|
||||||
func InvokeAsync(fn func()) {
|
func InvokeAsync(fn func()) {
|
||||||
globalApplication.dispatchOnMainThread(func() {
|
globalApplication.dispatchOnMainThread(func() {
|
||||||
defer processPanicHandlerRecover()
|
defer processPanicHandlerRecover()
|
||||||
|
Loading…
Reference in New Issue
Block a user