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

Add SwiftUI App Lifecycle #195

Merged
merged 16 commits into from
Jul 22, 2020
Merged

Add SwiftUI App Lifecycle #195

merged 16 commits into from
Jul 22, 2020

Conversation

carson-katri
Copy link
Member

This PR adds the App protocol, as well as Scenes, modifiers, and changes to the renderer to make building apps entirely with Tokamak simpler:

struct MyApp: App {
  var body: some Scene {
    WindowGroup("Tokamak") {
      Text("Hello, world!")
    }
  }
}

MyApp.main()

I've also implemented @AppStorage and @SceneStorage with the ability for renderers to override the default containers:

@AppStorage("count") var count: Int = 0 // Persists/updates across tabs.
@SceneStorage("count") var sceneCount: Int = 0  // Persists across single-tab reloads.

AppStorage will automatically update all tabs when one changes the value. SceneStorage will automatically be loaded on page-load as well as when any View in the app updates it. TokamakDOM uses localStorage and sessionStorage.

This PR also contains the ScenePhase environment value:

@Environment(\.scenePhase) var phase: ScenePhase
  • .active: Tab is active in the window of the browser.
  • .background: Tab is open, but not active.

This automatically updates with the PageVisibility API.

@ie-ahm-robox

This comment has been minimized.

@carson-katri carson-katri added the SwiftUI compatibility Tokamak API differences with SwiftUI label Jul 20, 2020
Sources/TokamakCore/App/App.swift Show resolved Hide resolved
Sources/TokamakCore/App/AppStorage.swift Outdated Show resolved Hide resolved
Sources/TokamakCore/App/Scenes/Scene.swift Outdated Show resolved Hide resolved
private enum ScenePhaseObserver {
static var publisher = CurrentValueSubject<ScenePhase, Never>(.active)

static func observe() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another cue to look for here is document.hasFocus() — if it’s false but document.visiblityState is visible, the page is .inactive. I couldn’t find out if there were any events that trigger when this changes, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem like it looking through MDN. If you find something lmk.

@MaxDesiatov
Copy link
Collaborator

MaxDesiatov commented Jul 21, 2020

Turns out this requires latest carton to work (because of JavaScriptKit runtime fix), which kind of pushes me towards releasing Tokamak 0.2.0 and carton 0.4.0 without this PR...

@carson-katri carson-katri requested review from MaxDesiatov, ie-ahm-robox and j-f1 and removed request for ie-ahm-robox July 21, 2020 18:59
@MaxDesiatov
Copy link
Collaborator

Is there anything in the lifecycle API that isn't covered by this PR? I'm just wondering if this should be added to the progress document

@carson-katri carson-katri requested review from j-f1 and MaxDesiatov July 22, 2020 20:28
@TokamakUI TokamakUI deleted a comment from ie-ahm-robox Jul 22, 2020
@TokamakUI TokamakUI deleted a comment from ie-ahm-robox Jul 22, 2020
@carson-katri
Copy link
Member Author

Custom scenes are not yet supported. We could support these in a future PR IMO:

struct TokamakDemoScene : Scene {
  var body: some Scene {
    WindowGroup { ... }
  }
}

@TokamakUI TokamakUI deleted a comment from ie-ahm-robox Jul 22, 2020
Copy link
Collaborator

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! 👏

@carson-katri carson-katri merged commit 2b93f37 into main Jul 22, 2020
@carson-katri carson-katri deleted the app-protocol branch July 22, 2020 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SwiftUI compatibility Tokamak API differences with SwiftUI
Development

Successfully merging this pull request may close these issues.

4 participants