跳到正文
DEV Community· joexk·· 54 分鐘前AI 評分49

將 SwiftUI 應用程式移植至 Rust 和 egui,以支援 Windows 和 Linux:哪些沿用、哪些未能沿用

Porting a SwiftUI app to Rust and egui for Windows and Linux: what carried over and what didn't

AI 導讀

作者把原先以 SwiftUI 編寫的 Mac 應用程式介面移植至 Rust 和 egui,製成供 Windows 和 Linux 使用的單一原生二進制檔 JoeBro Desktop。設計 tokens、圖示名稱和自訂字形得以沿用,元件則須重新編寫。Apple Intelligence、HTML 電郵顯示和內嵌網頁等功能沒有移植,或改用其他方式取代。

正文

I wrote a Mac app in SwiftUI first. To bring it to Windows and Linux, I ported the UI to Rust with egui instead of reaching for a web view. The result is JoeBro Desktop (GPL-3.0, I made it), a single native binary. This is what carried over from SwiftUI, what had to be rebuilt by hand, and what I gave up.

Why egui

I wanted one small native program with no web runtime, the same on Windows and Linux (Wayland or X11), and a UI I could change quickly. egui is immediate mode: every frame you describe the UI with code and it draws it. There are no view objects to keep in sync with state, which suits an app where a lot of the screen is live data (agent sessions, streaming chat, approvals).

The tradeoff is that egui draws everything itself. There are no native widgets, so the app won't pick up the system's look. For me that was a plus: I wanted the same design on both systems anyway, matching the Mac app.

What carried over cleanly: the design tokens

The Mac app has one file, JBDesign.swift, that holds every colour, radius, spacing step and font. I ported it nearly line for line into theme.rs. A panel gets a Palette struct per frame and never hard-codes a colour:

let p = sh.palette;
ui.label(RichText::new("Memory").color(p.ink2));

The names are the same as on the Mac (ink, ink2, ink3, line, raised, accent, live), and so are the eight accent palettes, the radii and the spacing scale. Because the token layer was already a clean abstraction in Swift, this was the most mechanical part of the port.

Applying the tokens to egui is one function that fills in egui::Visuals: text colour, window fill, stroke, rounding, and per-state widget colours for inactive, hovered, active and open widgets. Two details mattered:

  • Panels are transparent by default so the app's backdrop shows through, and each panel paints its own fill at the user's chosen opacity.
  • Only floating surfaces (menus, popovers) cast a shadow, to match the flat look of the Mac app.

What needed a workaround: fonts and weights

SwiftUI lets you ask for a font weight. egui doesn't have the concept. A font is a family, and a weight is a different file. So the app bundles Geist in five weights and Geist Mono in four, registers each as its own font, and exposes them as named families:

fonts.families.insert(FontFamily::Name("semibold".into()), fam("geist-semibold", false));

Then a small helper, theme::jb(size, W::Semibold), returns the right FontId. It's a few dozen lines, and after that nobody thinks about it. The fallback chain matters too: each family lists Geist first, then symbol fonts, then egui's built-in fonts, so an unusual character doesn't render as a box.

What needed a lookup table: icons

The Mac app uses SF Symbols, which are Apple's and not available elsewhere. I mapped symbol names to Phosphor icons through the egui-phosphor crate. The mapping is one large match:

"arrow.clockwise" => pr::ARROW_CLOCKWISE,
"arrow.triangle.branch" => pr::GIT_BRANCH,

The useful part was keeping the Swift names. Ported panels could keep calling icons::symbol(ui, "gearshape", 14.0, color) with the same string the Mac code used. Two guard rails made it livable:

  • Unknown names fall back to a small circle instead of panicking, so a missing icon is ugly but never a crash.
  • A test (every_mac_symbol_resolves) holds the list of symbol names the Mac panels and tabs use and asserts that each one maps to a real icon. The list is maintained by hand, so it's a safety net rather than an automatic check.

For the handful of concepts that are the app's own (agent, memory, Night Shift, skills, tools and so on), the Mac app draws custom glyphs on a 16-unit grid with a 1.5 stroke. I re-drew those eleven with egui's painter using the same grid, so they look the same at any size.

What I rebuilt: components

SwiftUI gives you a lot for free that egui doesn't: a styled button, a menu, a switch, an animated appear. I wrote a small component layer in widgets.rs that mirrors the Mac's reusable views: panel_chrome (a 44 pt header plus a hairline), hover_row with the selected tick, jb_switch, a menu popup, toasts, a command palette on Ctrl+K.

Motion is the same idea. The Mac app respects the system "reduce motion" setting. In Rust I wrapped egui's animation helper so one preference makes every transition instant:

pub fn anim(ctx: &egui::Context, id: egui::Id, on: bool, dur: f32) -> f32 {
    ctx.animate_bool_with_time(id, on, if reduce_motion() { 0.0 } else { dur })
}

Keyboard shortcuts needed one small translation: the Mac says ⌘K and everywhere else says Ctrl+K, so shortcut_label("K") picks which to display.

What didn't carry over

I'd rather list these than hide them. They are also in the README.

  • No Apple Intelligence. That is a macOS service. On Windows and Linux you use the local or cloud models you configure.
  • No EventKit. Calendars come from CalDAV (Nextcloud, Fastmail, iCloud over CalDAV) instead of a system calendar.
  • Email is plain text. HTML mail is shown as text, not rendered. egui has no HTML engine, and I didn't want to embed a browser for it.
  • Plugin panels are text-only. Plugins on the Mac can show their own panels. Here they still add tools and logic, but the panel is plain text.
  • Word and Excel open in LibreOffice, not in an embedded editor.
  • Web pages open in your browser, not inside the app.

Most of these are "SwiftUI or an Apple framework had it, and egui doesn't". The pattern is that anything depending on a platform framework got dropped or replaced with an open protocol, while anything that was my own UI logic ported.

Testing a UI that draws itself

Because egui renders to a texture, I can test the real UI without a window. There's a snapshot feature (never in the shipped build) that runs the actual frame function against a fake in-process backend serving fixture JSON, renders it with egui-wgpu on a headless adapter, and writes PNGs. That's how I check layouts at odd sizes and in both themes. There are also plain unit tests for things that tend to break in immediate-mode UIs, like text wrapping and dropdown behaviour.

Small Windows build notes

  • On Windows I use the OS TLS stack for reqwest (native-tls) and rustls elsewhere. That way the Windows build needs no C compiler for TLS.
  • The release workflow builds Linux (tarball and AppImage) and Windows (zip and Inno Setup installer) on tags, and attaches a SHA256SUMS file to a draft release that I publish by hand.
  • The Windows build isn't code-signed yet, so SmartScreen warns the first time.

Would I do it again?

For this app, yes. The shared design tokens meant the two apps look like one product, and one Rust binary is much easier to ship than a web runtime. What I'd warn anyone about is the long tail of components: the first 80% of the UI came quickly, and the last 20% is exactly the SwiftUI conveniences you didn't know you were using.

The code is at github.com/joexk1/JoeBro-Desktop, and the Mac app it descends from is github.com/joexk1/JoeBro. If you've ported a SwiftUI app to egui, iced or anything else, I'd like to hear what surprised you.

來源:DEV Community · dev.to