|
| 1 | +/* |
| 2 | + * Copyright [2025] [Gianluca Beil] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.spotifyxp.dialogs; |
| 18 | + |
| 19 | +import com.intellij.uiDesigner.core.GridConstraints; |
| 20 | +import com.intellij.uiDesigner.core.GridLayoutManager; |
| 21 | +import com.intellij.uiDesigner.core.Spacer; |
| 22 | +import com.spotifyxp.PublicValues; |
| 23 | +import com.spotifyxp.deps.se.michaelthelin.spotify.model_objects.specification.Episode; |
| 24 | +import com.spotifyxp.deps.se.michaelthelin.spotify.model_objects.specification.Track; |
| 25 | +import com.spotifyxp.events.EventSubscriber; |
| 26 | +import com.spotifyxp.events.Events; |
| 27 | +import com.spotifyxp.events.SpotifyXPEvents; |
| 28 | +import com.spotifyxp.logging.ConsoleLogging; |
| 29 | +import com.spotifyxp.manager.InstanceManager; |
| 30 | +import com.spotifyxp.panels.ContentPanel; |
| 31 | +import com.spotifyxp.panels.PlayerArea; |
| 32 | +import com.spotifyxp.swingextension.JFrame; |
| 33 | +import com.spotifyxp.swingextension.JImagePanel; |
| 34 | +import com.spotifyxp.theming.themes.DarkGreen; |
| 35 | +import com.spotifyxp.utils.ApplicationUtils; |
| 36 | +import com.spotifyxp.utils.SpotifyUtils; |
| 37 | +import com.spotifyxp.utils.TrackUtils; |
| 38 | +import com.spotifyxp.utils.Utils; |
| 39 | + |
| 40 | +import javax.swing.*; |
| 41 | +import javax.swing.plaf.FontUIResource; |
| 42 | +import javax.swing.text.StyleContext; |
| 43 | +import java.awt.*; |
| 44 | +import java.awt.event.*; |
| 45 | +import java.io.File; |
| 46 | +import java.net.MalformedURLException; |
| 47 | +import java.net.URL; |
| 48 | +import java.util.Locale; |
| 49 | + |
| 50 | +public class FullscreenPlayerDialog { |
| 51 | + public JPanel contentPanel; |
| 52 | + public JImagePanel image; |
| 53 | + public JButton closeButton; |
| 54 | + public JPanel controlsPanel; |
| 55 | + public JLabel playerTitleDesc; |
| 56 | + public JFrame frame; |
| 57 | + |
| 58 | + private int x, y, width, height; |
| 59 | + |
| 60 | + public FullscreenPlayerDialog() { |
| 61 | + $$$setupUI$$$(); |
| 62 | + |
| 63 | + x = ContentPanel.playerArea.getX(); |
| 64 | + y = ContentPanel.playerArea.getY(); |
| 65 | + width = ContentPanel.playerArea.getWidth(); |
| 66 | + height = ContentPanel.playerArea.getHeight(); |
| 67 | + |
| 68 | + controlsPanel.setLayout(null); |
| 69 | + |
| 70 | + PublicValues.contentPanel.remove(ContentPanel.playerArea); |
| 71 | + |
| 72 | + int centerX = (ContentPanel.playerArea.getWidth() - PlayerArea.playerPlayTime.getX()) / 2; |
| 73 | + centerX = centerX * -1; |
| 74 | + |
| 75 | + ContentPanel.playerArea.setBounds(centerX + 40, 0, width, height); |
| 76 | + |
| 77 | + controlsPanel.setPreferredSize(new Dimension(width, height)); |
| 78 | + |
| 79 | + controlsPanel.add(ContentPanel.playerArea); |
| 80 | + PublicValues.contentPanel.revalidate(); |
| 81 | + PublicValues.contentPanel.repaint(); |
| 82 | + |
| 83 | + PlayerArea.playerImage.setVisible(false); |
| 84 | + PlayerArea.playerTitle.setVisible(false); |
| 85 | + PlayerArea.playerDescription.setVisible(false); |
| 86 | + |
| 87 | + closeButton.addActionListener(new ActionListener() { |
| 88 | + @Override |
| 89 | + public void actionPerformed(ActionEvent e) { |
| 90 | + frame.dispose(); |
| 91 | + for (WindowListener listener : frame.getWindowListeners()) { |
| 92 | + listener.windowClosing(null); |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + Events.subscribe(SpotifyXPEvents.trackNext.getName(), new EventSubscriber() { |
| 98 | + @Override |
| 99 | + public void run(Object... data) { |
| 100 | + if (data[0] instanceof Track) { |
| 101 | + playerTitleDesc.setText(((Track) data[0]).getName() + " - " + TrackUtils.getArtists(((Track) data[0]).getArtists())); |
| 102 | + try { |
| 103 | + image.setImage(new URL(SpotifyUtils.getImageForSystem(((Track) data[0]).getAlbum().getImages()).getUrl())); |
| 104 | + } catch (MalformedURLException e) { |
| 105 | + ConsoleLogging.Throwable(e); |
| 106 | + } |
| 107 | + } else if (data[0] instanceof Episode) { |
| 108 | + playerTitleDesc.setText(((Episode) data[0]).getShow().getName() + " - " + ((Episode) data[0]).getName()); |
| 109 | + try { |
| 110 | + image.setImage(new URL(SpotifyUtils.getImageForSystem(((Episode) data[0]).getImages()).getUrl())); |
| 111 | + } catch (MalformedURLException e) { |
| 112 | + ConsoleLogging.Throwable(e); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + if (InstanceManager.getSpotifyPlayer().currentPlayable() != null) { |
| 119 | + if (InstanceManager.getSpotifyPlayer().currentPlayable().toSpotifyUri().split(":")[1].equals("track")) { |
| 120 | + playerTitleDesc.setText(PlayerArea.playerTitle.getText() + " - " + PlayerArea.playerDescription.getText()); |
| 121 | + } else { |
| 122 | + playerTitleDesc.setText(PlayerArea.playerDescription + " - " + PlayerArea.playerTitle.getText()); |
| 123 | + } |
| 124 | + |
| 125 | + image.setImage(PlayerArea.playerImage.getImageStream()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public void open() { |
| 130 | + frame = new JFrame(ApplicationUtils.getName() + " - Fullscreen Player"); |
| 131 | + frame.setContentPane(contentPanel); |
| 132 | + frame.setUndecorated(true); |
| 133 | + frame.setAlwaysOnTop(true); |
| 134 | + frame.addWindowListener(new WindowAdapter() { |
| 135 | + @Override |
| 136 | + public void windowClosing(WindowEvent e) { |
| 137 | + controlsPanel.remove(ContentPanel.playerArea); |
| 138 | + ContentPanel.playerArea.setBounds(x, y, width, height); |
| 139 | + PublicValues.contentPanel.add(ContentPanel.playerArea); |
| 140 | + PublicValues.contentPanel.revalidate(); |
| 141 | + PublicValues.contentPanel.repaint(); |
| 142 | + |
| 143 | + PlayerArea.playerImage.setVisible(true); |
| 144 | + PlayerArea.playerTitle.setVisible(true); |
| 145 | + PlayerArea.playerDescription.setVisible(true); |
| 146 | + } |
| 147 | + }); |
| 148 | + frame.pack(); |
| 149 | + frame.setVisible(true); |
| 150 | + Utils.moveToScreen(frame, Utils.getDisplayNumber(ContentPanel.frame)); |
| 151 | + ContentPanel.frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(frame); |
| 152 | + } |
| 153 | + |
| 154 | + public void close() { |
| 155 | + frame.dispose(); |
| 156 | + } |
| 157 | + |
| 158 | + public static void main(String[] args) { |
| 159 | + PublicValues.theme = new DarkGreen(); |
| 160 | + PublicValues.theme.initTheme(); |
| 161 | + ContentPanel.frame.setBackground(Color.decode("#3c3f41")); |
| 162 | + FullscreenPlayerDialog dialog = new FullscreenPlayerDialog(); |
| 163 | + dialog.open(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Method generated by IntelliJ IDEA GUI Designer |
| 168 | + * >>> IMPORTANT!! <<< |
| 169 | + * DO NOT edit this method OR call it in your code! |
| 170 | + * |
| 171 | + * @noinspection ALL |
| 172 | + */ |
| 173 | + private void $$$setupUI$$$() { |
| 174 | + contentPanel = new JPanel(); |
| 175 | + contentPanel.setLayout(new BorderLayout(0, 0)); |
| 176 | + final JPanel panel1 = new JPanel(); |
| 177 | + panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); |
| 178 | + panel1.setMaximumSize(new Dimension(720, 2147483647)); |
| 179 | + contentPanel.add(panel1, BorderLayout.SOUTH); |
| 180 | + controlsPanel = new JPanel(); |
| 181 | + controlsPanel.setLayout(new BorderLayout(0, 0)); |
| 182 | + panel1.add(controlsPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); |
| 183 | + final JPanel panel2 = new JPanel(); |
| 184 | + panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1)); |
| 185 | + contentPanel.add(panel2, BorderLayout.NORTH); |
| 186 | + closeButton = new JButton(); |
| 187 | + closeButton.setText("X"); |
| 188 | + panel2.add(closeButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); |
| 189 | + final Spacer spacer1 = new Spacer(); |
| 190 | + panel2.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); |
| 191 | + final JPanel panel3 = new JPanel(); |
| 192 | + panel3.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1)); |
| 193 | + contentPanel.add(panel3, BorderLayout.CENTER); |
| 194 | + image = new JImagePanel(); |
| 195 | + panel3.add(image, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); |
| 196 | + playerTitleDesc = new JLabel(); |
| 197 | + Font playerTitleDescFont = this.$$$getFont$$$(null, -1, 20, playerTitleDesc.getFont()); |
| 198 | + if (playerTitleDescFont != null) playerTitleDesc.setFont(playerTitleDescFont); |
| 199 | + playerTitleDesc.setText("Label"); |
| 200 | + panel3.add(playerTitleDesc, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_SOUTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 5, false)); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @noinspection ALL |
| 205 | + */ |
| 206 | + private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) { |
| 207 | + if (currentFont == null) return null; |
| 208 | + String resultName; |
| 209 | + if (fontName == null) { |
| 210 | + resultName = currentFont.getName(); |
| 211 | + } else { |
| 212 | + Font testFont = new Font(fontName, Font.PLAIN, 10); |
| 213 | + if (testFont.canDisplay('a') && testFont.canDisplay('1')) { |
| 214 | + resultName = fontName; |
| 215 | + } else { |
| 216 | + resultName = currentFont.getName(); |
| 217 | + } |
| 218 | + } |
| 219 | + Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize()); |
| 220 | + boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac"); |
| 221 | + Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize()); |
| 222 | + return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * @noinspection ALL |
| 227 | + */ |
| 228 | + public JComponent $$$getRootComponent$$$() { |
| 229 | + return contentPanel; |
| 230 | + } |
| 231 | + |
| 232 | +} |
0 commit comments