Skip to content

Commit d905361

Browse files
committed
WIP
1 parent 78773fc commit d905361

File tree

3 files changed

+42
-72
lines changed

3 files changed

+42
-72
lines changed

modules/javafx.graphics/src/main/java/com/sun/javafx/application/ThemeHelper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public static void setAccessor(Accessor accessor) {
4848
}
4949

5050
public interface Accessor {
51-
void platformThemeChanged(Theme theme, Map<String, String> changedProperties);
51+
void platformThemeChanged(Map<String, String> changedProperties);
5252
}
5353

54-
public static void platformThemeChanged(Theme theme, Map<String, String> changedProperties) {
55-
accessor.platformThemeChanged(theme, changedProperties);
54+
public static void platformThemeChanged(Map<String, String> changedProperties) {
55+
accessor.platformThemeChanged(changedProperties);
5656
}
5757

5858
public static Theme tryResolve(String uri) {

modules/javafx.graphics/src/main/java/com/sun/javafx/tk/quantum/QuantumToolkit.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import com.sun.javafx.application.ThemeHelper;
2929
import javafx.application.ConditionalFeature;
30-
import javafx.application.Theme;
3130
import javafx.geometry.Dimension2D;
3231
import javafx.scene.image.Image;
3332
import javafx.scene.image.PixelBuffer;
@@ -368,10 +367,7 @@ void runToolkit() {
368367
}
369368

370369
@Override public void handlePlatformThemeChanged(Map<String, String> properties) {
371-
Theme currentTheme = PlatformImpl.getCurrentTheme();
372-
if (currentTheme != null) {
373-
ThemeHelper.platformThemeChanged(currentTheme, properties);
374-
}
370+
ThemeHelper.platformThemeChanged(properties);
375371
}
376372
});
377373
}

modules/javafx.graphics/src/main/java/javafx/application/Theme.java

+38-64
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@
3232
import javafx.collections.ObservableList;
3333

3434
import java.lang.ref.WeakReference;
35-
import java.util.*;
35+
import java.util.Collections;
36+
import java.util.HashMap;
37+
import java.util.HashSet;
38+
import java.util.Iterator;
39+
import java.util.Map;
40+
import java.util.Objects;
41+
import java.util.Set;
42+
import java.util.UUID;
3643

3744
import static com.sun.javafx.application.ThemeHelper.THEME_INSTANCE_SCHEME;
3845

