mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 10:23:03 +08:00
[linux] Devtools + dispatch.
This commit is contained in:
parent
5f0352404b
commit
7eb8e6456e
@ -8,10 +8,19 @@ package linux
|
|||||||
|
|
||||||
#include "gtk/gtk.h"
|
#include "gtk/gtk.h"
|
||||||
|
|
||||||
|
extern void callDispatchedMethod(int id);
|
||||||
|
|
||||||
|
static inline void processDispatchID(gpointer id) {
|
||||||
|
callDispatchedMethod(GPOINTER_TO_INT(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void gtkDispatch(int id) {
|
||||||
|
gdk_threads_add_idle((GSourceFunc)processDispatchID, GINT_TO_POINTER(id));
|
||||||
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/wailsapp/wails/v2/internal/binding"
|
"github.com/wailsapp/wails/v2/internal/binding"
|
||||||
@ -19,11 +28,10 @@ import (
|
|||||||
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
"github.com/wailsapp/wails/v2/internal/logger"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options"
|
"github.com/wailsapp/wails/v2/pkg/options"
|
||||||
"github.com/xyproto/xpm"
|
|
||||||
"image/png"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,31 +56,9 @@ type Frontend struct {
|
|||||||
servingFromDisk bool
|
servingFromDisk bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func png2XPM(pngData []byte) string {
|
|
||||||
|
|
||||||
// Create a new XPM encoder
|
|
||||||
enc := xpm.NewEncoder("icon")
|
|
||||||
|
|
||||||
// Open the PNG file
|
|
||||||
m, err := png.Decode(bytes.NewReader(pngData))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
// Generate and output the XPM data
|
|
||||||
err = enc.Encode(&buf, m)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return buf.String()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend {
|
func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend {
|
||||||
|
|
||||||
// Set GDK_BACKEND=x11
|
// Set GDK_BACKEND=x11 to prevent warnings
|
||||||
os.Setenv("GDK_BACKEND", "x11")
|
os.Setenv("GDK_BACKEND", "x11")
|
||||||
|
|
||||||
result := &Frontend{
|
result := &Frontend{
|
||||||
@ -398,9 +384,19 @@ func (f *Frontend) startDrag() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *Frontend) ExecJS(js string) {
|
func (f *Frontend) ExecJS(js string) {
|
||||||
//f.gtkWindow.Dispatch(func() {
|
f.dispatch(func() {
|
||||||
// f.chromium.Eval(js)
|
f.mainWindow.ExecJS(js)
|
||||||
//})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Frontend) dispatch(fn func()) {
|
||||||
|
dispatchCallbackLock.Lock()
|
||||||
|
id := 0
|
||||||
|
for fn := dispatchCallbacks[id]; fn != nil; id++ {
|
||||||
|
}
|
||||||
|
dispatchCallbacks[id] = fn
|
||||||
|
dispatchCallbackLock.Unlock()
|
||||||
|
C.gtkDispatch(C.int(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) {
|
//func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) {
|
||||||
@ -424,3 +420,21 @@ func processMessage(message *C.char) {
|
|||||||
goMessage := C.GoString(message)
|
goMessage := C.GoString(message)
|
||||||
messageBuffer <- goMessage
|
messageBuffer <- goMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map of functions passed to dispatch()
|
||||||
|
var dispatchCallbacks = make(map[int]func())
|
||||||
|
var dispatchCallbackLock sync.Mutex
|
||||||
|
|
||||||
|
//export callDispatchedMethod
|
||||||
|
func callDispatchedMethod(cid C.int) {
|
||||||
|
id := int(cid)
|
||||||
|
fn := dispatchCallbacks[id]
|
||||||
|
if fn != nil {
|
||||||
|
go fn()
|
||||||
|
dispatchCallbackLock.Lock()
|
||||||
|
delete(dispatchCallbacks, id)
|
||||||
|
dispatchCallbackLock.Unlock()
|
||||||
|
} else {
|
||||||
|
println("Error: No dispatch method with id", id, cid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -111,6 +111,12 @@ GtkWidget* setupWebview(void* contentManager, GtkWindow* window) {
|
|||||||
return webview;
|
return webview;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void devtoolsEnabled(void* webview, int enabled) {
|
||||||
|
WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webview));
|
||||||
|
gboolean genabled = enabled == 1 ? true : false;
|
||||||
|
webkit_settings_set_enable_developer_extras(settings, genabled);
|
||||||
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
@ -153,6 +159,10 @@ func NewWindow(appoptions *options.App, debug bool) *Window {
|
|||||||
webview := C.setupWebview(result.contentManager, result.asGTKWindow())
|
webview := C.setupWebview(result.contentManager, result.asGTKWindow())
|
||||||
result.webview = unsafe.Pointer(webview)
|
result.webview = unsafe.Pointer(webview)
|
||||||
|
|
||||||
|
if debug {
|
||||||
|
C.devtoolsEnabled(unsafe.Pointer(webview), C.int(1))
|
||||||
|
}
|
||||||
|
|
||||||
// Setup window
|
// Setup window
|
||||||
result.SetKeepAbove(appoptions.AlwaysOnTop)
|
result.SetKeepAbove(appoptions.AlwaysOnTop)
|
||||||
result.SetResizable(!appoptions.DisableResize)
|
result.SetResizable(!appoptions.DisableResize)
|
||||||
@ -314,3 +324,9 @@ func (w *Window) SetTitle(title string) {
|
|||||||
defer C.free(unsafe.Pointer(cTitle))
|
defer C.free(unsafe.Pointer(cTitle))
|
||||||
C.gtk_window_set_title(w.asGTKWindow(), cTitle)
|
C.gtk_window_set_title(w.asGTKWindow(), cTitle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Window) ExecJS(js string) {
|
||||||
|
script := C.CString(js)
|
||||||
|
defer C.free(unsafe.Pointer(script))
|
||||||
|
C.webkit_web_view_run_javascript((*C.WebKitWebView)(w.webview), script, nil, nil, nil)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user