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 ColorScheme environment #136

Merged
merged 41 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9313bf1
Add ColorScheme environment
MaxDesiatov Jun 30, 2020
ce432b7
Fix wrong colorScheme computed property type
MaxDesiatov Jun 30, 2020
a3c69b6
Add DOMEnvironment view with colorScheme state
MaxDesiatov Jul 1, 2020
1776434
Fix linter line length warning
MaxDesiatov Jul 1, 2020
af498ac
Use onAppear/onDisappear in DOMEnvironment
MaxDesiatov Jul 2, 2020
26f7ef5
Merge branch 'main' of github.com:maxdesiatov/Tokamak into color-scheme
MaxDesiatov Jul 21, 2020
f792a9f
Generalize environment updates
MaxDesiatov Jul 21, 2020
855a438
Environment propagation fix in progress
MaxDesiatov Jul 21, 2020
a59f9bc
Button styles work
diskzero Jul 24, 2020
e9a94c1
Button style work
SemitoneGene Jul 24, 2020
95db246
Add basic `ButtonStyle` implementation
MaxDesiatov Jul 25, 2020
8962950
Fix pointer events
MaxDesiatov Jul 26, 2020
ab46418
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov Jul 26, 2020
c8b0f05
Fix compilation errors after merge
MaxDesiatov Jul 26, 2020
af6d2db
Fix build error on macOS
MaxDesiatov Jul 26, 2020
d0dca18
Fix linter warning
MaxDesiatov Jul 26, 2020
77ef826
Fix super.update call crashes
MaxDesiatov Jul 26, 2020
cbd3e2c
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov Jul 27, 2020
938d951
Add `DefaultApp` type to simplify DOMRenderer.init
MaxDesiatov Jul 27, 2020
74b4c66
Merge branch 'domrenderer-defaultapp' of github.com:swiftwasm/Tokamak…
MaxDesiatov Jul 27, 2020
22accdb
Add ColorSchemeObserver, App._colorSchemePublisher
MaxDesiatov Jul 27, 2020
c08b40d
Hook `colorScheme` observer to the environment
MaxDesiatov Jul 27, 2020
58c45c7
Fix build errors
MaxDesiatov Jul 27, 2020
94432a5
Remove redundant print statements
MaxDesiatov Jul 27, 2020
921f2c5
Merge branch 'main' of github.com:swiftwasm/Tokamak into buttonstyles
MaxDesiatov Jul 29, 2020
8acdf2a
Fix host view environment propagation
MaxDesiatov Jul 29, 2020
5b437e9
Fix unused result warnings
MaxDesiatov Jul 29, 2020
f0cf159
Unify environment updates in MountedElement.init
MaxDesiatov Jul 30, 2020
62de63a
Set appearance to `none` for non-default styles
MaxDesiatov Jul 30, 2020
5f275e1
Tweak button style reset CSS
MaxDesiatov Jul 30, 2020
fe183c3
Merge branch 'buttonstyles' of github.com:swiftwasm/Tokamak into colo…
MaxDesiatov Jul 30, 2020
20a0e82
Assorted cleanups to reduce the diff
MaxDesiatov Jul 30, 2020
feb9d18
Retain observer's `JSClosure`
MaxDesiatov Jul 30, 2020
edb5e70
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov Aug 1, 2020
e8044db
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov Aug 1, 2020
61632b8
Use `import CombineShim` in the observers code
MaxDesiatov Aug 1, 2020
08841e7
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov Aug 1, 2020
c397eb6
Fix environment updates propagation to children
MaxDesiatov Aug 1, 2020
8dfaf78
Update EnvironmentDemo.swift
MaxDesiatov Aug 1, 2020
88f7e36
Address PR feedback
MaxDesiatov Aug 2, 2020
b3ad0e2
Revert `TokamakStaticHTML` changes
MaxDesiatov Aug 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Sources/TokamakCore/Tokens/ColorScheme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

public enum ColorScheme: CaseIterable {
case dark
case light
}

