[windows] WebView2 component working

This commit is contained in:
Lea Anthony
2021-04-29 07:36:40 +10:00
parent 944e1c99ff
commit 6f6e810432
3 changed files with 52 additions and 38 deletions

View File

@@ -20,7 +20,7 @@ import (
#cgo darwin LDFLAGS: -framework WebKit -lobjc
#cgo windows CPPFLAGS: -std=c++11
#cgo windows,amd64 LDFLAGS: -L./windows/x64 -lwebview -lWebView2Loader -lgdi32
#cgo windows,amd64 LDFLAGS: -L./windows/x64 -lwebview -lWebView2Loader -lgdi32 -lole32 -lShlwapi -luser32 -loleaut32
#include <stdlib.h>
#include "ffenestri.h"
@@ -65,14 +65,6 @@ func NewApplicationWithConfig(config *options.App, logger *logger.Logger, menuMa
}
}
// NewApplication creates a new Application with the default config
func NewApplication(logger *logger.Logger) *Application {
return &Application{
config: options.Default,
logger: logger.CustomLogger("Ffenestri"),
}
}
func (a *Application) freeMemory() {
for _, mem := range a.memory {
// fmt.Printf("Freeing memory: %+v\n", mem)

View File

@@ -123,35 +123,48 @@ bool initWebView2(struct Application *app, int debug, messageCallback cb) {
std::atomic_flag flag = ATOMIC_FLAG_INIT;
flag.test_and_set();
// char currentExePath[MAX_PATH];
// GetModuleFileNameA(NULL, currentExePath, MAX_PATH);
// char *currentExeName = PathFindFileNameA(currentExePath);
//
// printf("current exe name = %s\n", currentExeName);
char currentExePath[MAX_PATH];
GetModuleFileNameA(NULL, currentExePath, MAX_PATH);
char *currentExeName = PathFindFileNameA(currentExePath);
// std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> 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, cb,
// [&](ICoreWebView2Controller *webviewController) {
// app->webviewController = webviewController;
// app->webviewController->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);
// }
// printf("userdata folder = %s\n", userDataFolder.c_str());
ICoreWebView2Controller *controller;
ICoreWebView2* webview;
HRESULT res = CreateCoreWebView2EnvironmentWithOptions(
nullptr, /*(userDataFolder + L"/" + currentExeNameW).c_str()*/ nullptr, nullptr,
new wv2ComHandler(app->window, cb,
[&](ICoreWebView2Controller *webviewController) {
controller = webviewController;
controller->get_CoreWebView2(&webview);
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);
}
app->webviewController = controller;
app->webview = webview;
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(app->window, &bounds);
app->webviewController->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
app->webview->Navigate(L"https://wails.app/");
//// init("window.external={invoke:s=>window.chrome.webview.postMessage(s)}");
return true;
}
@@ -203,6 +216,12 @@ void Run(struct Application* app, int argc, char **argv) {
// private center() as we are on main thread
center(app);
// if( debug == 1 ) {
// BOOL supported = SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// if( !supported ) {
// SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
// }
// }
ShowWindow(app->window, startVisibility);
UpdateWindow(app->window);
SetFocus(app->window);

View File

@@ -14,13 +14,13 @@ class wv2ComHandler
public ICoreWebView2WebMessageReceivedEventHandler,
public ICoreWebView2PermissionRequestedEventHandler {
struct Application *app;
HWND window;
messageCallback mcb;
comHandlerCallback cb;
public:
wv2ComHandler(struct Application *app, messageCallback mcb, comHandlerCallback cb) {
this->app = app;
wv2ComHandler(HWND window, messageCallback mcb, comHandlerCallback cb) {
this->window = window;
this->mcb = mcb;
this->cb = cb;
}
@@ -31,7 +31,7 @@ class wv2ComHandler
}
HRESULT STDMETHODCALLTYPE Invoke(HRESULT res,
ICoreWebView2Environment *env) {
env->CreateCoreWebView2Controller(app->window, this);
env->CreateCoreWebView2Controller(window, this);
return S_OK;
}
HRESULT STDMETHODCALLTYPE Invoke(HRESULT res,
@@ -49,6 +49,7 @@ class wv2ComHandler
}
HRESULT STDMETHODCALLTYPE Invoke(
ICoreWebView2 *sender, ICoreWebView2WebMessageReceivedEventArgs *args) {
printf("CCCCCCCCCCC\n");
LPWSTR message;
args->TryGetWebMessageAsString(&message);
@@ -62,6 +63,8 @@ class wv2ComHandler
HRESULT STDMETHODCALLTYPE
Invoke(ICoreWebView2 *sender,
ICoreWebView2PermissionRequestedEventArgs *args) {
printf("DDDDDDDDDDDD\n");
COREWEBVIEW2_PERMISSION_KIND kind;
args->get_PermissionKind(&kind);
if (kind == COREWEBVIEW2_PERMISSION_KIND_CLIPBOARD_READ) {