From ec78759e89886ffe20ac5eced88cab4c831263e5 Mon Sep 17 00:00:00 2001 From: Jiale Li Date: Sun, 15 Nov 2026 11:56:23 -0500 Subject: [PATCH 1/2] Fly the brand paper plane along practice and loading trails --- assets/academic_ui.css | 33 ---------- assets/paper_practice.css | 16 ++++- assets/paper_shell.css | 115 +++++++++++++++-------------------- components/flight_path.py | 93 ++++++++++++++++++++++++++++ pages/2_Practice_Center.py | 12 ++-- tests/test_paper_practice.py | 44 ++++++++++++++ tests/test_paper_shell.py | 35 +++++------ 7 files changed, 220 insertions(+), 128 deletions(-) create mode 100644 components/flight_path.py diff --git a/assets/academic_ui.css b/assets/academic_ui.css index 545c6b0..6b7b0db 100644 --- a/assets/academic_ui.css +++ b/assets/academic_ui.css @@ -400,36 +400,3 @@ div.st-key-create_course_button button > div > span { justify-content: flex-star padding: .75rem 0; } } - -.flight-waypoints { - display: flex; - position: relative; - justify-content: space-between; - align-items: center; - min-height: 2.35rem; - max-width: 38rem; - isolation: isolate; -} -.flight-waypoints::before { - content: ""; - position: absolute; - z-index: -1; - inset: 50% .4rem auto; - border-top: 1px dashed #aaa284; -} -.flight-waypoint { - display: grid; - place-items: center; - width: .56rem; - height: .56rem; - border: 1px solid #a49a7e; - border-radius: 50%; - background: var(--flight-paper); -} -.flight-waypoint.is-current { - width: 2.25rem; - height: 2.25rem; - color: #985831; - border-color: #d7ad8e; - font-size: 1.25rem; -} diff --git a/assets/paper_practice.css b/assets/paper_practice.css index fd4e12d..b691a24 100644 --- a/assets/paper_practice.css +++ b/assets/paper_practice.css @@ -72,10 +72,24 @@ min-width: 0; } /* Markdown pulls the next block up by 1rem; these hold no paragraph, so keep them centred. */ -.st-key-flight_practice_progress [data-testid="stMarkdownContainer"]:has(> :is(.flight-clock, .flight-waypoints)) { +.st-key-flight_practice_progress [data-testid="stMarkdownContainer"]:has(> :is(.flight-clock, .flight-route)) { margin-bottom: 0; } +.flight-route { + display: block; + width: min(100%, 38rem); + height: auto; + margin-inline: auto; +} + +@media (max-width: 560px) { + .st-key-flight_practice_progress > [data-testid="stLayoutWrapper"]:has(> .st-key-flight_progress_track) { + order: 1; + flex: 1 0 100%; + } +} + /* Session clock: a small paper box. The text is the server value; where the browser can, CSS counts on from it every second while the clock runs. */ .flight-clock { diff --git a/assets/paper_shell.css b/assets/paper_shell.css index 04a4570..5771a50 100644 --- a/assets/paper_shell.css +++ b/assets/paper_shell.css @@ -738,93 +738,74 @@ label[data-baseweb="radio"]:focus-within { border-color: #D9CFC2 !important; } -/* ---- Loading: a paper plane instead of a spinner --------------------------- */ -/* The brand plane (assets/brand-plane.svg), inlined so the sheet stays one file. */ +/* ---- Loading: the paper plane over the center of the page ----------------- */ :root { + /* The brand plane (assets/brand-plane.svg), inlined for the shared loader. */ --paper-plane: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 480 348'%3E%3Cpolygon points='28,158 436,34 140,212' fill='%23F4B45B'/%3E%3Cpolygon points='436,34 238,302 150,224' fill='%235F849F'/%3E%3Cpolygon points='126,230 170,256 130,296' fill='%235F849F'/%3E%3C/svg%3E"); + --paper-loading-trail: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='192' height='56' viewBox='0 0 192 56'%3E%3Cpath d='M 8 32 C 34 0 60 6 93 27 S 151 56 184 22' fill='none' stroke='%238F8266' stroke-width='1.4' stroke-linecap='round' stroke-dasharray='2 5'/%3E%3C/svg%3E"); } -/* Out and back along a short track, turning round at each end. */ -@keyframes paper-plane-patrol { - 0% { transform: translate(0, 0) scaleX(1); } - 22% { transform: translate(1.4rem, -.25rem) scaleX(1); } - 45% { transform: translate(2.8rem, 0) scaleX(1); } - 50% { transform: translate(2.8rem, 0) scaleX(-1); } - 72% { transform: translate(1.4rem, -.25rem) scaleX(-1); } - 95% { transform: translate(0, 0) scaleX(-1); } - 100% { transform: translate(0, 0) scaleX(1); } -} - -/* The same trip across the whole window. */ -@keyframes paper-plane-crossing { - 0% { transform: translate(8vw, 0) scaleX(1); } - 24% { transform: translate(46vw, -.25rem) scaleX(1); } - 47% { transform: translate(calc(92vw - 1.6rem), 0) scaleX(1); } - 50% { transform: translate(calc(92vw - 1.6rem), 0) scaleX(-1); } - 74% { transform: translate(46vw, -.25rem) scaleX(-1); } - 97% { transform: translate(8vw, 0) scaleX(-1); } - 100% { transform: translate(8vw, 0) scaleX(1); } -} - -/* st.spinner: the round spinner gives way to the plane on its track. The - icon's holder is widened to the track, so the message starts after it. */ -[data-testid="stSpinner"] span:has(> [data-testid="stSpinnerIcon"]) { - flex-shrink: 0; - justify-content: flex-start; - width: 4.9rem; - height: 1.5rem; +[data-testid="stMain"] { + anchor-name: --paper-loading-main; } -[data-testid="stSpinner"] [data-testid="stSpinnerIcon"] { - position: relative; - flex-shrink: 0; - width: 4.9rem; - height: 1.5rem; - border: 0; - border-radius: 0; - animation: none; +/* Keep the spinner messages and run status available while replacing their icons. */ +[data-testid="stSpinnerIcon"], +[data-testid="stStatusWidgetRunningIcon"] { + color: transparent; + opacity: 0; } -[data-testid="stSpinner"] [data-testid="stSpinnerIcon"]::after { +[data-testid="stApp"]:has(:is([data-testid="stSpinner"], [data-testid="stStatusWidgetRunningIcon"])) [data-testid="stAppViewContainer"]::after { content: ""; - position: absolute; - top: 0; - left: 0; - width: 2.1rem; - height: 1.5rem; + position: fixed; + inset: 0; + /* Modern browsers center the effect in the learning area with the sidebar open. */ + left: anchor(--paper-loading-main left, 0px); + right: anchor(--paper-loading-main right, 0px); + z-index: 100; + pointer-events: none; + background: rgba(251, 246, 237, .52) var(--paper-loading-trail) center / 192px 56px no-repeat; + backdrop-filter: blur(3px); + animation: paper-loading-veil .18s ease-out .16s both; +} + +[data-testid="stApp"]:has(:is([data-testid="stSpinner"], [data-testid="stStatusWidgetRunningIcon"]))::after { + content: ""; + position: fixed; + top: calc(50% - 28px); + left: calc(50% - 96px); + left: calc(anchor(--paper-loading-main center, 50%) - 96px); + z-index: 101; + pointer-events: none; + width: 38px; + height: 28px; background: var(--paper-plane) center / contain no-repeat; - animation: paper-plane-patrol 2s ease-in-out infinite; + /* Same coordinates as the visible dashed trail. */ + offset-path: path("M 8 32 C 34 0 60 6 93 27 S 151 56 184 22"); + offset-rotate: auto 24deg; + animation: paper-loading-flight 2.7s ease-in-out .16s infinite both; } -/* While the app is working, the plane crosses the window just under the - header controls, and Streamlit's own running figure (which would sit - behind the avatar) is hidden. Streamlit shows that state only after half - a second, so quick updates do not flash it. A spinner on the page already - has its own plane, so this one stays away then. */ -[data-testid="stStatusWidgetRunningIcon"] { - display: none; +@keyframes paper-loading-veil { + from { opacity: 0; } + to { opacity: 1; } } -[data-testid="stApp"]:has([data-testid="stStatusWidgetRunningIcon"]):not(:has([data-testid="stSpinner"])) [data-testid="stHeader"]::after { - content: ""; - position: absolute; - top: 3.2rem; - left: 0; - width: 1.6rem; - height: 1.15rem; - background: var(--paper-plane) center / contain no-repeat; - pointer-events: none; - animation: paper-plane-crossing 3.6s ease-in-out infinite; +@keyframes paper-loading-flight { + 0% { offset-distance: 0%; opacity: 0; } + 12%, 85% { opacity: 1; } + 100% { offset-distance: 100%; opacity: 0; } } -/* Reduced motion: the plane is shown, parked. */ +/* Keep a single, still paper plane on the path with reduced motion enabled. */ @media (prefers-reduced-motion: reduce) { - [data-testid="stSpinner"] [data-testid="stSpinnerIcon"]::after { + [data-testid="stApp"]:has(:is([data-testid="stSpinner"], [data-testid="stStatusWidgetRunningIcon"])) [data-testid="stAppViewContainer"]::after { animation: none; } - [data-testid="stApp"]:has([data-testid="stStatusWidgetRunningIcon"]):not(:has([data-testid="stSpinner"])) [data-testid="stHeader"]::after { - left: calc(50vw - .8rem); + [data-testid="stApp"]:has(:is([data-testid="stSpinner"], [data-testid="stStatusWidgetRunningIcon"]))::after { + offset-distance: 50%; animation: none; } } diff --git a/components/flight_path.py b/components/flight_path.py new file mode 100644 index 0000000..00f073e --- /dev/null +++ b/components/flight_path.py @@ -0,0 +1,93 @@ +"""Responsive paper-plane route for the practice worksheet.""" + +from base64 import b64encode +from functools import lru_cache +from math import cos, pi, sin + +from services.ui_style import BRAND_PLANE + + +@lru_cache(maxsize=1) +def _plane() -> str: + return BRAND_PLANE.read_text(encoding="utf-8").replace( + " tuple[float, float]: + x = 24 + 372 * fraction + y = 29 + 11 * sin(4 * pi * fraction - 0.28) + 3 * sin(2 * pi * fraction) + return x, y + + +def _slope(fraction: float) -> float: + """Vertical change per unit of route fraction, for matching curve tangents.""" + return 44 * pi * cos(4 * pi * fraction - 0.28) + 6 * pi * cos(2 * pi * fraction) + + +def _coordinates(point: tuple[float, float]) -> str: + return f"{point[0]:.2f} {point[1]:.2f}" + + +def _trail(stops: list[float]) -> str: + path = f"M {_coordinates(_point(stops[0]))}" + for start, end in zip(stops, stops[1:]): + x0, y0 = _point(start) + x1, y1 = _point(end) + handle = (end - start) / 3 + path += ( + f" C {_coordinates((x0 + 372 * handle, y0 + _slope(start) * handle))}" + f" {_coordinates((x1 - 372 * handle, y1 - _slope(end) * handle))}" + f" {_coordinates((x1, y1))}" + ) + return path + + +def practice_route(count: int, index: int, previous: int | None = None) -> str: + """Produce an SVG image that can animate from either neighboring question.""" + stops = [step / (count - 1) for step in range(count)] if count > 1 else [0.5] + # A short exercise still follows the same curves as a long one. Keep each + # question on a spline vertex by including its fraction in the full route. + bends = sorted(set(stops) | {step / 8 for step in range(9)}) + completed = [stop for stop in bends if stop <= stops[index]] + x, y = _point(stops[index]) + dots = [] + for step, fraction in enumerate(stops): + cx, cy = _point(fraction) + radius = 5 if step == index else (2.7 if count <= 16 else 1.45) + fill = "#FAF4EA" if step >= index else "#AD673C" + stroke = "#9B582F" if step == index else "#8F8266" + dots.append( + f'' + ) + paths = "" + if count > 1: + paths = ( + f'' + f'' + ) + + plane = _plane() + parked = f'{plane}' + motion = "" + if previous is not None and 0 <= previous < count and previous != index: + start, end = stops[previous], stops[index] + lower, upper = sorted((start, end)) + trip = [stop for stop in bends if lower <= stop <= upper] + if start > end: + trip.reverse() + motion = ( + '' + f'' + f'{plane}' + '' + ) + + svg = ( + '' + f'{paths}{"".join(dots)}{parked}{motion}' + ) + source = b64encode(svg.encode("utf-8")).decode("ascii") + return f'' diff --git a/pages/2_Practice_Center.py b/pages/2_Practice_Center.py index c114677..2407238 100644 --- a/pages/2_Practice_Center.py +++ b/pages/2_Practice_Center.py @@ -22,6 +22,7 @@ ) from services.auth_session import current_user from services import learning_radar +from components.flight_path import practice_route from components.knowledge_graph import load_course_radar, render_topic_radar from components.page_links import open_page_button from services.learning_flow import install_practice, remember_widget, hydrate_widgets, open_lab @@ -226,6 +227,7 @@ def move_question(practice_key, session_id, step): target = max(0, min(len(questions) - 1, current + step)) if target == current: return + active["worksheet_previous_index"] = current active["worksheet_index"] = target # Read once by the next render: it names the page-turn (see paper_practice.css). active["worksheet_move"] = "next" if step > 0 else "prev" @@ -448,15 +450,9 @@ def render_question_screen(course, active, topic_names): with st.container(key="flight_progress_count", width="content"): st.caption(tr("Question") + f" {index + 1:02} / {len(questions):02}") with st.container(key="flight_progress_track"): - stops = min(len(questions), 12) - current_stop = index * (stops - 1) // max(1, len(questions) - 1) - markers = "".join( - '✈' if step == current_stop - else '' - for step in range(stops) - ) + previous = active.pop("worksheet_previous_index", None) st.markdown( - f'', + practice_route(len(questions), index, previous), unsafe_allow_html=True, ) with st.container( diff --git a/tests/test_paper_practice.py b/tests/test_paper_practice.py index fb363ef..6763745 100644 --- a/tests/test_paper_practice.py +++ b/tests/test_paper_practice.py @@ -1,15 +1,18 @@ """Paper practice page: question card, one-click choices, Learning Radar and clean wording.""" import ast +from base64 import b64decode import re import sys from pathlib import Path from types import ModuleType +from xml.etree import ElementTree import pytest import streamlit as st from streamlit.testing.v1 import AppTest import components +from components.flight_path import practice_route from components.knowledge_graph import radar_count_text from models.schemas import ExamSession, Question from services import practice_insights_ui @@ -110,6 +113,47 @@ def test_badge_is_translated_with_the_interface(): assert badges(app) == ["第 1 题"] +def route_svg(markup): + source = re.search(r"data:image/svg\+xml;base64,([A-Za-z0-9+/=]+)", markup) + assert source + return ElementTree.fromstring(b64decode(source.group(1))) + + +def test_paper_plane_follows_the_same_dotted_route_forward_and_back(): + app, _ = start_app() + + def current_route(): + return route_svg(next(value for value in page_markdown(app) if 'class="flight-route"' in value)) + + svg = "{http://www.w3.org/2000/svg}" + assert current_route().find(f".//{svg}animateMotion") is None + app.button(key="worksheet_next_s").click().run() + route = current_route() + dots = route.findall(f"{svg}circle") + left = f"{dots[0].get('cx')} {dots[0].get('cy')}" + right = f"{dots[1].get('cx')} {dots[1].get('cy')}" + forward = route.find(f".//{svg}animateMotion").get("path") + assert forward.startswith(f"M {left} C ") and forward.endswith(right) + trail = route.find(f".//{svg}path") + assert trail.get("d").startswith(forward) + assert trail.get("stroke-dasharray") + assert route.find(f".//{svg}polygon").get("fill") == "#F4B45B" + + app.button(key="worksheet_previous_s").click().run() + back = current_route().find(f".//{svg}animateMotion").get("path") + assert back.startswith(f"M {right} C ") and back.endswith(left) + app.run() + assert current_route().find(f".//{svg}animateMotion") is None + + +def test_the_paper_route_has_one_stop_per_question(): + svg = "{http://www.w3.org/2000/svg}" + for count in (1, 50): + route = route_svg(practice_route(count, count - 1)) + assert len(route.findall(f"{svg}circle")) == count + assert route.find(f".//{svg}animateMotion") is None + + # 2 · Confidence in one click --------------------------------------------- def test_confidence_is_one_click_and_survives_worksheet_paging(): diff --git a/tests/test_paper_shell.py b/tests/test_paper_shell.py index 67327f6..21d4f71 100644 --- a/tests/test_paper_shell.py +++ b/tests/test_paper_shell.py @@ -294,7 +294,7 @@ def test_stacked_blocks_keep_a_gap_between_them(): assert "margin-bottom: .5rem;" in css_rule(shell, card_row) -def test_loading_shows_the_brand_plane_instead_of_a_round_spinner(): +def test_loading_centers_the_brand_plane_for_app_runs_and_spinners(): shell = (ASSETS / "paper_shell.css").read_text(encoding="utf-8") # The inlined plane is the brand mark: same polygons, same colours. @@ -306,25 +306,22 @@ def test_loading_shows_the_brand_plane_instead_of_a_round_spinner(): for points, fill in polygons: assert f"points='{points}' fill='%23{fill}'" in plane.group(1) - # st.spinner: no ring, the plane flies on a short track. - ring = css_rule(shell, '[data-testid="stSpinner"] [data-testid="stSpinnerIcon"]') - assert "border: 0;" in ring and "animation: none;" in ring - flying = css_rule(shell, '[data-testid="stSpinner"] [data-testid="stSpinnerIcon"]::after') - assert "var(--paper-plane)" in flying and "paper-plane-patrol" in flying - - # App running: Streamlit's figure is hidden and one plane crosses the window, - # unless a spinner on the page already shows one. - assert "display: none;" in css_rule(shell, '[data-testid="stStatusWidgetRunningIcon"]') - crossing = css_rule( - shell, - '[data-testid="stApp"]:has([data-testid="stStatusWidgetRunningIcon"])' - ':not(:has([data-testid="stSpinner"])) [data-testid="stHeader"]::after', + triggers = ( + '[data-testid="stApp"]:has(:is([data-testid="stSpinner"], ' + '[data-testid="stStatusWidgetRunningIcon"]))' ) - assert "var(--paper-plane)" in crossing and "paper-plane-crossing" in crossing - assert "pointer-events: none;" in crossing - for name in ("paper-plane-patrol", "paper-plane-crossing"): - assert f"@keyframes {name} {{" in shell - reduced = shell[shell.index("@media (prefers-reduced-motion: reduce)"):] + veil = css_rule(shell, triggers + ' [data-testid="stAppViewContainer"]::after') + flight = css_rule(shell, triggers + "::after") + assert "position: fixed;" in veil and "backdrop-filter: blur(3px);" in veil + assert "center / 192px 56px" in veil + assert "top: calc(50% - 28px);" in flight and "anchor(--paper-loading-main center, 50%)" in flight + assert "var(--paper-plane)" in flight and "paper-loading-flight" in flight + assert "pointer-events: none;" in veil and "pointer-events: none;" in flight + trail = re.search(r" Date: Sun, 15 Nov 2026 12:02:11 -0500 Subject: [PATCH 2/2] Verify new flight markup in worksheet and loader tests --- tests/test_paper_shell.py | 5 +++-- tests/test_practice_clock.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_paper_shell.py b/tests/test_paper_shell.py index 21d4f71..cba240e 100644 --- a/tests/test_paper_shell.py +++ b/tests/test_paper_shell.py @@ -317,8 +317,9 @@ def test_loading_centers_the_brand_plane_for_app_runs_and_spinners(): assert "top: calc(50% - 28px);" in flight and "anchor(--paper-loading-main center, 50%)" in flight assert "var(--paper-plane)" in flight and "paper-loading-flight" in flight assert "pointer-events: none;" in veil and "pointer-events: none;" in flight - trail = re.search(r"