Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Initial zoom level issue

Post Reply
petko
Posts: 52
Joined: Sun Jul 02, 2017 9:58 am

Initial zoom level issue

Post by petko »

I use CEF4Delphi in an application, which stores the browser zoom level between sessions, so when the user starts the app again, the browser is zoomed accordingly. It worked as expected until and including v71.0.3578.98, but after that it no longer works. Even though the ZoomPct is set on app start (after the chromium control is initialized), it no longer takes effect. Once app started is started and ready to use, changing the zoom works again.

I am aware that this is probably CEF bug and not CEF4Delphi bug, because I can't find any related change in the CEF4Delphi code regarding zooming. However I was wondering if you are aware of such an issue with CEF?
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Initial zoom level issue

Post by salvadordf »

Hi,

I did some quick tests and it seems that Chromium only accepts new zoom levels if you wait a second or so after the first web page is loaded. :shock:

Try to set the zoom using JavaScript
https://stackoverflow.com/questions/944 ... ad/9441618
https://stackoverflow.com/questions/210 ... 00-with-js
petko
Posts: 52
Joined: Sun Jul 02, 2017 9:58 am

Re: Initial zoom level issue

Post by petko »

Should I create a CEF issue in their source tracker? I don't think that they designed it to work after a delay..
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Initial zoom level issue

Post by salvadordf »

I searched for more solutions to this issue and it seems to be a known issue for several years.

You can add an issue in the CEF tracker but this seems to be a Chromium feature that changes with every Chromium update.

Some say that the browser initialization affects how the zoom is applied. If that's true, we can only find workarounds because it would be necessary to modify Chromium's code to fix the issue.

The workarounds I found are these :
  • Using JavaScript to set the zoom in CSS as described previously.
  • Starting a 50ms timer in the TChromium.OnAfterCreated to set the browser zoom on each timer tick.
  • Using the --force-device-scale-factor switch to change the device scale factor. This scale factor is NOT the same as the zoom level but it might be what you need.
If you want to test the --force-device-scale-factor switch then add this line before the GlobalCEFApp.StartMainProcess call in the DPR file :

Code: Select all

GlobalCEFApp.AddCustomCommandLine('--force-device-scale-factor', '1.5');
The value passed to that switch means that the browser will render the contents as if the screen has 1.5 times the standard 96 DPI. That means that it will force the browser to render the contents as if the screen has 144 DPI. You would need to set a new value depending on the monitor used by each user at run-time.
petko
Posts: 52
Joined: Sun Jul 02, 2017 9:58 am

Re: Initial zoom level issue

Post by petko »

Can the "--force-device-scale-factor" option be changed during the lifetime of the application or it requires app restart?
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Initial zoom level issue

Post by salvadordf »

petko wrote: Sat Apr 27, 2019 6:27 pm Can the "--force-device-scale-factor" option be changed during the lifetime of the application or it requires app restart?
That switch requires app restart. If you need to change the zoom at run-time use JavaScript to set the zoom in CSS.
petko
Posts: 52
Joined: Sun Jul 02, 2017 9:58 am

Re: Initial zoom level issue

Post by petko »

Thanks, I will use the OnAfterCreated approach. Seems to work!
Post Reply