-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
Changes from 5 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
9313bf1
Add ColorScheme environment
MaxDesiatov ce432b7
Fix wrong colorScheme computed property type
MaxDesiatov a3c69b6
Add DOMEnvironment view with colorScheme state
MaxDesiatov 1776434
Fix linter line length warning
MaxDesiatov af498ac
Use onAppear/onDisappear in DOMEnvironment
MaxDesiatov 26f7ef5
Merge branch 'main' of github.com:maxdesiatov/Tokamak into color-scheme
MaxDesiatov f792a9f
Generalize environment updates
MaxDesiatov 855a438
Environment propagation fix in progress
MaxDesiatov a59f9bc
Button styles work
diskzero e9a94c1
Button style work
SemitoneGene 95db246
Add basic `ButtonStyle` implementation
MaxDesiatov 8962950
Fix pointer events
MaxDesiatov ab46418
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov c8b0f05
Fix compilation errors after merge
MaxDesiatov af6d2db
Fix build error on macOS
MaxDesiatov d0dca18
Fix linter warning
MaxDesiatov 77ef826
Fix super.update call crashes
MaxDesiatov cbd3e2c
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov 938d951
Add `DefaultApp` type to simplify DOMRenderer.init
MaxDesiatov 74b4c66
Merge branch 'domrenderer-defaultapp' of github.com:swiftwasm/Tokamak…
MaxDesiatov 22accdb
Add ColorSchemeObserver, App._colorSchemePublisher
MaxDesiatov c08b40d
Hook `colorScheme` observer to the environment
MaxDesiatov 58c45c7
Fix build errors
MaxDesiatov 94432a5
Remove redundant print statements
MaxDesiatov 921f2c5
Merge branch 'main' of github.com:swiftwasm/Tokamak into buttonstyles
MaxDesiatov 8acdf2a
Fix host view environment propagation
MaxDesiatov 5b437e9
Fix unused result warnings
MaxDesiatov f0cf159
Unify environment updates in MountedElement.init
MaxDesiatov 62de63a
Set appearance to `none` for non-default styles
MaxDesiatov 5f275e1
Tweak button style reset CSS
MaxDesiatov fe183c3
Merge branch 'buttonstyles' of github.com:swiftwasm/Tokamak into colo…
MaxDesiatov 20a0e82
Assorted cleanups to reduce the diff
MaxDesiatov feb9d18
Retain observer's `JSClosure`
MaxDesiatov edb5e70
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov e8044db
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov 61632b8
Use `import CombineShim` in the observers code
MaxDesiatov 08841e7
Merge branch 'main' of github.com:swiftwasm/Tokamak into color-scheme
MaxDesiatov c397eb6
Fix environment updates propagation to children
MaxDesiatov 8dfaf78
Update EnvironmentDemo.swift
MaxDesiatov 88f7e36
Address PR feedback
MaxDesiatov b3ad0e2
Revert `TokamakStaticHTML` changes
MaxDesiatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 withwindow.matchMedia('(prefers-color-scheme: dark)')
and add an event listener to update this value on change.There was a problem hiding this comment.
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 newDOMEnvironment
view introduced in this PR, but this obviously needs a workingonAppear
implementation first, which is what I'm working on right now 🙂