Skip to content

Commit

Permalink
Fix Windows WebView CPU Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertShown committed Nov 22, 2024
1 parent 2a406f5 commit 3084ff3
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions src/webui.c
Original file line number Diff line number Diff line change
Expand Up @@ -10678,56 +10678,58 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
// Success
// Let `wait()` use safe main-thread WebView2 loop
_webui.is_webview = true;

_webui_mutex_is_webview_update(win, WEBUI_MUTEX_FALSE);

MSG msg;
while (true) {

// Win32 Messages
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE) > 0) {
// Check if there is any Win32 Messages

// Process
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);

// Check if window manually closed
// Window manually closed
if (msg.message == WM_QUIT) {
if (win->webView) {
DestroyWindow(win->webView->hwnd);
}
break;
}
}
else {

if (_webui_mutex_is_webview_update(win, WEBUI_MUTEX_NONE)) {

_webui_mutex_is_webview_update(win, WEBUI_MUTEX_FALSE);

if (win->webView) {

// Stop this thread
if (win->webView->stop) {
DestroyWindow(win->webView->hwnd);
break;
}

// Window Size
if (win->webView->size) {
win->webView->size = false;
_webui_wv_set_size(win->webView, win->webView->width, win->webView->height);
}
// Check if there is any WebUI Messages

// Window Position
if (win->webView->position) {
win->webView->position = false;
_webui_wv_set_position(win->webView, win->webView->x, win->webView->y);
if (_webui_mutex_is_webview_update(win, WEBUI_MUTEX_NONE)) {
_webui_mutex_is_webview_update(win, WEBUI_MUTEX_FALSE);
if (win->webView) {
// Stop this thread
if (win->webView->stop) {
DestroyWindow(win->webView->hwnd);
break;
}
// Window Size
if (win->webView->size) {
win->webView->size = false;
_webui_wv_set_size(win->webView, win->webView->width, win->webView->height);
}
// Window Position
if (win->webView->position) {
win->webView->position = false;
_webui_wv_set_position(win->webView, win->webView->x, win->webView->y);
}
// Navigation
if (win->webView->navigate) {
win->webView->navigate = false;
_webui_wv_navigate(win->webView, win->webView->url);
}
}
}
else {

// Navigation
if (win->webView->navigate) {
win->webView->navigate = false;
_webui_wv_navigate(win->webView, win->webView->url);
}
// At this moment, there is no Win32 or WebUI messages
// let's IDLE for 10ms in this current thread.
_webui_sleep(10);
}
}
}
Expand Down

0 comments on commit 3084ff3

Please sign in to comment.