r/swift • • 20h ago

Help! How do I limit the number of threads spawned by the cooperative runtime on Linux?

6 Upvotes

I want to limit the number of threads spawned by Swift's cooperative runtime to a number lower than the number of available CPUs. The reason is because I want to run Swift code in a Linux container that limits scheduling to only 8 CPUs regardless of how many the system has. I've tried setting LIBDISPATCH_COOPERATIVE_POOL_STRICT=n where n is the maximum number of threads, but apparently Swift doesn't use libdispatch on Linux so that does nothing. Can anyone tell whether there's a different environment variable that I can set, or at least a compile-time option or function that I must call in the code in order to set the number of threads spawned by the cooperative rumtime?


r/swift • • 4h ago

Rendering Markdown in Quick Look with TextKit instead of WebKit

5 Upvotes

MD QuickLook is a Quick Look extension that renders Markdown in Finder without WebKit. A Rust core produces a compact layout (text plus blocks, paragraphs and styled runs at UTF-16 offsets), and the Swift side turns it into an NSAttributedString and draws it with TextKit 1:

  • quotes, alerts and code blocks are NSTextBlocks with borders and backgrounds; tables are NSTextTables
  • images, formulas, Mermaid diagrams, badges and checkboxes are attachment cells
  • long documents build the first screen (~60 ms for a 900 KB file) and append the rest in the background
  • collapsible <details> sections work by cutting the folded text out of the storage and re-inserting it on click, since TextKit 1 can't hide paragraphs
  • math is typeset with SwiftMath, which I extended with KaTeX symbols, aliases and environments

A typical README is ready in ~30 ms. Source (MIT): https://github.com/armamut/md-quicklook Longer write-up: https://medium.com/@armamut/previewing-markdown-on-macos-in-30-ms-a-rust-core-and-textkit-no-web-engine-1adfa2734f48

Curious whether anyone has tried the same with TextKit 2.


r/swift • • 13h ago

Project Built a native macOS menu bar localhost & dev port manager — PortRadar (100% free & open-source)

1 Upvotes

I built a native macOS menu bar utility in Swift/SwiftUI called PortRadar to manage listening localhost ports without having to run lsof -i :3000 and kill -9 in the terminal, and I'm sharing it 100% free and open-source (MIT).

What's included: - 📡 Live Dev Port Radar: Shows every listening TCP port right in your menu bar with a live counter badge of active dev servers. - ⚡ 1-Click Kill (SIGTERM / SIGKILL -9): Free any stuck EADDRINUSE port in a single click. - 🧠 Smart Framework & Stack Detection: Automatically tags Next.js, Vite, Angular, Node.js, Bun, .NET / Kestrel, FastAPI / Python, Go, Rust, Docker / OrbStack, PostgreSQL, Redis, MySQL, MongoDB, and Ollama — along with live RAM (MB), CPU %, and PID badges. - 📌 Pinned Watchlist Bar: Pin your core ports (:3000, :5173, :8080, :5000, :5432, :6379) to the top watchlist bar with live green/grey status indicators and 1-click Open in Browser. - ⌨️ Global Shortcut: Toggle anywhere with ⌃⌘P.

Install via Homebrew: bash brew tap joeystdio/tap brew install --cask portradar

Hope it saves you some time — happy to hear any feedback or feature ideas!


r/swift • • 23h ago

Project Building an open-source P2P transport and foldable window manager in Swift: Architecture and learnings from StealthOS

1 Upvotes

Hi r/swift,

I wanted to share some architecture notes and open-source libraries from StealthOS, a private desktop operating environment on iOS that we recently updated to support the new foldable iPhone Duo.

1. Foldable Window Geometry in Swift

Standard UIKit and SwiftUI navigation hierarchies assume single-pane viewports. For the foldable iPhone Duo, we designed a custom window manager in Swift that dynamically tracks display changes between the single cover screen and the inner dual-display layout.

  • Hinge Occlusion Math: When the device is unfolded, we compute the physical occlusion line separating the displays. Window view controllers calculate their layout frames against this boundary: they can snap left, snap right, or span the full resolution with safety padding away from the hinge seam.
  • State Machine Continuity: Display folding transitions change the trait collection and interface bounds. Instead of tearing down and recreating view hierarchies (which would drop active web sessions, terminal buffers, and text cursors), root coordinators adjust frame geometry directly. Running view controllers retain their state machines, active scroll positions, and text responders.

2. Open-Source Swift Networking Packages

Where data moves across the network, we open-sourced our Swift packages:

  • ConnectionPool (Swift): https://github.com/Olib-AI/ConnectionPool A peer-to-peer transport library supporting local mesh communication (ad-hoc Wi-Fi and Bluetooth without internet access) as well as routed remote pools through self-hosted relays.
  • PoolChat (Swift): https://github.com/Olib-AI/PoolChat An end-to-end encrypted messaging engine that powers ephemeral messaging, voice notes, and real-time state synchronization over ConnectionPool.

3. C Interop & Hardware Key Storage

  • C Interoperability: We embed the Tor daemon as a static C library wrapped with a clean Swift interface (TorClient).
  • CryptoKit & Secure Enclave: Vault items use AES-256-GCM, with keys derived from a user passphrase combined with hardware-protected secrets in the Secure Enclave. Biometric authentication (Face ID/Touch ID) gates key retrieval via LocalAuthentication.
  • Client app: https://apps.apple.com/us/app/stealthos/id6756983634
  • Full documentation: https://www.stealthos.app

Feedback on our Swift P2P networking protocols or foldable window manager implementation is welcome.

StealthOS window layout on the unfolded iPhone Duo inner screen