summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/config/Themes.java
blob: b8a72c5e503d9aed996d54848372396d47730c6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cuchaz.enigma.gui.config;

import java.awt.Font;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.UIManager;

import de.sciss.syntaxpane.DefaultSyntaxKit;

import cuchaz.enigma.gui.EnigmaSyntaxKit;
import cuchaz.enigma.gui.events.ThemeChangeListener;
import cuchaz.enigma.gui.highlight.BoxHighlightPainter;
import cuchaz.enigma.gui.util.ScaleUtil;
import cuchaz.enigma.source.RenamableTokenType;

public class Themes {
	private static final Set<ThemeChangeListener> listeners = new HashSet<>();

	// Calling this after the UI is initialized (e.g. when the user changes
	// theme settings) is currently not functional.
	public static void setupTheme() {
		LookAndFeel laf = UiConfig.getActiveLookAndFeel();
		laf.setGlobalLAF();
		UiConfig.setLookAndFeelDefaults(UiConfig.getLookAndFeel(), LookAndFeel.isDarkLaf());
		UiConfig.snapshotConfig();
		Themes.setFonts();
		UIManager.put("ScrollBar.showButtons", true);
		EnigmaSyntaxKit.invalidate();
		DefaultSyntaxKit.initKit();
		DefaultSyntaxKit.registerContentType("text/enigma-sources", EnigmaSyntaxKit.class.getName());
		Map<RenamableTokenType, BoxHighlightPainter> boxHighlightPainters = getBoxHighlightPainters();
		listeners.forEach(l -> l.onThemeChanged(laf, boxHighlightPainters));
		ScaleUtil.applyScaling();
		UiConfig.save();
	}

	private static void setFonts() {
		if (UiConfig.activeUseCustomFonts()) {
			Font small = UiConfig.getSmallFont();
			Font bold = UiConfig.getDefaultFont();
			Font normal = UiConfig.getDefault2Font();

			UIManager.put("CheckBox.font", bold);
			UIManager.put("CheckBoxMenuItem.font", bold);
			UIManager.put("CheckBoxMenuItem.acceleratorFont", small);
			UIManager.put("ColorChooser.font", normal);
			UIManager.put("ComboBox.font", bold);
			UIManager.put("DesktopIcon.font", bold);
			UIManager.put("EditorPane.font", normal);
			UIManager.put("InternalFrame.titleFont", bold);
			UIManager.put("FormattedTextField.font", normal);
			UIManager.put("Label.font", bold);
			UIManager.put("List.font", bold);
			UIManager.put("Menu.acceleratorFont", small);
			UIManager.put("Menu.font", bold);
			UIManager.put("MenuBar.font", bold);
			UIManager.put("MenuItem.acceleratorFont", small);
			UIManager.put("MenuItem.font", bold);
			UIManager.put("OptionPane.font", normal);
			UIManager.put("Panel.font", normal);
			UIManager.put("PasswordField.font", normal);
			UIManager.put("PopupMenu.font", bold);
			UIManager.put("ProgressBar.font", bold);
			UIManager.put("RadioButton.font", bold);
			UIManager.put("RadioButtonMenuItem.acceleratorFont", small);
			UIManager.put("RadioButtonMenuItem.font", bold);
			UIManager.put("ScrollPane.font", normal);
			UIManager.put("Slider.font", bold);
			UIManager.put("Spinner.font", bold);
			UIManager.put("TabbedPane.font", bold);
			UIManager.put("Table.font", normal);
			UIManager.put("TableHeader.font", normal);
			UIManager.put("TextArea.font", normal);
			UIManager.put("TextField.font", normal);
			UIManager.put("TextPane.font", normal);
			UIManager.put("TitledBorder.font", bold);
			UIManager.put("ToggleButton.font", bold);
			UIManager.put("ToolBar.font", bold);
			UIManager.put("ToolTip.font", normal);
			UIManager.put("Tree.font", normal);
			UIManager.put("Viewport.font", normal);
			UIManager.put("Button.font", bold);
		}
	}

	public static Map<RenamableTokenType, BoxHighlightPainter> getBoxHighlightPainters() {
		return Map.of(
				RenamableTokenType.OBFUSCATED, BoxHighlightPainter.create(UiConfig.getObfuscatedColor(), UiConfig.getObfuscatedOutlineColor()),
				RenamableTokenType.PROPOSED, BoxHighlightPainter.create(UiConfig.getProposedColor(), UiConfig.getProposedOutlineColor()),
				RenamableTokenType.DEOBFUSCATED, BoxHighlightPainter.create(UiConfig.getDeobfuscatedColor(), UiConfig.getDeobfuscatedOutlineColor()),
				RenamableTokenType.UNOBFUSCATED, BoxHighlightPainter.create(UiConfig.getUnobfuscatedColor(), UiConfig.getUnobfuscatedOutlineColor())
		);
	}

	public static void addListener(ThemeChangeListener listener) {
		listeners.add(listener);
	}

	public static void removeListener(ThemeChangeListener listener) {
		listeners.remove(listener);
	}
}