Skip to content

Commit

Permalink
Add opt-in method to propagate zoom changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akoch-yatta committed Mar 28, 2024
1 parent d76f147 commit f98648b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DPIUtil {
private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
private static AutoScaleMethod autoScaleMethodSetting = AutoScaleMethod.AUTO;
private static AutoScaleMethod autoScaleMethod = AutoScaleMethod.NEAREST;
public static boolean autoScaleOnRuntime = false;

private static String autoScaleValue;
private static boolean useCairoAutoScale = false;
Expand Down Expand Up @@ -84,6 +85,16 @@ private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
* <a href="https://bugs.eclipse.org/493455">bug 493455</a>.
*/
private static final String SWT_AUTOSCALE_METHOD = "swt.autoScale.method";

/**
* System property to enable to scale the applicaiton on runtime
* when a DPI change is detected.
* <ul>
* <li>"true": the application is scaled on DPI changes</li>
* <li>"false": the application will remain in its initial scaling</li>
* </ul>
*/
private static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
static {
autoScaleValue = System.getProperty (SWT_AUTOSCALE);

Expand All @@ -95,6 +106,9 @@ private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
autoScaleMethod = autoScaleMethodSetting = AutoScaleMethod.SMOOTH;
}
}

String updateOnRuntimeValue = System.getProperty (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
autoScaleOnRuntime = Boolean.parseBoolean(updateOnRuntimeValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2024 Yatta Solutions and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.internal;

import org.eclipse.swt.widgets.*;

@FunctionalInterface
public interface DPIZoomChangeHandler {
public void handleDPIChange(Widget widget, int newZoom, float scalingFactor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2024 Yatta Solutions and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.internal;

import java.util.*;
import java.util.Map.*;

import org.eclipse.swt.widgets.*;

public class DPIZoomChangeRegistry {

private static Map<Class<? extends Widget>, DPIZoomChangeHandler> dpiZoomChangeHandlers = new TreeMap<>(
(o1, o2) -> {
if(o1.isAssignableFrom(o2)) {
return -1;
}
if(o2.isAssignableFrom(o1)) {
return 1;
}
return o1.getName().compareTo(o2.getName());
});

public static <E> void applyChange(Widget widget, int newZoom, float scalingFactor) {
for (Entry<Class<? extends Widget>, DPIZoomChangeHandler> entry : dpiZoomChangeHandlers.entrySet()) {
Class<? extends Widget> clazz = entry.getKey();
DPIZoomChangeHandler handler = entry.getValue();
if (clazz.isInstance(widget)) {
handler.handleDPIChange(widget, newZoom, scalingFactor);
}
}
}

public static <E> void registerHandler(DPIZoomChangeHandler zoomChangeVisitor, Class<? extends Widget> clazzToRegisterFor) {
dpiZoomChangeHandlers.put(clazzToRegisterFor, zoomChangeVisitor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*/
public abstract class Widget {

private int currentDeviceZoom;
int style, state;
Display display;
EventTable eventTable;
Expand Down Expand Up @@ -166,6 +168,7 @@ public Widget (Widget parent, int style) {
checkSubclass ();
checkParent (parent);
this.style = style;
this.currentDeviceZoom = parent != null ? parent.getCurrentDeviceZoom() : DPIUtil.getDeviceZoom();
display = parent.display;
reskinWidget ();
notifyCreationTracker();
Expand Down Expand Up @@ -2631,4 +2634,24 @@ void notifyDisposalTracker() {
}
}


/**
* The current DPI zoom level the widget is scaled for
* (Warning: This field is platform dependent)
* <p>
* <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms and should never be accessed from application code.
* </p>
*
* @noreference This field is not intended to be referenced by clients.
*/
public int getCurrentDeviceZoom() {
return currentDeviceZoom;
}

void setCurrentDeviceZoom(int currentDeviceZoom) {
this.currentDeviceZoom = currentDeviceZoom;
}
}

0 comments on commit f98648b

Please sign in to comment.