index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

20 April 2023 ob-swiftui updates

While experimenting with delegating Markdown blocks to Org babel in Emacs chatgpt-shell, I resurrected ob-swiftui. A package I had written to execute and render SwiftUI blocks in org babel.

ob-swiftui has two modes of rendering SwiftUI blocks: :results window, which runs outside of Emacs in a native window and :results file, which renders and saves to a file. The latter can be viewed directly from Emacs.

:results file was a little clunky. That is, it hardcoded dimensions I had to manually modify if the canvas wasn't big enough. It was also a little slow.

The clunkyness really came through with my chatgpt-shell experiments, so I took a closer look and made a few changes to remove hardcoding and speeds things up.

The results ain't too shabby.

file-render.gif

Another tiny improvement is that if you'd like to compose a more complex layout made of multiple custom views, ob-swiftui now looks for a ContentView as that root view by default. Specifying another root view was already possible but it had to be explicitly requested via :view param.

You can now omit the :view param if you name the root view ContentView:

#+begin_src swiftui
  struct ContentView: View {
    var body: some View {
        TopView()
        BottomView()
    }
  }

  struct TopView: View {
    var body: some View {
      Text("Top text")
    }
  }

  struct BottomView: View {
    var body: some View {
      Text("Bottom text")
    }
  }
#+end_src

The improvements have been pushed to ob-swiftui and will soon be picked up on melpa.

Edit: Added ContentView details.