From 6e30c6770ba9d4993a3e885fc513620459b79e35 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 20 Jan 2022 21:46:19 +1100 Subject: [PATCH] Tidy up and slight refactor --- v2/internal/frontend/desktop/linux/dialog.go | 9 +----- .../frontend/desktop/linux/frontend.go | 4 +-- v2/internal/frontend/desktop/linux/window.go | 32 ++++++++++++------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/v2/internal/frontend/desktop/linux/dialog.go b/v2/internal/frontend/desktop/linux/dialog.go index 7227410b4..b53d1dddc 100644 --- a/v2/internal/frontend/desktop/linux/dialog.go +++ b/v2/internal/frontend/desktop/linux/dialog.go @@ -11,15 +11,8 @@ import "C" var openFileResults = make(chan string) func (f *Frontend) OpenFileDialog(dialogOptions frontend.OpenDialogOptions) (result string, err error) { - - f.dispatch(func() { - println("Before OpenFileDialog") - f.mainWindow.OpenFileDialog(dialogOptions) - println("After OpenFileDialog") - }) - println("Waiting for result") + f.mainWindow.OpenFileDialog(dialogOptions) result = <-openFileResults - println("Got result") return } diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 0161c87f3..02050f2c9 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -16,7 +16,7 @@ static inline void processDispatchID(gpointer id) { } static void gtkDispatch(int id) { - gdk_threads_add_idle((GSourceFunc)processDispatchID, GINT_TO_POINTER(id)); + g_idle_add((GSourceFunc)processDispatchID, GINT_TO_POINTER(id)); } */ @@ -63,7 +63,6 @@ type Frontend struct { func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings, dispatcher frontend.Dispatcher) *Frontend { - println("[NewFrontend] PID:", os.Getpid()) // Set GDK_BACKEND=x11 to prevent warnings os.Setenv("GDK_BACKEND", "x11") @@ -303,7 +302,6 @@ var dispatchCallbackLock sync.Mutex //export callDispatchedMethod func callDispatchedMethod(cid C.int) { - println("[callDispatchedMethod] PID:", os.Getpid()) id := int(cid) fn := dispatchCallbacks[id] if fn != nil { diff --git a/v2/internal/frontend/desktop/linux/window.go b/v2/internal/frontend/desktop/linux/window.go index 463f04435..31e063202 100644 --- a/v2/internal/frontend/desktop/linux/window.go +++ b/v2/internal/frontend/desktop/linux/window.go @@ -196,15 +196,23 @@ int executeJS(gpointer data) { return G_SOURCE_REMOVE; } -void ExecuteOnMainThread(void* f, JSCallback* jscallback) { +void ExecuteOnMainThread(void* f, gpointer jscallback) { g_idle_add((GSourceFunc)f, (gpointer)jscallback); } void extern processOpenFileResult(char*); + +typedef struct OpenFileDialogOptions { + void* webview; + char* title; + char** filters; +} OpenFileDialogOptions; + + int opendialog(gpointer data) { - struct JSCallback *js = data; - GtkWidget *dlg = gtk_file_chooser_dialog_new(js->script, js->webview, GTK_FILE_CHOOSER_ACTION_OPEN, + struct OpenFileDialogOptions *options = data; + GtkWidget *dlg = gtk_file_chooser_dialog_new(options->title, options->webview, GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL); @@ -215,9 +223,11 @@ int opendialog(gpointer data) { gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg)); processOpenFileResult(filename); g_free(filename); - } + } else { + processOpenFileResult(""); + } gtk_widget_destroy(dlg); - free(js->script); + free(options->title); return G_SOURCE_REMOVE; } @@ -437,9 +447,9 @@ func (w *Window) SetTitle(title string) { func (w *Window) ExecJS(js string) { jscallback := C.JSCallback{ webview: w.webview, - script: C.CString(js), + script: C.CString(js), } - C.ExecuteOnMainThread(C.executeJS, &jscallback) + C.ExecuteOnMainThread(C.executeJS, C.gpointer(&jscallback)) } func (w *Window) StartDrag() { @@ -451,10 +461,10 @@ func (w *Window) Quit() { } func (w *Window) OpenFileDialog(dialogOptions frontend.OpenDialogOptions) { - data := C.JSCallback{ + data := C.OpenFileDialogOptions{ webview: w.webview, - script: C.CString(dialogOptions.Title), + title: C.CString(dialogOptions.Title), } - C.ExecuteOnMainThread(C.opendialog, &data) + // TODO: Filter + C.ExecuteOnMainThread(C.opendialog, C.gpointer(&data)) } -