|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * GwtMaterial |
| 4 | + * %% |
| 5 | + * Copyright (C) 2015 - 2023 GwtMaterialDesign |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package gwt.material.design.client.base.viewport; |
| 21 | + |
| 22 | +import com.google.gwt.user.client.Element; |
| 23 | +import com.google.gwt.user.client.ui.RootPanel; |
| 24 | +import gwt.material.design.client.ui.MaterialToast; |
| 25 | +import gwt.material.design.jscore.client.api.viewport.VisualViewport; |
| 26 | + |
| 27 | +public class DefaultViewPortZoomHandler implements ViewPortZoomHandler { |
| 28 | + |
| 29 | + protected int zoomBoundaryLevel; |
| 30 | + |
| 31 | + public DefaultViewPortZoomHandler(int zoomBoundaryLevel) { |
| 32 | + this.zoomBoundaryLevel = zoomBoundaryLevel; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void load() { |
| 37 | + gwt.material.design.jscore.client.api.Window.visualViewport.addEventListener("resize", e -> { |
| 38 | + checkZoomLevel(); |
| 39 | + return true; |
| 40 | + }); |
| 41 | + |
| 42 | + gwt.material.design.jscore.client.api.Window.visualViewport.addEventListener("scroll", e -> { |
| 43 | + checkZoomLevel(); |
| 44 | + return true; |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void unload() { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + protected void checkZoomLevel() { |
| 54 | + VisualViewport viewport = gwt.material.design.jscore.client.api.Window.visualViewport; |
| 55 | + double width = viewport.width; |
| 56 | + double clientWidth = gwt.material.design.jscore.client.api.Window.outerWidth; |
| 57 | + int currentZoomLevel = (int) ((clientWidth / width) * 100); |
| 58 | + Element element = RootPanel.get().getElement(); |
| 59 | + if (currentZoomLevel >= zoomBoundaryLevel) { |
| 60 | + double excessZoom = currentZoomLevel - zoomBoundaryLevel; // 25 |
| 61 | + double scale = ((zoomBoundaryLevel - excessZoom) / 100.0); |
| 62 | + MaterialToast.fireToast("Current Zoom Level : " + currentZoomLevel); |
| 63 | + MaterialToast.fireToast("Zoom Level Boundary : " + zoomBoundaryLevel); |
| 64 | + |
| 65 | + // Standardized the page scale or zoom level. |
| 66 | + /** |
| 67 | + * transform: scale(0.5); |
| 68 | + * transform-origin: 0 0; |
| 69 | + * width: calc(100% + 100%); |
| 70 | + */ |
| 71 | + |
| 72 | + element.getStyle().setProperty("transform", "scale(" + 0.5 + ")"); |
| 73 | + element.getStyle().setProperty("transformOrigin", "0 0"); |
| 74 | + element.getStyle().setProperty("width", "200%"); |
| 75 | + } else { |
| 76 | + element.getStyle().setProperty("transform", "scale(" + 1 + ")"); |
| 77 | + element.getStyle().setProperty("width", "100%"); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments