Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8343398: Add reducedData preference #1656

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions buildSrc/mac.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def dynamicLinkFlags = [
"-framework", "OpenGL",
"-framework", "QuartzCore",
"-framework", "Security",
"-framework", "Network",
"-dynamiclib", "-lobjc"].flatten();
def dynamicLinkFlagsAlt = ["-dynamiclib", commonParams].flatten()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ protected boolean _supportsInputMethods() {
"GTK.theme_bg_color", new PreferenceMapping<>("backgroundColor", Color.class),
"GTK.theme_selected_bg_color", new PreferenceMapping<>("accentColor", Color.class),
"GTK.enable_animations", new PreferenceMapping<>("reducedMotion", Boolean.class, b -> !b),
"GTK.overlay_scrolling", new PreferenceMapping<>("persistentScrollBars", Boolean.class, b -> !b)
"GTK.overlay_scrolling", new PreferenceMapping<>("persistentScrollBars", Boolean.class, b -> !b),
"GTK.network_metered", new PreferenceMapping<>("reducedData", Boolean.class)
);
}

Expand Down Expand Up @@ -482,7 +483,8 @@ public Map<String, Class<?>> getPlatformKeys() {
Map.entry("GTK.error_color", Color.class),
Map.entry("GTK.success_color", Color.class),
Map.entry("GTK.enable_animations", Boolean.class),
Map.entry("GTK.overlay_scrolling", Boolean.class)
Map.entry("GTK.overlay_scrolling", Boolean.class),
Map.entry("GTK.network_metered", Boolean.class)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ private static void finishKeepAliveThread() {

private Menu appleMenu;

native void _runLoop(ClassLoader classLoader, Runnable launchable,
boolean isTaskbarApplication);
private long delegateHandle;

native long _initDelegate(ClassLoader classLoader, Runnable launchable, boolean isTaskbarApplication);

native void _runLoop(long delegate);

@Override
protected void runLoop(final Runnable launchable) {
// For normal (not embedded) taskbar applications the masOS activation
Expand All @@ -128,7 +132,8 @@ protected void runLoop(final Runnable launchable) {
startKeepAliveThread();

ClassLoader classLoader = MacApplication.class.getClassLoader();
_runLoop(classLoader, wrappedRunnable, isTaskbarApplication);
delegateHandle = _initDelegate(classLoader, wrappedRunnable, isTaskbarApplication);
_runLoop(delegateHandle);
}

private final CountDownLatch reactivationLatch = new CountDownLatch(1);
Expand All @@ -154,10 +159,10 @@ void waitForReactivation() {
eventLoop.enter();
}

native private void _finishTerminating();
native private void _finishTerminating(long delegateHandle);
@Override
protected void finishTerminating() {
_finishTerminating();
_finishTerminating(delegateHandle);
finishKeepAliveThread();

super.finishTerminating();
Expand Down Expand Up @@ -420,8 +425,12 @@ public String getDataDirectory() {

private native String _getApplicationClassName();

private native Map<String, Object> _getPlatformPreferences(long delegateHandle);

@Override
public native Map<String, Object> getPlatformPreferences();
public Map<String, Object> getPlatformPreferences() {
return _getPlatformPreferences(delegateHandle);
}

@Override
public Map<String, PreferenceMapping<?, ?>> getPlatformKeyMappings() {
Expand All @@ -431,7 +440,8 @@ public String getDataDirectory() {
"macOS.NSColor.controlAccentColor", new PreferenceMapping<>("accentColor", Color.class),
"macOS.NSWorkspace.accessibilityDisplayShouldReduceMotion", new PreferenceMapping<>("reducedMotion", Boolean.class),
"macOS.NSWorkspace.accessibilityDisplayShouldReduceTransparency", new PreferenceMapping<>("reducedTransparency", Boolean.class),
"macOS.NSScroller.preferredScrollerStyle", new PreferenceMapping<>("persistentScrollBars", String.class, "NSScrollerStyleLegacy"::equals)
"macOS.NSScroller.preferredScrollerStyle", new PreferenceMapping<>("persistentScrollBars", String.class, "NSScrollerStyleLegacy"::equals),
"macOS.NWPathMonitor.currentPathConstrained", new PreferenceMapping<>("reducedData", Boolean.class)
);
}

Expand Down Expand Up @@ -487,7 +497,9 @@ public Map<String, Class<?>> getPlatformKeys() {
Map.entry("macOS.NSColor.systemYellowColor", Color.class),
Map.entry("macOS.NSWorkspace.accessibilityDisplayShouldReduceMotion", Boolean.class),
Map.entry("macOS.NSWorkspace.accessibilityDisplayShouldReduceTransparency", Boolean.class),
Map.entry("macOS.NSScroller.preferredScrollerStyle", String.class)
Map.entry("macOS.NSScroller.preferredScrollerStyle", String.class),
Map.entry("macOS.NWPathMonitor.currentPathConstrained", Boolean.class),
Map.entry("macOS.NWPathMonitor.currentPathExpensive", Boolean.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,12 @@ public String getDataDirectory() {
"Windows.UIColor.Accent", new PreferenceMapping<>("accentColor", Color.class),
"Windows.UISettings.AdvancedEffectsEnabled", new PreferenceMapping<>("reducedTransparency", Boolean.class, b -> !b),
"Windows.UISettings.AutoHideScrollBars", new PreferenceMapping<>("persistentScrollBars", Boolean.class, b -> !b),
"Windows.SPI.ClientAreaAnimation", new PreferenceMapping<>("reducedMotion", Boolean.class, b -> !b)
"Windows.SPI.ClientAreaAnimation", new PreferenceMapping<>("reducedMotion", Boolean.class, b -> !b),
"Windows.NetworkInformation.InternetCostType", new PreferenceMapping<>(
"reducedData", String.class, v -> switch (v) {
case "Unknown", "Unrestricted" -> false;
default -> true;
})
);
}

Expand Down Expand Up @@ -393,7 +398,8 @@ public Map<String, Class<?>> getPlatformKeys() {
Map.entry("Windows.UIColor.AccentLight2", Color.class),
Map.entry("Windows.UIColor.AccentLight3", Color.class),
Map.entry("Windows.UISettings.AdvancedEffectsEnabled", Boolean.class),
Map.entry("Windows.UISettings.AutoHideScrollBars", Boolean.class)
Map.entry("Windows.UISettings.AutoHideScrollBars", Boolean.class),
Map.entry("Windows.NetworkInformation.InternetCostType", String.class)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ public boolean isReducedTransparency() {
return properties.isReducedTransparency();
}

@Override
public ReadOnlyBooleanProperty reducedDataProperty() {
return properties.reducedDataProperty();
}

@Override
public boolean isReducedData() {
return properties.isReducedData();
}

@Override
public ReadOnlyBooleanProperty persistentScrollBarsProperty() {
return properties.persistentScrollBarsProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ final class PreferenceProperties {
private final ColorSchemeProperty colorScheme = new ColorSchemeProperty();
private final DeferredProperty<Boolean> reducedMotion = new DeferredProperty<>("reducedMotion", false);
private final DeferredProperty<Boolean> reducedTransparency = new DeferredProperty<>("reducedTransparency", false);
private final DeferredProperty<Boolean> reducedData = new DeferredProperty<>("reducedData", false);
private final DeferredProperty<Boolean> persistentScrollBars = new DeferredProperty<>("persistentScrollBars", false);
private final ReadOnlyBooleanWrapper reducedMotionFlag;
private final ReadOnlyBooleanWrapper reducedTransparencyFlag;
private final ReadOnlyBooleanWrapper reducedDataFlag;
private final ReadOnlyBooleanWrapper persistentScrollBarsFlag;
private final Object bean;

Expand All @@ -66,6 +68,9 @@ final class PreferenceProperties {
reducedTransparencyFlag = new ReadOnlyBooleanWrapper(bean, reducedTransparency.getName());
reducedTransparencyFlag.bind(reducedTransparency);

reducedDataFlag = new ReadOnlyBooleanWrapper(bean, reducedData.getName());
reducedDataFlag.bind(reducedData);

persistentScrollBarsFlag = new ReadOnlyBooleanWrapper(bean, persistentScrollBars.getName());
persistentScrollBarsFlag.bind(persistentScrollBars);
}
Expand Down Expand Up @@ -94,6 +99,18 @@ public void setReducedTransparency(boolean value) {
reducedTransparency.setValueOverride(value);
}

public ReadOnlyBooleanProperty reducedDataProperty() {
return reducedDataFlag.getReadOnlyProperty();
}

public boolean isReducedData() {
return reducedData.get();
}

public void setReducedData(boolean value) {
reducedData.setValueOverride(value);
}

public ReadOnlyBooleanProperty persistentScrollBarsProperty() {
return persistentScrollBarsFlag.getReadOnlyProperty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static Preferences getPreferences() {
}

/**
* Contains UI preferences of the current platform.
* Contains preferences of the current platform.
* <p>
* {@code Preferences} extends {@link ObservableMap} to expose platform preferences as key-value pairs.
* The map is unmodifiable, which means that keys and values cannot be added, removed, or updated.
Expand Down Expand Up @@ -498,6 +498,7 @@ public static Preferences getPreferences() {
* <tr><td>{@code Windows.UIColor.AccentLight3}</td><td>{@link Color}</td></tr>
* <tr><td>{@code Windows.UISettings.AdvancedEffectsEnabled}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code Windows.UISettings.AutoHideScrollBars}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code Windows.NetworkInformation.InternetCostType}</td><td>{@link String}</td></tr>
* <tr></tr>
* </tbody>
* </table>
Expand Down Expand Up @@ -553,6 +554,8 @@ public static Preferences getPreferences() {
* <tr><td>{@code macOS.NSWorkspace.accessibilityDisplayShouldReduceMotion}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code macOS.NSWorkspace.accessibilityDisplayShouldReduceTransparency}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code macOS.NSScroller.preferredScrollerStyle}</td><td>{@link String}</td></tr>
* <tr><td>{@code macOS.NWPathMonitor.currentPathConstrained}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code macOS.NWPathMonitor.currentPathExpensive}</td><td>{@link Boolean}</td></tr>
* <tr></tr>
* </tbody>
* </table>
Expand Down Expand Up @@ -580,6 +583,7 @@ public static Preferences getPreferences() {
* <tr><td>{@code GTK.success_color}</td><td>{@link Color}</td></tr>
* <tr><td>{@code GTK.enable_animations}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code GTK.overlay_scrolling}</td><td>{@link Boolean}</td></tr>
* <tr><td>{@code GTK.network_metered}</td><td>{@link Boolean}</td></tr>
* <tr></tr>
* </tbody>
* </table>
Expand Down Expand Up @@ -631,6 +635,20 @@ public sealed interface Preferences extends ObservableMap<String, Object>

boolean isReducedTransparency();

/**
* Specifies whether applications should minimize the amount of internet traffic, which users
* might request because they are on a metered network or a limited data plan.
* <p>
* If the platform does not report this preference, this property defaults to {@code false}.
*
* @return the {@code reducedData} property
* @defaultValue {@code false}
* @since 24
*/
ReadOnlyBooleanProperty reducedDataProperty();

boolean isReducedData();

/**
* The platform color scheme, which specifies whether applications should prefer light text on
* dark backgrounds, or dark text on light backgrounds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,6 @@ static gboolean call_runnable (gpointer data)
return FALSE;
}

static void call_update_preferences()
{
if (platformSupport) {
platformSupport->updatePreferences();
}
}

extern "C" {

#pragma GCC diagnostic push
Expand Down Expand Up @@ -197,14 +190,6 @@ JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1init
gdk_window_set_events(root, static_cast<GdkEventMask>(gdk_window_get_events(root) | GDK_PROPERTY_CHANGE_MASK));

platformSupport = new PlatformSupport(env, obj);

GtkSettings* settings = gtk_settings_get_default();
if (settings != NULL) {
for (const auto& setting : PlatformSupport::observedSettings) {
g_signal_connect_after(G_OBJECT(settings), setting,
G_CALLBACK(call_update_preferences), NULL);
}
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,45 @@ namespace
env->CallObjectMethod(preferences, jMapPut, prefKey, prefValue);
CHECK_JNI_EXCEPTION(env);
}

void notifySettingChanged(GObject*, GParamSpec*, PlatformSupport* instance) {
instance->updatePreferences();
}

void notifyNetworkChanged(GNetworkMonitor*, gboolean, PlatformSupport* instance) {
instance->updatePreferences();
}
}

PlatformSupport::PlatformSupport(JNIEnv* env, jobject application)
: env(env), application(env->NewGlobalRef(application)), preferences(NULL) {}
: env(env), application(env->NewGlobalRef(application)), preferences(NULL) {
GtkSettings* settings = gtk_settings_get_default();
if (settings != NULL) {
for (int i = 0; i < NUM_OBSERVED_SETTINGS; ++i) {
settingChangedHandlers[i] = g_signal_connect_data(
G_OBJECT(settings), OBSERVED_SETTINGS[i],
G_CALLBACK(notifySettingChanged), this,
NULL, G_CONNECT_AFTER);
}
}

networkChangedHandler = g_signal_connect_data(
G_OBJECT(g_network_monitor_get_default()), "network-changed",
G_CALLBACK(notifyNetworkChanged), this,
NULL, G_CONNECT_AFTER);
}

PlatformSupport::~PlatformSupport() {
GtkSettings* settings = gtk_settings_get_default();

for (int i = 0; i < NUM_OBSERVED_SETTINGS; ++i) {
if (settingChangedHandlers[i] != 0) {
g_signal_handler_disconnect(G_OBJECT(settings), settingChangedHandlers[i]);
}
}

g_signal_handler_disconnect(G_OBJECT(g_network_monitor_get_default()), networkChangedHandler);

env->DeleteGlobalRef(application);

if (preferences) {
Expand Down Expand Up @@ -129,6 +162,10 @@ jobject PlatformSupport::collectPreferences() const {
}
}

GNetworkMonitor* networkMonitor = g_network_monitor_get_default();
bool metered = g_network_monitor_get_network_metered(networkMonitor);
putBoolean(env, prefs, "GTK.network_metered", metered);

return prefs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
class PlatformSupport final
{
public:
static constexpr const char* observedSettings[] = {
"notify::gtk-theme-name",
"notify::gtk-enable-animations",
"notify::gtk-overlay-scrolling"
};

PlatformSupport(JNIEnv*, jobject);
~PlatformSupport();
PlatformSupport(PlatformSupport const&) = delete;
Expand All @@ -53,7 +47,17 @@ class PlatformSupport final
void updatePreferences() const;

private:
static constexpr const char* OBSERVED_SETTINGS[] = {
"notify::gtk-theme-name",
"notify::gtk-enable-animations",
"notify::gtk-overlay-scrolling"
};

static constexpr int NUM_OBSERVED_SETTINGS = sizeof(OBSERVED_SETTINGS) / sizeof(const char*);

JNIEnv* env;
jobject application;
unsigned long settingChangedHandlers[NUM_OBSERVED_SETTINGS] = {};
unsigned long networkChangedHandler = 0;
mutable jobject preferences;
};
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

- (void)runLoop:(id)selector;
- (BOOL)started;
- (jobject)getPlatformPreferences;

+ (jobject)enterNestedEventLoopWithEnv:(JNIEnv*)env;
+ (void)leaveNestedEventLoopWithEnv:(JNIEnv*)env retValue:(jobject)retValue;
Expand Down
Loading