5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:52:29 +08:00

Fix position runtime (#1123)

* [linux] Move SetTitle and startDrag to main thread

* [linux] Move SetPosition, Center, Fullscreen and UnFullscreen to main thread

* Fix runtime Window Get/Set Position signatures

* Fix vanilla template keyboard handling
This commit is contained in:
Lea Anthony 2022-02-03 21:42:14 +11:00 committed by GitHub
parent 6700a4e3bd
commit 0971857e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 110 additions and 60 deletions

View File

@ -16,8 +16,7 @@ window.greet = function () {
}; };
nameElement.onkeydown = function (e) { nameElement.onkeydown = function (e) {
console.log(e) if (e.code === "Enter") {
if (e.keyCode == 13) { window.greet();
window.greet()
} }
} };

View File

@ -39,7 +39,7 @@ void ExecJS(void* ctx, const char*);
void Quit(void*); void Quit(void*);
const char* GetSize(void *ctx); 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); void ProcessURLResponse(void *inctx, const char *url, int statusCode, const char *contentType, void* data, int datalength);

View File

@ -163,7 +163,7 @@ const char* GetSize(void *inctx) {
return [result UTF8String]; return [result UTF8String];
} }
const char* GetPos(void *inctx) { const char* GetPosition(void *inctx) {
WailsContext *ctx = (__bridge WailsContext*) inctx; WailsContext *ctx = (__bridge WailsContext*) inctx;
NSScreen* screen = [ctx getCurrentScreen]; NSScreen* screen = [ctx getCurrentScreen];
NSRect windowFrame = [ctx.mainWindow frame]; NSRect windowFrame = [ctx.mainWindow frame];

View File

@ -144,11 +144,11 @@ func (f *Frontend) WindowCenter() {
f.mainWindow.Center() f.mainWindow.Center()
} }
func (f *Frontend) WindowSetPos(x, y int) { func (f *Frontend) WindowSetPosition(x, y int) {
f.mainWindow.SetPos(x, y) f.mainWindow.SetPosition(x, y)
} }
func (f *Frontend) WindowGetPos() (int, int) { func (f *Frontend) WindowGetPosition() (int, int) {
return f.mainWindow.Pos() return f.mainWindow.GetPosition()
} }
func (f *Frontend) WindowSetSize(width, height int) { func (f *Frontend) WindowSetSize(width, height int) {

View File

@ -137,7 +137,7 @@ func (w *Window) ExecJS(js string) {
C.free(unsafe.Pointer(_js)) 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)) C.SetPosition(w.context, C.int(x), C.int(y))
} }
@ -204,8 +204,8 @@ func parseIntDuo(temp string) (int, int) {
return x, y return x, y
} }
func (w *Window) Pos() (int, int) { func (w *Window) GetPosition() (int, int) {
var _result *C.char = C.GetPos(w.context) var _result *C.char = C.GetPosition(w.context)
temp := C.GoString(_result) temp := C.GoString(_result)
return parseIntDuo(temp) return parseIntDuo(temp)
} }

View File

