|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package javafx.scene.control.theme; |
| 27 | + |
| 28 | +import com.sun.javafx.PlatformUtil; |
| 29 | +import com.sun.javafx.application.HighContrastScheme; |
| 30 | +import com.sun.javafx.application.PlatformImpl; |
| 31 | +import javafx.application.ConditionalFeature; |
| 32 | +import javafx.application.PlatformPreferences; |
| 33 | +import javafx.beans.value.WritableValue; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.ResourceBundle; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@code Modena} is a built-in JavaFX theme. |
| 39 | + * |
| 40 | + * @since 20 |
| 41 | + */ |
| 42 | +public class ModenaTheme extends ThemeBase { |
| 43 | + |
| 44 | + private final WritableValue<String> highContrastStylesheet; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new instance of the {@code ModenaTheme} class. |
| 48 | + */ |
| 49 | + public ModenaTheme() { |
| 50 | + addLast("com/sun/javafx/scene/control/skin/modena/modena.css"); |
| 51 | + |
| 52 | + if (PlatformImpl.isSupported(ConditionalFeature.INPUT_TOUCH)) { |
| 53 | + addLast("com/sun/javafx/scene/control/skin/modena/touch.css"); |
| 54 | + } |
| 55 | + |
| 56 | + if (PlatformUtil.isEmbedded()) { |
| 57 | + addLast("com/sun/javafx/scene/control/skin/modena/modena-embedded-performance.css"); |
| 58 | + } |
| 59 | + |
| 60 | + if (PlatformUtil.isAndroid()) { |
| 61 | + addLast("com/sun/javafx/scene/control/skin/modena/android.css"); |
| 62 | + } |
| 63 | + |
| 64 | + if (PlatformUtil.isIOS()) { |
| 65 | + addLast("com/sun/javafx/scene/control/skin/modena/ios.css"); |
| 66 | + } |
| 67 | + |
| 68 | + if (PlatformImpl.isSupported(ConditionalFeature.TWO_LEVEL_FOCUS)) { |
| 69 | + addLast("com/sun/javafx/scene/control/skin/modena/two-level-focus.css"); |
| 70 | + } |
| 71 | + |
| 72 | + if (PlatformImpl.isSupported(ConditionalFeature.VIRTUAL_KEYBOARD)) { |
| 73 | + addLast("com/sun/javafx/scene/control/skin/caspian/fxvk.css"); |
| 74 | + } |
| 75 | + |
| 76 | + if (!PlatformImpl.isSupported(ConditionalFeature.TRANSPARENT_WINDOW)) { |
| 77 | + addLast("com/sun/javafx/scene/control/skin/modena/modena-no-transparency.css"); |
| 78 | + } |
| 79 | + |
| 80 | + highContrastStylesheet = addLast(null); |
| 81 | + updateHighContrastTheme(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void onPreferencesChanged(Map<String, Object> preferences) { |
| 86 | + updateHighContrastTheme(); |
| 87 | + } |
| 88 | + |
| 89 | + private void updateHighContrastTheme() { |
| 90 | + String themeName = null; |
| 91 | + String overrideThemeName = System.getProperty("com.sun.javafx.highContrastTheme"); |
| 92 | + if (overrideThemeName != null) { |
| 93 | + themeName = overrideThemeName; |
| 94 | + } |
| 95 | + |
| 96 | + if (themeName == null) { |
| 97 | + PlatformPreferences preferences = PlatformImpl.getPlatformPreferences(); |
| 98 | + if (preferences.getBoolean("Windows.SPI.HighContrastOn", false)) { |
| 99 | + themeName = preferences.getString("Windows.SPI.HighContrastColorScheme"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (themeName != null) { |
| 104 | + String stylesheet = switch (themeName.toUpperCase()) { |
| 105 | + case "BLACKONWHITE" -> "com/sun/javafx/scene/control/skin/modena/blackOnWhite.css"; |
| 106 | + case "WHITEONBLACK" -> "com/sun/javafx/scene/control/skin/modena/whiteOnBlack.css"; |
| 107 | + case "YELLOWONBLACK" -> "com/sun/javafx/scene/control/skin/modena/yellowOnBlack.css"; |
| 108 | + default -> null; |
| 109 | + }; |
| 110 | + |
| 111 | + if (stylesheet == null) { |
| 112 | + ResourceBundle bundle = ResourceBundle.getBundle("com/sun/glass/ui/win/themes"); |
| 113 | + String enumValue = HighContrastScheme.fromThemeName(bundle::getString, themeName); |
| 114 | + |
| 115 | + stylesheet = enumValue != null ? switch (HighContrastScheme.valueOf(enumValue)) { |
| 116 | + case HIGH_CONTRAST_WHITE -> "com/sun/javafx/scene/control/skin/modena/blackOnWhite.css"; |
| 117 | + case HIGH_CONTRAST_BLACK -> "com/sun/javafx/scene/control/skin/modena/whiteOnBlack.css"; |
| 118 | + case HIGH_CONTRAST_1, HIGH_CONTRAST_2 -> "com/sun/javafx/scene/control/skin/modena/yellowOnBlack.css"; |
| 119 | + } : null; |
| 120 | + } |
| 121 | + |
| 122 | + highContrastStylesheet.setValue(stylesheet); |
| 123 | + } else { |
| 124 | + highContrastStylesheet.setValue(null); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments