From b473f3ec44ef37ad1f4bf7c0c57ffa52f34e13fc Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Tue, 3 Dec 2024 01:59:38 +0100 Subject: [PATCH 01/15] Implementation of reducedData --- .../com/sun/glass/ui/win/WinApplication.java | 10 ++- .../preferences/PlatformPreferences.java | 10 +++ .../preferences/PreferenceProperties.java | 17 +++++ .../java/javafx/application/Platform.java | 14 ++++ .../main/native-glass/win/PlatformSupport.cpp | 74 ++++++++++++++++--- .../main/native-glass/win/PlatformSupport.h | 3 + .../native-glass/win/RoActivationSupport.cpp | 9 +++ .../PlatformPreferencesChangedTest.java | 3 + 8 files changed, 128 insertions(+), 12 deletions(-) diff --git a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/win/WinApplication.java b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/win/WinApplication.java index 15be1949b92..eaa7e102c40 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/win/WinApplication.java +++ b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/win/WinApplication.java @@ -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; + }) ); } @@ -393,7 +398,8 @@ public Map> 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) ); } } diff --git a/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PlatformPreferences.java b/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PlatformPreferences.java index f5378393fbb..b29a12bda7e 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PlatformPreferences.java +++ b/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PlatformPreferences.java @@ -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(); diff --git a/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PreferenceProperties.java b/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PreferenceProperties.java index c7410ee0113..bf5cf532b15 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PreferenceProperties.java +++ b/modules/javafx.graphics/src/main/java/com/sun/javafx/application/preferences/PreferenceProperties.java @@ -51,9 +51,11 @@ final class PreferenceProperties { private final ColorSchemeProperty colorScheme = new ColorSchemeProperty(); private final DeferredProperty reducedMotion = new DeferredProperty<>("reducedMotion", false); private final DeferredProperty reducedTransparency = new DeferredProperty<>("reducedTransparency", false); + private final DeferredProperty reducedData = new DeferredProperty<>("reducedData", false); private final DeferredProperty 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; @@ -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); } @@ -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(); } diff --git a/modules/javafx.graphics/src/main/java/javafx/application/Platform.java b/modules/javafx.graphics/src/main/java/javafx/application/Platform.java index d70c8eb760d..8ff237b8f88 100644 --- a/modules/javafx.graphics/src/main/java/javafx/application/Platform.java +++ b/modules/javafx.graphics/src/main/java/javafx/application/Platform.java @@ -631,6 +631,20 @@ public sealed interface Preferences extends ObservableMap 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. + *