struct ColorSchemeKey: EnvironmentKey {
static let defaultValue: ColorScheme = .light
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible for this to match the value of the (prefers-color-scheme: dark) media query? You could create the query with window.matchMedia('(prefers-color-scheme: dark)') and add an event listener to update this value on change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's what I'm working right now. I think it would make sense to add the listener in an onAppear closure on the new DOMEnvironment view introduced in this PR, but this obviously needs a working onAppear implementation first, which is what I'm working on right now 🙂

}

public extension EnvironmentValues {
var colorScheme: ColorScheme {
get {
self[ColorSchemeKey.self]
}
set {
self[ColorSchemeKey.self] = newValue
}
}
}

public extension View {
func colorScheme(_ colorScheme: ColorScheme) -> some View {
environment(\.colorScheme, colorScheme)
}
}
2 changes: 1 addition & 1 deletion Sources/TokamakCore/Views/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protocol GroupView: ParentView {}
views is made in the reconciler in `TokamakCore` based on their `body` type, host views have body
type `Never`. `ViewDeferredToRenderer` allows renderers to override that per-platform and render
host views as composite by providing their own `deferredBody` implementation.
*/
*/
public protocol ViewDeferredToRenderer {
var deferredBody: AnyView { get }
}
Expand Down
8 changes: 5 additions & 3 deletions Sources/TokamakDOM/DOMRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ public final class DOMRenderer: Renderer {

public init<V: View>(_ view: V, _ ref: JSObjectRef) {
rootRef = ref
rootRef.style = "display: flex; width: 100%; height: 100%; justify-content: center; align-items: center; overflow: hidden;"
rootRef.style = .string(rootNodeStyles)

let rootStyle = document.createElement!("style").object!
rootStyle.innerHTML = .string(tokamakStyles)
_ = head.appendChild!(rootStyle)

let environmentView = DOMEnvironment(scheme: .light, content: view)

reconciler = StackReconciler(
view: view,
target: DOMNode(view, ref),
view: environmentView,
target: DOMNode(environmentView, ref),
renderer: self
) { closure in
let fn = JSClosure { _ in
Expand Down
44 changes: 44 additions & 0 deletions Sources/TokamakDOM/Environment/DOMEnvironment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import JavaScriptKit
import TokamakCore

/// This is effectively a singleton, but a mounted `DOMEnvironment` is assumed to be a singleton
/// too. It can't be declared as a property of `DOMEnvironment` as you can't modify it from within
/// `body` callbacks.
private var colorSchemeListener: JSClosure?
j-f1 marked this conversation as resolved.
Show resolved Hide resolved

struct DOMEnvironment<V: View>: View {
@State var scheme: ColorScheme

let content: V

private let matchMedia =
JSObjectRef.global.window.object!.matchMedia!("(prefers-color-scheme: dark)").object!

var body: some View {
content
.colorScheme(scheme)
.onAppear {
colorSchemeListener = JSClosure {
scheme = $0[0].object!.matches.boolean == true ? .dark : .light
return .undefined
}
_ = matchMedia.addEventListener!("change", colorSchemeListener!)
}.onDisappear {
_ = matchMedia.removeEventListener!("change", colorSchemeListener!)
}
}
}
9 changes: 9 additions & 0 deletions Sources/TokamakDOM/Resources/TokamakStyles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ let tokamakStyles = """
height: 0;
}
"""

let rootNodeStyles = """
display: flex;
width: 100%;
height: 100%;
justify-content: center;
align-items: center;
overflow: hidden;
"""
1 change: 1 addition & 0 deletions Sources/TokamakDOM/Tokens/Tokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TokamakCore

public typealias Font = TokamakCore.Font
public typealias Color = TokamakCore.Color
public typealias ColorScheme = TokamakCore.ColorScheme

extension Color: CustomStringConvertible {
public var description: String {
Expand Down
6 changes: 4 additions & 2 deletions Sources/TokamakDemo/EnvironmentDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import TokamakDOM

struct EnvironmentDemo: View {
@Environment(\.colorScheme) var scheme: ColorScheme

@Environment(\.font) var font: Font?

var body: some View {
if let font = font {
return Text("\(font)")
return Text("ColorScheme is \(scheme), font is \(font)")
} else {
return Text("`font` environment not set.")
return Text("ColorScheme is \(scheme), `font` environment not set.")
}
}
}