diff options
| author | 2019-02-07 10:05:10 +0100 | |
|---|---|---|
| committer | 2019-02-07 10:05:10 +0100 | |
| commit | 96c796d4d385b7a076a3e7ab7b374907472487e2 (patch) | |
| tree | 9828a6af02e103ec293f9ceed60e7527a9d271f6 /src | |
| parent | Fix entry remapping not applying to resolved entry (diff) | |
| download | enigma-96c796d4d385b7a076a3e7ab7b374907472487e2.tar.gz enigma-96c796d4d385b7a076a3e7ab7b374907472487e2.tar.xz enigma-96c796d4d385b7a076a3e7ab7b374907472487e2.zip | |
Add System and None (JVM Default) themes. (#107)
The current 2 editor color schemes are used depending on whether a LAF is detected to be dark ot light.
This is done by drawing an empty JPanel into a dummy image and getting the grayscale pixel brightness.
Move setting theme a bit earlier so it doesn't throw exceptions in some cases when using system theme.
Change the order of setting swing LAF and applying theme so that apply() can get the right colors.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/cuchaz/enigma/config/Config.java | 118 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/config/Themes.java | 2 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/gui/Gui.java | 4 |
3 files changed, 73 insertions, 51 deletions
diff --git a/src/main/java/cuchaz/enigma/config/Config.java b/src/main/java/cuchaz/enigma/config/Config.java index 6f5a337c..e2afb018 100644 --- a/src/main/java/cuchaz/enigma/config/Config.java +++ b/src/main/java/cuchaz/enigma/config/Config.java | |||
| @@ -7,6 +7,7 @@ import com.google.gson.*; | |||
| 7 | import javax.swing.*; | 7 | import javax.swing.*; |
| 8 | import javax.swing.plaf.metal.MetalLookAndFeel; | 8 | import javax.swing.plaf.metal.MetalLookAndFeel; |
| 9 | import java.awt.*; | 9 | import java.awt.*; |
| 10 | import java.awt.image.BufferedImage; | ||
| 10 | import java.io.File; | 11 | import java.io.File; |
| 11 | import java.io.IOException; | 12 | import java.io.IOException; |
| 12 | import java.lang.reflect.Type; | 13 | import java.lang.reflect.Type; |
| @@ -34,8 +35,12 @@ public class Config { | |||
| 34 | 35 | ||
| 35 | public enum LookAndFeel { | 36 | public enum LookAndFeel { |
| 36 | DEFAULT("Default"), | 37 | DEFAULT("Default"), |
| 37 | DARCULA("Darcula"); | 38 | DARCULA("Darcula"), |
| 39 | SYSTEM("System"), | ||
| 40 | NONE("None (JVM default)"); | ||
| 38 | 41 | ||
| 42 | // the "JVM default" look and feel, get it at the beginning and store it so we can set it later | ||
| 43 | private static javax.swing.LookAndFeel NONE_LAF = UIManager.getLookAndFeel(); | ||
| 39 | private final String name; | 44 | private final String name; |
| 40 | 45 | ||
| 41 | LookAndFeel(String name) { | 46 | LookAndFeel(String name) { |
| @@ -49,66 +54,83 @@ public class Config { | |||
| 49 | public void setGlobalLAF() { | 54 | public void setGlobalLAF() { |
| 50 | try { | 55 | try { |
| 51 | switch (this) { | 56 | switch (this) { |
| 57 | case NONE: | ||
| 58 | UIManager.setLookAndFeel(NONE_LAF); | ||
| 59 | break; | ||
| 52 | case DEFAULT: | 60 | case DEFAULT: |
| 53 | UIManager.setLookAndFeel(new MetalLookAndFeel()); | 61 | UIManager.setLookAndFeel(new MetalLookAndFeel()); |
| 54 | break; | 62 | break; |
| 55 | case DARCULA: | 63 | case DARCULA: |
| 56 | UIManager.setLookAndFeel(new DarculaLaf()); | 64 | UIManager.setLookAndFeel(new DarculaLaf()); |
| 57 | break; | 65 | break; |
| 66 | case SYSTEM: | ||
| 67 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | ||
| 58 | } | 68 | } |
| 59 | } catch (Exception e){ | 69 | } catch (Exception e){ |
| 60 | throw new Error("Failed to set global look and feel", e); | 70 | throw new Error("Failed to set global look and feel", e); |
| 61 | } | 71 | } |
| 62 | } | 72 | } |
| 63 | 73 | ||
| 74 | public static boolean isDarkLaf() { | ||
| 75 | // a bit of a hack because swing doesn't give any API for that, and we need colors that aren't defined in look and feel | ||
| 76 | JPanel panel = new JPanel(); | ||
| 77 | panel.setSize(new Dimension(10, 10)); | ||
| 78 | panel.doLayout(); | ||
| 79 | |||
| 80 | BufferedImage image = new BufferedImage(panel.getSize().width, panel.getSize().height, BufferedImage.TYPE_INT_RGB); | ||
| 81 | panel.printAll(image.getGraphics()); | ||
| 82 | |||
| 83 | Color c = new Color(image.getRGB(0, 0)); | ||
| 84 | |||
| 85 | // convert the color we got to grayscale | ||
| 86 | int b = (int) (0.3 * c.getRed() + 0.59 * c.getGreen() + 0.11 * c.getBlue()); | ||
| 87 | return b < 85; | ||
| 88 | } | ||
| 89 | |||
| 64 | public void apply(Config config) { | 90 | public void apply(Config config) { |
| 65 | switch (this) { | 91 | boolean isDark = this == LookAndFeel.DARCULA || isDarkLaf(); |
| 66 | case DEFAULT: | 92 | if (!isDark) {//Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139 |
| 67 | //Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139 | 93 | config.lineNumbersForeground = 0x333300; |
| 68 | config.lineNumbersForeground = 0x333300; | 94 | config.lineNumbersBackground = 0xEEEEFF; |
| 69 | config.lineNumbersBackground = 0xEEEEFF; | 95 | config.lineNumbersSelected = 0xCCCCEE; |
| 70 | config.lineNumbersSelected = 0xCCCCEE; | 96 | config.obfuscatedColor = new AlphaColorEntry(0xFFDCDC, 1.0f); |
| 71 | config.obfuscatedColor = new AlphaColorEntry(0xFFDCDC, 1.0f); | 97 | config.obfuscatedColorOutline = new AlphaColorEntry(0xA05050, 1.0f); |
| 72 | config.obfuscatedColorOutline = new AlphaColorEntry(0xA05050, 1.0f); | 98 | config.proposedColor = new AlphaColorEntry(0x000000, 0.075f); |
| 73 | config.proposedColor = new AlphaColorEntry(0x000000, 0.075f); | 99 | config.proposedColorOutline = new AlphaColorEntry(0x000000, 0.15f); |
| 74 | config.proposedColorOutline = new AlphaColorEntry(0x000000, 0.15f); | 100 | config.deobfuscatedColor = new AlphaColorEntry(0xDCFFDC, 1.0f); |
| 75 | config.deobfuscatedColor = new AlphaColorEntry(0xDCFFDC, 1.0f); | 101 | config.deobfuscatedColorOutline = new AlphaColorEntry(0x50A050, 1.0f); |
| 76 | config.deobfuscatedColorOutline = new AlphaColorEntry(0x50A050, 1.0f); | 102 | config.editorBackground = 0xFFFFFF; |
| 77 | config.editorBackground = 0xFFFFFF; | 103 | config.highlightColor = 0x3333EE; |
| 78 | config.highlightColor = 0x3333EE; | 104 | config.caretColor = 0x000000; |
| 79 | config.caretColor = 0x000000; | 105 | config.selectionHighlightColor = 0x000000; |
| 80 | config.selectionHighlightColor = 0x000000; | 106 | config.stringColor = 0xCC6600; |
| 81 | config.stringColor = 0xCC6600; | 107 | config.numberColor = 0x999933; |
| 82 | config.numberColor = 0x999933; | 108 | config.operatorColor = 0x000000; |
| 83 | config.operatorColor = 0x000000; | 109 | config.delimiterColor = 0x000000; |
| 84 | config.delimiterColor = 0x000000; | 110 | config.typeColor = 0x000000; |
| 85 | config.typeColor = 0x000000; | 111 | config.identifierColor = 0x000000; |
| 86 | config.identifierColor = 0x000000; | 112 | config.defaultTextColor = 0x000000; |
| 87 | config.defaultTextColor = 0x000000; | 113 | } else {//Based off colors found here: https://github.com/dracula/dracula-theme/ |
| 88 | break; | 114 | config.lineNumbersForeground = 0xA4A4A3; |
| 89 | case DARCULA: | 115 | config.lineNumbersBackground = 0x313335; |
| 90 | //Based off colors found here: https://github.com/dracula/dracula-theme/ | 116 | config.lineNumbersSelected = 0x606366; |
| 91 | config.lineNumbersForeground = 0xA4A4A3; | 117 | config.obfuscatedColor = new AlphaColorEntry(0xFF5555, 0.3f); |
| 92 | config.lineNumbersBackground = 0x313335; | 118 | config.obfuscatedColorOutline = new AlphaColorEntry(0xFF5555, 0.5f); |
| 93 | config.lineNumbersSelected = 0x606366; | 119 | config.deobfuscatedColor = new AlphaColorEntry(0x50FA7B, 0.3f); |
| 94 | config.obfuscatedColor = new AlphaColorEntry(0xFF5555, 0.3f); | 120 | config.deobfuscatedColorOutline = new AlphaColorEntry(0x50FA7B, 0.5f); |
| 95 | config.obfuscatedColorOutline = new AlphaColorEntry(0xFF5555, 0.5f); | 121 | config.proposedColor = new AlphaColorEntry(0x606366, 0.3f); |
| 96 | config.deobfuscatedColor = new AlphaColorEntry(0x50FA7B, 0.3f); | 122 | config.proposedColorOutline = new AlphaColorEntry(0x606366, 0.5f); |
| 97 | config.deobfuscatedColorOutline = new AlphaColorEntry(0x50FA7B, 0.5f); | 123 | config.editorBackground = 0x282A36; |
| 98 | config.proposedColor = new AlphaColorEntry(0x606366, 0.3f); | 124 | config.highlightColor = 0xFF79C6; |
| 99 | config.proposedColorOutline = new AlphaColorEntry(0x606366, 0.5f); | 125 | config.caretColor = 0xF8F8F2; |
| 100 | config.editorBackground = 0x282A36; | 126 | config.selectionHighlightColor = 0xF8F8F2; |
| 101 | config.highlightColor = 0xFF79C6; | 127 | config.stringColor = 0xF1FA8C; |
| 102 | config.caretColor = 0xF8F8F2; | 128 | config.numberColor = 0xBD93F9; |
| 103 | config.selectionHighlightColor = 0xF8F8F2; | 129 | config.operatorColor = 0xF8F8F2; |
| 104 | config.stringColor = 0xF1FA8C; | 130 | config.delimiterColor = 0xF8F8F2; |
| 105 | config.numberColor = 0xBD93F9; | 131 | config.typeColor = 0xF8F8F2; |
| 106 | config.operatorColor = 0xF8F8F2; | 132 | config.identifierColor = 0xF8F8F2; |
| 107 | config.delimiterColor = 0xF8F8F2; | 133 | config.defaultTextColor = 0xF8F8F2; |
| 108 | config.typeColor = 0xF8F8F2; | ||
| 109 | config.identifierColor = 0xF8F8F2; | ||
| 110 | config.defaultTextColor = 0xF8F8F2; | ||
| 111 | break; | ||
| 112 | } | 134 | } |
| 113 | } | 135 | } |
| 114 | } | 136 | } |
diff --git a/src/main/java/cuchaz/enigma/config/Themes.java b/src/main/java/cuchaz/enigma/config/Themes.java index 400ea3f0..753654e1 100644 --- a/src/main/java/cuchaz/enigma/config/Themes.java +++ b/src/main/java/cuchaz/enigma/config/Themes.java | |||
| @@ -19,8 +19,8 @@ public class Themes { | |||
| 19 | 19 | ||
| 20 | public static void updateTheme(Gui gui) { | 20 | public static void updateTheme(Gui gui) { |
| 21 | Config config = Config.getInstance(); | 21 | Config config = Config.getInstance(); |
| 22 | config.lookAndFeel.apply(config); | ||
| 23 | config.lookAndFeel.setGlobalLAF(); | 22 | config.lookAndFeel.setGlobalLAF(); |
| 23 | config.lookAndFeel.apply(config); | ||
| 24 | try { | 24 | try { |
| 25 | config.saveConfig(); | 25 | config.saveConfig(); |
| 26 | } catch (IOException e) { | 26 | } catch (IOException e) { |
diff --git a/src/main/java/cuchaz/enigma/gui/Gui.java b/src/main/java/cuchaz/enigma/gui/Gui.java index a6e20a27..3593e4fa 100644 --- a/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/src/main/java/cuchaz/enigma/gui/Gui.java | |||
| @@ -91,13 +91,13 @@ public class Gui { | |||
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | public Gui() { | 93 | public Gui() { |
| 94 | Config.getInstance().lookAndFeel.setGlobalLAF(); | ||
| 95 | |||
| 94 | // init frame | 96 | // init frame |
| 95 | this.frame = new JFrame(Constants.NAME); | 97 | this.frame = new JFrame(Constants.NAME); |
| 96 | final Container pane = this.frame.getContentPane(); | 98 | final Container pane = this.frame.getContentPane(); |
| 97 | pane.setLayout(new BorderLayout()); | 99 | pane.setLayout(new BorderLayout()); |
| 98 | 100 | ||
| 99 | Config.getInstance().lookAndFeel.setGlobalLAF(); | ||
| 100 | |||
| 101 | if (Boolean.parseBoolean(System.getProperty("enigma.catchExceptions", "true"))) { | 101 | if (Boolean.parseBoolean(System.getProperty("enigma.catchExceptions", "true"))) { |
| 102 | // install a global exception handler to the event thread | 102 | // install a global exception handler to the event thread |
| 103 | CrashDialog.init(this.frame); | 103 | CrashDialog.init(this.frame); |