changeset 2001:f7acca46f795

Win: Fixes for Edge (Chromium) embedding, the loading can be delayed so... prior to finishing loading save location and raw HTML data requests and then actually load it when the browser context has finished loading. Also actually detect Edge (Chromium) instead of just returning TRUE.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 31 Oct 2019 21:28:32 +0000
parents 77e43d71eaa7
children a2931caa3422
files win/dw.c win/edge.cpp
diffstat 2 files changed, 70 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/win/dw.c	Thu Oct 31 08:26:58 2019 +0000
+++ b/win/dw.c	Thu Oct 31 21:28:32 2019 +0000
@@ -4285,25 +4285,6 @@
 
    RegisterClass(&wc);
 
-#if (defined(BUILD_DLL) || defined(BUILD_HTML))
-   /* Register HTML renderer class */
-   memset(&wc, 0, sizeof(WNDCLASS));
-   wc.lpszClassName = BrowserClassName;
-   wc.style = CS_HREDRAW | CS_VREDRAW;
-#ifdef BUILD_EDGE
-   /* Check if Microsoft Edge (Chromium) is installed */
-   if(_DW_EDGE_DETECTED = _dw_edge_detect())
-   {
-      wc.lpfnWndProc = (WNDPROC)_edgeWindowProc;
-   }
-   else
-#endif
-   {
-      wc.lpfnWndProc = (WNDPROC)_browserWindowProc;
-   }
-   RegisterClass(&wc);
-#endif
-
    /* Create a set of brushes using the default OS/2 and DOS colors */
    for(z=0;z<18;z++)
       _colors[z] = CreateSolidBrush(RGB(_red[z],_green[z],_blue[z]));
@@ -4340,6 +4321,25 @@
       exit(1);
    }
    
+#if (defined(BUILD_DLL) || defined(BUILD_HTML))
+   /* Register HTML renderer class */
+   memset(&wc, 0, sizeof(WNDCLASS));
+   wc.lpszClassName = BrowserClassName;
+   wc.style = CS_HREDRAW | CS_VREDRAW;
+#ifdef BUILD_EDGE
+   /* Check if Microsoft Edge (Chromium) is installed */
+   if (_DW_EDGE_DETECTED = _dw_edge_detect())
+   {
+	   wc.lpfnWndProc = (WNDPROC)_edgeWindowProc;
+   }
+   else
+#endif
+   {
+	   wc.lpfnWndProc = (WNDPROC)_browserWindowProc;
+   }
+   RegisterClass(&wc);
+#endif
+
    /* Create a tooltip. */
    hwndTooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
                   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, DW_HWND_OBJECT, NULL, DWInstance,NULL);
--- a/win/edge.cpp	Thu Oct 31 08:26:58 2019 +0000
+++ b/win/edge.cpp	Thu Oct 31 21:28:32 2019 +0000
@@ -16,15 +16,34 @@
 using namespace Microsoft::WRL;
 
 #define _DW_HTML_DATA_NAME (char *)"_dw_edge"
+#define _DW_HTML_DATA_LOCATION (char *)"_dw_edge_location"
+#define _DW_HTML_DATA_RAW (char *)"_dw_edge_raw"
 
 extern "C" {
 
+	extern HWND DW_HWND_OBJECT;
+	BOOL DW_EDGE_DETECTED = FALSE;
+
+	/******************************* dw_edge_detect() **************************
+	 * Attempts to create a temporary Edge (Chromium) browser context...
+	 * If we succeed return TRUE and use Edge for HTML windows.
+	 * If it fails return FALSE and fall back to using embedded IE.
+	 */
 	BOOL _dw_edge_detect(VOID)
 	{
-		return TRUE;
+		HWND hWnd = DW_HWND_OBJECT;
+
+		CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr,
+			Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>(
+				[hWnd](HRESULT result, IWebView2Environment* env) -> HRESULT {
+					// Successfully created Edge environment, return TRUE 
+					DW_EDGE_DETECTED = TRUE;
+					return S_OK;
+				}).Get());
+		return DW_EDGE_DETECTED;
 	}
 
-	/******************************* dw_html_action() **************************
+	/******************************* dw_edge_action() **************************
 	 * Implements the functionality of a "Back". "Forward", "Home", "Search",
 	 * "Refresh", or "Stop" button.
 	 *
@@ -96,7 +115,7 @@
 		}
 	}
 
-	/******************************* dw_html_raw() ****************************
+	/******************************* dw_edge_raw() ****************************
 	 * Takes a string containing some HTML BODY, and displays it in the specified
 	 * window. For example, perhaps you want to display the HTML text of...
 	 *
@@ -118,13 +137,13 @@
 		webview = (IWebView2WebView*)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME);
 
 		if (webview)
-		{
-			return DW_ERROR_NONE;
-		}
-		return DW_ERROR_UNKNOWN;
+			webview->NavigateToString(string);
+		else
+			dw_window_set_data(hwnd, _DW_HTML_DATA_RAW, _wcsdup(string));
+		return DW_ERROR_NONE;
 	}
 
-	/******************************* dw_html_url() ****************************
+	/******************************* dw_edge_url() ****************************
 	 * Displays a URL, or HTML file on disk.
 	 *
 	 * hwnd =		Handle to the window hosting the browser object.
@@ -142,14 +161,13 @@
 		webview = (IWebView2WebView*)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME);
 
 		if (webview)
-		{
 			webview->Navigate(url);
-			return DW_ERROR_NONE;
-		}
-		return DW_ERROR_UNKNOWN;
+		else
+			dw_window_set_data(hwnd, _DW_HTML_DATA_LOCATION, _wcsdup(url));
+		return DW_ERROR_NONE;
 	}
 
-	/************************** browserWindowProc() *************************
+	/************************** edgeWindowProc() *************************
 	 * Our message handler for our window to host the browser.
 	 */
 
@@ -160,16 +178,19 @@
 		case WM_SIZE:
 		{
 			// Resize the browser object to fit the window
-			RECT bounds;
 			IWebView2WebView* webview;
 
 			// Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
 			// we initially attached the browser object to this window.
 			webview = (IWebView2WebView*)dw_window_get_data(hWnd, _DW_HTML_DATA_NAME);
-			GetClientRect(hWnd, &bounds);
 			// Resize WebView to fit the bounds of the parent window
 			if (webview)
+			{
+				RECT bounds;
+
+				GetClientRect(hWnd, &bounds);
 				webview->put_Bounds(bounds);
+			}
 			return(0);
 		}
 
@@ -200,6 +221,21 @@
 								RECT bounds;
 								GetClientRect(hWnd, &bounds);
 								webview->put_Bounds(bounds);
+
+								// Handle cached load requests due to delayed
+								// loading of the edge webview contexts
+								LPCWSTR url = (LPCWSTR)dw_window_get_data(hWnd, _DW_HTML_DATA_LOCATION);
+								if(url)
+								{
+									webview->Navigate(url);
+									free((void *)url);
+								}
+								LPCWSTR raw = (LPCWSTR)dw_window_get_data(hWnd, _DW_HTML_DATA_RAW);
+								if (raw)
+								{
+									webview->NavigateToString(raw);
+									free((void *)raw);
+								}
 								return S_OK;
 							}).Get());
 						return S_OK;