Tidy up and slight refactor

This commit is contained in:
Lea Anthony
2022-01-25 16:27:39 -06:00
committed by Travis McLane
parent a2d447aecf
commit 6e30c6770b
3 changed files with 23 additions and 22 deletions
+1 -8
View File
@@ -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
}
@@ -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 {
+21 -11
View File
@@ -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))
}