-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathob-swiftui.el
329 lines (294 loc) · 9.89 KB
/
ob-swiftui.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
;;; ob-swiftui.el --- Org babel functions for SwiftUI evaluation -*- lexical-binding: t; -*-
;; Copyright (C) Alvaro Ramirez
;; Author: Alvaro Ramirez
;; Package-Requires: ((emacs "25.1") (swift-mode "8.2.0") (org "9.2.0"))
;; URL: https://github.com/xenodium/ob-swiftui
;; Version: 0.10
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Run and render SwiftUI blocks using org babel.
;;
;; Install with:
;;
;; (require 'ob-swiftui)
;; (ob-swiftui-setup)
;;
;; Relevant header arguments:
;;
;; `:results' window
;;
;; Runs SwiftUI in a separate window (default and can be omitted).
;;
;; `:results' file
;;
;; Runs SwiftUI in the background and saves an image snapshot to
;; a file.
;;
;; `:view' FooView
;;
;; If `view:' is given, use FooView as the root view. Otherwise,
;; generate a root view and embed source block in body.
;;
;; Examples:
;;
;; Use 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
;;
;; Using your own root view:
;;
;; #+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
;;; Requirements:
;; Depends on `swift-mode' for editing Swift code.
;;; Code:
(require 'ob)
(require 'org)
(require 'swift-mode)
(require 'map)
;; Aliasing enables block syntax highlighting.
(defalias 'swiftui-mode #'swift-mode)
(defvar org-babel-default-header-args:swiftui '((:results . "window")
(:view . "none")
(:file . nil)
(:exports . "results"))
"Default ob-swiftui header args.
Must be named `org-babel-default-header-args:swiftui' to integrate with `ob'.")
(defun org-babel-execute:swiftui (body params)
"Execute a block of SwiftUI code in BODY with org-babel header PARAMS.
This function is called by `org-babel-execute-src-block'"
(message "executing SwiftUI source code block")
(let* ((write-to-file (member "file" (map-elt params :result-params)))
(binary (make-temp-file "ob-swiftui-"))
(source (concat binary ".swift"))
(png-path
(if (map-elt params :file)
(if (functionp (map-elt params :file))
(funcall (map-elt params :file))
(map-elt params :file))
(concat binary ".png")))
(command (format "swiftc %s -o %s && %s" source binary binary))
(output))
(when (and (map-elt params :file)
(not write-to-file))
(user-error "When setting :file, must also use \":results file\""))
(with-temp-buffer
(insert (ob-swiftui--expand-body
body (cons `(:file . ,png-path) params)))
(let ((inhibit-message t))
(write-file source)))
(with-temp-buffer
(shell-command command (current-buffer))
(setq output (string-trim (buffer-string))))
;; Checking for error: string as opposed to exit code
;; as there's a currently a bug in the Swift code
;; preventing exit with 0.
(if (string-match "error:" output)
(cond ((map-elt params :file)
(user-error output))
(t
output))
(cond ((map-elt params :file)
nil)
(t
output)))))
(defun ob-swiftui-setup ()
"Set up babel SwiftUI support."
(add-to-list 'org-babel-tangle-lang-exts '("swiftui" . "swift"))
(org-babel-do-load-languages 'org-babel-load-languages
(append org-babel-load-languages
'((swiftui . t))))
(add-to-list 'org-src-lang-modes '("swiftui" . swift)))
(defun ob-swiftui--expand-body (body params)
"Expand BODY according to PARAMS and PROCESSED-PARAMS, return the expanded body."
(let ((write-to-file (member "file" (map-elt params :result-params)))
(root-view (if (and (map-elt params :view)
(not (string-equal (map-elt params :view) "none")))
(map-elt params :view)
"ContentView"))
(output-file (map-elt params :file)))
(when (and (not (string-match root-view body))
(or (string-match "struct" body)
(string-match "class" body)))
(user-error "Either name one of the views ContentView or specify :view param."))
(if write-to-file
(ob-swiftui--expand-body-preview body root-view output-file)
(ob-swiftui--expand-body-window body root-view))))
(defun ob-swiftui--expand-body-preview (body root-view output-file)
(format
"
import SwiftUI
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { timer in
Task.detached { @MainActor in
let renderer = ImageRenderer(content: %s())
renderer.scale = NSScreen.main?.backingScaleFactor ?? 1.0
let data = renderer.cgImage?.pngData(compressionFactor: 1)
do {
let url = URL(fileURLWithPath: \"%s\")
try data?.write(to: url)
print(url.path)
exit(0)
} catch {
print(\"Error: \\(error.localizedDescription)\")
exit(1)
}
}
}
RunLoop.current.run()
extension CGImage {
func pngData(compressionFactor: Float) -> Data? {
NSBitmapImageRep(cgImage: self).representation(
using: .png, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: compressionFactor])
}
}
// Additional view definitions.
%s
"
root-view
output-file
(if (string-match root-view body)
body
(format "
struct ContentView: View {
var body: some View {
VStack{
%s
}
.frame(maxWidth:.infinity, maxHeight:.infinity)
}
}
" body))))
(defun ob-swiftui--expand-body-window (body root-view)
(format
"
// Swift snippet based on Chris Eidhof's code at:
// https://gist.github.com/chriseidhof/26768f0b63fa3cdf8b46821e099df5ff
import Cocoa
import Foundation
import SwiftUI
extension NSApplication {
public func start() {
let appDelegate = AppDelegate()
NSApp.setActivationPolicy(.regular)
mainMenu = customMenu
delegate = appDelegate
run()
}
}
extension NSApplication {
var customMenu: NSMenu {
let appMenu = NSMenuItem()
appMenu.submenu = NSMenu()
let quitItem = NSMenuItem(
title: \"Quit \(ProcessInfo.processInfo.processName)\",
action: #selector(NSApplication.terminate(_:)), keyEquivalent: \"q\")
quitItem.keyEquivalentModifierMask = []
appMenu.submenu?.addItem(quitItem)
let mainMenu = NSMenu(title: \"Main Menu\")
mainMenu.addItem(appMenu)
return mainMenu
}
}
let settingsFilePath = NSHomeDirectory() + \"/.ob-swiftui.plist\"
let frameUserDefaultsKey = \"ob.swiftui.frameUserDefaultsKey\"
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
var window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 200, height: 200),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
var contentView = %s()
func applicationDidFinishLaunching(_ notification: Notification) {
window.delegate = self
window.center()
window.contentView = NSHostingView(rootView: contentView)
window.title = \"press \\\"q\\\" to exit\"
// Can't use window.setFrameAutosaveName since the binary name is
// generated for every execution, this in a different namespace.
if let data = try? Data(contentsOf: URL(fileURLWithPath: settingsFilePath)),
let rect = try? PropertyListDecoder().decode(NSRect.self, from: data) {
window.setFrame(rect, display: true)
}
window.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
return NSSize(width: max(frameSize.width, 200), height: max(frameSize.height, 200))
}
func windowWillClose(_ notification: Notification) {
let encoder = PropertyListEncoder()
guard let data = try? encoder.encode(window.frame) else {
print(\"Warning: Could not encode frame details\")
return
}
try? data.write(to: URL(fileURLWithPath: settingsFilePath))
}
}
NSApplication.shared.start()
// Additional view definitions.
%s
"
root-view
(if (string-match root-view body)
body
(format "
struct ContentView: View {
var body: some View {
VStack{
%s
}
.frame(maxWidth:.infinity, maxHeight:.infinity)
}
}
" body))))
(provide 'ob-swiftui)
;;; ob-swiftui.el ends here