From dbdc1b80f831386c0f103377c8927972dd2be92b Mon Sep 17 00:00:00 2001 From: Valentin Radu Date: Sat, 28 May 2022 13:43:54 +0300 Subject: [PATCH] Weather: Show "Reload" link when data fails to load --- ep_weather_host/ep_weather.h | 2 +- ep_weather_host/ep_weather_error_html.h | 19 +++++++++++++++++ ep_weather_host/ep_weather_host.c | 28 +++++++++++++++++++++++-- ep_weather_host/ep_weather_host.h | 13 ++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/ep_weather_host/ep_weather.h b/ep_weather_host/ep_weather.h index 3cf072a..21a5367 100644 --- a/ep_weather_host/ep_weather.h +++ b/ep_weather_host/ep_weather.h @@ -69,7 +69,7 @@ DEFINE_GUID(IID_IEPWeather, #define EP_WEATHER_WM_SETDEVMODE (WM_USER + 13) #define EP_WEATHER_WM_SETZOOMFACTOR (WM_USER + 14) -#define EP_WEATHER_HEIGHT_ERROR 260 +#define EP_WEATHER_HEIGHT_ERROR 280 #define EP_WEATHER_HEIGHT 353 #define EP_WEATHER_WIDTH 673 diff --git a/ep_weather_host/ep_weather_error_html.h b/ep_weather_host/ep_weather_error_html.h index 6213c43..e293bb5 100644 --- a/ep_weather_host/ep_weather_error_html.h +++ b/ep_weather_host/ep_weather_error_html.h @@ -19,6 +19,24 @@ body {\n\ justify-content: center;\n\ align-items: center;\n\ }\n\ +@media (prefers-color-scheme: dark) {\n\ + .refreshLink { color: #43a7ff; }\n\ +}\n\ +@media (prefers-color-scheme: light) {\n\ + .refreshLink { color: #096bda; }\n\ +}\n\ +a:link {\n\ + text-decoration: none;\n\ +}\n\ +a:visited {\n\ + text-decoration: none;\n\ +}\n\ +a:hover {\n\ + text-decoration: underline;\n\ +}\n\ +a:active {\n\ + text-decoration: underline;\n\ +}\n\ \n\ \n\
\n\ @@ -26,6 +44,7 @@ body {\n\

Unable to load weather information

\n\

Make sure that the location you have entered is correct.
\n\ Verify that you are connected to the Internet.

\n\ +Reload\n\
\n\ "; #endif diff --git a/ep_weather_host/ep_weather_host.c b/ep_weather_host/ep_weather_host.c index 4415057..c8c6405 100644 --- a/ep_weather_host/ep_weather_host.c +++ b/ep_weather_host/ep_weather_host.c @@ -351,9 +351,9 @@ HRESULT STDMETHODCALLTYPE ICoreWebView2_CreateCoreWebView2ControllerCompleted(IC LONG64 dwDarkMode = InterlockedAdd64(&_this->g_darkModeEnabled, 0); epw_Weather_SetDarkMode(_this, dwDarkMode, FALSE); - _this->pCoreWebView2->lpVtbl->add_PermissionRequested(_this->pCoreWebView2, &EPWeather_ICoreWebView2PermissionRequestedEventHandler, &_this->tkOnPermissionRequested); - + _this->pCoreWebView2->lpVtbl->add_NavigationStarting(_this->pCoreWebView2, &EPWeather_ICoreWebView2NavigationStartingEventHandler, &_this->tkOnNavigationStarting); _this->pCoreWebView2->lpVtbl->add_NavigationCompleted(_this->pCoreWebView2, &EPWeather_ICoreWebView2NavigationCompletedEventHandler, &_this->tkOnNavigationCompleted); + _this->pCoreWebView2->lpVtbl->add_PermissionRequested(_this->pCoreWebView2, &EPWeather_ICoreWebView2PermissionRequestedEventHandler, &_this->tkOnPermissionRequested); _epw_Weather_NavigateToProvider(_this); @@ -384,8 +384,28 @@ HRESULT STDMETHODCALLTYPE ICoreWebView2_CallDevToolsProtocolMethodCompleted(ICor return S_OK; } +HRESULT STDMETHODCALLTYPE ICoreWebView2_NavigationStarting(ICoreWebView2NavigationStartingEventHandler* _this2, ICoreWebView2* pCoreWebView2, ICoreWebView2NavigationStartingEventArgs* pCoreWebView2NavigationStartingEventArgs) +{ + EPWeather* _this = EPWeather_Instance; // GetWindowLongPtrW(FindWindowW(_T(EPW_WEATHER_CLASSNAME), NULL), GWLP_USERDATA); + LPWSTR wszUri = NULL; + pCoreWebView2NavigationStartingEventArgs->lpVtbl->get_Uri(pCoreWebView2NavigationStartingEventArgs, &wszUri); + if (wszUri) + { + if (!_wcsicmp(wszUri, L"epweather://refresh")) + { + pCoreWebView2NavigationStartingEventArgs->lpVtbl->put_Cancel(pCoreWebView2NavigationStartingEventArgs, TRUE); + PostMessageW(_this->hWnd, EP_WEATHER_WM_FETCH_DATA, 0, 0); + } + CoTaskMemFree(wszUri); + } + return S_OK; +} + HRESULT STDMETHODCALLTYPE ICoreWebView2_NavigationCompleted(ICoreWebView2NavigationCompletedEventHandler* _this2, ICoreWebView2* pCoreWebView2, ICoreWebView2NavigationCompletedEventArgs* pCoreWebView2NavigationCompletedEventArgs) { + COREWEBVIEW2_WEB_ERROR_STATUS dwStatus = COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN; + pCoreWebView2NavigationCompletedEventArgs->lpVtbl->get_WebErrorStatus(pCoreWebView2NavigationCompletedEventArgs, &dwStatus); + if (dwStatus == COREWEBVIEW2_WEB_ERROR_STATUS_OPERATION_CANCELED) return S_OK; AcquireSRWLockShared(&Lock_EPWeather_Instance); EPWeather* _this = EPWeather_Instance; // GetWindowLongPtrW(FindWindowW(_T(EPW_WEATHER_CLASSNAME), NULL), GWLP_USERDATA); BOOL bIsSuccess = FALSE; @@ -1375,6 +1395,10 @@ DWORD WINAPI epw_Weather_MainThread(EPWeather* _this) cleanup: + if (_this->tkOnNavigationStarting.value) + { + _this->pCoreWebView2->lpVtbl->remove_NavigationStarting(_this->pCoreWebView2, _this->tkOnNavigationStarting); + } if (_this->tkOnNavigationCompleted.value) { _this->pCoreWebView2->lpVtbl->remove_NavigationCompleted(_this->pCoreWebView2, _this->tkOnNavigationCompleted); diff --git a/ep_weather_host/ep_weather_host.h b/ep_weather_host/ep_weather_host.h index 19182e4..fbe1036 100644 --- a/ep_weather_host/ep_weather_host.h +++ b/ep_weather_host/ep_weather_host.h @@ -78,6 +78,7 @@ typedef interface EPWeather ITaskbarList* pTaskList; ICoreWebView2Controller* pCoreWebView2Controller; ICoreWebView2* pCoreWebView2; + EventRegistrationToken tkOnNavigationStarting; EventRegistrationToken tkOnNavigationCompleted; EventRegistrationToken tkOnPermissionRequested; RECT rc; @@ -168,6 +169,7 @@ HRESULT STDMETHODCALLTYPE epw_Weather_static_QueryInterface(EPWeather* _this, RE HRESULT STDMETHODCALLTYPE ICoreWebView2_CreateCoreWebView2EnvironmentCompleted(ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler* _this, HRESULT errorCode, ICoreWebView2Environment* pCoreWebView2Environment); HRESULT STDMETHODCALLTYPE ICoreWebView2_CreateCoreWebView2ControllerCompleted(ICoreWebView2CreateCoreWebView2ControllerCompletedHandler* _this, HRESULT hr, ICoreWebView2Controller* pCoreWebView2Controller); +HRESULT STDMETHODCALLTYPE ICoreWebView2_NavigationStarting(ICoreWebView2NavigationStartingEventHandler* _this, ICoreWebView2* pCoreWebView2, ICoreWebView2NavigationStartingEventArgs* pCoreWebView2NavigationStartingEventArgs); HRESULT STDMETHODCALLTYPE ICoreWebView2_NavigationCompleted(ICoreWebView2NavigationCompletedEventHandler* _this, ICoreWebView2* pCoreWebView2, ICoreWebView2NavigationCompletedEventArgs* pCoreWebView2NavigationCompletedEventArgs); HRESULT STDMETHODCALLTYPE ICoreWebView2_ExecuteScriptCompleted(ICoreWebView2ExecuteScriptCompletedHandler* _this, HRESULT hr, LPCWSTR pResultObjectAsJson); HRESULT STDMETHODCALLTYPE ICoreWebView2_get_AdditionalBrowserArguments(ICoreWebView2EnvironmentOptions* _this, LPWSTR* value); @@ -201,6 +203,17 @@ static const ICoreWebView2CreateCoreWebView2ControllerCompletedHandler EPWeather .lpVtbl = &EPWeather_ICoreWebView2CreateCoreWebView2ControllerCompletedHandlerVtbl }; +static const ICoreWebView2NavigationStartingEventHandlerVtbl EPWeather_ICoreWebView2NavigationStartingEventHandlerVtbl = { + .QueryInterface = epw_Weather_static_QueryInterface, + .AddRef = epw_Weather_static_AddRefRelease, + .Release = epw_Weather_static_AddRefRelease, + .Invoke = ICoreWebView2_NavigationStarting, +}; + +static const ICoreWebView2NavigationStartingEventHandler EPWeather_ICoreWebView2NavigationStartingEventHandler = { + .lpVtbl = &EPWeather_ICoreWebView2NavigationStartingEventHandlerVtbl +}; + static const ICoreWebView2NavigationCompletedEventHandlerVtbl EPWeather_ICoreWebView2NavigationCompletedEventHandlerVtbl = { .QueryInterface = epw_Weather_static_QueryInterface, .AddRef = epw_Weather_static_AddRefRelease,