pywebview icon indicating copy to clipboard operation
pywebview copied to clipboard

Prevent pinch zoom

Open RodolfoPichardo opened this issue 5 years ago • 17 comments

Is there any way to prevent/limit pinch zoom on the browser?

Currently, when one does pinch zoom one could make the page as small as one wants, which does not look aesthetically pleasing.

RodolfoPichardo avatar Dec 20 '18 17:12 RodolfoPichardo

Platform: Ubuntu/GTK Version: 2.2.1

RodolfoPichardo avatar Dec 20 '18 18:12 RodolfoPichardo

Currently there isn't. Have you tried using HTML/JavaScript?

As to the philosophical question of disabling zoom... A11Y. http://adrianroselli.com/2015/10/dont-disable-zoom.html

shivaprsd avatar Dec 21 '18 12:12 shivaprsd

I agree with the article; however, webview takes this to an extreme:

  • You can zoom-in all you want, heck, I even made the browser unresponsive by zooming-in too far (about ten "pinches" did the trick)
  • You can zoom-out all you want too, which could make the whole page as small as a pixel, which is useless; in fact, zooming-out in its entirety is pretty useless to the user in most situations.

RodolfoPichardo avatar Dec 21 '18 14:12 RodolfoPichardo

I would leave it to the discernment of the user whether to do or not to do such apparently stupid things. I would only bother to intervene considering:

  • The user can land in such troubles accidentally (but ten pinches is far from an accident).
  • To make the behaviour uniform across platforms (for instance, zoom is not enabled by default on macOS webview).

Let's hear Roman's take on this.

shivaprsd avatar Dec 22 '18 04:12 shivaprsd

In some applications it is necessary to "idiot-proof" the software.

For instance, if we were to use the browser in a library to allow the public to lookup books, we would not want an user to be able to incapacitate the machine and prevent others from using it.

RodolfoPichardo avatar Dec 24 '18 14:12 RodolfoPichardo

You can prevent pinch zoom using HTML https://stackoverflow.com/questions/11689353/disable-pinch-zoom-on-mobile-web

I don't feel that making this a default setting is a right choice.

r0x0r avatar Dec 24 '18 15:12 r0x0r

The solutions provided in the stackoverflow thread do not work.

I've managed to get my hands on an iOS device (the only other product with WebKit), and can confirm that the fifth answer works on safari but not on pywebview.

Also, it is important to note, that all major browsers, have the pinch-out feature disabled.

RodolfoPichardo avatar Dec 24 '18 15:12 RodolfoPichardo

If it can be implemented with little fuss (on all platforms), I have no objection against making it a create_window flag (zoomable=[True|False]).

shivaprsd avatar Dec 24 '18 18:12 shivaprsd

Stale issue message

github-actions[bot] avatar Oct 29 '19 00:10 github-actions[bot]

This can be done by injecting the JS snippet described here https://stackoverflow.com/questions/37808180/disable-viewport-zooming-ios-10-safari

I have also added <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0"> to the default HTML.

r0x0r avatar Mar 02 '20 13:03 r0x0r

What about windows IE/11? I have tried everything but can't make it work in order to disable zoom.

martinlombana avatar May 28 '20 15:05 martinlombana

Ok, I found it. I was missing the mousewheel, which is the thing I wanted to disable the most.

""" setTimeout(function() { document.getElementById('loader').style.display = 'none' document.getElementById('main').style.display = 'block' }, 5000)

      document.addEventListener('touchmove', function (event) {

if (event.scale !== 1) { event.preventDefault(); } }, false);

var lastTouchEnd = 0; document.addEventListener('touchend', function (event) { var now = (new Date()).getTime(); if (now - lastTouchEnd <= 300) { event.preventDefault(); } lastTouchEnd = now; }, false);

window.addEventListener("wheel", function(e){e.preventDefault();}, {passive: false} ); """

martinlombana avatar May 28 '20 15:05 martinlombana

@martinlombana Good work! Care to create a PR? Public API could be create_window(...zoomable=False)

r0x0r avatar Jun 16 '20 21:06 r0x0r

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Aug 26 '20 01:08 github-actions[bot]

I was also experiencing this problem. I run a kios powered by Raspbian 10 Buster with a touchscreen and I use pywebview as GUI for a python application. I could not find any way to disable the pinch zoom through javascript or html. If it was possible to create a parameter such as zoom_enabled=False/True it would be fantastic

gmm9200 avatar Dec 02 '20 13:12 gmm9200

Hey, I implemented this code to disable pinch zoom using javascript:

document.body.addEventListener('touchstart', function(e) { if ( (e.touches.length > 1) || e.targetTouches.length > 1) { e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); } }, {passive: false});

Hope it helps.

m-bagnarelli avatar Oct 07 '21 20:10 m-bagnarelli

I have implemented @m-bagnarelli's zoomable solution in the 4.0 branch. The syntax is webview.create_window(zoomable=False)

Please test.

r0x0r avatar Apr 13 '22 20:04 r0x0r

I have implemented @m-bagnarelli's zoomable solution in the 4.0 branch. The syntax is webview.create_window(zoomable=False)

Please test.

It doesn't work on windows with Control+MouseWheel: Touchstart works for touch screens.

I guess a mix of both his solution and mine, should be implemented.

martinlombana avatar Sep 24 '22 21:09 martinlombana

This one is needed for WINDOWS/MAC? Control+MouseWheel:

    window.addEventListener("wheel", function (e) {
        if (e.ctrlKey) {
            e.preventDefault();
        }
    }, {passive: false});

martinlombana avatar Sep 26 '22 17:09 martinlombana

@martinlombana Thanks for the fix, I pushed it to the 4.0 branch.

r0x0r avatar Nov 15 '22 17:11 r0x0r