@@ -127,44 +134,31 @@ public abstract class Theme {
127134
static {
128135
ThemeHelper.setAccessor(new ThemeHelper.Accessor() {
129136
@Override
130-
public void platformThemeChanged(Theme theme, Map<String, String> properties) {
131-
theme.handlePlatformThemeChanged(properties);
137+
public void platformThemeChanged(Map<String, String> properties) {
138+
Iterator<WeakReference<Theme>> it = instances.iterator();
139+
while (it.hasNext()) {
140+
Theme theme = it.next().get();
141+
if (theme != null) {
142+
theme.handlePlatformThemeChanged(properties);
143+
} else {
144+
it.remove();
145+
}
146+
}
132147
}
133148
});
134149
}
135150

136151
private static final Set<WeakReference<Theme>> instances = new HashSet<>();
137152

138-
private final Theme baseTheme;
139-
private Map<String, String> properties;
140-
private Map<String, String> propertiesUnmodifiable;
141-
private String url;
153+
private final String url = getClass().getName() + ":" + UUID.randomUUID();
154+
private Map<String, String> properties = Toolkit.getToolkit().getPlatformThemeProperties();
155+
private Map<String, String> propertiesUnmodifiable = Collections.unmodifiableMap(properties);
142156

143157
/**
144158
* Creates a new {@code Theme} instance.
145159
*/
146160
protected Theme() {
147-
this.baseTheme = null;
148-
this.properties = Toolkit.getToolkit().getPlatformThemeProperties();
149-
this.propertiesUnmodifiable = Collections.unmodifiableMap(properties);
150-
}
151-
152-
/**
153-
* Creates a new {@code Theme} instance that extends the specified base theme.
154-
* <p>
155-
* Sub-classes should implement {@link #getStylesheets()} by returning a concatenation
156-
* of the base theme's stylesheets with additional extended stylesheets.
157-
*
158-
* @param baseTheme the base theme
159-
*/
160-
protected Theme(Theme baseTheme) {
161-
if (baseTheme == null) {
162-
throw new NullPointerException("baseTheme cannot be null");
163-
}
164-
165-
this.baseTheme = baseTheme;
166-
this.properties = baseTheme.properties;
167-
this.propertiesUnmodifiable = baseTheme.propertiesUnmodifiable;
161+
instances.add(new WeakReference<>(this));
168162
}
169163

170164
/**
@@ -214,15 +208,6 @@ public static Theme of(String... stylesheets) {
214208
FXCollections.observableArrayList(stylesheets) : null);
215209
}
216210

217-
/**
218-
* Gets the base theme that is extended by this theme.
219-
*
220-
* @return the base theme, or {@code null} if this theme has no base theme
221-
*/
222-
protected final Theme getBaseTheme() {
223-
return baseTheme;
224-
}
225-
226211
/**
227212
* The list of stylesheets that comprise this theme.
228213
* <p>
@@ -251,13 +236,8 @@ private void handlePlatformThemeChanged(Map<String, String> properties) {
251236
changed.keySet().removeIf(key -> Objects.equals(properties.get(key), this.properties.get(key)));
252237

253238
if (!changed.isEmpty()) {
254-
this.properties = new HashMap<>(properties);
239+
this.properties = properties;
255240
this.propertiesUnmodifiable = Collections.unmodifiableMap(this.properties);
256-
257-
if (baseTheme != null) {
258-
baseTheme.platformThemeChanged(changed);
259-
}
260-
261241
platformThemeChanged(changed);
262242
}
263243
}
@@ -285,15 +265,6 @@ protected final Map<String, String> getPlatformThemeProperties() {
285265
* @return the URL that corresponds to this {@code Theme} instance.
286266
*/
287267
public final String toURL() {
288-
if (url == null) {
289-
url = getClass().getName() + ":" + UUID.randomUUID();
290-
291-
synchronized (instances) {
292-
instances.removeIf(ref -> ref.get() == null);
293-
instances.add(new WeakReference<>(this));
294-
}
295-
}
296-
297268
return THEME_INSTANCE_SCHEME + url;
298269
}
299270

@@ -318,12 +289,10 @@ public static Theme fromURL(String url) {
318289

319290
url = url.substring(15);
320291

321-
synchronized (instances) {
322-
for (var instance : instances) {
323-
Theme theme = instance.get();
324-
if (theme != null && theme.url.equals(url)) {
325-
return theme;
326-
}
292+
for (var instance : instances) {
293+
Theme theme = instance.get();
294+
if (theme != null && theme.url.equals(url)) {
295+
return theme;
327296
}
328297
}
329298

@@ -344,20 +313,20 @@ public ObservableList<String> getStylesheets() {
344313
}
345314

346315
private static class ExtensionTheme extends Theme {
316+
final Theme baseTheme;
347317
final ObservableList<String> allStylesheets;
348318

349319
ExtensionTheme(Theme baseTheme, ObservableList<String> additionalStylesheets) {
350-
super(baseTheme);
351-
352-
allStylesheets = new ListBinding<>() {
320+
this.baseTheme = baseTheme;
321+
this.allStylesheets = new ListBinding<>() {
353322
{
354-
bind(getBaseTheme().getStylesheets(), additionalStylesheets);
323+
bind(baseTheme.getStylesheets(), additionalStylesheets);
355324
}
356325

357326
@Override
358-
@SuppressWarnings({"unchecked", "ConstantConditions"})
327+
@SuppressWarnings("unchecked")
359328
protected ObservableList<String> computeValue() {
360-
return FXCollections.concat(getBaseTheme().getStylesheets(), additionalStylesheets);
329+
return FXCollections.concat(baseTheme.getStylesheets(), additionalStylesheets);
361330
}
362331
};
363332
}
@@ -366,6 +335,11 @@ protected ObservableList<String> computeValue() {
366335
public ObservableList<String> getStylesheets() {
367336
return allStylesheets;
368337
}
338+
339+
@Override
340+
protected void platformThemeChanged(Map<String, String> properties) {
341+
baseTheme.platformThemeChanged(properties);
342+
}
369343
}
370344

371345
}

0 commit comments

Comments
 (0)