Spaces:
Paused
Paused
Download floating_tutor.py from jobinv/MathematicsApps: direct link, hf CLI and curl.
- Browser
- Download file 11.6 kB
-
https://huggingface.co/spaces/jobinv/MathematicsApps/resolve/main/floating_tutor.py
- Command line
-
hf download hf://spaces/jobinv/MathematicsApps/floating_tutor.py
-
curl -L -o floating_tutor.py https://huggingface.co/spaces/jobinv/MathematicsApps/resolve/main/floating_tutor.py
11.6 kB
| """Fixed-position assets for three native Gradio mechanics-tutor dialogs.""" | |
| from __future__ import annotations | |
| FLOATING_TUTOR_CSS = r""" | |
| :where( | |
| #floating-tutor-launcher-top, | |
| #floating-tutor-launcher-middle, | |
| #floating-tutor-launcher | |
| ) { | |
| background: linear-gradient(110deg, #6d28d9, #db2777) !important; | |
| border: 1px solid rgba(255, 255, 255, .48) !important; | |
| border-radius: 999px !important; | |
| box-shadow: 0 14px 34px rgba(76, 29, 149, .36) !important; | |
| color: white !important; | |
| font-weight: 850 !important; | |
| left: auto !important; | |
| margin: 0 !important; | |
| min-height: 44px !important; | |
| min-width: 142px !important; | |
| opacity: 1 !important; | |
| pointer-events: auto !important; | |
| position: fixed !important; | |
| right: max(18px, env(safe-area-inset-right)) !important; | |
| visibility: visible !important; | |
| width: auto !important; | |
| z-index: 2147483000 !important; | |
| } | |
| :where( | |
| #floating-tutor-launcher-top, | |
| #floating-tutor-launcher-middle, | |
| #floating-tutor-launcher | |
| ) button { | |
| background: transparent !important; | |
| border: 0 !important; | |
| border-radius: inherit !important; | |
| color: inherit !important; | |
| font-weight: inherit !important; | |
| } | |
| :where( | |
| #floating-tutor-launcher-top, | |
| #floating-tutor-launcher-middle, | |
| #floating-tutor-launcher | |
| ):hover { | |
| box-shadow: 0 17px 40px rgba(76, 29, 149, .44) !important; | |
| } | |
| #floating-tutor-launcher-top { | |
| bottom: auto !important; | |
| top: max(18px, env(safe-area-inset-top)) !important; | |
| } | |
| #floating-tutor-launcher-middle { | |
| bottom: auto !important; | |
| top: 50% !important; | |
| transform: translateY(-50%) !important; | |
| } | |
| #floating-tutor-launcher { | |
| bottom: max(18px, env(safe-area-inset-bottom)) !important; | |
| top: auto !important; | |
| } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) { | |
| background: rgba(255, 255, 255, .99) !important; | |
| border: 1px solid rgba(109, 40, 217, .24) !important; | |
| border-radius: 22px !important; | |
| box-shadow: 0 24px 68px rgba(30, 41, 59, .32) !important; | |
| left: auto !important; | |
| margin: 0 !important; | |
| max-height: calc(100vh - 36px) !important; | |
| overflow-x: hidden !important; | |
| overflow-y: auto !important; | |
| padding: 14px !important; | |
| position: fixed !important; | |
| right: 178px !important; | |
| width: min(430px, calc(100vw - 210px)) !important; | |
| z-index: 2147482999 !important; | |
| } | |
| #floating-tutor-panel-top { | |
| bottom: auto !important; | |
| top: max(18px, env(safe-area-inset-top)) !important; | |
| } | |
| #floating-tutor-panel-middle { | |
| bottom: auto !important; | |
| top: 50% !important; | |
| transform: translateY(-50%) !important; | |
| } | |
| #floating-tutor-panel { | |
| bottom: max(18px, env(safe-area-inset-bottom)) !important; | |
| top: auto !important; | |
| } | |
| .floating-tutor-dialog-header { | |
| align-items: center; | |
| border-bottom: 1px solid #e2e8f0; | |
| margin-bottom: 7px; | |
| padding-bottom: 7px; | |
| } | |
| .floating-tutor-dialog-title h3 { | |
| color: #172554; | |
| font-size: 17px; | |
| margin: 0 0 2px; | |
| } | |
| .floating-tutor-dialog-title p { | |
| color: #7c3aed; | |
| font-size: 10px; | |
| font-weight: 850; | |
| letter-spacing: .06em; | |
| margin: 0; | |
| } | |
| .floating-tutor-close { | |
| border-radius: 50% !important; | |
| flex: 0 0 36px !important; | |
| font-size: 18px !important; | |
| height: 36px !important; | |
| min-width: 36px !important; | |
| padding: 0 !important; | |
| width: 36px !important; | |
| } | |
| .floating-open-full-tutor { | |
| color: #6d28d9 !important; | |
| font-size: 12px !important; | |
| margin-bottom: 5px !important; | |
| } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) .floating-tutor-context { | |
| background: linear-gradient(100deg, #eef2ff, #fdf2f8); | |
| border-radius: 12px; | |
| font-size: 11px; | |
| padding: 7px 10px; | |
| } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) .floating-tutor-context h3 { font-size: 13px; margin: 0 0 3px; } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) .floating-tutor-context p { margin: 0; } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) .floating-tutor-chat { min-height: 210px; } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) .floating-tutor-composer { | |
| background: rgba(255, 255, 255, .99); | |
| border: 1px solid rgba(109, 40, 217, .15); | |
| border-radius: 14px; | |
| bottom: 0; | |
| padding: 9px !important; | |
| position: sticky; | |
| z-index: 2; | |
| } | |
| .floating-tutor-composer-note { font-size: 11px; margin: 0; } | |
| .floating-tutor-status { font-size: 10px; margin: 0; } | |
| body.exam-mode-active :where( | |
| #floating-tutor-launcher-top, | |
| #floating-tutor-launcher-middle, | |
| #floating-tutor-launcher | |
| ) { | |
| display: none !important; | |
| } | |
| @media (max-width: 640px) { | |
| :where( | |
| #floating-tutor-launcher-top, | |
| #floating-tutor-launcher-middle, | |
| #floating-tutor-launcher | |
| ) { | |
| min-height: 40px !important; | |
| min-width: 118px !important; | |
| right: max(8px, env(safe-area-inset-right)) !important; | |
| } | |
| :where( | |
| #floating-tutor-panel-top, | |
| #floating-tutor-panel-middle, | |
| #floating-tutor-panel | |
| ) { | |
| max-height: calc(100vh - 20px) !important; | |
| right: max(8px, env(safe-area-inset-right)) !important; | |
| width: calc(100vw - 16px) !important; | |
| } | |
| } | |
| @media (prefers-reduced-motion: reduce) { | |
| :where( | |
| #floating-tutor-launcher-top, | |
| #floating-tutor-launcher-middle, | |
| #floating-tutor-launcher | |
| ) { | |
| transition: none !important; | |
| } | |
| } | |
| """ | |
| # Every visible launcher below is the real native Gradio component. JavaScript | |
| # only corrects fixed positioning under transformed or scrolling ancestors; it | |
| # never clones, clicks, hides, reparents, or replaces a Gradio control. | |
| FLOATING_TUTOR_VIEWPORT_JS = r""" | |
| () => { | |
| const SLOTS = [ | |
| {name: "top", launcherId: "floating-tutor-launcher-top", panelId: "floating-tutor-panel-top"}, | |
| {name: "middle", launcherId: "floating-tutor-launcher-middle", panelId: "floating-tutor-panel-middle"}, | |
| {name: "bottom", launcherId: "floating-tutor-launcher", panelId: "floating-tutor-panel"}, | |
| ]; | |
| const viewportBounds = () => { | |
| const viewport = window.visualViewport; | |
| const left = viewport ? viewport.offsetLeft : 0; | |
| const top = viewport ? viewport.offsetTop : 0; | |
| const width = viewport ? viewport.width : document.documentElement.clientWidth; | |
| const height = viewport ? viewport.height : document.documentElement.clientHeight; | |
| const inset = window.matchMedia("(max-width: 640px)").matches ? 8 : 18; | |
| return {left, top, width, height, right: left + width, bottom: top + height, inset}; | |
| }; | |
| const clamp = (value, minimum, maximum) => | |
| Math.min(Math.max(value, minimum), Math.max(minimum, maximum)); | |
| const setFixedPosition = (element, desiredLeft, desiredTop, zIndex) => { | |
| if (!element) return; | |
| element.style.setProperty("position", "fixed", "important"); | |
| element.style.setProperty("left", `${desiredLeft}px`, "important"); | |
| element.style.setProperty("top", `${desiredTop}px`, "important"); | |
| element.style.setProperty("right", "auto", "important"); | |
| element.style.setProperty("bottom", "auto", "important"); | |
| element.style.setProperty("transform", "none", "important"); | |
| element.style.setProperty("z-index", String(zIndex), "important"); | |
| // Correct coordinates when a transformed Gradio ancestor establishes a | |
| // containing block for fixed descendants. The component remains native. | |
| const actual = element.getBoundingClientRect(); | |
| if (Math.abs(actual.left - desiredLeft) > 0.5) { | |
| element.style.setProperty( | |
| "left", | |
| `${desiredLeft + desiredLeft - actual.left}px`, | |
| "important", | |
| ); | |
| } | |
| if (Math.abs(actual.top - desiredTop) > 0.5) { | |
| element.style.setProperty( | |
| "top", | |
| `${desiredTop + desiredTop - actual.top}px`, | |
| "important", | |
| ); | |
| } | |
| }; | |
| const slotTop = (name, height, bounds) => { | |
| const safeTop = bounds.top + bounds.inset; | |
| const safeBottom = bounds.bottom - bounds.inset; | |
| if (name === "top") return safeTop; | |
| if (name === "middle") return safeTop + Math.max(0, (safeBottom - safeTop - height) / 2); | |
| return safeBottom - height; | |
| }; | |
| const positionSlot = (slot) => { | |
| const launcher = document.getElementById(slot.launcherId); | |
| if (!launcher) return; | |
| const bounds = viewportBounds(); | |
| const launcherRect = launcher.getBoundingClientRect(); | |
| const launcherWidth = Math.max(118, launcherRect.width || 142); | |
| const launcherHeight = Math.max(40, launcherRect.height || 44); | |
| const launcherLeft = bounds.right - bounds.inset - launcherWidth; | |
| const launcherTop = slotTop(slot.name, launcherHeight, bounds); | |
| setFixedPosition(launcher, launcherLeft, launcherTop, 2147483000); | |
| launcher.style.setProperty("opacity", "1", "important"); | |
| launcher.style.setProperty("visibility", "visible", "important"); | |
| launcher.style.setProperty("pointer-events", "auto", "important"); | |
| const panel = document.getElementById(slot.panelId); | |
| if (!panel) return; | |
| const availableWidth = Math.max(260, bounds.width - bounds.inset * 2); | |
| const desktopWidth = Math.min(430, Math.max(260, availableWidth - launcherWidth - 14)); | |
| const mobile = window.matchMedia("(max-width: 640px)").matches; | |
| const panelWidth = mobile ? availableWidth : desktopWidth; | |
| const maximumHeight = Math.max(180, bounds.height - bounds.inset * 2); | |
| panel.style.setProperty("width", `${panelWidth}px`, "important"); | |
| panel.style.setProperty("max-height", `${maximumHeight}px`, "important"); | |
| const panelRect = panel.getBoundingClientRect(); | |
| const panelHeight = Math.min(maximumHeight, Math.max(260, panelRect.height || 560)); | |
| const safeLeft = bounds.left + bounds.inset; | |
| const panelLeft = mobile | |
| ? safeLeft | |
| : clamp(launcherLeft - 12 - panelWidth, safeLeft, bounds.right - bounds.inset - panelWidth); | |
| const panelTop = slotTop(slot.name, panelHeight, bounds); | |
| setFixedPosition(panel, panelLeft, panelTop, 2147482999); | |
| const button = launcher.matches("button") ? launcher : launcher.querySelector("button"); | |
| if (button && button.dataset.threeTutorPositionHook !== "true") { | |
| button.dataset.threeTutorPositionHook = "true"; | |
| button.addEventListener("click", () => { | |
| window.requestAnimationFrame(positionAll); | |
| window.setTimeout(positionAll, 120); | |
| window.setTimeout(positionAll, 360); | |
| }); | |
| } | |
| }; | |
| const positionAll = () => SLOTS.forEach(positionSlot); | |
| if (!window.__m1ThreeTutorLaunchersInstalled) { | |
| window.__m1ThreeTutorLaunchersInstalled = true; | |
| let frame = 0; | |
| const schedulePosition = () => { | |
| window.cancelAnimationFrame(frame); | |
| frame = window.requestAnimationFrame(positionAll); | |
| }; | |
| window.addEventListener("scroll", schedulePosition, {passive: true, capture: true}); | |
| document.addEventListener("scroll", schedulePosition, {passive: true, capture: true}); | |
| window.addEventListener("resize", schedulePosition, {passive: true}); | |
| window.addEventListener("orientationchange", schedulePosition, {passive: true}); | |
| if (window.visualViewport) { | |
| window.visualViewport.addEventListener("scroll", schedulePosition, {passive: true}); | |
| window.visualViewport.addEventListener("resize", schedulePosition, {passive: true}); | |
| } | |
| new MutationObserver(schedulePosition).observe(document.body, { | |
| childList: true, | |
| subtree: true, | |
| }); | |
| if (window.ResizeObserver) { | |
| const observer = new ResizeObserver(schedulePosition); | |
| observer.observe(document.documentElement); | |
| observer.observe(document.body); | |
| window.__m1ThreeTutorResizeObserver = observer; | |
| } | |
| } | |
| positionAll(); | |
| return []; | |
| } | |
| """ | |