diff options
| author | 2020-06-03 13:39:42 -0400 | |
|---|---|---|
| committer | 2020-06-03 18:39:42 +0100 | |
| commit | 0f47403d0220757fed189b76e2071e25b1025cb8 (patch) | |
| tree | 879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/gui/util | |
| parent | Fix search dialog hanging for a short time sometimes (#250) (diff) | |
| download | enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip | |
Split GUI code to separate module (#242)
* Split into modules
* Post merge compile fixes
Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/util')
4 files changed, 0 insertions, 244 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java b/src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java deleted file mode 100644 index 612e3e9..0000000 --- a/src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java +++ /dev/null | |||
| @@ -1,77 +0,0 @@ | |||
| 1 | package cuchaz.enigma.gui.util; | ||
| 2 | |||
| 3 | import java.awt.Component; | ||
| 4 | import java.awt.event.MouseEvent; | ||
| 5 | |||
| 6 | import javax.swing.*; | ||
| 7 | import javax.swing.border.Border; | ||
| 8 | |||
| 9 | public abstract class AbstractListCellRenderer<E> extends JPanel implements ListCellRenderer<E> { | ||
| 10 | |||
| 11 | private static final Border NO_FOCUS_BORDER = BorderFactory.createEmptyBorder(1, 1, 1, 1); | ||
| 12 | |||
| 13 | private Border noFocusBorder; | ||
| 14 | |||
| 15 | public AbstractListCellRenderer() { | ||
| 16 | setBorder(getNoFocusBorder()); | ||
| 17 | } | ||
| 18 | |||
| 19 | protected Border getNoFocusBorder() { | ||
| 20 | if (noFocusBorder == null) { | ||
| 21 | Border border = UIManager.getLookAndFeel().getDefaults().getBorder("List.List.cellNoFocusBorder"); | ||
| 22 | noFocusBorder = border != null ? border : NO_FOCUS_BORDER; | ||
| 23 | } | ||
| 24 | return noFocusBorder; | ||
| 25 | } | ||
| 26 | |||
| 27 | protected Border getBorder(boolean isSelected, boolean cellHasFocus) { | ||
| 28 | Border b = null; | ||
| 29 | if (cellHasFocus) { | ||
| 30 | UIDefaults defaults = UIManager.getLookAndFeel().getDefaults(); | ||
| 31 | if (isSelected) { | ||
| 32 | b = defaults.getBorder("List.focusSelectedCellHighlightBorder"); | ||
| 33 | } | ||
| 34 | if (b == null) { | ||
| 35 | b = defaults.getBorder("List.focusCellHighlightBorder"); | ||
| 36 | } | ||
| 37 | } else { | ||
| 38 | b = getNoFocusBorder(); | ||
| 39 | } | ||
| 40 | return b; | ||
| 41 | } | ||
| 42 | |||
| 43 | public abstract void updateUiForEntry(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus); | ||
| 44 | |||
| 45 | @Override | ||
| 46 | public Component getListCellRendererComponent(JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) { | ||
| 47 | updateUiForEntry(list, value, index, isSelected, cellHasFocus); | ||
| 48 | |||
| 49 | if (isSelected) { | ||
| 50 | setBackground(list.getSelectionBackground()); | ||
| 51 | setForeground(list.getSelectionForeground()); | ||
| 52 | } else { | ||
| 53 | setBackground(list.getBackground()); | ||
| 54 | setForeground(list.getForeground()); | ||
| 55 | } | ||
| 56 | |||
| 57 | setEnabled(list.isEnabled()); | ||
| 58 | setFont(list.getFont()); | ||
| 59 | |||
| 60 | setBorder(getBorder(isSelected, cellHasFocus)); | ||
| 61 | |||
| 62 | // This isn't the width of the cell, but it's close enough for where it's needed (getComponentAt in getToolTipText) | ||
| 63 | setSize(list.getWidth(), getPreferredSize().height); | ||
| 64 | |||
| 65 | return this; | ||
| 66 | } | ||
| 67 | |||
| 68 | @Override | ||
| 69 | public String getToolTipText(MouseEvent event) { | ||
| 70 | Component c = getComponentAt(event.getPoint()); | ||
| 71 | if (c instanceof JComponent) { | ||
| 72 | return ((JComponent) c).getToolTipText(); | ||
| 73 | } | ||
| 74 | return getToolTipText(); | ||
| 75 | } | ||
| 76 | |||
| 77 | } | ||
diff --git a/src/main/java/cuchaz/enigma/gui/util/History.java b/src/main/java/cuchaz/enigma/gui/util/History.java deleted file mode 100644 index 94f3105..0000000 --- a/src/main/java/cuchaz/enigma/gui/util/History.java +++ /dev/null | |||
| @@ -1,49 +0,0 @@ | |||
| 1 | package cuchaz.enigma.gui.util; | ||
| 2 | |||
| 3 | import com.google.common.collect.Queues; | ||
| 4 | |||
| 5 | import java.util.Deque; | ||
| 6 | |||
| 7 | public class History<T> { | ||
| 8 | private final Deque<T> previous = Queues.newArrayDeque(); | ||
| 9 | private final Deque<T> next = Queues.newArrayDeque(); | ||
| 10 | private T current; | ||
| 11 | |||
| 12 | public History(T initial) { | ||
| 13 | current = initial; | ||
| 14 | } | ||
| 15 | |||
| 16 | public T getCurrent() { | ||
| 17 | return current; | ||
| 18 | } | ||
| 19 | |||
| 20 | public void push(T value) { | ||
| 21 | previous.addLast(current); | ||
| 22 | current = value; | ||
| 23 | next.clear(); | ||
| 24 | } | ||
| 25 | |||
| 26 | public void replace(T value) { | ||
| 27 | current = value; | ||
| 28 | } | ||
| 29 | |||
| 30 | public boolean canGoBack() { | ||
| 31 | return !previous.isEmpty(); | ||
| 32 | } | ||
| 33 | |||
| 34 | public T goBack() { | ||
| 35 | next.addFirst(current); | ||
| 36 | current = previous.removeLast(); | ||
| 37 | return current; | ||
| 38 | } | ||
| 39 | |||
| 40 | public boolean canGoForward() { | ||
| 41 | return !next.isEmpty(); | ||
| 42 | } | ||
| 43 | |||
| 44 | public T goForward() { | ||
| 45 | previous.addLast(current); | ||
| 46 | current = next.removeFirst(); | ||
| 47 | return current; | ||
| 48 | } | ||
| 49 | } | ||
diff --git a/src/main/java/cuchaz/enigma/gui/util/ScaleChangeListener.java b/src/main/java/cuchaz/enigma/gui/util/ScaleChangeListener.java deleted file mode 100644 index d045c6d..0000000 --- a/src/main/java/cuchaz/enigma/gui/util/ScaleChangeListener.java +++ /dev/null | |||
| @@ -1,8 +0,0 @@ | |||
| 1 | package cuchaz.enigma.gui.util; | ||
| 2 | |||
| 3 | @FunctionalInterface | ||
| 4 | public interface ScaleChangeListener { | ||
| 5 | |||
| 6 | void onScaleChanged(float scale, float oldScale); | ||
| 7 | |||
| 8 | } | ||
diff --git a/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java b/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java deleted file mode 100644 index 9f722e9..0000000 --- a/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java +++ /dev/null | |||
| @@ -1,110 +0,0 @@ | |||
| 1 | package cuchaz.enigma.gui.util; | ||
| 2 | |||
| 3 | import java.awt.Dimension; | ||
| 4 | import java.awt.Font; | ||
| 5 | import java.io.IOException; | ||
| 6 | import java.lang.reflect.Field; | ||
| 7 | import java.util.ArrayList; | ||
| 8 | import java.util.List; | ||
| 9 | |||
| 10 | import javax.swing.BorderFactory; | ||
| 11 | import javax.swing.UIManager; | ||
| 12 | import javax.swing.border.Border; | ||
| 13 | |||
| 14 | import com.github.swingdpi.UiDefaultsScaler; | ||
| 15 | import com.github.swingdpi.plaf.BasicTweaker; | ||
| 16 | import com.github.swingdpi.plaf.MetalTweaker; | ||
| 17 | import com.github.swingdpi.plaf.NimbusTweaker; | ||
| 18 | import com.github.swingdpi.plaf.WindowsTweaker; | ||
| 19 | import cuchaz.enigma.config.Config; | ||
| 20 | import de.sciss.syntaxpane.DefaultSyntaxKit; | ||
| 21 | |||
| 22 | public class ScaleUtil { | ||
| 23 | |||
| 24 | private static List<ScaleChangeListener> listeners = new ArrayList<>(); | ||
| 25 | |||
| 26 | public static float getScaleFactor() { | ||
| 27 | return Config.getInstance().scaleFactor; | ||
| 28 | } | ||
| 29 | |||
| 30 | public static void setScaleFactor(float scaleFactor) { | ||
| 31 | float oldScale = getScaleFactor(); | ||
| 32 | float clamped = Math.min(Math.max(0.25f, scaleFactor), 10.0f); | ||
| 33 | Config.getInstance().scaleFactor = clamped; | ||
| 34 | try { | ||
| 35 | Config.getInstance().saveConfig(); | ||
| 36 | } catch (IOException e) { | ||
| 37 | e.printStackTrace(); | ||
| 38 | } | ||
| 39 | listeners.forEach(l -> l.onScaleChanged(clamped, oldScale)); | ||
| 40 | } | ||
| 41 | |||
| 42 | public static void addListener(ScaleChangeListener listener) { | ||
| 43 | listeners.add(listener); | ||
| 44 | } | ||
| 45 | |||
| 46 | public static void removeListener(ScaleChangeListener listener) { | ||
| 47 | listeners.remove(listener); | ||
| 48 | } | ||
| 49 | |||
| 50 | public static Dimension getDimension(int width, int height) { | ||
| 51 | return new Dimension(scale(width), scale(height)); | ||
| 52 | } | ||
| 53 | |||
| 54 | public static Font getFont(String fontName, int plain, int fontSize) { | ||
| 55 | return scaleFont(new Font(fontName, plain, fontSize)); | ||
| 56 | } | ||
| 57 | |||
| 58 | public static Font scaleFont(Font font) { | ||
| 59 | return createTweakerForCurrentLook(getScaleFactor()).modifyFont("", font); | ||
| 60 | } | ||
| 61 | |||
| 62 | public static float scale(float f) { | ||
| 63 | return f * getScaleFactor(); | ||
| 64 | } | ||
| 65 | |||
| 66 | public static float invert(float f) { | ||
| 67 | return f / getScaleFactor(); | ||
| 68 | } | ||
| 69 | |||
| 70 | public static int scale(int i) { | ||
| 71 | return (int) (i * getScaleFactor()); | ||
| 72 | } | ||
| 73 | |||
| 74 | public static Border createEmptyBorder(int top, int left, int bottom, int right) { | ||
| 75 | return BorderFactory.createEmptyBorder(scale(top), scale(left), scale(bottom), scale(right)); | ||
| 76 | } | ||
| 77 | |||
| 78 | public static int invert(int i) { | ||
| 79 | return (int) (i / getScaleFactor()); | ||
| 80 | } | ||
| 81 | |||
| 82 | public static void applyScaling() { | ||
| 83 | float scale = getScaleFactor(); | ||
| 84 | UiDefaultsScaler.updateAndApplyGlobalScaling((int) (100 * scale), true); | ||
| 85 | try { | ||
| 86 | Field defaultFontField = DefaultSyntaxKit.class.getDeclaredField("DEFAULT_FONT"); | ||
| 87 | defaultFontField.setAccessible(true); | ||
| 88 | Font font = (Font) defaultFontField.get(null); | ||
| 89 | font = font.deriveFont(12 * scale); | ||
| 90 | defaultFontField.set(null, font); | ||
| 91 | } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| 92 | e.printStackTrace(); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | private static BasicTweaker createTweakerForCurrentLook(float dpiScaling) { | ||
| 97 | String testString = UIManager.getLookAndFeel().getName().toLowerCase(); | ||
| 98 | if (testString.contains("windows")) { | ||
| 99 | return new WindowsTweaker(dpiScaling, testString.contains("classic")); | ||
| 100 | } | ||
| 101 | if (testString.contains("metal")) { | ||
| 102 | return new MetalTweaker(dpiScaling); | ||
| 103 | } | ||
| 104 | if (testString.contains("nimbus")) { | ||
| 105 | return new NimbusTweaker(dpiScaling); | ||
| 106 | } | ||
| 107 | return new BasicTweaker(dpiScaling); | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||