Coder Social home page Coder Social logo

ob-swiftui's Introduction

👉 Support this work via GitHub Sponsors

ob-swiftui

UPDATE: See blog post for latest changes.

Evaluate SwiftUI snippets using Emacs org babel.

Big thanks to Chris Eidhof, who tweeted a snippet to run any SwiftUI view as a macOS app. Much of ob-swiftui’s Swift code is based on that snippet as well as some additional tweaks he made (like writing to png).

ob-swiftui.gif

Install

(require 'ob-swiftui)
(ob-swiftui-setup)

Header arguments

:results window

Runs SwiftUI in a separate window (default and can be omitted).

Use a generated root view and render in external window (default):

#+begin_src swiftui
  Rectangle()
    .fill(Color.yellow)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
#+end_src

is equivalent to:

#+begin_src swiftui :results window :view none
  Rectangle()
    .fill(Color.yellow)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
#+end_src

:view FooView

If view: is given, use FooView as the root view. Otherwise, generate a root view and embed source block in view body.

#+begin_src swiftui :results window :view FooView
  struct FooView: View {
    var body: some View {
      VStack(spacing: 10) {
        BarView()
        BazView()
      }
    }
  }

  struct BarView: View {
    var body: some View {
      Rectangle()
        .fill(Color.yellow)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
  }

  struct BazView: View {
    var body: some View {
      Rectangle()
        .fill(Color.blue)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
  }
#+end_src

Note: Defining a view named ContentView, implies view: ContentView

:results file

Runs SwiftUI in the background and saves an image snapshot to a file. You can save the image to the location specified by the :file header argument, otherwise, it will be saved to a temporary location if :file is omitted. Please note that currently, png is the only supported image format.

#+begin_src swiftui :results file
  Rectangle()
    .fill(Color.yellow)
    .frame(maxWidth: .infinity, maxHeight: .infinity)
#+end_src

Auto refresh :results file image

If executing the block does not refresh the inlined image, try adding org-redisplay-inline-images in a hook. org-babel-after-execute-hook

(add-hook 'org-babel-after-execute-hook (lambda ()
                                          (when org-inline-image-overlays
                                            (org-redisplay-inline-images))))

Display retina image

For retina (high-resolution) displays, if you found the output image is too large, you can instruct Org to scale the image to 0.5 to display in its native size:

(advice-add 'org--create-inline-image :filter-return
            (defun org--create-inline-image/filter-return (x)
              (setcdr x (plist-put (cdr x) :scale 0.5))
              x))

👉 Support my work via GitHub Sponsors

ob-swiftui's People

Contributors

cxa avatar xenodium avatar

Stargazers

Rusty avatar Jeoffrey Thirot avatar Sigurd Fosseng avatar  avatar Bing.L. avatar Samuel avatar Michael R. Kirchner avatar dengwww avatar roife avatar Mark Olson avatar shg avatar  avatar s i j avatar  avatar Marco Craveiro avatar FCP avatar stardiviner avatar tomyli avatar Tornike Gomareli avatar  avatar Ivan Lugo avatar  avatar Gene De Lisa avatar Delon R. Newman avatar Wing Kwok avatar Charles Gardner avatar Skye Freeman avatar Brad Svercl avatar anyinfa avatar  avatar Bastien avatar Min-Ki Park avatar 布衣男儿 avatar Justin Smestad avatar Spencer Schoeben avatar Christian Tietze avatar samuca avatar Jeremy Fleshman avatar Charles Choi avatar Jay Lee avatar  avatar Mikael Konradsson avatar Takuro Ito avatar Federico Zanetello avatar Angel Peralta avatar Chris avatar Kilian Koeltzsch avatar Helge Heß avatar Oliver Epper avatar Shawn Koh avatar Luis Osa avatar Will B Chang avatar Shohi Wang avatar Feraidoon Mehri avatar Matteo Pacini avatar  avatar Jessica Claire Edwards avatar Owain West avatar Bror avatar Colin McLear avatar  avatar Aaron Madlon-Kay avatar Eude Kinsley Lesperance avatar Dylan Garrett avatar Joel Boehland avatar Rafael Bugajewski avatar Theo avatar  avatar Evo Stamatov avatar Scott Nicholes avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

emacsmirror cxa

ob-swiftui's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.