From ee05884c9c18a1fd5e8c88ec0aeff1b0f238eb76 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 27 Apr 2021 21:08:53 +1000 Subject: [PATCH] [windows] ON_MAIN_THEAD macro. Misc updates --- v2/internal/ffenestri/ffenestri.go | 12 +- v2/internal/ffenestri/ffenestri_client.go | 6 - v2/internal/ffenestri/ffenestri_windows.cpp | 154 ++++++++++-------- v2/internal/ffenestri/ffenestri_windows.go | 10 -- v2/internal/ffenestri/ffenestri_windows.h | 7 + .../ffenestri/windows/test/compile.bat | 3 +- v2/internal/ffenestri/windows/test/main.c | 4 +- v2/pkg/options/options.go | 1 + 8 files changed, 107 insertions(+), 90 deletions(-) diff --git a/v2/internal/ffenestri/ffenestri.go b/v2/internal/ffenestri/ffenestri.go index 96061443..51cdf45a 100644 --- a/v2/internal/ffenestri/ffenestri.go +++ b/v2/internal/ffenestri/ffenestri.go @@ -13,17 +13,17 @@ import ( ) /* - #cgo linux CFLAGS: -DFFENESTRI_LINUX=1 #cgo linux pkg-config: gtk+-3.0 webkit2gtk-4.0 #cgo darwin CFLAGS: -DFFENESTRI_DARWIN=1 #cgo darwin LDFLAGS: -framework WebKit -lobjc +#cgo windows CPPFLAGS: -std=c++11 +#cgo windows,amd64 LDFLAGS: -L./windows/x64 -lwebview -lWebView2Loader -lgdi32 + #include #include "ffenestri.h" - - */ import "C" @@ -139,9 +139,9 @@ func (a *Application) Run(incomingDispatcher Dispatcher, bindings string, debug C.SetDebug(app, a.bool2Cint(debug)) // TODO: Move frameless to Linux options - // if a.config.Frameless { - // C.DisableFrame(a.app) - // } + if a.config.Frameless { + C.DisableFrame(a.app) + } if a.config.RGBA != 0 { r, g, b, alpha := intToColour(a.config.RGBA) diff --git a/v2/internal/ffenestri/ffenestri_client.go b/v2/internal/ffenestri/ffenestri_client.go index 7c7accf4..9611769a 100644 --- a/v2/internal/ffenestri/ffenestri_client.go +++ b/v2/internal/ffenestri/ffenestri_client.go @@ -1,13 +1,7 @@ package ffenestri /* - -#cgo linux CFLAGS: -DFFENESTRI_LINUX=1 -#cgo linux pkg-config: gtk+-3.0 webkit2gtk-4.0 - -#include #include "ffenestri.h" - */ import "C" diff --git a/v2/internal/ffenestri/ffenestri_windows.cpp b/v2/internal/ffenestri/ffenestri_windows.cpp index c534b9ff..2ab6014e 100644 --- a/v2/internal/ffenestri/ffenestri_windows.cpp +++ b/v2/internal/ffenestri/ffenestri_windows.cpp @@ -1,34 +1,22 @@ // Some code may be inspired by or directly used from Webview. #include "ffenestri_windows.h" +//#include "wv2ComHandler_windows.h" +#include int debug = 0; DWORD mainThread; -typedef void(*dispatchMethod)(void); -typedef void(*dispatchMethod1)(void*); -typedef void(*dispatchMethod2)(void*,void*); - -typedef struct { - void* func; - void* args[5]; - int argc; -} dispatchFunction; - -dispatchFunction* NewDispatchFunction(void *func) { - dispatchFunction *result = (dispatchFunction *)malloc(sizeof(dispatchFunction)); - result->func = func; - result->argc = 0; - return result; -} - // dispatch will execute the given `func` pointer -void dispatch(dispatchFunction *func) { - PostThreadMessage(mainThread, WM_APP, 0, (LPARAM) func); +void dispatch(dispatchFunction func) { + PostThreadMessage(mainThread, WM_APP, 0, (LPARAM) new dispatchFunction(func)); } struct Application{ // Window specific HWND window; +// WebViewControl webview; +// ICoreWebView2Controller* controller; + // Application const char *title; @@ -54,6 +42,10 @@ struct Application *NewApplication(const char *title, int width, int height, int // Create application struct Application *result = (struct Application*)malloc(sizeof(struct Application)); + result->window = nullptr; +// result->webview = nullptr; +// result->controller = nullptr; + result->title = title; result->width = width; result->height = height; @@ -138,6 +130,43 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { } +//void initWebView2(struct Application *app, int debug, messageCallback cb) { +// CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); +// +// std::atomic_flag flag = ATOMIC_FLAG_INIT; +// flag.test_and_set(); +// +// char currentExePath[MAX_PATH]; +// GetModuleFileNameA(NULL, currentExePath, MAX_PATH); +// char *currentExeName = PathFindFileNameA(currentExePath); +// +// std::wstring_convert> wideCharConverter; +// std::wstring userDataFolder = wideCharConverter.from_bytes(std::getenv("APPDATA")); +// std::wstring currentExeNameW = wideCharConverter.from_bytes(currentExeName); +// +// HRESULT res = CreateCoreWebView2EnvironmentWithOptions( +// nullptr, (userDataFolder + L"/" + currentExeNameW).c_str(), nullptr, +// new wv2ComHandler(app->window, cb, +// [&](ICoreWebView2Controller *controller) { +// app->controller = controller; +// app->controller->get_CoreWebView2(&(app->webview)); +// app->webview->AddRef(); +// flag.clear(); +// })); +// if (res != S_OK) { +// CoUninitialize(); +// return false; +// } +// +// MSG msg = {}; +// while (flag.test_and_set() && GetMessage(&msg, NULL, 0, 0)) { +// TranslateMessage(&msg); +// DispatchMessage(&msg); +// } +// init("window.external={invoke:s=>window.chrome.webview.postMessage(s)}"); +// return true; +//} + void Run(struct Application* app, int argc, char **argv) { WNDCLASSEX wc; @@ -163,9 +192,9 @@ void Run(struct Application* app, int argc, char **argv) { app->window = CreateWindow((LPCWSTR)"ffenestri", (LPCWSTR)"", windowStyle, CW_USEDEFAULT, CW_USEDEFAULT, app->width, app->height, NULL, NULL, GetModuleHandle(NULL), NULL); - // Set Title - setTitle(app, app->title); + // Private setTitle as we're on the main thread + setTitle(app, app->title); // Store application pointer in window handle SetWindowLongPtr(app->window, GWLP_USERDATA, (LONG_PTR)app); @@ -175,6 +204,7 @@ void Run(struct Application* app, int argc, char **argv) { if ( app->startHidden == 1 ) { startVisibility = SW_HIDE; } + // private center() as we are on main thread center(app); ShowWindow(app->window, startVisibility); @@ -182,6 +212,7 @@ void Run(struct Application* app, int argc, char **argv) { SetFocus(app->window); // TODO: Add webview2 +// initWebView2(app->window); // Main event loop MSG msg; @@ -193,15 +224,9 @@ void Run(struct Application* app, int argc, char **argv) { continue; } if (msg.message == WM_APP) { - dispatchFunction *m = (dispatchFunction*) msg.lParam; - void *method = m->func; - if (m->argc == 1) { - ((dispatchMethod1)method)(m->args[0]); - } - if (m->argc == 2) { - ((dispatchMethod2)method)(m->args[0], m->args[1]); - } - free(m); + dispatchFunction *f = (dispatchFunction*) msg.lParam; + (*f)(); + delete(f); } else if (msg.message == WM_QUIT) { return; } @@ -226,10 +251,9 @@ void hide(struct Application* app) { } void Hide(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)hide); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + hide(app); + ); } void show(struct Application* app) { @@ -237,10 +261,9 @@ void show(struct Application* app) { } void Show(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)show); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + show(app); + ); } void center(struct Application* app) { @@ -268,10 +291,9 @@ void center(struct Application* app) { } void Center(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)center); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + center(app); + ); } UINT getWindowPlacement(struct Application* app) { @@ -294,10 +316,9 @@ void maximise(struct Application* app) { } void Maximise(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)maximise); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + maximise(app); + ); } void unmaximise(struct Application* app) { @@ -305,10 +326,9 @@ void unmaximise(struct Application* app) { } void Unmaximise(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)unmaximise); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + unmaximise(app); + ); } @@ -328,10 +348,9 @@ void minimise(struct Application* app) { } void Minimise(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)minimise); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + minimise(app); + ); } void unminimise(struct Application* app) { @@ -339,10 +358,9 @@ void unminimise(struct Application* app) { } void Unminimise(struct Application* app) { - dispatchFunction *f = NewDispatchFunction((void*)unminimise); - f->args[0] = app; - f->argc = 1; - dispatch(f); + ON_MAIN_THREAD( + unminimise(app); + ); } void ToggleMinimise(struct Application* app) { @@ -353,13 +371,23 @@ void ToggleMinimise(struct Application* app) { } void SetColour(struct Application* app, int red, int green, int blue, int alpha) { - +// TBD } void SetSize(struct Application* app, int width, int height) { + // TBD } + +void setPosition(struct Application* app, int x, int y) { + // TBD +} + void SetPosition(struct Application* app, int x, int y) { + ON_MAIN_THREAD( + setPosition(app, x, y); + ); } + void Quit(struct Application* app) { } @@ -373,11 +401,9 @@ void setTitle(struct Application* app, const char *title) { } void SetTitle(struct Application* app, const char *title) { - dispatchFunction *f = NewDispatchFunction((void*)setTitle); - f->args[0] = app; - f->args[1] = (void*)title; - f->argc = 2; - dispatch(f); + ON_MAIN_THREAD( + setTitle(app, title); + ); } void Fullscreen(struct Application* app) { diff --git a/v2/internal/ffenestri/ffenestri_windows.go b/v2/internal/ffenestri/ffenestri_windows.go index d269bfaf..d1d0106e 100644 --- a/v2/internal/ffenestri/ffenestri_windows.go +++ b/v2/internal/ffenestri/ffenestri_windows.go @@ -1,15 +1,5 @@ package ffenestri -/* - -#cgo windows,amd64 LDFLAGS: -L./windows/x64 -lwebview -lWebView2Loader -lgdi32 - -#include "ffenestri.h" -#include "ffenestri_windows.h" - -*/ -import "C" - func (a *Application) processPlatformSettings() error { return nil diff --git a/v2/internal/ffenestri/ffenestri_windows.h b/v2/internal/ffenestri/ffenestri_windows.h index c70db19b..0dee460b 100644 --- a/v2/internal/ffenestri/ffenestri_windows.h +++ b/v2/internal/ffenestri/ffenestri_windows.h @@ -7,6 +7,13 @@ #include "ffenestri.h" #include #include +#include + +#define ON_MAIN_THREAD(code) dispatch( [=]{ code; } ) + +typedef std::function dispatchFunction; +//typedef std::function messageCallback; +//typedef std::function comHandlerCallback; void center(struct Application*); void setTitle(struct Application* app, const char *title); diff --git a/v2/internal/ffenestri/windows/test/compile.bat b/v2/internal/ffenestri/windows/test/compile.bat index 5340e6d8..9dfd45d1 100644 --- a/v2/internal/ffenestri/windows/test/compile.bat +++ b/v2/internal/ffenestri/windows/test/compile.bat @@ -1,2 +1 @@ -g++ main.c ..\..\ffenestri_windows.cpp -lgdi32 -a.exe \ No newline at end of file +g++ main.c ..\..\ffenestri_windows.cpp -lgdi32 -std=c++11 \ No newline at end of file diff --git a/v2/internal/ffenestri/windows/test/main.c b/v2/internal/ffenestri/windows/test/main.c index 2deee540..d925253e 100644 --- a/v2/internal/ffenestri/windows/test/main.c +++ b/v2/internal/ffenestri/windows/test/main.c @@ -3,8 +3,8 @@ int main() { struct Application *app = NewApplication("Wails ❤️ Unicode", 800, 600, 1, 1, 0, 0, 1, 0); - SetMinWindowSize(app, 100, 100); - SetMaxWindowSize(app, 800, 800); +// SetMinWindowSize(app, 100, 100); +// SetMaxWindowSize(app, 800, 800); Run(app,0, NULL); return 0; } diff --git a/v2/pkg/options/options.go b/v2/pkg/options/options.go index 9e4d3b89..bc5fe4a3 100644 --- a/v2/pkg/options/options.go +++ b/v2/pkg/options/options.go @@ -18,6 +18,7 @@ type App struct { Width int Height int DisableResize bool + Frameless bool Fullscreen bool Frameless bool MinWidth int