|
| 1 | +import { createRoot } from "react-dom/client"; |
| 2 | +import { getCurrentThemeCSS } from "../../ui/theming"; |
| 3 | + |
| 4 | +let pipWindow: Window | null = null; |
| 5 | + |
| 6 | +export function copyAllStyles(pipWindow: Window) { |
| 7 | + document.querySelectorAll('link[rel="stylesheet"]').forEach((link) => { |
| 8 | + const newLink = pipWindow.document.createElement("link"); |
| 9 | + newLink.rel = "stylesheet"; |
| 10 | + newLink.href = (link as HTMLLinkElement).href; |
| 11 | + newLink.type = "text/css"; |
| 12 | + pipWindow.document.head.appendChild(newLink); |
| 13 | + }); |
| 14 | + |
| 15 | + document.querySelectorAll("style").forEach((style) => { |
| 16 | + const newStyle = pipWindow.document.createElement("style"); |
| 17 | + newStyle.textContent = style.textContent; |
| 18 | + |
| 19 | + Array.from(style.attributes).forEach((attr) => { |
| 20 | + newStyle.setAttribute(attr.name, attr.value); |
| 21 | + }); |
| 22 | + |
| 23 | + pipWindow.document.head.appendChild(newStyle); |
| 24 | + }); |
| 25 | + |
| 26 | + let copiedRulesCount = 0; |
| 27 | + try { |
| 28 | + [...document.styleSheets].forEach((styleSheet) => { |
| 29 | + try { |
| 30 | + const cssRules = [...styleSheet.cssRules] |
| 31 | + .map((rule) => rule.cssText) |
| 32 | + .join(""); |
| 33 | + if (cssRules) { |
| 34 | + const style = pipWindow.document.createElement("style"); |
| 35 | + style.textContent = cssRules; |
| 36 | + pipWindow.document.head.appendChild(style); |
| 37 | + copiedRulesCount += styleSheet.cssRules.length; |
| 38 | + } |
| 39 | + } catch (e) { |
| 40 | + console.warn("Could not access stylesheet:", styleSheet.href, e); |
| 41 | + } |
| 42 | + }); |
| 43 | + } catch (e) { |
| 44 | + console.warn("Could not copy some stylesheets:", e); |
| 45 | + } |
| 46 | + |
| 47 | + console.log(`Copied ${copiedRulesCount} CSS rules to PiP window`); |
| 48 | +} |
| 49 | + |
| 50 | +export const isMiniplayerSupported = (): boolean => { |
| 51 | + return ( |
| 52 | + "documentPictureInPicture" in window && !!window.documentPictureInPicture |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +export interface MiniplayerControls { |
| 57 | + togglePlayPause: () => void; |
| 58 | + next: () => void; |
| 59 | + previous: () => void; |
| 60 | + playerState: { |
| 61 | + currentSong: any; |
| 62 | + isPlaying: boolean; |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +declare global { |
| 67 | + interface Window { |
| 68 | + documentPictureInPicture?: { |
| 69 | + requestWindow: (options: { |
| 70 | + width: number; |
| 71 | + height: number; |
| 72 | + }) => Promise<Window & { close: () => void }>; |
| 73 | + }; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +export async function toggleMiniplayer( |
| 78 | + controls: MiniplayerControls, |
| 79 | + MiniplayerContent: React.ComponentType<{ controls: MiniplayerControls }>, |
| 80 | +) { |
| 81 | + if (!controls.playerState.currentSong) { |
| 82 | + console.error("No song is currently playing"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + if (pipWindow) { |
| 88 | + pipWindow.close(); |
| 89 | + pipWindow = null; |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if ( |
| 94 | + !("documentPictureInPicture" in window) || |
| 95 | + !window.documentPictureInPicture |
| 96 | + ) { |
| 97 | + console.error("Document Picture-in-Picture not supported"); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const newPipWindow = await window.documentPictureInPicture.requestWindow({ |
| 102 | + width: 400, |
| 103 | + height: 70, |
| 104 | + }); |
| 105 | + pipWindow = newPipWindow; |
| 106 | + |
| 107 | + if (document.documentElement.classList.contains("dark")) { |
| 108 | + newPipWindow.document.documentElement.classList.add("dark"); |
| 109 | + } |
| 110 | + |
| 111 | + copyAllStyles(newPipWindow); |
| 112 | + |
| 113 | + await new Promise((resolve) => setTimeout(resolve, 300)); |
| 114 | + |
| 115 | + const themeCSS = getCurrentThemeCSS(); |
| 116 | + if (themeCSS && themeCSS.trim() !== ":root {\n \n}") { |
| 117 | + const themeStyle = newPipWindow.document.createElement("style"); |
| 118 | + themeStyle.textContent = themeCSS; |
| 119 | + themeStyle.setAttribute("data-theme-variables", "true"); |
| 120 | + newPipWindow.document.head.appendChild(themeStyle); |
| 121 | + } else { |
| 122 | + applyFallbackThemeVariables(newPipWindow); |
| 123 | + } |
| 124 | + |
| 125 | + newPipWindow.document.body.style.margin = "0"; |
| 126 | + newPipWindow.document.body.style.padding = "0"; |
| 127 | + newPipWindow.document.body.style.overflow = "hidden"; |
| 128 | + |
| 129 | + const rootElement = createRoot(newPipWindow.document.body); |
| 130 | + rootElement.render(<MiniplayerContent controls={controls} />); |
| 131 | + |
| 132 | + newPipWindow.addEventListener("pagehide", () => { |
| 133 | + rootElement.unmount(); |
| 134 | + pipThemeChannel.close(); |
| 135 | + pipWindow = null; |
| 136 | + }); |
| 137 | + |
| 138 | + const pipThemeChannel = new BroadcastChannel("theme-updates"); |
| 139 | + |
| 140 | + pipThemeChannel.onmessage = (event) => { |
| 141 | + if (event.data.type === "theme-css") { |
| 142 | + if (event.data.darkMode) { |
| 143 | + newPipWindow.document.documentElement.classList.add("dark"); |
| 144 | + } else { |
| 145 | + newPipWindow.document.documentElement.classList.remove("dark"); |
| 146 | + } |
| 147 | + |
| 148 | + const existingThemeStyles = newPipWindow.document.querySelectorAll( |
| 149 | + 'style[data-theme-variables], style[data-fallback-theme-variables]', |
| 150 | + ); |
| 151 | + existingThemeStyles.forEach((style) => style.remove()); |
| 152 | + |
| 153 | + const styleElement = newPipWindow.document.createElement("style"); |
| 154 | + styleElement.textContent = event.data.css; |
| 155 | + styleElement.setAttribute("data-theme-variables", "true"); |
| 156 | + if (event.data.fallback) { |
| 157 | + styleElement.setAttribute("data-fallback-theme-variables", "true"); |
| 158 | + } |
| 159 | + newPipWindow.document.head.appendChild(styleElement); |
| 160 | + newPipWindow.document.body.offsetHeight; |
| 161 | + } |
| 162 | + }; |
| 163 | + } catch (err) { |
| 164 | + console.error("PiP failed:", err); |
| 165 | + pipWindow = null; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +function applyFallbackThemeVariables(pipWindow: Window) { |
| 170 | + const rootStyle = getComputedStyle(document.documentElement); |
| 171 | + const fallbackVariables: string[] = []; |
| 172 | + const themeVars = [ |
| 173 | + "--themecolor", "--themecolor2", "--themecolor3", "--themecolor4", |
| 174 | + "--themegradient", "--themecolor-transparent", "--themecolor2-transparent", |
| 175 | + "--themecolor3-transparent", "--foreground", "--foreground-strong", |
| 176 | + "--foreground-stronger", "--foreground-muted", "--foreground-subtle", |
| 177 | + "--background", "--surface", "--surface-foreground", |
| 178 | + "--surface-transparent-05", "--surface-transparent-1", "--surface-transparent-2", |
| 179 | + "--primary", "--primary-foreground", "--primary-transparent", |
| 180 | + "--primary-border", "--primary-border-strong", "--secondary", |
| 181 | + "--secondary-foreground", "--menu-background", "--spacing-1", |
| 182 | + "--spacing-2", "--spacing-3", "--spacing-4", "--radius", "--radius-lg", |
| 183 | + ]; |
| 184 | + |
| 185 | + themeVars.forEach((varName) => { |
| 186 | + const value = rootStyle.getPropertyValue(varName).trim(); |
| 187 | + if (value) { |
| 188 | + fallbackVariables.push(`${varName}: ${value};`); |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + if (fallbackVariables.length > 0) { |
| 193 | + const fallbackStyle = pipWindow.document.createElement("style"); |
| 194 | + fallbackStyle.textContent = `:root {\n ${fallbackVariables.join("\n ")}\n}`; |
| 195 | + fallbackStyle.setAttribute("data-fallback-theme-variables", "true"); |
| 196 | + pipWindow.document.head.appendChild(fallbackStyle); |
| 197 | + } |
| 198 | +} |
0 commit comments