| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- extension View {
- /// Adds an action to perform when the user's pointer enters/leaves this
- /// view.
- ///
- /// - Parameter action: The action to perform when the hover state changes.
- /// Receives a `Bool` parameter indicated whether the pointer entered or
- /// left the view.
- public func onHover(
- perform action: @escaping (_ hovering: Bool) -> Void
- ) -> some View {
- OnHoverModifier(body: TupleView1(self), action: action)
- }
- }
- struct OnHoverModifier<Content: View>: TypeSafeView {
- typealias Children = TupleView1<Content>.Children
- var body: TupleView1<Content>
- var action: (Bool) -> Void
- func children<Backend: BaseAppBackend>(
- backend: Backend,
- snapshots: [ViewGraphSnapshotter.NodeSnapshot]?,
- environment: EnvironmentValues
- ) -> Children {
- body.children(
- backend: backend,
- snapshots: snapshots,
- environment: environment
- )
- }
- func asWidget<Backend: BaseAppBackend>(
- _ children: Children,
- backend: Backend
- ) -> Backend.Widget {
- func castBackend_inner<NewBackend: BaseAppBackend & BackendFeatures.HoverGestures>(_ backend: NewBackend) -> NewBackend.Widget {
- return backend.createHoverTarget(wrapping: children.child0.widget.into())
- }
- guard let castedBackend = backend as? any (BaseAppBackend & BackendFeatures.HoverGestures) else {
- fatalError("Backend does not implement BackendFeatures.HoverGestures")
- }
- return castBackend_inner(castedBackend) as! Backend.Widget
- }
- func computeLayout<Backend: BaseAppBackend>(
- _ widget: Backend.Widget,
- children: Children,
- proposedSize: ProposedViewSize,
- environment: EnvironmentValues,
- backend: Backend
- ) -> ViewLayoutResult {
- children.child0.computeLayout(
- with: body.view0,
- proposedSize: proposedSize,
- environment: environment
- )
- }
- func commit<Backend: BaseAppBackend>(
- _ widget: Backend.Widget,
- children: Children,
- layout: ViewLayoutResult,
- environment: EnvironmentValues,
- backend: Backend
- ) {
- func castBackend_inner<NewBackend: BaseAppBackend & BackendFeatures.HoverGestures>(_ backend: NewBackend) {
- let widget = widget as! NewBackend.Widget
- let size = children.child0.commit().size.vector
- backend.setSize(of: widget, to: size)
- backend.updateHoverTarget(
- widget,
- environment: environment,
- action: action
- )
- }
- guard let castedBackend = backend as? any (BaseAppBackend & BackendFeatures.HoverGestures) else {
- fatalError("Backend does not implement BackendFeatures.HoverGestures")
- }
- castBackend_inner(castedBackend)
- }
- }
|