Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support LiveView.swift #47

Open
macteo opened this issue Nov 9, 2016 · 6 comments
Open

Support LiveView.swift #47

macteo opened this issue Nov 9, 2016 · 6 comments

Comments

@macteo
Copy link

macteo commented Nov 9, 2016

In the sample code provided by Apple called TalkingToTheLiveView, for each page there is a LiveView.swift file that contains few lines of code

import PlaygroundSupport
let page = PlaygroundPage.current
page.liveView = FaceViewController()

Those files are located besides Contents.swift inside each .playgroundpage folder.

live

I find them very useful if the reader shouldn't be able to mess with most of the code of the Live View, but instead just interact with it through the PlaygroundSupport Module.

I don't have specific tests but probably the LiveView.swift file won't be recompiled each time the editor is updated, to achieve optimal performances.

@ashfurrow
Copy link
Member

Good idea! We should totally put this in our library. We will probably want to wait for #34 to be wrapped up first before working on this 👍

@macteo
Copy link
Author

macteo commented Nov 9, 2016

Using the LiveView.swift file seems the only way to have the code rendered immediately when the page is opened:

An optional file used to specify an always-on live view. The code in this file is executed when the page is opened.

Unfortunately adding the LiveView.swift file and moving there the code responsible for the rendering, makes the playground incompatible with Xcode on macOS.

Probably the best way would be to wrap the code you want to move to LiveView.swift between tags inside the standard Contents.swift

//#-live-view-code
import PlaygroundSupport
let page = PlaygroundPage.current
page.liveView = FaceViewController()
//#-end-live-view-code

then let the gem parse the file, extract the code between those tags (and remove it from Contents.swift), create the LiveView.swift file and write it there.
This way we can keep the playground compatible with macOS and Swift Playground.app on iOS once converted.

What do you think?

@macteo
Copy link
Author

macteo commented Nov 11, 2016

In the meantime I've implemented the aforementioned solution. I've started from #34 as suggested, so before posting the pull-request it would make sense to merge the other one. I still need to add some tests.

IMHO, based on the preview of a small playgroundbook I'm writing on Bézier curves, the experience is improved a lot when the Live View is rendered immediately on page load.

@ashfurrow
Copy link
Member

This makes sense to me 👍 Thanks a lot!

@ryanoconnor7
Copy link

Just taking a quick looks at this, do you think that there is a way to always move a LiveView.swift file to the right place if it's present? For example, I can create a LiveView.swift file in the specific sources folder for a page in Xcode:
screen shot 2017-04-03 at 4 56 28 pm
And the desired location in our .playgroundbook bundle is here:
screen shot 2017-04-03 at 4 58 00 pm

I think that'd be the best way to go about doing this.

@macteo
Copy link
Author

macteo commented Apr 4, 2017

My proposed implementation was intended let you have a working playground both on macOS and iOS.

However it would be only a partial solution as if you want leverage the completely separate LiveView process on Swift Playgrounds on iPad, I don't think there's a solution suitable for both platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants