diff --git a/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.js b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.js index db404e459..5ceb7ed1d 100644 --- a/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.js +++ b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.js @@ -16,8 +16,7 @@ window.greet = function () { }; nameElement.onkeydown = function (e) { - console.log(e) - if (e.keyCode == 13) { - window.greet() + if (e.code === "Enter") { + window.greet(); } -} \ No newline at end of file +}; diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 1c96abe57..7d57e9a88 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -39,7 +39,7 @@ void ExecJS(void* ctx, const char*); void Quit(void*); const char* GetSize(void *ctx); -const char* GetPos(void *ctx); +const char* GetPosition(void *ctx); void ProcessURLResponse(void *inctx, const char *url, int statusCode, const char *contentType, void* data, int datalength); diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index 14932c319..ae7d9d67f 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -163,7 +163,7 @@ const char* GetSize(void *inctx) { return [result UTF8String]; } -const char* GetPos(void *inctx) { +const char* GetPosition(void *inctx) { WailsContext *ctx = (__bridge WailsContext*) inctx; NSScreen* screen = [ctx getCurrentScreen]; NSRect windowFrame = [ctx.mainWindow frame]; diff --git a/v2/internal/frontend/desktop/darwin/frontend.go b/v2/internal/frontend/desktop/darwin/frontend.go index 6b98f30e1..4787e2afe 100644 --- a/v2/internal/frontend/desktop/darwin/frontend.go +++ b/v2/internal/frontend/desktop/darwin/frontend.go @@ -144,11 +144,11 @@ func (f *Frontend) WindowCenter() { f.mainWindow.Center() } -func (f *Frontend) WindowSetPos(x, y int) { - f.mainWindow.SetPos(x, y) +func (f *Frontend) WindowSetPosition(x, y int) { + f.mainWindow.SetPosition(x, y) } -func (f *Frontend) WindowGetPos() (int, int) { - return f.mainWindow.Pos() +func (f *Frontend) WindowGetPosition() (int, int) { + return f.mainWindow.GetPosition() } func (f *Frontend) WindowSetSize(width, height int) { diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index ec18e3f3c..360a5389c 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -137,7 +137,7 @@ func (w *Window) ExecJS(js string) { C.free(unsafe.Pointer(_js)) } -func (w *Window) SetPos(x int, y int) { +func (w *Window) SetPosition(x int, y int) { C.SetPosition(w.context, C.int(x), C.int(y)) } @@ -204,8 +204,8 @@ func parseIntDuo(temp string) (int, int) { return x, y } -func (w *Window) Pos() (int, int) { - var _result *C.char = C.GetPos(w.context) +func (w *Window) GetPosition() (int, int) { + var _result *C.char = C.GetPosition(w.context) temp := C.GoString(_result) return parseIntDuo(temp) } diff --git a/v2/internal/frontend/desktop/linux/frontend.go b/v2/internal/frontend/desktop/linux/frontend.go index 0b5f232a0..7ca948cfd 100644 --- a/v2/internal/frontend/desktop/linux/frontend.go +++ b/v2/internal/frontend/desktop/linux/frontend.go @@ -137,11 +137,11 @@ func (f *Frontend) WindowCenter() { f.mainWindow.Center() } -func (f *Frontend) WindowSetPos(x, y int) { - f.mainWindow.SetPos(x, y) +func (f *Frontend) WindowSetPosition(x, y int) { + f.mainWindow.SetPosition(x, y) } -func (f *Frontend) WindowGetPos() (int, int) { - return f.mainWindow.Pos() +func (f *Frontend) WindowGetPosition() (int, int) { + return f.mainWindow.GetPosition() } func (f *Frontend) WindowSetSize(width, height int) { diff --git a/v2/internal/frontend/desktop/linux/window.go b/v2/internal/frontend/desktop/linux/window.go index 74053f78c..1a1f01004 100644 --- a/v2/internal/frontend/desktop/linux/window.go +++ b/v2/internal/frontend/desktop/linux/window.go @@ -11,6 +11,11 @@ package linux #include #include + +void ExecuteOnMainThread(void* f, gpointer jscallback) { + g_idle_add((GSourceFunc)f, (gpointer)jscallback); +} + static GtkWidget* GTKWIDGET(void *pointer) { return GTK_WIDGET(pointer); } @@ -60,13 +65,10 @@ GdkRectangle getCurrentMonitorGeometry(GtkWindow *window) { return result; } -void SetPosition(GtkWindow *window, int x, int y) { - GdkRectangle monitorDimensions = getCurrentMonitorGeometry(window); - gtk_window_move(window, monitorDimensions.x + x, monitorDimensions.y + y); -} - -void Center(GtkWindow *window) +void Center(gpointer data) { + GtkWindow *window = (GtkWindow*)data; + // Get the geometry of the monitor GdkRectangle m = getCurrentMonitorGeometry(window); @@ -183,13 +185,28 @@ void loadIndex(void* webview) { webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "wails:///"); } -static void startDrag(void *webview, GtkWindow* mainwindow) -{ - // Ignore non-toplevel widgets - GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webview)); - if (!GTK_IS_WINDOW(window)) return; +typedef struct DragOptions { + void *webview; + GtkWindow* mainwindow; +} DragOptions; - gtk_window_begin_move_drag(mainwindow, 1, xroot, yroot, dragTime); +static void startDrag(gpointer data) +{ + DragOptions* options = (DragOptions*)data; + + // Ignore non-toplevel widgets + GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(options->webview)); + if (!GTK_IS_WINDOW(window)) return; + + gtk_window_begin_move_drag(options->mainwindow, 1, xroot, yroot, dragTime); + free(data); +} + +static void StartDrag(void *webview, GtkWindow* mainwindow) { + DragOptions* data = malloc(sizeof(DragOptions)); + data->webview = webview; + data->mainwindow = mainwindow; + ExecuteOnMainThread(startDrag, (gpointer)data); } typedef struct JSCallback { @@ -204,10 +221,6 @@ int executeJS(gpointer data) { return G_SOURCE_REMOVE; } -void ExecuteOnMainThread(void* f, gpointer jscallback) { - g_idle_add((GSourceFunc)f, (gpointer)jscallback); -} - void extern processMessageDialogResult(char*); typedef struct MessageDialogOptions { @@ -393,6 +406,48 @@ void setRGBA(gpointer* data) { GdkRGBA colour = {options->r / 255.0, options->g / 255.0, options->b / 255.0, options->a / 255.0}; webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(options->webview), &colour); } + +typedef struct SetTitleArgs { + GtkWindow* window; + char* title; +} SetTitleArgs; + +void setTitle(gpointer data) { + SetTitleArgs* args = (SetTitleArgs*)data; + gtk_window_set_title(args->window, args->title); + free((void*)args->title); + free((void*)data); +} + +void SetTitle(GtkWindow* window, char* title) { + SetTitleArgs* args = malloc(sizeof(SetTitleArgs)); + args->window = window; + args->title = title; + ExecuteOnMainThread(setTitle, (gpointer)args); +} + +typedef struct SetPositionArgs { + int x; + int y; + void* window; +} SetPositionArgs; + +void setPosition(gpointer data) { + SetPositionArgs* args = (SetPositionArgs*)data; + gtk_window_move((GtkWindow*)args->window, args->x, args->y); + free(args); +} + +void SetPosition(void* window, int x, int y) { + GdkRectangle monitorDimensions = getCurrentMonitorGeometry(window); + SetPositionArgs* args = malloc(sizeof(SetPositionArgs)); + args->window = window; + args->x = monitorDimensions.x + x; + args->y = monitorDimensions.y + y; + ExecuteOnMainThread(setPosition, (gpointer)args); +} + + */ import "C" import ( @@ -495,11 +550,11 @@ func (w *Window) cWebKitUserContentManager() *C.WebKitUserContentManager { } func (w *Window) Fullscreen() { - C.gtk_window_fullscreen(w.asGTKWindow()) + C.ExecuteOnMainThread(C.gtk_window_fullscreen, C.gpointer(w.asGTKWindow())) } func (w *Window) UnFullscreen() { - C.gtk_window_unfullscreen(w.asGTKWindow()) + C.ExecuteOnMainThread(C.gtk_window_unfullscreen, C.gpointer(w.asGTKWindow())) } func (w *Window) Destroy() { @@ -512,13 +567,11 @@ func (w *Window) Close() { } func (w *Window) Center() { - C.Center(w.asGTKWindow()) + C.ExecuteOnMainThread(C.Center, C.gpointer(w.asGTKWindow())) } -func (w *Window) SetPos(x int, y int) { - cX := C.int(x) - cY := C.int(y) - C.gtk_window_move(w.asGTKWindow(), cX, cY) +func (w *Window) SetPosition(x int, y int) { + C.SetPosition(unsafe.Pointer(w.asGTKWindow()), C.int(x), C.int(y)) } func (w *Window) Size() (int, int) { @@ -527,7 +580,7 @@ func (w *Window) Size() (int, int) { return int(width), int(height) } -func (w *Window) Pos() (int, int) { +func (w *Window) GetPosition() (int, int) { var width, height C.int C.gtk_window_get_position(w.asGTKWindow(), &width, &height) return int(width), int(height) @@ -621,9 +674,7 @@ func (w *Window) SetDecorated(frameless bool) { } func (w *Window) SetTitle(title string) { - cTitle := C.CString(title) - defer C.free(unsafe.Pointer(cTitle)) - C.gtk_window_set_title(w.asGTKWindow(), cTitle) + C.SetTitle(w.asGTKWindow(), C.CString(title)) } func (w *Window) ExecJS(js string) { @@ -635,7 +686,7 @@ func (w *Window) ExecJS(js string) { } func (w *Window) StartDrag() { - C.startDrag(w.webview, w.asGTKWindow()) + C.StartDrag(w.webview, w.asGTKWindow()) } func (w *Window) Quit() { diff --git a/v2/internal/frontend/desktop/windows/frontend.go b/v2/internal/frontend/desktop/windows/frontend.go index 355efda33..f12a78a31 100644 --- a/v2/internal/frontend/desktop/windows/frontend.go +++ b/v2/internal/frontend/desktop/windows/frontend.go @@ -145,13 +145,13 @@ func (f *Frontend) WindowCenter() { f.mainWindow.Center() } -func (f *Frontend) WindowSetPos(x, y int) { +func (f *Frontend) WindowSetPosition(x, y int) { runtime.LockOSThread() - f.mainWindow.SetPos(x, y) + f.mainWindow.SetPosition(x, y) } -func (f *Frontend) WindowGetPos() (int, int) { +func (f *Frontend) WindowGetPosition() (int, int) { runtime.LockOSThread() - return f.mainWindow.Pos() + return f.mainWindow.GetPosition() } func (f *Frontend) WindowSetSize(width, height int) { diff --git a/v2/internal/frontend/devserver/devserver.go b/v2/internal/frontend/devserver/devserver.go index 29a0332a0..b11271b7d 100644 --- a/v2/internal/frontend/devserver/devserver.go +++ b/v2/internal/frontend/devserver/devserver.go @@ -199,12 +199,12 @@ func (d *DevWebServer) WindowUnminimise() { d.desktopFrontend.WindowUnminimise() } -func (d *DevWebServer) WindowSetPos(x int, y int) { - d.desktopFrontend.WindowSetPos(x, y) +func (d *DevWebServer) WindowSetPosition(x int, y int) { + d.desktopFrontend.WindowSetPosition(x, y) } -func (d *DevWebServer) WindowGetPos() (int, int) { - return d.desktopFrontend.WindowGetPos() +func (d *DevWebServer) WindowGetPosition() (int, int) { + return d.desktopFrontend.WindowGetPosition() } func (d *DevWebServer) WindowSetSize(width int, height int) { diff --git a/v2/internal/frontend/dispatcher/systemcalls.go b/v2/internal/frontend/dispatcher/systemcalls.go index 8d23d54bf..53a7d107f 100644 --- a/v2/internal/frontend/dispatcher/systemcalls.go +++ b/v2/internal/frontend/dispatcher/systemcalls.go @@ -26,7 +26,7 @@ func (d *Dispatcher) processSystemCall(payload callMessage, sender frontend.Fron switch name { case "WindowGetPos": - x, y := sender.WindowGetPos() + x, y := sender.WindowGetPosition() return &position{x, y}, nil case "WindowGetSize": w, h := sender.WindowGetSize() diff --git a/v2/internal/frontend/dispatcher/window.go b/v2/internal/frontend/dispatcher/window.go index 204eefbd5..59a1ec733 100644 --- a/v2/internal/frontend/dispatcher/window.go +++ b/v2/internal/frontend/dispatcher/window.go @@ -41,7 +41,7 @@ func (d *Dispatcher) processWindowMessage(message string, sender frontend.Fronte parts := strings.Split(message[3:], ":") x := d.mustAtoI(parts[0]) y := d.mustAtoI(parts[1]) - go sender.WindowSetPos(x, y) + go sender.WindowSetPosition(x, y) case 'H': go sender.WindowHide() case 'S': diff --git a/v2/internal/frontend/frontend.go b/v2/internal/frontend/frontend.go index d023d7f0d..324ae6cc1 100644 --- a/v2/internal/frontend/frontend.go +++ b/v2/internal/frontend/frontend.go @@ -76,8 +76,8 @@ type Frontend interface { WindowUnmaximise() WindowMinimise() WindowUnminimise() - WindowSetPos(x int, y int) - WindowGetPos() (int, int) + WindowSetPosition(x int, y int) + WindowGetPosition() (int, int) WindowSetSize(width int, height int) WindowGetSize() (int, int) WindowSetMinSize(width int, height int) diff --git a/v2/pkg/runtime/window.go b/v2/pkg/runtime/window.go index e492134f1..ad0bbad84 100644 --- a/v2/pkg/runtime/window.go +++ b/v2/pkg/runtime/window.go @@ -74,12 +74,12 @@ func WindowSetMaxSize(ctx context.Context, width int, height int) { // WindowSetPosition sets the position of the window func WindowSetPosition(ctx context.Context, x int, y int) { appFrontend := getFrontend(ctx) - appFrontend.WindowSetPos(x, y) + appFrontend.WindowSetPosition(x, y) } -func WindowGetPos(ctx context.Context) (int, int) { +func WindowGetPosition(ctx context.Context) (int, int) { appFrontend := getFrontend(ctx) - return appFrontend.WindowGetPos() + return appFrontend.WindowGetPosition() } // WindowMaximise the window