Skip to content

web: print straight to the system print dialog, not just export - #143

Closed
Dejan-Port wants to merge 5 commits into
storytold:mainfrom
Dejan-Port:print-to-system-dialog
Closed

Dejan-Port wants to merge 5 commits into
storytold:mainfrom
Dejan-Port:print-to-system-dialog

Conversation

@Dejan-Port

Copy link
Copy Markdown
Contributor

What

`file.print` already builds a PDF and returns it as base64, per its own doc comment: "the PDF the UI hands to the system print dialog" — but nothing actually did that. On the web build, Ctrl+P (Cmd+P is declared as the command's shortcut, but there's no keyboard dispatch wired to it anywhere) did nothing, and the ribbon's "Print…" menu item ran `file.print` via `run_or_alert`, which just discards the result on success.

This wires it up: a dedicated Print action that runs `file.print`, decodes the returned PDF, and (on the web build) opens it in a hidden iframe and calls the iframe's own `print()` once it's loaded — the standard way to drive the browser's native print dialog on a PDF without a download or a popup window the browser might block.

How

  • `ui-egui`: new `Services::print` hook (`Option<Box<dyn Fn(&[u8])>>`).
  • `ui-egui`: `SheetApp::print_now()` — runs `file.print`, decodes the `base64` field of the result with the engine's own `base64_decode`, hands the bytes to `services.print`. Falls back to the existing alert-on-error behavior on failure (same as `run_or_alert`), and is a silent no-op on success when there's no `services.print` (desktop today — nothing regresses, `file.print` still runs, there's just no UI action on success, same as before this change).
  • `ui-egui` `ribbon.rs`: `Key::P` now dispatches to `print_now` (previously no `Key::P` arm existed in the shortcut `match` at all); the "More commands" menu's "Print…" item calls `print_now` instead of `run_or_alert("file.print", …)`.
  • `gridcraft-web`: implements the hook — `Blob` → object URL → hidden `<iframe>` → `iframe.contentWindow.print()` on `onload`. Added `HtmlIFrameElement` to the `web-sys` feature list.

Testing

  • `cargo check --target wasm32-unknown-unknown -p gridcraft-web` — clean (one pre-existing unrelated warning in `crates/engine/src/cmd/spelling.rs`, not from this change).
  • `cargo fmt --check -p gridcraft-web -p gridcraft-ui-egui` — clean.
  • `trunk build --release` succeeds; deployed the build and confirmed in a real browser (Firefox) that Ctrl+P and the ribbon's Print… both now open the system print dialog directly on the spreadsheet.

Opened the same fix for WordCraft earlier today: storytold/wordcraft#209 — same underlying pattern (iframe + onload + print()), adapted to GridCraft's existing `file.print`/base64 plumbing instead of WordCraft's path-based export.

The web build had no way to actually print — Ctrl+P (Cmd+P, declared on
file.print's shortcut) did nothing at all, and the ribbon's "Print…" menu
item ran file.print and silently discarded the result. file.print already
builds a PDF and returns it as base64 specifically "for the UI to hand to
the system print dialog" per its own doc comment — this wires that up.

- ui-egui: new Services::print hook (web: hand PDF bytes to the browser's
  print flow directly, no download); SheetApp::print_now runs file.print,
  decodes the base64 result, and calls the hook.
- ui-egui: Key::P now dispatches to print_now (was unbound); the ribbon's
  "Print…" menu item calls print_now instead of discarding file.print's
  result.
- gridcraft-web: implements the hook (Blob -> object URL -> hidden iframe
  -> iframe.contentWindow.print() on load).

Desktop builds are unaffected (no Services::print set there; print_now
falls back to the existing alert-on-error behaviour with no UI action on
success, same as before this change).
GridCraft had no File-tab equivalent at all — file operations were
scattered across the Quick Access toolbar and a "More commands" popup,
with no single place mirroring Word's or PowerPoint's backstage view.

This adds a File tab (first in the ribbon, like real Excel) that opens
a full-screen backstage panel:

- New (blank workbook + recents)
- Open (browse… + recents)
- Save / Save As (run immediately, same commands the toolbar already uses)
- Print (the system print dialog added in storytold#143)
- Export (xlsx/csv/html, same formats already offered from Share)
- About

The panel is filled with Excel's green (0x107C41), fixed regardless of
light/dark mode — the same treatment WordCraft's backstage gives its
blue. Mirrors storytold/wordcraft#209's backstage and the matching
addition to storytold/deckcraft.

Builds on this branch's existing print commit; no behavior changes to
anything outside the new File tab and backstage module.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Dejan-Port

Copy link
Copy Markdown
Contributor Author

Pushed a second commit here: a File tab + full backstage panel (New/Open/Save/Print/Export/About), filled with Excel's green, mirroring WordCraft's blue backstage (storytold/wordcraft#209) and the matching one just added to DeckCraft. GridCraft had no File-tab equivalent at all before this, so it seemed natural to land alongside the print feature rather than as a separate PR, since Print is one of its pages.

🤖 Generated with Claude Code

Dejan-Port and others added 2 commits October 10, 2026 16:28
Found while working on something else, but worth their own commit:

- cmd/file.rs: `file.open`'s `path` filter ended in `|| true`, so the
  FileKind::Xlsx check in front of it never did anything — the path was
  always kept. Removed the dead condition (same effective behavior,
  minus the clippy::overly_complex_bool_expr it tripped — this also
  means a workbook opened from a .xlsm/.csv/etc. path keeps that path,
  so Save doesn't force Save As for it, same as .xlsx).
- formula_bar.rs: `tab || (enter && false)` — the `enter` arm could
  never fire. Simplifies to `tab`.
- grid.rs: a cell's wrapped-text layout condition had
  `wrap || justify || newline && wrap` — the third term is subsumed by
  the first (`&& wrap` makes it fire only when the first term already
  did). Simplifies to `wrap || justify`.
- grid.rs: the filter-menu's table lookup for a header click was
  `t.range.start.row <= at.y as u32 || true`, which made it always match
  the first table in the sheet regardless of where the click actually
  was. On a sheet with more than one table sharing header columns,
  right-clicking one table's filter button could open the wrong
  table's filter. Fixed to check the click is actually within the
  table's row range.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
GridCraft had no VBA support at all: opening a macro-enabled workbook
silently dropped the vbaProject.bin part, and there was no path to
running a macro, recorded or otherwise.

Full VBA (the language, plus the whole Range/Worksheet/Application
object model) is a huge undertaking — this is deliberately a basic
subset, aimed at the common shape of small, button-run macros rather
than arbitrary existing code:

  Sub/End Sub (zero-argument only), Dim, assignment, If/ElseIf/Else
  (block and single-line), For/Next with Step, MsgBox, arithmetic and
  string-concat expressions, and Range("A1")/Cells(r,c) reads and
  writes (optionally qualified by Sheets("Name")/ActiveSheet).

What's new:

- crates/vba (new crate, `gridcraft-vba`): extracts module source out
  of a `vbaProject.bin` via the `ovba` crate (handles the [MS-CFB]
  container and [MS-OVBA] decompression, pure Rust, MIT), then a small
  hand-rolled lexer/parser/interpreter for the subset above. No tree-
  sitter or other C dependency, so it compiles straight to wasm32 for
  the web build. 5 unit tests.
- gridcraft-model: `Workbook::vba` holds the raw `vbaProject.bin` bytes
  — opaque, round-tripped byte-for-byte on save. GridCraft doesn't
  write VBA code, only runs a subset of what's already there.
- gridcraft-xlsx: reads `xl/vbaProject.bin` into `Workbook::vba`
  instead of dropping it with a warning; writes it back (plus the
  `vbaProject` relationship and the macro-enabled main content type)
  when present, so a round-tripped `.xlsm` keeps its macros.
- gridcraft-engine: `macro.list` and `macro.run` commands. The new
  `EngineHost` adapts `gridcraft-vba`'s `Host` trait to a live
  `Session` — reads go straight to the sheet, writes go through
  `cell.set` (the same command the ribbon and formula bar use), so a
  macro's edits are undoable and journaled exactly like a person's.
- gridcraft-ui-egui: a "Macros" button on the Automate tab opens a
  dialog listing the open workbook's macros with a Run button each;
  MsgBox output shows in the usual message box.

Tests: 5 in gridcraft-vba (parsing + running loops/If/MsgBox against a
mock host), 2 in gridcraft-engine (the Session adapter, including that
a macro's cell writes are undoable; list/run behavior with no VBA
project present).

Caveat worth being upfront about: the extraction/decompression side
(`ovba`) hasn't been exercised here against a real-world `.xlsm` with
an actual Microsoft-authored vbaProject.bin — LibreOffice (the one
VBA-capable tool available while building this) doesn't export real
OVBA from its own Basic macros on a fresh document, only round-trips
one that's already there. The interpreter itself is unit-tested
directly against parsed source. Worth a try against a real client
file before leaning on it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Dejan-Port

Copy link
Copy Markdown
Contributor Author

Pushed two more commits here:

  1. Three dead-code logic bugs clippy caught in unrelated code while I was working on something else (file.open's path filter ending in || true, a dead enter arm in the formula bar, and — the one that actually changes behavior — the filter menu's table lookup matching the first table on the sheet regardless of click position, also via a stray || true). Each has the details in its own commit message.

  2. Basic VBA macro support: GridCraft currently drops vbaProject.bin entirely when opening a .xlsm. This adds a new gridcraft-vba crate (extraction via the ovba crate — pure Rust, MIT, no C deps so it's wasm-friendly — plus a small hand-rolled interpreter) covering the common shape of small macros: Sub/If/For/assignment/MsgBox/Range-Cells reads and writes. Wired up as macro.list/macro.run engine commands and a "Macros" button on the Automate tab. vbaProject.bin itself round-trips byte-for-byte on save now instead of being dropped.

Full disclosure in the commit message: I didn't have a real Microsoft-authored .xlsm with actual VBA on hand to test the extraction path end-to-end (LibreOffice doesn't export real VBA from its own Basic macros on a fresh file, only round-trips existing VBA). The interpreter itself is unit-tested directly. Worth a try against a real file before trusting it fully — happy to iterate if something doesn't decode right.

🤖 Generated with Claude Code

GridCraft had no localization at all — every label was a hardcoded
English &str. This adds WordCraft's catalog-based i18n system
(crates/ui-egui/src/i18n, ported largely as-is — it's generic, no
WordCraft-specific dependencies) and wires it up:

- lib.rs: a `tl!` macro (English string → current-language text), an
  `i18n` module, a persisted `language` setting (default: follow the
  OS), and `i18n::set_current` once per frame.
- Translation points chosen for leverage, not per call site: the
  ribbon's shared widgets (big_button, small_button, icon_button,
  toggle_button, split_button, color_palette) translate their
  label/tip internally, so every existing call across ribbon.rs,
  dialogs.rs, panes.rs, tabs.rs and formula_bar.rs is covered without
  touching each one. Same for dialogs.rs's generic `form()` renderer
  (Field::Text/Number/Choice/Check/Note) and `ok_cancel`, and for
  `menu_items`. Title bar, ribbon tab names and the File backstage
  (written by hand, not through those helpers) are translated
  directly. A new Options page in the backstage holds the language
  picker.
- 503 strings translated clean-room (from the plain meaning of the
  English label, never from Microsoft Excel's or another product's
  localization) into Serbian Latin (sr-latn.tsv), then mechanically
  transliterated into Cyrillic (sr.tsv) the same way as the approach
  in storytold/wordcraft#250.
- 11 tests: locale matching, catalog well-formedness, full key-set
  parity between the two scripts, and — checked against the actual
  font bytes, not assumed — that egui's own bundled default font
  (Ubuntu-Light; GridCraft loads no fonts of its own on web, see
  `theme::try_load`'s wasm32 branch) has real glyphs for every
  Cyrillic character and for Serbian Latin's š č ć ž đ used in the
  catalogs.

Scope: this covers the chrome — ribbon buttons/tooltips, dialog
titles, generic dialog fields, the File backstage. It does not reach
every bespoke dialog body (format cells, pivot builder, chart design
and similar hand-built panes draw a lot of their own text outside the
shared widgets, which would need their own pass). A string without a
translation shows in English rather than breaking, so this is additive
and safe to land incrementally — more coverage can follow the same
pattern.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Dejan-Port

Copy link
Copy Markdown
Contributor Author

Pushed one more commit: an interface-language system (ported from WordCraft's catalog-based i18n module, which is generic enough to carry over as-is) with Serbian, both scripts (sr Cyrillic, sr-latn Latin) — same pairing as storytold/wordcraft#250.

GridCraft had zero localization before this — every label was a hardcoded &str. Wrapping 500+ individual call sites one by one wasn't realistic, so translation happens at a few high-leverage points instead: the shared ribbon widgets (big_button, icon_button, etc.) translate their own label/tip, and dialogs.rs's generic form() renderer + menu_items + ok_cancel do the same — so every existing caller across ribbon.rs, dialogs.rs, panes.rs, tabs.rs and formula_bar.rs picks up translation for free, present and future. Title bar, ribbon tab names and the File backstage (hand-drawn, not through those helpers) are translated directly, and the backstage gets a new Options page with the language picker.

503 strings, clean-room translated into Serbian Latin, then mechanically transliterated into Cyrillic. 11 tests, including one that checks actual font bytes (not just assumes) — GridCraft bundles no fonts of its own on web, so I verified egui's own default font (Ubuntu-Light) genuinely has glyphs for every Cyrillic character and Serbian Latin's š č ć ž đ used here, rather than silently falling back to tofu boxes.

Scope, stated plainly: this is the chrome — ribbon, dialog titles, generic dialog fields, backstage. Bespoke dialog bodies (format cells, pivot builder, chart design) draw a lot of their own text outside the shared widgets and aren't covered yet; a string without a translation just shows in English rather than breaking anything, so more coverage can land incrementally.

🤖 Generated with Claude Code

Dejan-Port added a commit to Dejan-Port/deckcraft that referenced this pull request Oct 10, 2026
Same shape as storytold/wordcraft#250 and storytold/gridcraft#143's
most recent commit: DeckCraft had no localization at all, so this adds
a catalog-based i18n system (crates/ui-egui/src/i18n — generic, ported
with only the env var and a doc-comment product name changed) plus
Serbian in both scripts.

- lib.rs: a `tl!` macro, an `i18n` module, a persisted `language`
  setting (default: follow the OS), `i18n::set_current` once per
  frame.
- Translation at a few high-leverage points rather than per call site:
  `big_button`/`big_button_ex`/`small_button`/`small_button_ex`/
  `icon_toggle`/`color_split`/`color_grid`/`section`/`drop_button` in
  widgets.rs translate their own label/tip, so every ribbon caller
  picks it up for free. `dialogs.rs`'s `title()` (every dialog window
  title goes through one match statement), `buttons()` (OK/Cancel),
  `field()` and `check()` (the generic field renderers used by most of
  the simple dialogs) do the same. The 19 remaining hardcoded
  `.on_hover_text("...")` call sites in ribbon.rs/credits.rs (DeckCraft
  attaches tooltips to the Response after the widget call, unlike
  GridCraft's widgets which take a `tip` param directly, so these
  couldn't be swept up the same way) are wrapped individually. Title
  bar, ribbon tab names and the File backstage are translated directly
  where they're drawn. The backstage gets a new Options page with the
  language picker.
- 216 strings, clean-room translated into Serbian Latin (sr-latn.tsv),
  then mechanically transliterated into Cyrillic (sr.tsv).
- 11 tests: locale matching, catalog well-formedness (including that a
  translation's leading/trailing whitespace matches its English
  source's — DeckCraft has a couple of intentionally-indented field
  labels, unlike WordCraft/GridCraft, so the stricter blanket check
  those two use doesn't fit here), full key-set parity between
  scripts, and font glyph coverage checked against the actual bytes of
  egui's bundled default font (Ubuntu-Light) for both Cyrillic and
  Serbian Latin's š č ć ž đ — DeckCraft's own fonts (deckcraft-fonts)
  only embed real files with CRAFT_FONTS_DIR set at build time, which
  wasn't available building this.

Scope: ribbon, dialog titles/fields/buttons, File backstage. Several
dialog bodies (table/chart data editors, SmartArt picker, equation
editor) draw bespoke UI outside the shared helpers and aren't covered
yet. Missing entries just show in English, so this is safe to land
incrementally.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@echelon

echelon commented Oct 10, 2026

Copy link
Copy Markdown
Member

Thank you! The print commit (11c41e1) looks good: a Services::print hook plus a hidden iframe on the web is a clean approach. But this PR also carries a VBA interpreter that runs macros from .xlsm files (a security-sensitive product decision), a second i18n system (main already has one), a File backstage and clippy changes — and it conflicts in 17 files.

Could you resubmit just the print change, rebased onto main? Small fix while you're there: revoke the Blob URL and remove the iframe after printing so they don't leak on each print. The VBA work would need a separate design discussion first.

If you haven't already, please come join us on Discord: https://discord.gg/artcraft — happy to talk any of this through there.

@Dejan-Port

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks for the detailed look. Split it out: #209 has just the print commit, rebased onto current main, with the blob URL / iframe cleanup you asked for.

Leaving this PR open for now since it's where the VBA/i18n/backstage discussion already started — happy to move that conversation to Discord or a design doc, whatever's easier on your end. No rush on my end to land any of that; I know it's a bigger product decision (especially the VBA interpreter, I get why that needs real discussion — it runs arbitrary macro code from untrusted files).

Will join Discord too.

🤖 Generated with Claude Code

@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Thank you — there's a lot of work here. This branch mixes four features (web print, Serbian UI, a Backstage view and a VBA interpreter), and print is now in #209 while Backstage landed as #331. The VBA interpreter is a security decision for the maintainers: it executes macros with no trust prompt, macro.run is reachable over MCP and the control channel without consent, and nested loops can still hang the app. We'd welcome the Serbian strings as their own PR on main's i18n (crates/ui-egui/src/i18n/).

If you haven't already, come join us on the ArtCraft Discord: https://discord.gg/artcraft

@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Update: the web print part of this PR has landed via #209.

@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Note: VBA projects are now preserved (never executed) via #19, so any interpreter discussion would build on that.

@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Closing this one now that its pieces have landed separately: web print via #209 and VBA preservation via #19 (macros are kept but never run). The Serbian UI strings would be very welcome as their own PR against current main's crates/ui-egui/src/i18n/. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants