| 1 | // --reasonix-draggable marks OS drag regions. Electron rewrites chrome |
| 2 | // drag/no-drag to -webkit-app-region. Transcript content must not become |
| 3 | // native no-drag or Chromium punches a hole in the titlebar (#10105). |
| 4 | |
| 5 | const DRAG_VALUE = /--reasonix-draggable(\s*:\s*)(drag|no-drag)/g; |
| 6 | const APP_REGION_DECL = /\s*-webkit-app-region\s*:\s*(?:no-)?drag;?/g; |
| 7 | const CHROME_NO_DRAG_SELECTOR = |
| 8 | /(^|[,{\s])\.(?:topicbar(?:__|--)[\w-]*|topbar(?:__|--)[\w-]*|topicbar|topbar|sidebar[\w-]*|windows-window-control[\w-]*|management-screen[\w-]*|heartbeat[\w-]*|app-chrome[\w-]*|tabbar[\w-]*|chip|modal-close-button|[\w-]*backdrop|theme-gallery__editor-overlay)\b/i; |
| 9 | const CONTENT_APP_REGION_SELECTOR = |
| 10 | /(^|[,{\s])\.(?:msg|transcript|reasoning|tool|process-card|composer|compaction|turn-collapse)(?:__|-|[\s,{.#:]|$)/i; |
| 11 | |
| 12 | export function shellFromEnv(env = process.env) { |
| 13 | const shell = (env.REASONIX_SHELL ?? "").trim().toLowerCase(); |
| 14 | if (shell === "") return "browser"; |
| 15 | if (shell === "electron") return "electron"; |
| 16 | throw new Error(`REASONIX_SHELL must be "electron" or unset, got ${JSON.stringify(shell)}`); |
| 17 | } |
| 18 | |
| 19 | function enclosingSelector(css, offset) { |
| 20 | const open = css.lastIndexOf("{", offset); |
| 21 | if (open < 0) return ""; |
| 22 | const close = css.lastIndexOf("}", open); |
| 23 | return css.slice(close + 1, open); |
| 24 | } |
| 25 | |
| 26 | function rewriteNoDrag(css) { |
| 27 | return css.replace(DRAG_VALUE, (match, sep, value, offset) => { |
| 28 | if (value === "drag") return `-webkit-app-region${sep}drag`; |
| 29 | const selector = enclosingSelector(css, offset); |
| 30 | if (CONTENT_APP_REGION_SELECTOR.test(selector)) return match; |
| 31 | if (CHROME_NO_DRAG_SELECTOR.test(selector)) return `-webkit-app-region${sep}no-drag`; |
| 32 | return match; |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | function stripContentAppRegion(css) { |
| 37 | return css.replace(/([^{}]+)\{([^{}]*)\}/g, (rule, selector, body) => { |
| 38 | if (!CONTENT_APP_REGION_SELECTOR.test(selector) || !body.includes("-webkit-app-region")) return rule; |
| 39 | return `${selector}{${body.replace(APP_REGION_DECL, "")}}`; |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | export function rewriteDragRegions(css, shell) { |
| 44 | if (shell !== "electron") return css; |
| 45 | return stripContentAppRegion(rewriteNoDrag(css)); |
| 46 | } |
| 47 |