|
| 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