Best way to set a ZoomLevel at browser startup

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Best way to set a ZoomLevel at browser startup

Postby remistp » Wed Aug 27, 2014 8:00 am

I'm building a project from the cefclient. I have to set a default ZoomValue at startup (-1.5). I tried to it by several ways to call the modifyzoom function:

cefclient_win.cpp
- end of InitInstance()
- in WndProcess at the end of the WM_CREATE case
client_handler.cpp
- in ClientHandler::OnAfterCreated

The modification was never effective, like if the page as to be loaded before I can modify a zoom level. For a debugging purpose, I threw a messagebox in the "ModifyZoom(CefRefPtr<CefBrowser> browser, double delta)" function to make sure it was going through that code. That way, zoom level applied correctly, but not when I removed the messageBox.

The way i found it to work was adding the call to setzoomlevel in ClientHandler::OnLoadingStateChange every time a page is loaded. It works but that's not very clean. Would there be a prettier way of doing that ?
Thank you

Code: Select all
void ClientHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
                                         bool isLoading,
                                         bool canGoBack,
                                         bool canGoForward) {
  REQUIRE_UI_THREAD();
  char str[256];

  SetLoading(isLoading);
  SetNavState(canGoBack, canGoForward);

  if (!isLoading) {
   if(browser->GetHost()->GetZoomLevel() != ZOOM_LEVEL)
      browser->GetHost()->SetZoomLevel(ZOOM_LEVEL);
   sprintf(str, "OnLoadingStateChange notLoading\n");
    OutputDebugStringA(str);
    // Continue the DOM test.
    if (browser->GetMainFrame()->GetURL() == dom_test::kTestUrl)
      dom_test::OnLoadEnd(browser);
  }
  else
  {
   sprintf(str, "OnLoadingStateChange Loading\n");
    OutputDebugStringA(str);
  }
}
remistp
Newbie
 
Posts: 1
Joined: Wed Aug 27, 2014 7:39 am

Re: Best way to set a ZoomLevel at browser startup

Postby magreenblatt » Wed Aug 27, 2014 3:53 pm

remistp wrote:The way i found it to work was adding the call to setzoomlevel in ClientHandler::OnLoadingStateChange every time a page is loaded.

That's the best way to do it for now. A better way may be provided by https://code.google.com/p/chromiumembed ... il?id=1314.
magreenblatt
Site Admin
 
Posts: 12402
Joined: Fri May 29, 2009 6:57 pm

Re: Best way to set a ZoomLevel at browser startup

Postby Czarek » Thu Aug 28, 2014 1:22 am

Using OnLoadingStateChange to set zoom level is not a good idea from user experience, as it causes a jump effect of the whole page when text resizes after page has completed loading. In my case I'm setting zoom level in CefLifeSpanHandler::OnAfterCreated and am checking constantly every 50ms if the zoom level is correct and if not try to set new zoom level. You can use CefPostDelayedTask to call your function at 50ms interval. This way zoom level is set during loading of a page and user doesn't see page jumping when fonts are resized.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 36 guests