Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 82 additions & 18 deletions src/CrossEscPos.App.Browser/wwwroot/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ html, body {
height: 100vh;
}

/* Startup splash: a receipt feeds out of a printer slot as the runtime downloads. */
/* Startup splash: a thermal printer prints a receipt of the files as the runtime downloads. */
#avalonia-splash {
position: fixed;
inset: 0;
Expand All @@ -35,32 +35,87 @@ html, body {
}

.splash-printer {
width: 132px;
height: 150px;
width: 248px;
max-width: 100%;
}

.splash-slot {
/* The paper rises out of the slot: new lines join at the bottom, older lines fade out at the top. */
.splash-window {
display: flex;
flex-direction: column;
justify-content: flex-end;
height: 196px;
margin-bottom: -6px;
padding: 0 20px;
overflow: hidden;
-webkit-mask-image: linear-gradient(to bottom, transparent, #000 56px);
mask-image: linear-gradient(to bottom, transparent, #000 56px);
}

.splash-paper {
padding: 10px 12px 18px; /* keeps the newest line clear of the slot */
background: #fff;
color: #111;
font: 10px/14px ui-monospace, "Cascadia Mono", Consolas, "DejaVu Sans Mono", monospace;
text-align: left;
}

.splash-paper .line {
height: 14px;
overflow: hidden;
white-space: pre;
}

.splash-paper .center { text-align: center; }
.splash-paper .bold { font-weight: 700; }

.splash-paper .barcode {
height: 26px;
margin: 6px 22px 2px;
background: repeating-linear-gradient(to right,
#111 0 2px, #fff 2px 3px, #111 3px 4px, #fff 4px 7px, #111 7px 10px, #fff 10px 11px,
#111 11px 12px, #fff 12px 14px, #111 14px 16px, #fff 16px 19px);
}

.splash-body {
position: relative;
z-index: 1;
height: 64px;
border-radius: 10px 10px 16px 16px;
background: linear-gradient(#484c50, #34373a);
box-shadow: 0 12px 28px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.splash-slot {
position: absolute;
top: 0;
left: 8px;
right: 8px;
height: 12px;
border-radius: 6px;
background: #0e0f10;
box-shadow: inset 0 3px 4px rgba(0, 0, 0, 0.6);
}

/* The paper's height is the download progress (set from main.js via --progress, 0..1). */
.splash-paper {
--progress: 0;
height: calc(var(--progress) * 138px);
margin: -6px 12px 0;
background-color: #fff;
background-image: repeating-linear-gradient(to bottom, transparent 0 10px, #c4c4c4 10px 13px);
background-size: 64% calc(100% - 4px);
background-position: 12px 4px;
background-repeat: no-repeat;
transition: height 0.2s ease-out;
/* Blinks while printing, holds when done, turns red on failure. */
.splash-led {
position: absolute;
right: 18px;
bottom: 16px;
width: 8px;
height: 8px;
border-radius: 50%;
background: #3ddc84;
box-shadow: 0 0 6px #3ddc84;
animation: splash-blink 0.8s steps(2, jump-none) infinite;
}

@keyframes splash-blink {
from { opacity: 1; }
to { opacity: 0.25; }
}

#avalonia-splash.splash-done .splash-led { animation: none; }

.splash-title {
margin: 20px 0 6px;
font-size: 20px;
Expand All @@ -84,9 +139,18 @@ html, body {
color: #ff6b7d;
}

#avalonia-splash.splash-error .splash-led {
animation: none;
background: #ff4d5e;
box-shadow: 0 0 6px #ff4d5e;
}

@media (prefers-reduced-motion: reduce) {
#avalonia-splash,
.splash-paper {
#avalonia-splash {
transition: none;
}

.splash-led {
animation: none;
}
}
20 changes: 12 additions & 8 deletions src/CrossEscPos.App.Browser/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
</head>
<body>
<div id="out"></div>
<!-- Startup splash. Avalonia adds .splash-close to #avalonia-splash on its first frame; main.js
drives the paper (download progress) and the status text until then. -->
<div id="avalonia-splash" role="status" aria-live="polite">
<div class="splash-printer">
<div class="splash-slot"></div>
<div class="splash-paper" id="splash-paper" role="progressbar" aria-label="Download progress"
aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"></div>
<!-- Startup splash. Avalonia adds .splash-close to #avalonia-splash on its first frame; until then
main.js prints each downloaded file as a receipt line and keeps the status text current. -->
<div id="avalonia-splash">
<div class="splash-printer" aria-hidden="true">
<div class="splash-window">
<div class="splash-paper" id="splash-paper"></div>
</div>
<div class="splash-body">
<div class="splash-slot"></div>
<span class="splash-led"></span>
</div>
</div>
<p class="splash-title">CrossEscPos</p>
<p class="splash-status" id="splash-status">Loading the emulator…</p>
<p class="splash-status" id="splash-status" role="status">Loading the emulator…</p>
<p class="splash-detail" id="splash-detail"></p>
</div>
<!-- Shared browser transports (Web Serial / WebUSB); classic script runs before the deferred module. -->
Expand Down
97 changes: 85 additions & 12 deletions src/CrossEscPos.App.Browser/wwwroot/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,112 @@ import { dotnet } from './_framework/dotnet.js'
const is_browser = typeof window != "undefined";
if (!is_browser) throw new Error(`Expected to be running in a browser`);

// Startup splash (index.html): the paper feeds out as the runtime files download. Avalonia hides it on
// its first frame by adding .splash-close; this script shows progress, the hand-off, and failures.
// Startup splash (index.html): a thermal printer prints one receipt line per downloaded file, fed by the
// Resource Timing API (real names and sizes, cache hits included). Avalonia hides the splash on its first
// frame by adding .splash-close; this script prints the downloads, the hand-off, and failures.
const splash = document.getElementById('avalonia-splash');
const paper = document.getElementById('splash-paper');
const status = document.getElementById('splash-status');
const detail = document.getElementById('splash-detail');

function showProgress(loaded, total) {
const fraction = total > 0 ? Math.min(loaded / total, 1) : 0;
paper.style.setProperty('--progress', fraction.toFixed(3));
paper.setAttribute('aria-valuenow', String(Math.round(fraction * 100)));
if (loaded >= total) {
status.textContent = 'Starting the emulator…';
detail.textContent = '';
} else {
detail.textContent = `Downloaded ${loaded} of ${total} files`;
const COLUMNS = 30; // characters per receipt line
const LINE_HEIGHT = 14; // px, matches .splash-paper .line
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;

// Printer: queued lines print one at a time, each feeding the paper up one line in motor steps.
const queue = [];
let printing = false;

function print(text, ...classes) {
queue.push({ text, classes });
if (!printing) printNext();
}

function printNext() {
const job = queue.shift();
if (!job) {
printing = false;
return;
}
printing = true;
const line = document.createElement('div');
line.className = ['line', ...job.classes].join(' ');
line.textContent = job.text;
paper.append(line);
// Keep the DOM small: lines far above the fade are gone for good on a real printer too.
while (paper.childElementCount > 40) paper.firstElementChild.remove();
const height = line.offsetHeight || LINE_HEIGHT;
if (!reduceMotion) {
paper.animate([{ transform: `translateY(${height}px)` }, { transform: 'none' }],
{ duration: 120, easing: 'steps(3, end)' });
}
// Catch up when files arrive faster than a printer would print them.
setTimeout(printNext, queue.length > 12 ? 20 : 90);
}

function row(left, right) {
const room = COLUMNS - right.length - 1;
const name = left.length > room ? left.slice(0, room - 1) + '…' : left;
return name.padEnd(COLUMNS - right.length) + right;
}

function formatSize(bytes) {
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
return `${Math.max(1, Math.round(bytes / 1024))} KB`;
}

// "System.Private.CoreLib.acqhqif0l6.wasm" -> "System.Private.CoreLib.wasm"
function displayName(url) {
const file = decodeURIComponent(new URL(url).pathname.split('/').pop() || url);
return file.replace(/\.[a-z0-9]{10}(?=\.[a-z]+$)/, '');
}

let files = 0;
let bytes = 0;

function onResource(entry) {
if (!entry.name.startsWith(location.origin)) return;
const size = entry.encodedBodySize || entry.decodedBodySize || entry.transferSize;
if (!size || entry.responseStatus >= 400) return; // failed or empty requests aren't downloads
files++;
bytes += size;
print(row(displayName(entry.name), formatSize(size)));
detail.textContent = `Downloaded ${files} files (${formatSize(bytes)})`;
}

print('CrossEscPos', 'center', 'bold');
print('Loading the emulator', 'center');
print('-'.repeat(COLUMNS));

const downloads = new PerformanceObserver((list) => list.getEntries().forEach(onResource));
downloads.observe({ type: 'resource', buffered: true });

function showError(error) {
downloads.disconnect();
splash.classList.remove('splash-close');
splash.classList.add('splash-error');
queue.length = 0; // drop pending download lines so the failure prints right away
print('-'.repeat(COLUMNS));
print('*** PRINT FAILED ***', 'center', 'bold');
status.textContent = "The emulator couldn't start. Reload the page to try again.";
detail.textContent = String(error?.message ?? error);
}

try {
const dotnetRuntime = await dotnet
.withModuleConfig({ onDownloadResourceProgress: showProgress })
.withDiagnosticTracing(false)
.withApplicationArgumentsFromQuery()
.create();

// Every runtime file is in: total the receipt while Avalonia starts.
downloads.disconnect();
splash.classList.add('splash-done');
print('-'.repeat(COLUMNS));
print(row(`TOTAL ${files} files`, formatSize(bytes)), 'bold');
print('Starting the emulator', 'center');
print('', 'barcode');
status.textContent = 'Starting the emulator…';

const config = dotnetRuntime.getConfig();

// Wire the shared transports.js delivery slots to the app's [JSExport] callbacks. transports.js loads
Expand Down
Loading