APOLLO TERMINAL v1.0 –
> █type '/help' for commands
Skip to main content
0200 / 00:00:00 / 1970.01.01
×
HOME ABOUT NEWS PROJECTS REFERENCES LINKS

PREVIEWING SWIFTUI WITHOUT A MAC IN THE ROOM

A solid navy diamond sitting over an offset amber rounded rectangle

Xcode’s SwiftUI canvas is not a mockup. When you write a #Preview block, Xcode compiles that view and runs it on a real simulator, live, and updates it as you type. The #Preview macro arrived with Swift 5.9 and Xcode 15, and it is macOS and Xcode only by design, because it is driving an actual simulator process, not drawing a picture of one.

The preview panel I wrote for Flexible Swift is an approximation of that. It runs no Swift code, drives no simulator, and does not touch Apple’s rendering engine.

How the panel renders

SwiftPreviewPanel reads your Swift file through the plugin’s own PSI tree, the same syntax tree the rest of the IDE uses for completion and navigation. SwiftViewTreeBuilder looks for an explicit #Preview block first, and falls back to a plain View struct’s body if there is none. That tree, not your Swift compiler, is handed to SwiftUIHtmlRenderer, which turns it into a single self-contained HTML and CSS document. That document loads into an embedded Chromium browser, JCEF, docked in a JetBrains tool window, and a 500 millisecond debounce timer redoes the whole thing on every edit. If JCEF is not available in the current IDE and JRE, the panel says so outright rather than quietly disappearing.

Because no Swift code runs, a #Preview that would crash instantly on a real device or in Xcode’s canvas, a force-unwrapped nil, an out-of-bounds array access, renders perfectly fine in my panel, because nothing is actually executing. The renderer only ever reads what you wrote.

Layout, colors and controls

The panel covers layout and hierarchy: stack nesting, spacing, and text sizing across Dynamic Type steps, which the renderer drives with a single CSS scale variable rather than Apple’s actual size-class layout metrics. It has device frames for a few common iPhone and iPad sizes, a light and dark color table mapped from SwiftUI’s semantic color names, and rough icon substitutes for SF Symbols.

SwiftUIColorMap resolves a name like systemGray6 to a fixed hex value, #F2F2F7, hand-written once for every OS version, with light and dark handled by a second static table. If Apple changes what that semantic color means, the table goes quietly wrong.

Nothing animates: the panel is a static document regenerated on a timer. Buttons, toggles, and navigation bars are CSS shapes standing in for real UIKit-backed controls, so exact corner radii, shadows, and press states will not match. The fonts and icons are substitutes too, a bundled font called InterPreview standing in for San Francisco, a custom icon font standing in for real SF Symbols glyphs. Anything that depends on the real UIKit implementation, such as a ScrollView’s bounce, a List’s cell reuse or a Sheet’s drag and height behavior, has no equivalent here.

Use without a Mac

It is a layout check you can run without a Mac, which matters if you write Swift on Windows or Linux and only reach a Mac occasionally. It catches wrong nesting, wrong spacing and text that will not fit at larger Dynamic Type sizes. It does not replace checking the real thing in Xcode’s canvas before anything goes to the App Store.

Flexible Swift is on the JetBrains Marketplace.

Background
EducationEBS Business School, SFSU, HSG
Experience20 Years Enterprise Systems
BuiltScipio ERP - ERP & Ecommerce System
Author"Start/Up - Set the net on fire"
JetBrains5x Plugins Published
Tech Stack
LanguagesJava, Python, JS, TS, Rust, Go, Swift, PHP
FrontendReact, Vue, Angular, HTML/CSS
DatabasesPostgreSQL, MySQL, MongoDB
CachingRedis, Memcached
Key Clients
EnterpriseL'Oréal, Amadeus, Konica Minolta, Orange
Digital1&1 Internet, Sedo.com, Creditsafe
PublicDRK, Kanton Basel, Aktion Mensch, Börsenverein
← PREV NEXT → TOP ↑