Skip to content

Commit 1bd5649

Browse files
authored
Merge branch 'mica-bg' into 2.0
2 parents 3d636c4 + ecf9d17 commit 1bd5649

5 files changed

Lines changed: 408 additions & 3 deletions

File tree

src/main/java/net/minecraftforge/installer/InstallerPanel.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,17 @@ private void updateFilePath() {
255255
public void run(ProgressCallback monitor) {
256256
JOptionPane optionPane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
257257

258-
dialog = optionPane.createDialog("Forge Installer");
258+
if (Win11MicaEffect.isSupported()) {
259+
dialog = Win11Dialog.createWin11Dialog(optionPane, "Forge Installer");
260+
Win11MicaEffect.prepare(optionPane);
261+
Win11MicaEffect.prepare(this);
262+
Win11MicaEffect.install(dialog);
263+
} else {
264+
dialog = optionPane.createDialog("Forge Installer");
265+
}
259266
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
260267
dialog.setVisible(true);
261-
int result = (Integer) (optionPane.getValue() != null ? optionPane.getValue() : -1);
268+
int result = optionPane.getValue() instanceof Integer selected ? selected : JOptionPane.CLOSED_OPTION;
262269

263270
if (result == JOptionPane.OK_OPTION) {
264271
ProgressFrame prog = new ProgressFrame(monitor, "Installing " + profile.getVersion(), Thread.currentThread()::interrupt);

src/main/java/net/minecraftforge/installer/SimpleInstaller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static void main(String[] args) throws IOException, URISyntaxException {
9999
"sessionserver.mojang.com",
100100
"authserver.mojang.com",
101101
}) {
102-
monitor.message("Host: " + host + " [" + DownloadUtils.getIpString(host) + "]");
102+
monitor.message("Host: " + host + " [" + DownloadUtils.getIpString(host) + ']');
103103
}
104104

105105
for (String host : new String[] {
@@ -178,6 +178,7 @@ private static void launchGui(ProgressCallback monitor, File installer, String b
178178
private static void launchGui(ProgressCallback monitor, File installer, String badCerts, OptionParser parser) {
179179
try {
180180
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
181+
if (Win11MicaEffect.isSupported()) SwingUtil.applyGlobalFont("Segoe UI");
181182
} catch (HeadlessException headless) {
182183
// if ran in a headless CLI environment with no args, show some help text and exit gracefully
183184
if (parser == null) {

src/main/java/net/minecraftforge/installer/SwingUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@
55
package net.minecraftforge.installer;
66

77
import javax.swing.*;
8+
import javax.swing.plaf.FontUIResource;
89
import java.awt.*;
910
import java.io.File;
1011
import java.io.IOException;
12+
import java.util.Enumeration;
1113

1214
public class SwingUtil {
15+
public static void applyGlobalFont(String fontFamily) {
16+
UIDefaults defaults = UIManager.getDefaults();
17+
Enumeration<Object> keys = defaults.keys();
18+
while (keys.hasMoreElements()) {
19+
Object key = keys.nextElement();
20+
Object value = defaults.get(key);
21+
if (value instanceof FontUIResource) {
22+
FontUIResource font = (FontUIResource) value;
23+
defaults.put(key, new FontUIResource(fontFamily, font.getStyle(), font.getSize()));
24+
}
25+
}
26+
}
27+
1328
public static JButton createLogButton() {
1429
JButton button = new JButton("Open log");
1530
button.addActionListener(ev -> {
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.installer;
6+
7+
import java.awt.*;
8+
import java.awt.geom.Line2D;
9+
import java.awt.event.MouseAdapter;
10+
import java.awt.event.MouseEvent;
11+
import java.awt.event.WindowAdapter;
12+
import java.awt.event.WindowEvent;
13+
14+
import javax.swing.*;
15+
16+
final class Win11Dialog {
17+
private static final Dimension CAPTION_BUTTON_SIZE = new Dimension(46, 30);
18+
private static final Color WINDOW_HIT_TEST_COLOR = new Color(255, 255, 255, 1);
19+
private static final Color TITLE_BAR_OVERLAY = new Color(255, 255, 255, 28);
20+
private static final Color CLOSE_BUTTON_HOVER = new Color(196, 43, 28);
21+
private static final Color CLOSE_BUTTON_PRESSED = new Color(143, 32, 20);
22+
private static final Color CLOSE_BUTTON_GLYPH = new Color(32, 32, 32);
23+
24+
private Win11Dialog() {}
25+
26+
static JDialog createWin11Dialog(JOptionPane optionPane, String title) {
27+
JDialog installerDialog = new JDialog(null, title, Dialog.ModalityType.APPLICATION_MODAL);
28+
installerDialog.setUndecorated(true);
29+
installerDialog.setType(Window.Type.NORMAL);
30+
installerDialog.setContentPane(createInstallerChrome(installerDialog, optionPane));
31+
installerDialog.addWindowListener(new WindowAdapter() {
32+
@Override
33+
public void windowClosing(WindowEvent e) {
34+
optionPane.setValue(JOptionPane.CLOSED_OPTION);
35+
}
36+
});
37+
optionPane.addPropertyChangeListener(event -> {
38+
String propertyName = event.getPropertyName();
39+
if (!installerDialog.isVisible() || event.getSource() != optionPane) {
40+
return;
41+
}
42+
if (!JOptionPane.VALUE_PROPERTY.equals(propertyName) && !JOptionPane.INPUT_VALUE_PROPERTY.equals(propertyName)) {
43+
return;
44+
}
45+
installerDialog.setVisible(false);
46+
});
47+
installerDialog.pack();
48+
installerDialog.setLocationRelativeTo(null);
49+
optionPane.selectInitialValue();
50+
return installerDialog;
51+
}
52+
53+
private static JPanel createInstallerChrome(JDialog installerDialog, JOptionPane optionPane) {
54+
JPanel chrome = new JPanel(new BorderLayout()) {
55+
@Override
56+
protected void paintComponent(Graphics graphics) {
57+
Graphics2D g2 = (Graphics2D) graphics.create();
58+
try {
59+
g2.setColor(WINDOW_HIT_TEST_COLOR);
60+
g2.fillRect(0, 0, getWidth(), getHeight());
61+
} finally {
62+
g2.dispose();
63+
}
64+
super.paintComponent(graphics);
65+
}
66+
};
67+
chrome.setOpaque(false);
68+
chrome.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
69+
chrome.add(createTitleBar(installerDialog, optionPane), BorderLayout.NORTH);
70+
chrome.add(optionPane, BorderLayout.CENTER);
71+
return chrome;
72+
}
73+
74+
private static JPanel createTitleBar(JDialog installerDialog, JOptionPane optionPane) {
75+
JPanel titleBar = new JPanel(new BorderLayout()) {
76+
@Override
77+
protected void paintComponent(Graphics graphics) {
78+
Graphics2D g2 = (Graphics2D) graphics.create();
79+
try {
80+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
81+
g2.setColor(TITLE_BAR_OVERLAY);
82+
g2.fillRect(0, 0, getWidth(), getHeight());
83+
} finally {
84+
g2.dispose();
85+
}
86+
super.paintComponent(graphics);
87+
}
88+
};
89+
titleBar.setOpaque(false);
90+
titleBar.setBorder(BorderFactory.createEmptyBorder(0, 12, 0, 0));
91+
92+
JLabel titleLabel = new JLabel(installerDialog.getTitle());
93+
titleLabel.setHorizontalAlignment(SwingConstants.LEFT);
94+
titleLabel.setBorder(BorderFactory.createEmptyBorder(8, 0, 8, 0));
95+
titleBar.add(titleLabel, BorderLayout.WEST);
96+
97+
JButton closeButton = new JButton() {
98+
@Override
99+
protected void paintComponent(Graphics graphics) {
100+
Graphics2D g2 = (Graphics2D) graphics.create();
101+
try {
102+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
103+
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
104+
105+
ButtonModel model = getModel();
106+
if (model.isPressed()) {
107+
g2.setColor(CLOSE_BUTTON_PRESSED);
108+
g2.fillRect(0, 0, getWidth(), getHeight());
109+
} else if (model.isRollover()) {
110+
g2.setColor(CLOSE_BUTTON_HOVER);
111+
g2.fillRect(0, 0, getWidth(), getHeight());
112+
}
113+
114+
g2.setColor(model.isPressed() || model.isRollover() ? Color.WHITE : CLOSE_BUTTON_GLYPH);
115+
g2.setStroke(new BasicStroke(1.2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
116+
int cx = getWidth() / 2;
117+
int cy = getHeight() / 2;
118+
g2.draw(new Line2D.Float(cx - 4.5f, cy - 4.5f, cx + 4.5f, cy + 4.5f));
119+
g2.draw(new Line2D.Float(cx + 4.5f, cy - 4.5f, cx - 4.5f, cy + 4.5f));
120+
} finally {
121+
g2.dispose();
122+
}
123+
}
124+
};
125+
closeButton.setFocusable(false);
126+
closeButton.setOpaque(false);
127+
closeButton.setContentAreaFilled(false);
128+
closeButton.setBorderPainted(false);
129+
closeButton.setRolloverEnabled(true);
130+
closeButton.setMargin(new Insets(8, 12, 8, 12));
131+
closeButton.setPreferredSize(CAPTION_BUTTON_SIZE);
132+
closeButton.setMinimumSize(CAPTION_BUTTON_SIZE);
133+
closeButton.setMaximumSize(CAPTION_BUTTON_SIZE);
134+
closeButton.setToolTipText("Close");
135+
closeButton.addActionListener(ignored -> optionPane.setValue(JOptionPane.CLOSED_OPTION));
136+
titleBar.add(closeButton, BorderLayout.EAST);
137+
138+
Point[] dragOffset = {null};
139+
MouseAdapter dragListener = new MouseAdapter() {
140+
@Override
141+
public void mousePressed(MouseEvent e) {
142+
dragOffset[0] = e.getPoint();
143+
}
144+
145+
@Override
146+
public void mouseDragged(MouseEvent e) {
147+
if (dragOffset[0] == null) {
148+
return;
149+
}
150+
Point screen = e.getLocationOnScreen();
151+
installerDialog.setLocation(screen.x - dragOffset[0].x, screen.y - dragOffset[0].y);
152+
}
153+
};
154+
titleBar.addMouseListener(dragListener);
155+
titleBar.addMouseMotionListener(dragListener);
156+
titleLabel.addMouseListener(dragListener);
157+
titleLabel.addMouseMotionListener(dragListener);
158+
159+
return titleBar;
160+
}
161+
}
162+

0 commit comments

Comments
 (0)