From be46962ec25b4ed3bf10260f3cc56825f6488a0e Mon Sep 17 00:00:00 2001 From: cztomczak Date: Sun, 19 Aug 2018 13:55:46 +0200 Subject: [PATCH] Add DisplayHandler.OnLoadingProgressChange (#403) --- README.md | 5 +- api/API-index.md | 5 +- api/DisplayHandler.md | 66 +++++++++++++++----------- src/browser.pyx | 3 +- src/cef_v59..v66_changes.txt | 2 +- src/client_handler/display_handler.cpp | 6 +++ src/client_handler/display_handler.h | 3 ++ src/handlers/display_handler.pyx | 15 ++++++ unittests/main_test.py | 11 ++++- 9 files changed, 82 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index cbe04d95..f736baef 100644 --- a/README.md +++ b/README.md @@ -527,10 +527,11 @@ Additional information for v31.2 release: * [DisplayHandler (interface)](api/DisplayHandler.md#displayhandler-interface) * [OnAddressChange](api/DisplayHandler.md#onaddresschange) * [OnAutoResize](api/DisplayHandler.md#onautoresize) + * [OnConsoleMessage](api/DisplayHandler.md#onconsolemessage) + * [OnLoadingProgressChange](api/DisplayHandler.md#onloadingprogresschange) + * [OnStatusMessage](api/DisplayHandler.md#onstatusmessage) * [OnTitleChange](api/DisplayHandler.md#ontitlechange) * [OnTooltip](api/DisplayHandler.md#ontooltip) - * [OnStatusMessage](api/DisplayHandler.md#onstatusmessage) - * [OnConsoleMessage](api/DisplayHandler.md#onconsolemessage) * [DownloadHandler](api/DownloadHandler.md#downloadhandler) * [DpiAware (class)](api/DpiAware.md#dpiaware-class) * [CalculateWindowSize](api/DpiAware.md#calculatewindowsize) diff --git a/api/API-index.md b/api/API-index.md index a7b17aaf..050fb753 100644 --- a/api/API-index.md +++ b/api/API-index.md @@ -215,10 +215,11 @@ * [DisplayHandler (interface)](DisplayHandler.md#displayhandler-interface) * [OnAddressChange](DisplayHandler.md#onaddresschange) * [OnAutoResize](DisplayHandler.md#onautoresize) + * [OnConsoleMessage](DisplayHandler.md#onconsolemessage) + * [OnLoadingProgressChange](DisplayHandler.md#onloadingprogresschange) + * [OnStatusMessage](DisplayHandler.md#onstatusmessage) * [OnTitleChange](DisplayHandler.md#ontitlechange) * [OnTooltip](DisplayHandler.md#ontooltip) - * [OnStatusMessage](DisplayHandler.md#onstatusmessage) - * [OnConsoleMessage](DisplayHandler.md#onconsolemessage) * [DownloadHandler](DownloadHandler.md#downloadhandler) * [DpiAware (class)](DpiAware.md#dpiaware-class) * [CalculateWindowSize](DpiAware.md#calculatewindowsize) diff --git a/api/DisplayHandler.md b/api/DisplayHandler.md index 43f6342f..2c32ec1f 100644 --- a/api/DisplayHandler.md +++ b/api/DisplayHandler.md @@ -3,19 +3,19 @@ # DisplayHandler (interface) -Implement this interface to handle events related to browser display state. The methods of this class will be called on the UI thread. - -For an example of how to implement handler see [cefpython](cefpython.md).CreateBrowser(). For a list of all handler interfaces see [API > Client handlers](API#Client_handlers). +Implement this interface to handle events related to browser display +state. The methods of this class will be called on the UI thread. Table of contents: * [Callbacks](#callbacks) * [OnAddressChange](#onaddresschange) * [OnAutoResize](#onautoresize) + * [OnConsoleMessage](#onconsolemessage) + * [OnLoadingProgressChange](#onloadingprogresschange) + * [OnStatusMessage](#onstatusmessage) * [OnTitleChange](#ontitlechange) * [OnTooltip](#ontooltip) - * [OnStatusMessage](#onstatusmessage) - * [OnConsoleMessage](#onconsolemessage) ## Callbacks @@ -49,31 +49,34 @@ Description from upstream CEF: > default handling. -### OnTitleChange +### OnConsoleMessage | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| title | string | -| __Return__ | void | +| level | int | +| message | string | +| source | string | +| line | int | +| __Return__ | bool | -Called when the page title changes. +Called to display a console message. Return true to stop the message from +being output to the console. +|level| can be one of the same values as in ApplicationSettings.[log_severity](ApplicationSettings.md#log_severity). -### OnTooltip + +### OnLoadingProgressChange | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| text_out | list | -| __Return__ | bool | +| progress | double | +| __Return__ | void | -Called when the browser is about to display a tooltip. `text_out[0]` contains the -text that will be displayed in the tooltip. To handle the display of the -tooltip yourself return true. Otherwise, you can optionally modify `text_out[0]` -and then return false to allow the browser to display the tooltip. -When window rendering is disabled the application is responsible for -drawing tooltips and the return value is ignored. +Description from upstream CEF: +> Called when the overall page loading progress has changed. |progress| +> ranges from 0.0 to 1.0. ### OnStatusMessage @@ -87,19 +90,28 @@ drawing tooltips and the return value is ignored. Called when the browser receives a status message. -### OnConsoleMessage +### OnTitleChange | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| level | int | -| message | string | -| source | string | -| line | int | -| __Return__ | bool | +| title | string | +| __Return__ | void | -Called to display a console message. Return true to stop the message from -being output to the console. +Called when the page title changes. -|level| can be one of the same values as in ApplicationSettings.[log_severity](ApplicationSettings.md#log_severity). +### OnTooltip + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| text_out | list | +| __Return__ | bool | + +Called when the browser is about to display a tooltip. `text_out[0]` contains the +text that will be displayed in the tooltip. To handle the display of the +tooltip yourself return true. Otherwise, you can optionally modify `text_out[0]` +and then return false to allow the browser to display the tooltip. +When window rendering is disabled the application is responsible for +drawing tooltips and the return value is ignored. diff --git a/src/browser.pyx b/src/browser.pyx index bec49de2..c6060d44 100644 --- a/src/browser.pyx +++ b/src/browser.pyx @@ -235,7 +235,8 @@ cdef class PyBrowser: # DisplayHandler self.allowedClientCallbacks += [ "OnAddressChange", "OnTitleChange", "OnTooltip", - "OnStatusMessage", "OnConsoleMessage", "OnAutoResize"] + "OnStatusMessage", "OnConsoleMessage", "OnAutoResize", + "OnLoadingProgressChange"] # KeyboardHandler self.allowedClientCallbacks += ["OnPreKeyEvent", "OnKeyEvent"] # RequestHandler diff --git a/src/cef_v59..v66_changes.txt b/src/cef_v59..v66_changes.txt index 73e8ed2f..a0429e0c 100644 --- a/src/cef_v59..v66_changes.txt +++ b/src/cef_v59..v66_changes.txt @@ -107,7 +107,7 @@ cef_cookie.h cef_display_handler.h + OnAutoResize -- OnLoadingProgressChange ++ OnLoadingProgressChange cef_drag_data.h + GetImage (cross-platform) diff --git a/src/client_handler/display_handler.cpp b/src/client_handler/display_handler.cpp index f5ad596e..b00bf6a5 100644 --- a/src/client_handler/display_handler.cpp +++ b/src/client_handler/display_handler.cpp @@ -54,3 +54,9 @@ bool DisplayHandler::OnAutoResize(CefRefPtr browser, REQUIRE_UI_THREAD(); return DisplayHandler_OnAutoResize(browser, new_size); } + +void DisplayHandler::OnLoadingProgressChange(CefRefPtr browser, + double progress) { + REQUIRE_UI_THREAD(); + return DisplayHandler_OnLoadingProgressChange(browser, progress); +} \ No newline at end of file diff --git a/src/client_handler/display_handler.h b/src/client_handler/display_handler.h index f058c475..a281713d 100644 --- a/src/client_handler/display_handler.h +++ b/src/client_handler/display_handler.h @@ -36,6 +36,9 @@ class DisplayHandler : public CefDisplayHandler bool OnAutoResize(CefRefPtr browser, const CefSize& new_size) override; + void OnLoadingProgressChange(CefRefPtr browser, + double progress) override; + private: IMPLEMENT_REFCOUNTING(DisplayHandler); }; diff --git a/src/handlers/display_handler.pyx b/src/handlers/display_handler.pyx index c60e49bc..7d66d00f 100644 --- a/src/handlers/display_handler.pyx +++ b/src/handlers/display_handler.pyx @@ -128,3 +128,18 @@ cdef public cpp_bool DisplayHandler_OnConsoleMessage( except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) + +cdef public void DisplayHandler_OnLoadingProgressChange( + CefRefPtr[CefBrowser] cefBrowser, + double progress + ) except * with gil: + cdef PyBrowser pyBrowser + cdef object callback + try: + pyBrowser = GetPyBrowser(cefBrowser, "OnLoadingProgressChange") + callback = pyBrowser.GetClientCallback("OnLoadingProgressChange") + if callback: + callback(browser=pyBrowser, progress=progress) + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/unittests/main_test.py b/unittests/main_test.py index 5f856591..3ffd1d4f 100644 --- a/unittests/main_test.py +++ b/unittests/main_test.py @@ -168,9 +168,10 @@ def test_main(self): subtest_message("cef.CreateBrowserSync() ok") # Client handlers + display_handler2 = DisplayHandler2(self) client_handlers = [LoadHandler(self, g_datauri), DisplayHandler(self), - DisplayHandler2(self)] + display_handler2] for handler in client_handlers: browser.SetClientHandler(handler) subtest_message("browser.SetClientHandler() ok") @@ -269,6 +270,8 @@ def test_main(self): do_message_loop_work(25) # Asserts before shutdown + self.assertEqual(display_handler2.OnLoadingProgressChange_Progress, + 1.0) # noinspection PyTypeChecker check_auto_asserts(self, [] + client_handlers + [global_handler, @@ -290,6 +293,8 @@ def __init__(self, test_case): # Test whether asserts are working correctly. self.test_for_True = True self.OnAutoResize_True = False + self.OnLoadingProgressChange_True = False + self.OnLoadingProgressChange_Progress = 0.0 def OnAutoResize(self, new_size, **_): self.OnAutoResize_True = True @@ -298,6 +303,10 @@ def OnAutoResize(self, new_size, **_): self.test_case.assertGreaterEqual(new_size[1], 600) self.test_case.assertLessEqual(new_size[1], 768) + def OnLoadingProgressChange(self, progress, **_): + self.OnLoadingProgressChange_True = True + self.OnLoadingProgressChange_Progress = progress + class External(object): """Javascript 'window.external' object."""