+ * 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. diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp index d03ec93ff8f..ec8fbb236ee 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp @@ -31,9 +31,10 @@ using namespace Microsoft::WRL; using namespace ABI::Windows::Foundation; using namespace ABI::Windows::UI; using namespace ABI::Windows::UI::ViewManagement; +using namespace ABI::Windows::Networking::Connectivity; PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) - : env(env), application(application), initialized(false), preferences(NULL) + : env(env), application(application), initialized(false), networkInformation(NULL), preferences(NULL) { javaClasses.Object = (jclass)env->FindClass("java/lang/Object"); if (CheckAndClearException(env)) return; @@ -88,14 +89,7 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) try { RO_CHECKED("RoActivateInstance", RoActivateInstance(hstring("Windows.UI.ViewManagement.UISettings"), (IInspectable**)&settings)); - } catch (RoException const&) { - // If an activation exception occurs, it probably means that we're on a Windows system - // that doesn't support the UISettings API. This is not a problem, it simply means that - // we don't report the UISettings properties back to the JavaFX application. - return; - } - try { ComPtr settings5; RO_CHECKED("IUISettings::QueryInterface", settings->QueryInterface(&settings5)); @@ -109,7 +103,33 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) }).Get(), &token); } catch (RoException const&) { - return; + // If an activation exception occurs, it probably means that we're on a Windows system + // that doesn't support the UISettings API. This is not a problem, it simply means that + // we don't report the UISettings properties back to the JavaFX application. + } + + try { + IActivationFactory* activationFactory = NULL; + + RO_CHECKED("RoGetActivationFactory", + RoGetActivationFactory(hstring("Windows.Networking.Connectivity.NetworkInformation"), + IID_IActivationFactory, + (void**)&activationFactory)); + + RO_CHECKED("IActivationFactory::QueryInterface", + activationFactory->QueryInterface(&networkInformation)); + + EventRegistrationToken token; + networkInformation->add_NetworkStatusChanged( + Callback( + [this](IInspectable*) { + updatePreferences(); + return S_OK; + }).Get(), + &token); + } catch (RoException const&) { + // If an activation exception occurs, it probably means that we're on a Windows system + // that doesn't support the NetworkInformation API. } } @@ -131,6 +151,7 @@ jobject PlatformSupport::collectPreferences() const querySystemParameters(prefs); querySystemColors(prefs); queryUISettings(prefs); + queryNetworkInformation(prefs); return prefs; } @@ -276,6 +297,39 @@ void PlatformSupport::queryUISettings(jobject properties) const } } +void PlatformSupport::queryNetworkInformation(jobject properties) const +{ + if (!this->networkInformation) { + return; + } + + try { + ComPtr connectionProfile; + ComPtr connectionCost; + NetworkCostType networkCostType; + const char* internetCostType; + + RO_CHECKED("INetworkInformation::GetInternetConnectionProfile", + this->networkInformation->GetInternetConnectionProfile(&connectionProfile)); + + RO_CHECKED("IConnectionProfile::GetConnectionCost", + connectionProfile->GetConnectionCost(&connectionCost)); + + RO_CHECKED("IConnectionCost::get_NetworkCostType", + connectionCost->get_NetworkCostType(&networkCostType)); + + switch (networkCostType) { + case NetworkCostType_Unrestricted: internetCostType = "Unrestricted"; break; + case NetworkCostType_Variable: internetCostType = "Variable"; break; + case NetworkCostType_Fixed: internetCostType = "Fixed"; break; + default: internetCostType = "Unknown"; break; + } + + putString(properties, "Windows.NetworkInformation.InternetCostType", internetCostType); + } catch (RoException const&) { + } +} + void PlatformSupport::putString(jobject properties, const char* key, const char* value) const { jobject prefKey = env->NewStringUTF(key); @@ -298,7 +352,7 @@ void PlatformSupport::putString(jobject properties, const char* key, const wchar jobject prefValue = NULL; if (value != NULL) { - prefValue = env->NewString((jchar*)value, wcslen(value)); + prefValue = env->NewString((jchar*)value, (jsize)wcslen(value)); if (CheckAndClearException(env)) return; } diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h index 9994e0aff4e..7aa706c0887 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h @@ -34,6 +34,7 @@ #include #include #include +#include class PlatformSupport final { @@ -64,6 +65,7 @@ class PlatformSupport final jobject application; bool initialized; Microsoft::WRL::ComPtr settings; + ABI::Windows::Networking::Connectivity::INetworkInformationStatics* networkInformation; mutable JGlobalRef preferences; struct { @@ -78,6 +80,7 @@ class PlatformSupport final void querySystemColors(jobject properties) const; void querySystemParameters(jobject properties) const; void queryUISettings(jobject properties) const; + void queryNetworkInformation(jobject properties) const; void putString(jobject properties, const char* key, const char* value) const; void putString(jobject properties, const char* key, const wchar_t* value) const; diff --git a/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp b/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp index 38293e932cc..987163e90ca 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp @@ -34,6 +34,7 @@ namespace typedef HRESULT WINAPI FnRoInitialize(RO_INIT_TYPE initType); typedef void WINAPI FnRoUninitialize(); typedef HRESULT WINAPI FnRoActivateInstance(HSTRING activatableClassId, IInspectable** instance); + typedef HRESULT WINAPI FnRoGetActivationFactory(HSTRING activatableClassId, REFIID iid, void** factory); typedef HRESULT WINAPI FnWindowsCreateString(PCNZWCH sourceString, UINT32 length, HSTRING* string); typedef HRESULT WINAPI FnWindowsDeleteString(HSTRING string); @@ -43,6 +44,7 @@ namespace FnRoInitialize* pRoInitialize = NULL; FnRoUninitialize* pRoUninitialize = NULL; FnRoActivateInstance* pRoActivateInstance = NULL; + FnRoGetActivationFactory* pRoGetActivationFactory = NULL; FnWindowsCreateString* pWindowsCreateString = NULL; FnWindowsDeleteString* pWindowsDeleteString = NULL; @@ -92,6 +94,7 @@ void tryInitializeRoActivationSupport() loadFunction(hLibComBase, pRoInitialize, "RoInitialize") && loadFunction(hLibComBase, pRoUninitialize, "RoUninitialize") && loadFunction(hLibComBase, pRoActivateInstance, "RoActivateInstance") && + loadFunction(hLibComBase, pRoGetActivationFactory, "RoGetActivationFactory") && loadFunction(hLibComBase, pWindowsCreateString, "WindowsCreateString") && loadFunction(hLibComBase, pWindowsDeleteString, "WindowsDeleteString"); @@ -122,6 +125,7 @@ void uninitializeRoActivationSupport() pRoInitialize = NULL; pRoUninitialize = NULL; pRoActivateInstance = NULL; + pRoGetActivationFactory = NULL; pWindowsCreateString = NULL; pWindowsDeleteString = NULL; } @@ -147,6 +151,11 @@ HRESULT WINAPI RoActivateInstance(HSTRING activatableClassId, IInspectable** ins return pRoActivateInstance(activatableClassId, instance); } +HRESULT WINAPI RoGetActivationFactory(HSTRING activatableClassId, REFIID iid, void** factory) +{ + return pRoGetActivationFactory(activatableClassId, iid, factory); +} + HRESULT WINAPI WindowsCreateString(PCNZWCH sourceString, UINT32 length, HSTRING* string) { return pWindowsCreateString(sourceString, length, string); diff --git a/tests/manual/events/PlatformPreferencesChangedTest.java b/tests/manual/events/PlatformPreferencesChangedTest.java index 4afad180442..3351746ff32 100644 --- a/tests/manual/events/PlatformPreferencesChangedTest.java +++ b/tests/manual/events/PlatformPreferencesChangedTest.java @@ -66,6 +66,7 @@ public void start(Stage stage) { var colorSchemeLabel = new Label(); var reducedMotionLabel = new Label(); var reducedTransparencyLabel = new Label(); + var reducedDataLabel = new Label(); var persistentScrollBarsLabel = new Label(); Runnable updateColorProperties = () -> { @@ -76,6 +77,7 @@ public void start(Stage stage) { colorSchemeLabel.setText(preferences.getColorScheme().toString()); reducedMotionLabel.setText(Boolean.toString(preferences.isReducedMotion())); reducedTransparencyLabel.setText(Boolean.toString(preferences.isReducedTransparency())); + reducedDataLabel.setText(Boolean.toString(preferences.isReducedData())); persistentScrollBarsLabel.setText(Boolean.toString(preferences.isPersistentScrollBars())); }; @@ -96,6 +98,7 @@ public void start(Stage stage) { new HBox(new BoldLabel(" colorScheme: "), colorSchemeLabel), new HBox(new BoldLabel(" reducedMotion: "), reducedMotionLabel), new HBox(new BoldLabel(" reducedTransparency: "), reducedTransparencyLabel), + new HBox(new BoldLabel(" reducedData: "), reducedDataLabel), new HBox(new BoldLabel(" persistentScrollBars: "), persistentScrollBarsLabel)), new Label("4. Click \"Pass\" if the changes were correctly reported, otherwise click \"Fail\"."), new HBox(5, passButton, failButton, clearButton) From 9d227329d481b6a9836b40eae78e020326f845c9 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:10:25 +0100 Subject: [PATCH 02/15] add linux implementation --- .../com/sun/glass/ui/gtk/GtkApplication.java | 6 ++- .../native-glass/gtk/GlassApplication.cpp | 15 ------- .../main/native-glass/gtk/PlatformSupport.cpp | 39 ++++++++++++++++++- .../main/native-glass/gtk/PlatformSupport.h | 16 +++++--- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java index 56b9ae2fac0..4706c15ff01 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java +++ b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java @@ -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) ); } @@ -482,7 +483,8 @@ public Map> 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) ); } } diff --git a/modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp b/modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp index 5bd9f1974a5..0b9be20392a 100644 --- a/modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp +++ b/modules/javafx.graphics/src/main/native-glass/gtk/GlassApplication.cpp @@ -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 @@ -197,14 +190,6 @@ JNIEXPORT void JNICALL Java_com_sun_glass_ui_gtk_GtkApplication__1init gdk_window_set_events(root, static_cast(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); - } - } } /* diff --git a/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.cpp b/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.cpp index d3f80c874db..b5ef97fada1 100644 --- a/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.cpp @@ -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) { @@ -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; } diff --git a/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.h index b537708430f..4a06c9b8c5e 100644 --- a/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/gtk/PlatformSupport.h @@ -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; @@ -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; }; From fe22bd5bc3d67d98e308c55b6e69b9a752399c07 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:00:42 +0100 Subject: [PATCH 03/15] add macOS implementation --- buildSrc/mac.gradle | 1 + .../com/sun/glass/ui/mac/MacApplication.java | 7 +++++-- .../main/java/javafx/application/Platform.java | 4 ++++ .../src/main/native-glass/mac/GlassApplication.m | 10 ++++++++++ .../src/main/native-glass/mac/PlatformSupport.h | 7 ++++++- .../src/main/native-glass/mac/PlatformSupport.m | 16 ++++++++++++++++ 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/buildSrc/mac.gradle b/buildSrc/mac.gradle index 194889a0795..30ca50782f4 100644 --- a/buildSrc/mac.gradle +++ b/buildSrc/mac.gradle @@ -156,6 +156,7 @@ def dynamicLinkFlags = [ "-framework", "OpenGL", "-framework", "QuartzCore", "-framework", "Security", + "-framework", "Network", "-dynamiclib", "-lobjc"].flatten(); def dynamicLinkFlagsAlt = ["-dynamiclib", commonParams].flatten() diff --git a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java index 890ff432083..7782d86301d 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java +++ b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java @@ -431,7 +431,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) ); } @@ -487,7 +488,9 @@ public Map> 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) ); } diff --git a/modules/javafx.graphics/src/main/java/javafx/application/Platform.java b/modules/javafx.graphics/src/main/java/javafx/application/Platform.java index 8ff237b8f88..0808f708bfa 100644 --- a/modules/javafx.graphics/src/main/java/javafx/application/Platform.java +++ b/modules/javafx.graphics/src/main/java/javafx/application/Platform.java @@ -498,6 +498,7 @@ public static Preferences getPreferences() { * {@code Windows.UIColor.AccentLight3}{@link Color} * {@code Windows.UISettings.AdvancedEffectsEnabled}{@link Boolean} * {@code Windows.UISettings.AutoHideScrollBars}{@link Boolean} + * {@code Windows.NetworkInformation.InternetCostType}{@link String} * * * @@ -553,6 +554,8 @@ public static Preferences getPreferences() { * {@code macOS.NSWorkspace.accessibilityDisplayShouldReduceMotion}{@link Boolean} * {@code macOS.NSWorkspace.accessibilityDisplayShouldReduceTransparency}{@link Boolean} * {@code macOS.NSScroller.preferredScrollerStyle}{@link String} + * {@code macOS.NWPathMonitor.currentPathConstrained}{@link Boolean} + * {@code macOS.NWPathMonitor.currentPathExpensive}{@link Boolean} * * * @@ -580,6 +583,7 @@ public static Preferences getPreferences() { * {@code GTK.success_color}{@link Color} * {@code GTK.enable_animations}{@link Boolean} * {@code GTK.overlay_scrolling}{@link Boolean} + * {@code GTK.network_metered}{@link Boolean} * * * diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index 9f0fd599210..5ad55df0c06 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -40,6 +40,7 @@ #import "ProcessInfo.h" #import #import +#import #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -256,6 +257,15 @@ - (void)applicationWillFinishLaunching:(NSNotification *)aNotification name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:nil]; + nw_path_monitor_t pathMonitor = nw_path_monitor_create(); + nw_path_monitor_set_update_handler(pathMonitor, ^(nw_path_t path) { + [PlatformSupport updateNetworkPath:self->jApplication + constrained:nw_path_is_constrained(path) + expensive:nw_path_is_expensive(path)]; + }); + nw_path_monitor_set_queue(pathMonitor, dispatch_get_main_queue()); + nw_path_monitor_start(pathMonitor); + // localMonitor = [NSEvent addLocalMonitorForEventsMatchingMask: NSRightMouseDownMask // handler:^(NSEvent *incomingEvent) { // NSEvent *result = incomingEvent; diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h index 309c2468aa5..9c7e9d95ace 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -43,4 +43,9 @@ */ + (void)updatePreferences:(jobject)application; +/** + * Notify the JavaFX application about changes of the current network path. + */ ++ (void)updateNetworkPath:(jobject)application constrained:(bool)constrained expensive:(bool)expensive; + @end diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m index f44439b0677..b8c9c2382c2 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m @@ -64,6 +64,8 @@ } static jobject currentPreferences = nil; +static bool currentPathConstrained = false; +static bool currentPathExpensive = false; @implementation PlatformSupport @@ -119,6 +121,14 @@ + (jobject)collectPreferences { value:[NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay ? "NSScrollerStyleOverlay" : "NSScrollerStyleLegacy"]; + [PlatformSupport putBoolean:preferences + key:"macOS.NWPathMonitor.currentPathConstrained" + value:currentPathConstrained]; + + [PlatformSupport putBoolean:preferences + key:"macOS.NWPathMonitor.currentPathExpensive" + value:currentPathExpensive]; + return preferences; } @@ -235,6 +245,12 @@ + (void)updatePreferences:(jobject)application { (*env)->DeleteLocalRef(env, newPreferences); } ++ (void)updateNetworkPath:(jobject)application constrained:(bool)constrained expensive:(bool)expensive { + currentPathConstrained = constrained; + currentPathExpensive = expensive; + [PlatformSupport updatePreferences:application]; +} + + (void)putBoolean:(jobject)preferences key:(const char*)key value:(bool)value { GET_MAIN_JENV; From 0245c2e4353a2d04c2e3d9cb1b6584df68442c4e Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:09:00 +0100 Subject: [PATCH 04/15] error message tweak --- .../src/main/native-glass/win/PlatformSupport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp index ec8fbb236ee..851af86ac03 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp @@ -116,7 +116,7 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) IID_IActivationFactory, (void**)&activationFactory)); - RO_CHECKED("IActivationFactory::QueryInterface", + RO_CHECKED("IActivationFactory::QueryInterface", activationFactory->QueryInterface(&networkInformation)); EventRegistrationToken token; From 5c2e255f70e25822edec56723cc23d73a0525f84 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Wed, 4 Dec 2024 01:39:21 +0100 Subject: [PATCH 05/15] javadoc change --- .../src/main/java/javafx/application/Platform.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/javafx.graphics/src/main/java/javafx/application/Platform.java b/modules/javafx.graphics/src/main/java/javafx/application/Platform.java index 0808f708bfa..d504d3ebd4a 100644 --- a/modules/javafx.graphics/src/main/java/javafx/application/Platform.java +++ b/modules/javafx.graphics/src/main/java/javafx/application/Platform.java @@ -456,7 +456,7 @@ public static Preferences getPreferences() { } /** - * Contains UI preferences of the current platform. + * Contains preferences of the current platform. *

* {@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. From 8b8b1bdbf7ec8d32a2609ff318c4e94314751bdb Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:51:34 +0100 Subject: [PATCH 06/15] refactor macOS PlatformSupport --- .../main/native-glass/mac/GlassApplication.h | 1 + .../main/native-glass/mac/GlassApplication.m | 63 +---- .../main/native-glass/mac/PlatformSupport.h | 19 +- .../main/native-glass/mac/PlatformSupport.m | 231 +++++++++++------- 4 files changed, 159 insertions(+), 155 deletions(-) diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.h b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.h index ca9a9b558ba..8fc86d6b4c5 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.h +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.h @@ -61,6 +61,7 @@ - (void)runLoop:(id)selector; - (BOOL)started; +- (jobject)getPlatformPreferences; + (jobject)enterNestedEventLoopWithEnv:(JNIEnv*)env; + (void)leaveNestedEventLoopWithEnv:(JNIEnv*)env retValue:(jobject)retValue; diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index 5ad55df0c06..da418c7dfca 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -40,7 +40,6 @@ #import "ProcessInfo.h" #import #import -#import #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -140,7 +139,9 @@ @implementation NSApplicationFX #pragma mark --- GlassApplication -@implementation GlassApplication +@implementation GlassApplication { + PlatformSupport* platformSupport; +} - (id)initWithEnv:(JNIEnv*)env application:(jobject)application launchable:(jobject)launchable taskbarApplication:(jboolean)isTaskbarApplication classLoader:(jobject)classLoader { @@ -178,25 +179,6 @@ - (void)GlassApplicationDidChangeScreenParameters } } -- (void)platformPreferencesDidChange { - // Some dynamic colors like NSColor.controlAccentColor don't seem to be reliably updated - // at the exact moment AppleColorPreferencesChangedNotification is received. - // As a workaround, we wait for a short period of time (one second seems sufficient) before - // we query the updated platform preferences. - - [NSObject cancelPreviousPerformRequestsWithTarget:self - selector:@selector(updatePlatformPreferences) - object:nil]; - - [self performSelector:@selector(updatePlatformPreferences) - withObject:nil - afterDelay:1.0]; -} - -- (void)updatePlatformPreferences { - [PlatformSupport updatePreferences:self->jApplication]; -} - - (void)applicationWillFinishLaunching:(NSNotification *)aNotification { LOG("GlassApplication:applicationWillFinishLaunching"); @@ -205,6 +187,7 @@ - (void)applicationWillFinishLaunching:(NSNotification *)aNotification NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; { // unblock main thread. Glass is started at this point. + self->platformSupport = [[PlatformSupport alloc] initWithEnv:env application:jApplication]; self->started = YES; if (self->jLaunchable != NULL) @@ -236,36 +219,6 @@ - (void)applicationWillFinishLaunching:(NSNotification *)aNotification name:NSApplicationDidChangeScreenParametersNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(platformPreferencesDidChange) - name:NSPreferredScrollerStyleDidChangeNotification - object:nil]; - - [[NSDistributedNotificationCenter defaultCenter] addObserver:self - selector:@selector(platformPreferencesDidChange) - name:@"AppleInterfaceThemeChangedNotification" - object:nil]; - - [[NSDistributedNotificationCenter defaultCenter] addObserver:self - selector:@selector(platformPreferencesDidChange) - name:@"AppleColorPreferencesChangedNotification" - object:nil]; - - [[[NSWorkspace sharedWorkspace] notificationCenter] - addObserver:self - selector:@selector(platformPreferencesDidChange) - name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification - object:nil]; - - nw_path_monitor_t pathMonitor = nw_path_monitor_create(); - nw_path_monitor_set_update_handler(pathMonitor, ^(nw_path_t path) { - [PlatformSupport updateNetworkPath:self->jApplication - constrained:nw_path_is_constrained(path) - expensive:nw_path_is_expensive(path)]; - }); - nw_path_monitor_set_queue(pathMonitor, dispatch_get_main_queue()); - nw_path_monitor_start(pathMonitor); - // localMonitor = [NSEvent addLocalMonitorForEventsMatchingMask: NSRightMouseDownMask // handler:^(NSEvent *incomingEvent) { // NSEvent *result = incomingEvent; @@ -789,6 +742,11 @@ - (BOOL)started return self->started; } +- (jobject)getPlatformPreferences +{ + return platformSupport != nil ? [platformSupport collectPreferences] : nil; +} + + (jobject)enterNestedEventLoopWithEnv:(JNIEnv*)env { jobject ret = NULL; @@ -1309,5 +1267,6 @@ + (BOOL)syncRenderingDisabled { JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_mac_MacApplication_getPlatformPreferences (JNIEnv *env, jobject self) { - return [PlatformSupport collectPreferences]; + GlassApplication* app = (GlassApplication*)[NSApp delegate]; + return [app getPlatformPreferences]; } diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h index 9c7e9d95ace..695c4088066 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h @@ -28,24 +28,21 @@ #import #import -@interface PlatformSupport : NSObject +@interface PlatformSupport : NSObject { + JNIEnv* env; + jobject application; +} + (void)initIDs:(JNIEnv*)env; /** - * Collect all platform preferences and return them as a new java/util/Map. + * Initializes a new PlatformSupport instance. */ -+ (jobject)collectPreferences; +- (id)initWithEnv:(JNIEnv*)env application:(jobject)app; /** - * Collect all platform preferences and notify the JavaFX application when a preference has changed. - * The change notification includes all preferences, not only the changed preferences. - */ -+ (void)updatePreferences:(jobject)application; - -/** - * Notify the JavaFX application about changes of the current network path. + * Collect all platform preferences and return them as a new java/util/Map. */ -+ (void)updateNetworkPath:(jobject)application constrained:(bool)constrained expensive:(bool)expensive; +- (jobject)collectPreferences; @end diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m index b8c9c2382c2..4a1fb5b05eb 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m @@ -25,6 +25,7 @@ #import "PlatformSupport.h" #import "GlassMacros.h" +#import #define INIT_CLASS(CLS, NAME)\ if (CLS == nil) {\ @@ -63,11 +64,11 @@ }\ } -static jobject currentPreferences = nil; -static bool currentPathConstrained = false; -static bool currentPathExpensive = false; - -@implementation PlatformSupport +@implementation PlatformSupport { + jobject currentPreferences; + bool currentPathConstrained; + bool currentPathExpensive; +} + (void)initIDs:(JNIEnv*)env { INIT_CLASS(jMapClass, "java/util/Map"); @@ -90,9 +91,67 @@ + (void)initIDs:(JNIEnv*)env { INIT_STATIC_METHOD(jColorClass, jColorRgbMethod, "rgb", "(IIID)Ljavafx/scene/paint/Color;"); } -+ (jobject)collectPreferences { - GET_MAIN_JENV; +- (id)initWithEnv:(JNIEnv*)jEnv application:(jobject)jApp { + if (!(self = [super init])) { + return nil; + } + + self->env = jEnv; + self->application = jApp; + self->currentPreferences = nil; + self->currentPathConstrained = false; + self->currentPathExpensive = false; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(platformPreferencesDidChange) + name:NSPreferredScrollerStyleDidChangeNotification + object:nil]; + + [[NSDistributedNotificationCenter defaultCenter] addObserver:self + selector:@selector(platformPreferencesDidChange) + name:@"AppleInterfaceThemeChangedNotification" + object:nil]; + + [[NSDistributedNotificationCenter defaultCenter] addObserver:self + selector:@selector(platformPreferencesDidChange) + name:@"AppleColorPreferencesChangedNotification" + object:nil]; + + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(platformPreferencesDidChange) + name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification + object:nil]; + + nw_path_monitor_t pathMonitor = nw_path_monitor_create(); + nw_path_monitor_set_update_handler(pathMonitor, ^(nw_path_t path) { + self->currentPathConstrained = nw_path_is_constrained(path); + self->currentPathExpensive = nw_path_is_expensive(path); + [self updatePreferences]; + }); + + nw_path_monitor_set_queue(pathMonitor, dispatch_get_main_queue()); + nw_path_monitor_start(pathMonitor); + + return self; +} + +- (void)platformPreferencesDidChange { + // Some dynamic colors like NSColor.controlAccentColor don't seem to be reliably updated + // at the exact moment AppleColorPreferencesChangedNotification is received. + // As a workaround, we wait for a short period of time (one second seems sufficient) before + // we query the updated platform preferences. + + [NSObject cancelPreviousPerformRequestsWithTarget:self + selector:@selector(updatePreferences) + object:nil]; + + [self performSelector:@selector(updatePreferences) + withObject:nil + afterDelay:1.0]; +} +- (jobject)collectPreferences { jobject preferences = (*env)->NewObject(env, jHashMapClass, jHashMapInitMethod); GLASS_CHECK_EXCEPTION(env); if (preferences == nil) { @@ -105,73 +164,73 @@ + (jobject)collectPreferences { // colors. NSAppearance* lastAppearance = [NSAppearance currentAppearance]; [NSAppearance setCurrentAppearance:[NSApp effectiveAppearance]]; - [PlatformSupport queryNSColors:preferences]; + [self queryNSColors:preferences]; [NSAppearance setCurrentAppearance:lastAppearance]; - [PlatformSupport putBoolean:preferences - key:"macOS.NSWorkspace.accessibilityDisplayShouldReduceMotion" - value:[[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceMotion]]; + [self putBoolean:preferences + key:"macOS.NSWorkspace.accessibilityDisplayShouldReduceMotion" + value:[[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceMotion]]; - [PlatformSupport putBoolean:preferences - key:"macOS.NSWorkspace.accessibilityDisplayShouldReduceTransparency" - value:[[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceTransparency]]; + [self putBoolean:preferences + key:"macOS.NSWorkspace.accessibilityDisplayShouldReduceTransparency" + value:[[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceTransparency]]; - [PlatformSupport putString:preferences - key:"macOS.NSScroller.preferredScrollerStyle" - value:[NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay - ? "NSScrollerStyleOverlay" : "NSScrollerStyleLegacy"]; + [self putString:preferences + key:"macOS.NSScroller.preferredScrollerStyle" + value:[NSScroller preferredScrollerStyle] == NSScrollerStyleOverlay + ? "NSScrollerStyleOverlay" : "NSScrollerStyleLegacy"]; - [PlatformSupport putBoolean:preferences - key:"macOS.NWPathMonitor.currentPathConstrained" - value:currentPathConstrained]; + [self putBoolean:preferences + key:"macOS.NWPathMonitor.currentPathConstrained" + value:currentPathConstrained]; - [PlatformSupport putBoolean:preferences - key:"macOS.NWPathMonitor.currentPathExpensive" - value:currentPathExpensive]; + [self putBoolean:preferences + key:"macOS.NWPathMonitor.currentPathExpensive" + value:currentPathExpensive]; return preferences; } -+ (void)queryNSColors:(jobject)preferences { +- (void)queryNSColors:(jobject)preferences { // Label colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.labelColor" value:[NSColor labelColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.secondaryLabelColor" value:[NSColor secondaryLabelColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.tertiaryLabelColor" value:[NSColor tertiaryLabelColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.quaternaryLabelColor" value:[NSColor quaternaryLabelColor]]; + [self putColor:preferences key:"macOS.NSColor.labelColor" value:[NSColor labelColor]]; + [self putColor:preferences key:"macOS.NSColor.secondaryLabelColor" value:[NSColor secondaryLabelColor]]; + [self putColor:preferences key:"macOS.NSColor.tertiaryLabelColor" value:[NSColor tertiaryLabelColor]]; + [self putColor:preferences key:"macOS.NSColor.quaternaryLabelColor" value:[NSColor quaternaryLabelColor]]; // Text colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.textColor" value:[NSColor textColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.placeholderTextColor" value:[NSColor placeholderTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.selectedTextColor" value:[NSColor selectedTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.textBackgroundColor" value:[NSColor textBackgroundColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.selectedTextBackgroundColor" value:[NSColor selectedTextBackgroundColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.keyboardFocusIndicatorColor" value:[NSColor keyboardFocusIndicatorColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.unemphasizedSelectedTextColor" value:[NSColor unemphasizedSelectedTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.unemphasizedSelectedTextBackgroundColor" value:[NSColor unemphasizedSelectedTextBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.textColor" value:[NSColor textColor]]; + [self putColor:preferences key:"macOS.NSColor.placeholderTextColor" value:[NSColor placeholderTextColor]]; + [self putColor:preferences key:"macOS.NSColor.selectedTextColor" value:[NSColor selectedTextColor]]; + [self putColor:preferences key:"macOS.NSColor.textBackgroundColor" value:[NSColor textBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.selectedTextBackgroundColor" value:[NSColor selectedTextBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.keyboardFocusIndicatorColor" value:[NSColor keyboardFocusIndicatorColor]]; + [self putColor:preferences key:"macOS.NSColor.unemphasizedSelectedTextColor" value:[NSColor unemphasizedSelectedTextColor]]; + [self putColor:preferences key:"macOS.NSColor.unemphasizedSelectedTextBackgroundColor" value:[NSColor unemphasizedSelectedTextBackgroundColor]]; // Content colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.linkColor" value:[NSColor linkColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.separatorColor" value:[NSColor separatorColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.selectedContentBackgroundColor" value:[NSColor selectedContentBackgroundColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.unemphasizedSelectedContentBackgroundColor" value:[NSColor unemphasizedSelectedContentBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.linkColor" value:[NSColor linkColor]]; + [self putColor:preferences key:"macOS.NSColor.separatorColor" value:[NSColor separatorColor]]; + [self putColor:preferences key:"macOS.NSColor.selectedContentBackgroundColor" value:[NSColor selectedContentBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.unemphasizedSelectedContentBackgroundColor" value:[NSColor unemphasizedSelectedContentBackgroundColor]]; // Menu colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.selectedMenuItemTextColor" value:[NSColor selectedMenuItemTextColor]]; + [self putColor:preferences key:"macOS.NSColor.selectedMenuItemTextColor" value:[NSColor selectedMenuItemTextColor]]; // Table colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.gridColor" value:[NSColor gridColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.headerTextColor" value:[NSColor headerTextColor]]; - [PlatformSupport putColors:preferences key:"macOS.NSColor.alternatingContentBackgroundColors" value:[NSColor alternatingContentBackgroundColors]]; + [self putColor:preferences key:"macOS.NSColor.gridColor" value:[NSColor gridColor]]; + [self putColor:preferences key:"macOS.NSColor.headerTextColor" value:[NSColor headerTextColor]]; + [self putColors:preferences key:"macOS.NSColor.alternatingContentBackgroundColors" value:[NSColor alternatingContentBackgroundColors]]; // Control colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.controlAccentColor" value:[NSColor controlAccentColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.controlColor" value:[NSColor controlColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.controlBackgroundColor" value:[NSColor controlBackgroundColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.controlTextColor" value:[NSColor controlTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.disabledControlTextColor" value:[NSColor disabledControlTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.selectedControlColor" value:[NSColor selectedControlColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.selectedControlTextColor" value:[NSColor selectedControlTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.alternateSelectedControlTextColor" value:[NSColor alternateSelectedControlTextColor]]; + [self putColor:preferences key:"macOS.NSColor.controlAccentColor" value:[NSColor controlAccentColor]]; + [self putColor:preferences key:"macOS.NSColor.controlColor" value:[NSColor controlColor]]; + [self putColor:preferences key:"macOS.NSColor.controlBackgroundColor" value:[NSColor controlBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.controlTextColor" value:[NSColor controlTextColor]]; + [self putColor:preferences key:"macOS.NSColor.disabledControlTextColor" value:[NSColor disabledControlTextColor]]; + [self putColor:preferences key:"macOS.NSColor.selectedControlColor" value:[NSColor selectedControlColor]]; + [self putColor:preferences key:"macOS.NSColor.selectedControlTextColor" value:[NSColor selectedControlTextColor]]; + [self putColor:preferences key:"macOS.NSColor.alternateSelectedControlTextColor" value:[NSColor alternateSelectedControlTextColor]]; const char* controlTint = nil; switch ([NSColor currentControlTint]) { @@ -181,36 +240,38 @@ + (void)queryNSColors:(jobject)preferences { case NSClearControlTint: controlTint = "NSClearControlTint"; break; } if (controlTint != nil) { - [PlatformSupport putString:preferences key:"macOS.NSColor.currentControlTint" value:controlTint]; + [self putString:preferences key:"macOS.NSColor.currentControlTint" value:controlTint]; } // Window colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.windowBackgroundColor" value:[NSColor windowBackgroundColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.windowFrameTextColor" value:[NSColor windowFrameTextColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.underPageBackgroundColor" value:[NSColor underPageBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.windowBackgroundColor" value:[NSColor windowBackgroundColor]]; + [self putColor:preferences key:"macOS.NSColor.windowFrameTextColor" value:[NSColor windowFrameTextColor]]; + [self putColor:preferences key:"macOS.NSColor.underPageBackgroundColor" value:[NSColor underPageBackgroundColor]]; // Highlights and shadows - [PlatformSupport putColor:preferences key:"macOS.NSColor.findHighlightColor" value:[NSColor findHighlightColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.highlightColor" value:[NSColor highlightColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.shadowColor" value:[NSColor shadowColor]]; + [self putColor:preferences key:"macOS.NSColor.findHighlightColor" value:[NSColor findHighlightColor]]; + [self putColor:preferences key:"macOS.NSColor.highlightColor" value:[NSColor highlightColor]]; + [self putColor:preferences key:"macOS.NSColor.shadowColor" value:[NSColor shadowColor]]; // Adaptable system colors - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemBlueColor" value:[NSColor systemBlueColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemBrownColor" value:[NSColor systemBrownColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemGrayColor" value:[NSColor systemGrayColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemGreenColor" value:[NSColor systemGreenColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemIndigoColor" value:[NSColor systemIndigoColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemOrangeColor" value:[NSColor systemOrangeColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemPinkColor" value:[NSColor systemPinkColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemPurpleColor" value:[NSColor systemPurpleColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemRedColor" value:[NSColor systemRedColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemTealColor" value:[NSColor systemTealColor]]; - [PlatformSupport putColor:preferences key:"macOS.NSColor.systemYellowColor" value:[NSColor systemYellowColor]]; + [self putColor:preferences key:"macOS.NSColor.systemBlueColor" value:[NSColor systemBlueColor]]; + [self putColor:preferences key:"macOS.NSColor.systemBrownColor" value:[NSColor systemBrownColor]]; + [self putColor:preferences key:"macOS.NSColor.systemGrayColor" value:[NSColor systemGrayColor]]; + [self putColor:preferences key:"macOS.NSColor.systemGreenColor" value:[NSColor systemGreenColor]]; + [self putColor:preferences key:"macOS.NSColor.systemIndigoColor" value:[NSColor systemIndigoColor]]; + [self putColor:preferences key:"macOS.NSColor.systemOrangeColor" value:[NSColor systemOrangeColor]]; + [self putColor:preferences key:"macOS.NSColor.systemPinkColor" value:[NSColor systemPinkColor]]; + [self putColor:preferences key:"macOS.NSColor.systemPurpleColor" value:[NSColor systemPurpleColor]]; + [self putColor:preferences key:"macOS.NSColor.systemRedColor" value:[NSColor systemRedColor]]; + [self putColor:preferences key:"macOS.NSColor.systemTealColor" value:[NSColor systemTealColor]]; + [self putColor:preferences key:"macOS.NSColor.systemYellowColor" value:[NSColor systemYellowColor]]; } -+ (void)updatePreferences:(jobject)application { - GET_MAIN_JENV; - +/** + * Collect all platform preferences and notify the JavaFX application when a preference has changed. + * The change notification includes all preferences, not only the changed preferences. + */ +- (void)updatePreferences { jobject newPreferences = [self collectPreferences]; if (newPreferences == nil) { return; @@ -245,15 +306,7 @@ + (void)updatePreferences:(jobject)application { (*env)->DeleteLocalRef(env, newPreferences); } -+ (void)updateNetworkPath:(jobject)application constrained:(bool)constrained expensive:(bool)expensive { - currentPathConstrained = constrained; - currentPathExpensive = expensive; - [PlatformSupport updatePreferences:application]; -} - -+ (void)putBoolean:(jobject)preferences key:(const char*)key value:(bool)value { - GET_MAIN_JENV; - +- (void)putBoolean:(jobject)preferences key:(const char*)key value:(bool)value { jobject prefKey = (*env)->NewStringUTF(env, key); GLASS_CHECK_NONNULL_EXCEPTION_RETURN(env, prefKey); @@ -264,9 +317,7 @@ + (void)putBoolean:(jobject)preferences key:(const char*)key value:(bool)value { GLASS_CHECK_EXCEPTION(env); } -+ (void)putString:(jobject)preferences key:(const char*)key value:(const char*)value { - GET_MAIN_JENV; - +- (void)putString:(jobject)preferences key:(const char*)key value:(const char*)value { jobject prefKey = (*env)->NewStringUTF(env, key); GLASS_CHECK_NONNULL_EXCEPTION_RETURN(env, prefKey); @@ -280,9 +331,7 @@ + (void)putString:(jobject)preferences key:(const char*)key value:(const char*)v GLASS_CHECK_EXCEPTION(env); } -+ (void)putColor:(jobject)preferences key:(const char*)colorName value:(NSColor*)color { - GET_MAIN_JENV; - +- (void)putColor:(jobject)preferences key:(const char*)colorName value:(NSColor*)color { jobject prefKey = (*env)->NewStringUTF(env, colorName); GLASS_CHECK_NONNULL_EXCEPTION_RETURN(env, prefKey); @@ -299,9 +348,7 @@ + (void)putColor:(jobject)preferences key:(const char*)colorName value:(NSColor* GLASS_CHECK_EXCEPTION(env); } -+ (void)putColors:(jobject)preferences key:(const char*)colorName value:(NSArray*)colors { - GET_MAIN_JENV; - +- (void)putColors:(jobject)preferences key:(const char*)colorName value:(NSArray*)colors { jobject prefKey = (*env)->NewStringUTF(env, colorName); GLASS_CHECK_NONNULL_EXCEPTION_RETURN(env, prefKey); From 4bfcb3374dcf02e9383da87c223bbe8ba22a31a5 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:58:57 +0100 Subject: [PATCH 07/15] changed formatting --- .../main/native-glass/mac/PlatformSupport.m | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m index 4a1fb5b05eb..1110fc33c1d 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m @@ -102,20 +102,23 @@ - (id)initWithEnv:(JNIEnv*)jEnv application:(jobject)jApp { self->currentPathConstrained = false; self->currentPathExpensive = false; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(platformPreferencesDidChange) - name:NSPreferredScrollerStyleDidChangeNotification - object:nil]; - - [[NSDistributedNotificationCenter defaultCenter] addObserver:self - selector:@selector(platformPreferencesDidChange) - name:@"AppleInterfaceThemeChangedNotification" - object:nil]; - - [[NSDistributedNotificationCenter defaultCenter] addObserver:self - selector:@selector(platformPreferencesDidChange) - name:@"AppleColorPreferencesChangedNotification" - object:nil]; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(platformPreferencesDidChange) + name:NSPreferredScrollerStyleDidChangeNotification + object:nil]; + + [[NSDistributedNotificationCenter defaultCenter] + addObserver:self + selector:@selector(platformPreferencesDidChange) + name:@"AppleInterfaceThemeChangedNotification" + object:nil]; + + [[NSDistributedNotificationCenter defaultCenter] + addObserver:self + selector:@selector(platformPreferencesDidChange) + name:@"AppleColorPreferencesChangedNotification" + object:nil]; [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self From cbd3cb6399e7c2b571edaec3d108cea65551f8fe Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Thu, 12 Dec 2024 02:00:24 +0100 Subject: [PATCH 08/15] formatting --- .../src/main/native-glass/win/RoActivationSupport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp b/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp index 987163e90ca..fcef056d608 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/RoActivationSupport.cpp @@ -34,7 +34,7 @@ namespace typedef HRESULT WINAPI FnRoInitialize(RO_INIT_TYPE initType); typedef void WINAPI FnRoUninitialize(); typedef HRESULT WINAPI FnRoActivateInstance(HSTRING activatableClassId, IInspectable** instance); - typedef HRESULT WINAPI FnRoGetActivationFactory(HSTRING activatableClassId, REFIID iid, void** factory); + typedef HRESULT WINAPI FnRoGetActivationFactory(HSTRING activatableClassId, REFIID iid, void** factory); typedef HRESULT WINAPI FnWindowsCreateString(PCNZWCH sourceString, UINT32 length, HSTRING* string); typedef HRESULT WINAPI FnWindowsDeleteString(HSTRING string); From 43254d3513bcf2d4b5ba22026af57726ca2f0abc Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Wed, 18 Dec 2024 21:47:45 +0100 Subject: [PATCH 09/15] store the app delegate reference in MacApplication --- .../com/sun/glass/ui/mac/MacApplication.java | 21 ++++-- .../main/native-glass/mac/GlassApplication.m | 68 ++++++++++++------- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java index 7782d86301d..a9d93a75bf5 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java +++ b/modules/javafx.graphics/src/main/java/com/sun/glass/ui/mac/MacApplication.java @@ -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 @@ -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); @@ -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(); @@ -420,8 +425,12 @@ public String getDataDirectory() { private native String _getApplicationClassName(); + private native Map _getPlatformPreferences(long delegateHandle); + @Override - public native Map getPlatformPreferences(); + public Map getPlatformPreferences() { + return _getPlatformPreferences(delegateHandle); + } @Override public Map> getPlatformKeyMappings() { diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index da418c7dfca..1f4f721beda 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -952,32 +952,49 @@ + (BOOL)syncRenderingDisabled { /* * Class: com_sun_glass_ui_mac_MacApplication - * Method: _runLoop - * Signature: (Ljava/lang/ClassLoader;Ljava/lang/Runnable;Z)V + * Method: _initDelegate + * Signature: (Ljava/lang/ClassLoader;Ljava/lang/Runnable;Z)J */ -JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacApplication__1runLoop +JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_mac_MacApplication__1initDelegate (JNIEnv *env, jobject japplication, jobject classLoader, jobject jlaunchable, jboolean isTaskbarApplication) { - LOG("Java_com_sun_glass_ui_mac_MacApplication__1runLoop"); + LOG("Java_com_sun_glass_ui_mac_MacApplication__1initDelegate"); - NSAutoreleasePool *glasspool = [[NSAutoreleasePool alloc] init]; + if ([NSThread isMainThread] == YES) { - if ([NSThread isMainThread] == YES) - { - // fprintf(stderr, "\nWARNING: Glass was started on 1st thread and will block this thread.\nYou most likely do not want to do this - please remove \"-XstartOnFirstThread\" from VM arguments.\n\n"); - } - else + // fprintf(stderr, "\nWARNING: Glass was started on 1st thread and will block this thread.\nYou most likely do not want to do this - please remove \"-XstartOnFirstThread\" from VM arguments.\n\n"); + } + else + { + if ([[NSThread currentThread] name] == nil) { - if ([[NSThread currentThread] name] == nil) - { - [[NSThread currentThread] setName:@"Main Java Thread"]; - } + [[NSThread currentThread] setName:@"Main Java Thread"]; } + } + + return (jlong)[[GlassApplication alloc] initWithEnv:env + application:japplication + launchable:jlaunchable + taskbarApplication:isTaskbarApplication + classLoader:classLoader]; +} - GlassApplication *glass = [[GlassApplication alloc] initWithEnv:env application:japplication launchable:jlaunchable taskbarApplication:isTaskbarApplication classLoader:classLoader]; +/* + * Class: com_sun_glass_ui_mac_MacApplication + * Method: _runLoop + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacApplication__1runLoop +(JNIEnv *env, jobject japplication, jlong appDelegate) +{ + LOG("Java_com_sun_glass_ui_mac_MacApplication__1runLoop"); + + NSAutoreleasePool *glasspool = [[NSAutoreleasePool alloc] init]; + { + GlassApplication* glass = (GlassApplication*)appDelegate; if ([NSThread isMainThread] == YES) { - [glass runLoop: glass]; + [glass runLoop: glassApplication]; } else { [glass performSelectorOnMainThread:@selector(runLoop:) withObject:glass waitUntilDone:[NSThread isMainThread]]; @@ -998,13 +1015,17 @@ + (BOOL)syncRenderingDisabled { /* * Class: com_sun_glass_ui_mac_MacApplication * Method: _finishTerminating - * Signature: ()V + * Signature: (J)V */ JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacApplication__1finishTerminating -(JNIEnv *env, jobject japplication) +(JNIEnv *env, jobject japplication, jlong appDelegate) { LOG("Java_com_sun_glass_ui_mac_MacApplication__1finishTerminating"); + if (appDelegate) { + [(GlassApplication*)appDelegate release]; + } + if (isEmbedded) { return; } @@ -1262,11 +1283,12 @@ + (BOOL)syncRenderingDisabled { /* * Class: com_sun_glass_ui_mac_MacApplication * Method: getPlatformPreferences - * Signature: ()Ljava/util/Map; + * Signature: (J)Ljava/util/Map; */ -JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_mac_MacApplication_getPlatformPreferences -(JNIEnv *env, jobject self) +JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_mac_MacApplication__1getPlatformPreferences +(JNIEnv *env, jobject self, jlong appDelegate) { - GlassApplication* app = (GlassApplication*)[NSApp delegate]; - return [app getPlatformPreferences]; + return appDelegate + ? [(GlassApplication*)appDelegate getPlatformPreferences] + : nil; } From fd4846a9bf0bc3ef650fff012ac37e8c98cda767 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Wed, 18 Dec 2024 21:50:09 +0100 Subject: [PATCH 10/15] typo --- .../src/main/native-glass/mac/GlassApplication.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index 1f4f721beda..c959b7eddbb 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -994,7 +994,7 @@ + (BOOL)syncRenderingDisabled { { GlassApplication* glass = (GlassApplication*)appDelegate; if ([NSThread isMainThread] == YES) { - [glass runLoop: glassApplication]; + [glass runLoop: glass]; } else { [glass performSelectorOnMainThread:@selector(runLoop:) withObject:glass waitUntilDone:[NSThread isMainThread]]; From a590b31bfcb9c588bf3a48078f742feb0f21a3aa Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Thu, 19 Dec 2024 05:13:43 +0100 Subject: [PATCH 11/15] smaller diff --- .../main/native-glass/mac/GlassApplication.m | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index c959b7eddbb..7e91f2aef50 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -961,18 +961,6 @@ + (BOOL)syncRenderingDisabled { { LOG("Java_com_sun_glass_ui_mac_MacApplication__1initDelegate"); - if ([NSThread isMainThread] == YES) - { - // fprintf(stderr, "\nWARNING: Glass was started on 1st thread and will block this thread.\nYou most likely do not want to do this - please remove \"-XstartOnFirstThread\" from VM arguments.\n\n"); - } - else - { - if ([[NSThread currentThread] name] == nil) - { - [[NSThread currentThread] setName:@"Main Java Thread"]; - } - } - return (jlong)[[GlassApplication alloc] initWithEnv:env application:japplication launchable:jlaunchable @@ -992,6 +980,18 @@ + (BOOL)syncRenderingDisabled { NSAutoreleasePool *glasspool = [[NSAutoreleasePool alloc] init]; { + if ([NSThread isMainThread] == YES) + { + // fprintf(stderr, "\nWARNING: Glass was started on 1st thread and will block this thread.\nYou most likely do not want to do this - please remove \"-XstartOnFirstThread\" from VM arguments.\n\n"); + } + else + { + if ([[NSThread currentThread] name] == nil) + { + [[NSThread currentThread] setName:@"Main Java Thread"]; + } + } + GlassApplication* glass = (GlassApplication*)appDelegate; if ([NSThread isMainThread] == YES) { [glass runLoop: glass]; From 7feba0bf18e7603c6d8f6c3593a9751e46675d0c Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Thu, 19 Dec 2024 05:15:59 +0100 Subject: [PATCH 12/15] method name --- .../src/main/native-glass/mac/GlassApplication.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index 7e91f2aef50..2b5a9783c98 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -1282,7 +1282,7 @@ + (BOOL)syncRenderingDisabled { /* * Class: com_sun_glass_ui_mac_MacApplication - * Method: getPlatformPreferences + * Method: _getPlatformPreferences * Signature: (J)Ljava/util/Map; */ JNIEXPORT jobject JNICALL Java_com_sun_glass_ui_mac_MacApplication__1getPlatformPreferences From f83849253c6f453e6e03530217506d7cf99425d0 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Thu, 19 Dec 2024 20:31:58 +0100 Subject: [PATCH 13/15] only update preferences that may have changed --- .../main/native-glass/win/PlatformSupport.cpp | 58 ++++++++++++------- .../main/native-glass/win/PlatformSupport.h | 18 ++++-- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp index 851af86ac03..92977418664 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp @@ -98,7 +98,7 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) settings5->add_AutoHideScrollBarsChanged( Callback>( [this](IUISettings*, IUISettingsAutoHideScrollBarsChangedEventArgs*) { - updatePreferences(); + updatePreferences(PreferenceType::UI_SETTINGS); return S_OK; }).Get(), &token); @@ -123,7 +123,7 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) networkInformation->add_NetworkStatusChanged( Callback( [this](IInspectable*) { - updatePreferences(); + updatePreferences(PreferenceType::NETWORK_INFORMATION); return S_OK; }).Get(), &token); @@ -139,7 +139,7 @@ PlatformSupport::~PlatformSupport() uninitializeRoActivationSupport(); } -jobject PlatformSupport::collectPreferences() const +jobject PlatformSupport::collectPreferences(PreferenceType preferenceType) const { if (!initialized) { return NULL; @@ -148,20 +148,32 @@ jobject PlatformSupport::collectPreferences() const jobject prefs = env->NewObject(javaClasses.HashMap, javaIDs.HashMap.init); if (CheckAndClearException(env)) return NULL; - querySystemParameters(prefs); - querySystemColors(prefs); - queryUISettings(prefs); - queryNetworkInformation(prefs); + if (preferenceType & PreferenceType::SYSTEM_COLORS) { + querySystemColors(prefs); + } + + if (preferenceType & PreferenceType::SYSTEM_PARAMS) { + querySystemParameters(prefs); + } + + if (preferenceType & PreferenceType::UI_SETTINGS) { + queryUISettings(prefs); + } + + if (preferenceType & PreferenceType::NETWORK_INFORMATION) { + queryNetworkInformation(prefs); + } + return prefs; } -bool PlatformSupport::updatePreferences() const +bool PlatformSupport::updatePreferences(PreferenceType preferenceType) const { if (!initialized) { return false; } - jobject newPreferences = collectPreferences(); + jobject newPreferences = collectPreferences(preferenceType); jboolean preferencesChanged = newPreferences != NULL && @@ -191,11 +203,11 @@ bool PlatformSupport::onSettingChanged(WPARAM wParam, LPARAM lParam) const switch ((UINT)wParam) { case SPI_SETHIGHCONTRAST: case SPI_SETCLIENTAREAANIMATION: - return updatePreferences(); + return updatePreferences(PreferenceType::SYSTEM_PARAMS); } if (lParam != NULL && wcscmp(LPCWSTR(lParam), L"ImmersiveColorSet") == 0) { - return updatePreferences(); + return updatePreferences(PreferenceType::UI_SETTINGS); } return false; @@ -307,25 +319,27 @@ void PlatformSupport::queryNetworkInformation(jobject properties) const ComPtr connectionProfile; ComPtr connectionCost; NetworkCostType networkCostType; - const char* internetCostType; + const char* internetCostType = NULL; RO_CHECKED("INetworkInformation::GetInternetConnectionProfile", this->networkInformation->GetInternetConnectionProfile(&connectionProfile)); - RO_CHECKED("IConnectionProfile::GetConnectionCost", - connectionProfile->GetConnectionCost(&connectionCost)); + if (connectionProfile) { + RO_CHECKED("IConnectionProfile::GetConnectionCost", + connectionProfile->GetConnectionCost(&connectionCost)); - RO_CHECKED("IConnectionCost::get_NetworkCostType", - connectionCost->get_NetworkCostType(&networkCostType)); + RO_CHECKED("IConnectionCost::get_NetworkCostType", + connectionCost->get_NetworkCostType(&networkCostType)); - switch (networkCostType) { - case NetworkCostType_Unrestricted: internetCostType = "Unrestricted"; break; - case NetworkCostType_Variable: internetCostType = "Variable"; break; - case NetworkCostType_Fixed: internetCostType = "Fixed"; break; - default: internetCostType = "Unknown"; break; + switch (networkCostType) { + case NetworkCostType_Unrestricted: internetCostType = "Unrestricted"; break; + case NetworkCostType_Variable: internetCostType = "Variable"; break; + case NetworkCostType_Fixed: internetCostType = "Fixed"; break; + } } - putString(properties, "Windows.NetworkInformation.InternetCostType", internetCostType); + putString(properties, "Windows.NetworkInformation.InternetCostType", + internetCostType != NULL ? internetCostType : "Unknown"); } catch (RoException const&) { } } diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h index 7aa706c0887..dfe12035c11 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h @@ -39,21 +39,29 @@ class PlatformSupport final { public: + enum PreferenceType { + SYSTEM_COLORS = 1, + SYSTEM_PARAMS = 2, + UI_SETTINGS = 4, + NETWORK_INFORMATION = 8, + ANY = SYSTEM_COLORS | SYSTEM_PARAMS | UI_SETTINGS | NETWORK_INFORMATION + }; + PlatformSupport(JNIEnv*, jobject application); ~PlatformSupport(); PlatformSupport(PlatformSupport const&) = delete; PlatformSupport& operator=(PlatformSupport const&) = delete; /** - * Collect all platform preferences and return them as a new java/util/Map. + * Collect the specified platform preferences and return them as a new java/util/Map. */ - jobject collectPreferences() const; + jobject collectPreferences(PreferenceType = ANY) const; /** - * Collect all platform preferences and notify the JavaFX application when a preference has changed. - * The change notification includes all preferences, not only the changed preferences. + * Collect the specified platform preferences and notify the JavaFX application when a preference has changed. + * The change notification includes all specified preferences, not only the changed preferences. */ - bool updatePreferences() const; + bool updatePreferences(PreferenceType = ANY) const; /** * Handles the WM_SETTINGCHANGE message. From b7840bd6c9c1af25685bbcec9ccd04652ebb5bc4 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:01:08 +0100 Subject: [PATCH 14/15] change enum constant names --- .../native-glass/win/GlassApplication.cpp | 2 +- .../main/native-glass/win/GlassApplication.h | 2 +- .../main/native-glass/win/PlatformSupport.cpp | 39 ++++++++++--------- .../main/native-glass/win/PlatformSupport.h | 14 +++---- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.cpp b/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.cpp index 6a20afad36d..7072769a3ae 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.cpp @@ -182,7 +182,7 @@ LRESULT GlassApplication::WindowProc(UINT msg, WPARAM wParam, LPARAM lParam) case WM_THEMECHANGED: case WM_SYSCOLORCHANGE: case WM_DWMCOLORIZATIONCOLORCHANGED: - if (m_platformSupport.updatePreferences()) { + if (m_platformSupport.updatePreferences(PlatformSupport::PT_SYSTEM_COLORS)) { return 0; } break; diff --git a/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.h b/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.h index 971a6729a2b..2c9ffd94eb4 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.h +++ b/modules/javafx.graphics/src/main/native-glass/win/GlassApplication.h @@ -83,7 +83,7 @@ class GlassApplication : protected BaseWnd { void UnregisterClipboardViewer(); static jobject GetPlatformPreferences() { - return pInstance ? pInstance->m_platformSupport.collectPreferences() : NULL; + return pInstance ? pInstance->m_platformSupport.collectPreferences(PlatformSupport::PT_ALL) : NULL; } inline static DWORD GetMainThreadId() diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp index 92977418664..6e951da9358 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.cpp @@ -98,7 +98,7 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) settings5->add_AutoHideScrollBarsChanged( Callback>( [this](IUISettings*, IUISettingsAutoHideScrollBarsChangedEventArgs*) { - updatePreferences(PreferenceType::UI_SETTINGS); + updatePreferences(PT_UI_SETTINGS); return S_OK; }).Get(), &token); @@ -123,7 +123,7 @@ PlatformSupport::PlatformSupport(JNIEnv* env, jobject application) networkInformation->add_NetworkStatusChanged( Callback( [this](IInspectable*) { - updatePreferences(PreferenceType::NETWORK_INFORMATION); + updatePreferences(PT_NETWORK_INFORMATION); return S_OK; }).Get(), &token); @@ -148,19 +148,19 @@ jobject PlatformSupport::collectPreferences(PreferenceType preferenceType) const jobject prefs = env->NewObject(javaClasses.HashMap, javaIDs.HashMap.init); if (CheckAndClearException(env)) return NULL; - if (preferenceType & PreferenceType::SYSTEM_COLORS) { + if (preferenceType & PT_SYSTEM_COLORS) { querySystemColors(prefs); } - if (preferenceType & PreferenceType::SYSTEM_PARAMS) { + if (preferenceType & PT_SYSTEM_PARAMS) { querySystemParameters(prefs); } - if (preferenceType & PreferenceType::UI_SETTINGS) { + if (preferenceType & PT_UI_SETTINGS) { queryUISettings(prefs); } - if (preferenceType & PreferenceType::NETWORK_INFORMATION) { + if (preferenceType & PT_NETWORK_INFORMATION) { queryNetworkInformation(prefs); } @@ -203,11 +203,11 @@ bool PlatformSupport::onSettingChanged(WPARAM wParam, LPARAM lParam) const switch ((UINT)wParam) { case SPI_SETHIGHCONTRAST: case SPI_SETCLIENTAREAANIMATION: - return updatePreferences(PreferenceType::SYSTEM_PARAMS); + return updatePreferences(PT_SYSTEM_PARAMS); } if (lParam != NULL && wcscmp(LPCWSTR(lParam), L"ImmersiveColorSet") == 0) { - return updatePreferences(PreferenceType::UI_SETTINGS); + return updatePreferences(PT_UI_SETTINGS); } return false; @@ -217,20 +217,21 @@ void PlatformSupport::querySystemParameters(jobject properties) const { HIGHCONTRAST contrastInfo; contrastInfo.cbSize = sizeof(HIGHCONTRAST); - ::SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(HIGHCONTRAST), &contrastInfo, 0); - - // Property names need to be kept in sync with WinApplication.java: - if (contrastInfo.dwFlags & HCF_HIGHCONTRASTON) { - putBoolean(properties, "Windows.SPI.HighContrast", true); - putString(properties, "Windows.SPI.HighContrastColorScheme", contrastInfo.lpszDefaultScheme); - } else { - putBoolean(properties, "Windows.SPI.HighContrast", false); - putString(properties, "Windows.SPI.HighContrastColorScheme", (const char*)NULL); + if (::SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(HIGHCONTRAST), &contrastInfo, 0)) { + // Property names need to be kept in sync with WinApplication.java: + if (contrastInfo.dwFlags & HCF_HIGHCONTRASTON) { + putBoolean(properties, "Windows.SPI.HighContrast", true); + putString(properties, "Windows.SPI.HighContrastColorScheme", contrastInfo.lpszDefaultScheme); + } else { + putBoolean(properties, "Windows.SPI.HighContrast", false); + putString(properties, "Windows.SPI.HighContrastColorScheme", (const char*)NULL); + } } BOOL value; - ::SystemParametersInfo(SPI_GETCLIENTAREAANIMATION, 0, &value, 0); - putBoolean(properties, "Windows.SPI.ClientAreaAnimation", value); + if (::SystemParametersInfo(SPI_GETCLIENTAREAANIMATION, 0, &value, 0)) { + putBoolean(properties, "Windows.SPI.ClientAreaAnimation", value); + } } void PlatformSupport::querySystemColors(jobject properties) const diff --git a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h index dfe12035c11..89e70e58b07 100644 --- a/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/win/PlatformSupport.h @@ -40,11 +40,11 @@ class PlatformSupport final { public: enum PreferenceType { - SYSTEM_COLORS = 1, - SYSTEM_PARAMS = 2, - UI_SETTINGS = 4, - NETWORK_INFORMATION = 8, - ANY = SYSTEM_COLORS | SYSTEM_PARAMS | UI_SETTINGS | NETWORK_INFORMATION + PT_SYSTEM_COLORS = 1, + PT_SYSTEM_PARAMS = 2, + PT_UI_SETTINGS = 4, + PT_NETWORK_INFORMATION = 8, + PT_ALL = PT_SYSTEM_COLORS | PT_SYSTEM_PARAMS | PT_UI_SETTINGS | PT_NETWORK_INFORMATION }; PlatformSupport(JNIEnv*, jobject application); @@ -55,13 +55,13 @@ class PlatformSupport final /** * Collect the specified platform preferences and return them as a new java/util/Map. */ - jobject collectPreferences(PreferenceType = ANY) const; + jobject collectPreferences(PreferenceType) const; /** * Collect the specified platform preferences and notify the JavaFX application when a preference has changed. * The change notification includes all specified preferences, not only the changed preferences. */ - bool updatePreferences(PreferenceType = ANY) const; + bool updatePreferences(PreferenceType) const; /** * Handles the WM_SETTINGCHANGE message. From 6f015a475277fbc6bc2dca4f93ed0fafb5deead6 Mon Sep 17 00:00:00 2001 From: mstr2 <43553916+mstr2@users.noreply.github.com> Date: Fri, 20 Dec 2024 18:58:20 +0100 Subject: [PATCH 15/15] release macOS PlatformSupport instance --- .../src/main/native-glass/mac/GlassApplication.m | 9 +++++++++ .../src/main/native-glass/mac/PlatformSupport.h | 5 +++++ .../src/main/native-glass/mac/PlatformSupport.m | 10 +++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m index 2b5a9783c98..b543b5d6392 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/GlassApplication.m @@ -165,6 +165,15 @@ - (id)initWithEnv:(JNIEnv*)env application:(jobject)application launchable:(jobj return self; } +- (void)dealloc { + if (platformSupport) { + [platformSupport stopEventProcessing]; + [platformSupport release]; + } + + [super dealloc]; +} + #pragma mark --- delegate methods - (void)GlassApplicationDidChangeScreenParameters diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h index 695c4088066..8624ca89f1e 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.h @@ -40,6 +40,11 @@ */ - (id)initWithEnv:(JNIEnv*)env application:(jobject)app; +/** + * Stops event processing, which must be done prior to releasing this PlatformSupport instance. + */ +- (void)stopEventProcessing; + /** * Collect all platform preferences and return them as a new java/util/Map. */ diff --git a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m index 1110fc33c1d..f1d515e73c5 100644 --- a/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m +++ b/modules/javafx.graphics/src/main/native-glass/mac/PlatformSupport.m @@ -65,6 +65,7 @@ } @implementation PlatformSupport { + nw_path_monitor_t pathMonitor; jobject currentPreferences; bool currentPathConstrained; bool currentPathExpensive; @@ -126,7 +127,7 @@ - (id)initWithEnv:(JNIEnv*)jEnv application:(jobject)jApp { name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:nil]; - nw_path_monitor_t pathMonitor = nw_path_monitor_create(); + pathMonitor = nw_path_monitor_create(); nw_path_monitor_set_update_handler(pathMonitor, ^(nw_path_t path) { self->currentPathConstrained = nw_path_is_constrained(path); self->currentPathExpensive = nw_path_is_expensive(path); @@ -139,6 +140,13 @@ - (id)initWithEnv:(JNIEnv*)jEnv application:(jobject)jApp { return self; } +- (void)stopEventProcessing { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; + [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; + nw_path_monitor_cancel(pathMonitor); +} + - (void)platformPreferencesDidChange { // Some dynamic colors like NSColor.controlAccentColor don't seem to be reliably updated // at the exact moment AppleColorPreferencesChangedNotification is received.