@ -137,11 +137,11 @@ func (f *Frontend) WindowCenter() {
f.mainWindow.Center() f.mainWindow.Center()
} }
func (f *Frontend) WindowSetPos(x, y int) { func (f *Frontend) WindowSetPosition(x, y int) {
f.mainWindow.SetPos(x, y) f.mainWindow.SetPosition(x, y)
} }
func (f *Frontend) WindowGetPos() (int, int) { func (f *Frontend) WindowGetPosition() (int, int) {
return f.mainWindow.Pos() return f.mainWindow.GetPosition()
} }
func (f *Frontend) WindowSetSize(width, height int) { func (f *Frontend) WindowSetSize(width, height int) {

View File

@ -11,6 +11,11 @@ package linux
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
void ExecuteOnMainThread(void* f, gpointer jscallback) {
g_idle_add((GSourceFunc)f, (gpointer)jscallback);
}
static GtkWidget* GTKWIDGET(void *pointer) { static GtkWidget* GTKWIDGET(void *pointer) {
return GTK_WIDGET(pointer); return GTK_WIDGET(pointer);
} }
@ -60,13 +65,10 @@ GdkRectangle getCurrentMonitorGeometry(GtkWindow *window) {
return result; return result;
} }
void SetPosition(GtkWindow *window, int x, int y) { void Center(gpointer data)
GdkRectangle monitorDimensions = getCurrentMonitorGeometry(window);
gtk_window_move(window, monitorDimensions.x + x, monitorDimensions.y + y);
}
void Center(GtkWindow *window)
{ {
GtkWindow *window = (GtkWindow*)data;
// Get the geometry of the monitor // Get the geometry of the monitor
GdkRectangle m = getCurrentMonitorGeometry(window); GdkRectangle m = getCurrentMonitorGeometry(window);
@ -183,13 +185,28 @@ void loadIndex(void* webview) {
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "wails:///"); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "wails:///");
} }
static void startDrag(void *webview, GtkWindow* mainwindow) typedef struct DragOptions {
{ void *webview;
// Ignore non-toplevel widgets GtkWindow* mainwindow;
GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(webview)); } DragOptions;
if (!GTK_IS_WINDOW(window)) return;
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 { typedef struct JSCallback {
@ -204,10 +221,6 @@ int executeJS(gpointer data) {
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
void ExecuteOnMainThread(void* f, gpointer jscallback) {
g_idle_add((GSourceFunc)f, (gpointer)jscallback);
}
void extern processMessageDialogResult(char*); void extern processMessageDialogResult(char*);
typedef struct MessageDialogOptions { 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}; 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); 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 "C"
import ( import (
@ -495,11 +550,11 @@ func (w *Window) cWebKitUserContentManager() *C.WebKitUserContentManager {
} }
func (w *Window) Fullscreen() { func (w *Window) Fullscreen() {
C.gtk_window_fullscreen(w.asGTKWindow()) C.ExecuteOnMainThread(C.gtk_window_fullscreen, C.gpointer(w.asGTKWindow()))
} }
func (w *Window) UnFullscreen() { func (w *Window) UnFullscreen() {
C.gtk_window_unfullscreen(w.asGTKWindow()) C.ExecuteOnMainThread(C.gtk_window_unfullscreen, C.gpointer(w.asGTKWindow()))
} }
func (w *Window) Destroy() { func (w *Window) Destroy() {
@ -512,13 +567,11 @@ func (w *Window) Close() {
} }
func (w *Window) Center() { func (w *Window) Center() {
C.Center(w.asGTKWindow()) C.ExecuteOnMainThread(C.Center, C.gpointer(w.asGTKWindow()))
} }
func (w *Window) SetPos(x int, y int) { func (w *Window) SetPosition(x int, y int) {
cX := C.int(x) C.SetPosition(unsafe.Pointer(w.asGTKWindow()), C.int(x), C.int(y))
cY := C.int(y)
C.gtk_window_move(w.asGTKWindow(), cX, cY)
} }
func (w *Window) Size() (int, int) { func (w *Window) Size() (int, int) {
@ -527,7 +580,7 @@ func (w *Window) Size() (int, int) {
return int(width), int(height) return int(width), int(height)
} }
func (w *Window) Pos() (int, int) { func (w *Window) GetPosition() (int, int) {
var width, height C.int var width, height C.int
C.gtk_window_get_position(w.asGTKWindow(), &width, &height) C.gtk_window_get_position(w.asGTKWindow(), &width, &height)
return int(width), int(height) return int(width), int(height)
@ -621,9 +674,7 @@ func (w *Window) SetDecorated(frameless bool) {
} }
func (w *Window) SetTitle(title string) { func (w *Window) SetTitle(title string) {
cTitle := C.CString(title) C.SetTitle(w.asGTKWindow(), C.CString(title))
defer C.free(unsafe.Pointer(cTitle))
C.gtk_window_set_title(w.asGTKWindow(), cTitle)
} }
func (w *Window) ExecJS(js string) { func (w *Window) ExecJS(js string) {
@ -635,7 +686,7 @@ func (w *Window) ExecJS(js string) {
} }
func (w *Window) StartDrag() { func (w *Window) StartDrag() {
C.startDrag(w.webview, w.asGTKWindow()) C.StartDrag(w.webview, w.asGTKWindow())
} }
func (w *Window) Quit() { func (w *Window) Quit() {

View File

@ -145,13 +145,13 @@ func (f *Frontend) WindowCenter() {
f.mainWindow.Center() f.mainWindow.Center()
} }
func (f *Frontend) WindowSetPos(x, y int) { func (f *Frontend) WindowSetPosition(x, y int) {
runtime.LockOSThread() 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() runtime.LockOSThread()
return f.mainWindow.Pos() return f.mainWindow.GetPosition()
} }
func (f *Frontend) WindowSetSize(width, height int) { func (f *Frontend) WindowSetSize(width, height int) {

View File

@ -199,12 +199,12 @@ func (d *DevWebServer) WindowUnminimise() {
d.desktopFrontend.WindowUnminimise() d.desktopFrontend.WindowUnminimise()
} }
func (d *DevWebServer) WindowSetPos(x int, y int) { func (d *DevWebServer) WindowSetPosition(x int, y int) {
d.desktopFrontend.WindowSetPos(x, y) d.desktopFrontend.WindowSetPosition(x, y)
} }
func (d *DevWebServer) WindowGetPos() (int, int) { func (d *DevWebServer) WindowGetPosition() (int, int) {
return d.desktopFrontend.WindowGetPos() return d.desktopFrontend.WindowGetPosition()
} }
func (d *DevWebServer) WindowSetSize(width int, height int) { func (d *DevWebServer) WindowSetSize(width int, height int) {

View File

@ -26,7 +26,7 @@ func (d *Dispatcher) processSystemCall(payload callMessage, sender frontend.Fron
switch name { switch name {
case "WindowGetPos": case "WindowGetPos":
x, y := sender.WindowGetPos() x, y := sender.WindowGetPosition()
return &position{x, y}, nil return &position{x, y}, nil
case "WindowGetSize": case "WindowGetSize":
w, h := sender.WindowGetSize() w, h := sender.WindowGetSize()

View File

@ -41,7 +41,7 @@ func (d *Dispatcher) processWindowMessage(message string, sender frontend.Fronte
parts := strings.Split(message[3:], ":") parts := strings.Split(message[3:], ":")
x := d.mustAtoI(parts[0]) x := d.mustAtoI(parts[0])
y := d.mustAtoI(parts[1]) y := d.mustAtoI(parts[1])
go sender.WindowSetPos(x, y) go sender.WindowSetPosition(x, y)
case 'H': case 'H':
go sender.WindowHide() go sender.WindowHide()
case 'S': case 'S':

View File

@ -76,8 +76,8 @@ type Frontend interface {
WindowUnmaximise() WindowUnmaximise()
WindowMinimise() WindowMinimise()
WindowUnminimise() WindowUnminimise()
WindowSetPos(x int, y int) WindowSetPosition(x int, y int)
WindowGetPos() (int, int) WindowGetPosition() (int, int)
WindowSetSize(width int, height int) WindowSetSize(width int, height int)
WindowGetSize() (int, int) WindowGetSize() (int, int)
WindowSetMinSize(width int, height int) WindowSetMinSize(width int, height int)

View File

@ -74,12 +74,12 @@ func WindowSetMaxSize(ctx context.Context, width int, height int) {
// WindowSetPosition sets the position of the window // WindowSetPosition sets the position of the window
func WindowSetPosition(ctx context.Context, x int, y int) { func WindowSetPosition(ctx context.Context, x int, y int) {
appFrontend := getFrontend(ctx) 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) appFrontend := getFrontend(ctx)
return appFrontend.WindowGetPos() return appFrontend.WindowGetPosition()
} }
// WindowMaximise the window // WindowMaximise the window