-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
GeometryReader
implementation (#239)
This is just an empty API at the moment. I hope it can be implemented purely in the `deferredBody` of `GeometryReader` with [the ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) without requiring any tweaks in the `Renderer` protocol or the reconciler. Seems like I need the `domRef` modifier that writes `JSObjectRef` to a given binding working first, as discussed in #231.
- Loading branch information
1 parent
c43d2db
commit b7434a2
Showing
13 changed files
with
228 additions
and
11 deletions.
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
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,24 @@ | ||
// 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. | ||
|
||
// FIXME: these should have standalone implementations | ||
extension View { | ||
public func _onMount(perform action: (() -> ())? = nil) -> some View { | ||
modifier(_AppearanceActionModifier(appear: action)) | ||
} | ||
|
||
public func _onUnmount(perform action: (() -> ())? = nil) -> some View { | ||
modifier(_AppearanceActionModifier(disappear: action)) | ||
} | ||
} |
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,15 @@ | ||
// 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 typealias StateObject = ObservedObject |
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,52 @@ | ||
// 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 struct GeometryProxy { | ||
public let size: CGSize | ||
} | ||
|
||
public func makeProxy(from size: CGSize) -> GeometryProxy { | ||
.init(size: size) | ||
} | ||
|
||
// FIXME: to be implemented | ||
// public enum CoordinateSpace { | ||
// case global | ||
// case local | ||
// case named(AnyHashable) | ||
// } | ||
|
||
// public struct Anchor<Value> { | ||
// let box: AnchorValueBoxBase<Value> | ||
// public struct Source { | ||
// private var box: AnchorBoxBase<Value> | ||
// } | ||
// } | ||
|
||
// extension GeometryProxy { | ||
// public let safeAreaInsets: EdgeInsets | ||
// public func frame(in coordinateSpace: CoordinateSpace) -> CGRect | ||
// public subscript<T>(anchor: Anchor<T>) -> T {} | ||
// } | ||
|
||
public struct GeometryReader<Content>: View where Content: View { | ||
public let content: (GeometryProxy) -> Content | ||
public init(@ViewBuilder content: @escaping (GeometryProxy) -> Content) { | ||
self.content = content | ||
} | ||
|
||
public var body: Never { | ||
neverBody("GeometryReader") | ||
} | ||
} |
File renamed without changes.
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,80 @@ | ||
// 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 | ||
import TokamakStaticHTML | ||
|
||
private let ResizeObserver = JSObjectRef.global.ResizeObserver.function! | ||
|
||
extension GeometryReader: ViewDeferredToRenderer { | ||
public var deferredBody: AnyView { | ||
AnyView(_GeometryReader(content: content)) | ||
} | ||
} | ||
|
||
struct _GeometryReader<Content: View>: View { | ||
final class State: ObservableObject { | ||
/** Holds a strong reference to a `JSClosure` instance that has to stay alive as long as | ||
the `_GeometryReader` owner is alive. | ||
*/ | ||
var closure: JSClosure? | ||
|
||
/// A reference to a DOM node being observed for size updates. | ||
var observedNodeRef: JSObjectRef? | ||
|
||
/// A reference to a `ResizeObserver` instance. | ||
var observerRef: JSObjectRef? | ||
|
||
/// The last known size of the `observedNodeRef` DOM node. | ||
@Published var size: CGSize? | ||
} | ||
|
||
let content: (GeometryProxy) -> Content | ||
|
||
@StateObject private var state = State() | ||
|
||
var body: some View { | ||
HTML("div", ["class": "_tokamak-geometryreader"]) { | ||
if let size = state.size { | ||
content(makeProxy(from: size)) | ||
} else { | ||
EmptyView() | ||
} | ||
} | ||
._domRef($state.observedNodeRef) | ||
._onMount { | ||
let closure = JSClosure { [weak state] args in | ||
// FIXME: `JSArrayRef` is not a `RandomAccessCollection` for some reason, which forces | ||
// us to use a string subscript | ||
guard | ||
let rect = args[0].object?[dynamicMember: "0"].object?.contentRect.object, | ||
let width = rect.width.number, | ||
let height = rect.height.number | ||
else { return .undefined } | ||
|
||
state?.size = .init(width: width, height: height) | ||
|
||
return .undefined | ||
} | ||
state.closure = closure | ||
|
||
let observerRef = ResizeObserver.new(closure) | ||
|
||
_ = observerRef.observe!(state.observedNodeRef!) | ||
|
||
state.observerRef = observerRef | ||
} | ||
} | ||
} |
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,23 @@ | ||
// 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 TokamakShim | ||
|
||
struct GeometryReaderDemo: View { | ||
var body: some View { | ||
GeometryReader { | ||
Text("\(String(describing: $0.size))") | ||
} | ||
} | ||
} |
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