Files
ghostgram/submodules/Display/Source/ContextContentSourceNode.swift
T
ichmagmaus111andClaude Opus 5.5 534cabe35b bug fixes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 16:55:08 +01:00

241 lines
8.7 KiB
Swift

import Foundation
import UIKit
import AsyncDisplayKit
private var morphsIntoContextMenuKey: UInt8 = 0
public extension UIView {
/// Opts this view in to UIKit's liquid morph (iOS 26+) when it is the reference
/// view of a context menu; every other source keeps the standard presentation.
/// For now only navigation-header glass capsules holding a single button set it.
var morphsIntoContextMenu: Bool {
get {
return (objc_getAssociatedObject(self, &morphsIntoContextMenuKey) as? Bool) ?? false
}
set {
objc_setAssociatedObject(self, &morphsIntoContextMenuKey, newValue ? true : nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
open class ContextReferenceContentNode: ASDisplayNode {
/// Source decorations rendered outside this node (for example, a shared blur mask).
public var additionalContextMenuSourceViews: [UIView] = []
/// Visible source outline in this node's bounds, including separately drawn backdrops.
public var contextMenuSourcePath: UIBezierPath?
/// Supplies a live foreground/backdrop container for sources with a shared background.
public var makeContextMenuSourceContent: (() -> ContextMenuSourceContent?)?
public static func makeSourceContent(for view: UIView) -> (() -> ContextMenuSourceContent?)? {
return (view.asyncdisplaykit_node as? ContextReferenceContentNode)?.makeContextMenuSourceContent
}
public static func sourcePath(for view: UIView) -> UIBezierPath? {
return (view.asyncdisplaykit_node as? ContextReferenceContentNode)?.contextMenuSourcePath
}
public static func additionalSourceViews(for view: UIView) -> [UIView] {
return (view.asyncdisplaykit_node as? ContextReferenceContentNode)?.additionalContextMenuSourceViews ?? []
}
override public init() {
super.init()
}
}
public final class ContextExtractedContentContainingNode: ASDisplayNode {
public let contentNode: ContextExtractedContentNode
public var contentRect: CGRect = CGRect()
public var isExtractedToContextPreview: Bool = false
public var willUpdateIsExtractedToContextPreview: ((Bool, ContainedViewLayoutTransition) -> Void)?
public var isExtractedToContextPreviewUpdated: ((Bool) -> Void)?
public var updateAbsoluteRect: ((CGRect, CGSize) -> Void)?
public var layoutUpdated: ((CGSize, ListViewItemUpdateAnimation) -> Void)?
public var updateDistractionFreeMode: ((Bool) -> Void)?
public var requestDismiss: (() -> Void)?
public var onDismiss: (() -> Void)?
public override init() {
self.contentNode = ContextExtractedContentNode()
super.init()
self.addSubnode(self.contentNode)
}
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.contentNode.supernode === self {
return self.contentNode.hitTest(self.view.convert(point, to: self.contentNode.view), with: event)
} else {
return nil
}
}
/// `rect`, in the coordinate space of `view` (a view inside `contentNode`), converted to
/// `targetView` (a view outside it) as if the content had not been extracted.
///
/// While the content is in a context preview, `contentNode` lives in the context controller's
/// hierarchy, and a direct `UIView.convert` from inside it to a view outside it returns
/// `CGRect.null` or a rect in the wrong place. The content keeps its own coordinate space while
/// extracted, so the conversion goes through it. Precondition: the owner lays the content out at
/// this node's bounds (`contentNode.frame == bounds`) while it is in place.
public func convertFromContent(_ rect: CGRect, in view: UIView, to targetView: UIView) -> CGRect {
if self.contentNode.view.superview === self.view {
return view.convert(rect, to: targetView)
}
let contentRect = view.convert(rect, to: self.contentNode.view)
return self.view.convert(contentRect, to: targetView)
}
}
public enum ContextExtractableContainerState {
public enum ExtractionState {
case animatedOut
case animatedIn
}
case normal
case extracted(size: CGSize, cornerRadius: CGFloat, state: ExtractionState)
}
public struct ContextExtractableContainerNormalState {
public let size: CGSize
public let cornerRadius: CGFloat
public init(size: CGSize, cornerRadius: CGFloat) {
self.size = size
self.cornerRadius = cornerRadius
}
}
public enum ContextExtractableContainerTransition {
case transition(ContainedViewLayoutTransition)
case spring(duration: Double, stiffness: CGFloat, damping: CGFloat)
}
public protocol ContextExtractableContainer: UIView {
typealias State = ContextExtractableContainerState
typealias NormalState = ContextExtractableContainerNormalState
typealias Transition = ContextExtractableContainerTransition
var normalState: NormalState { get }
var extractableContentView: UIView { get }
func updateState(state: State, transition: Transition, completion: ((Bool) -> Void)?)
}
public final class ContextExtractedContentContainingView: UIView {
public let contentView: ContextExtractedContentView
public var contentRect: CGRect = CGRect()
public var isExtractedToContextPreview: Bool = false
public var willUpdateIsExtractedToContextPreview: ((Bool, ContainedViewLayoutTransition) -> Void)?
public var isExtractedToContextPreviewUpdated: ((Bool) -> Void)?
public var updateAbsoluteRect: ((CGRect, CGSize) -> Void)?
public var layoutUpdated: ((CGSize, ListViewItemUpdateAnimation) -> Void)?
public var updateDistractionFreeMode: ((Bool) -> Void)?
public var requestDismiss: (() -> Void)?
public var onDismiss: (() -> Void)?
public override init(frame: CGRect) {
self.contentView = ContextExtractedContentView()
super.init(frame: frame)
self.addSubview(self.contentView)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.contentView.superview === self {
return self.contentView.hitTest(self.convert(point, to: self.contentView), with: event)
} else {
return nil
}
}
}
public final class ContextExtractedContentNode: ASDisplayNode {
private var viewImpl: ContextExtractedContentView {
return self.view as! ContextExtractedContentView
}
public var customHitTest: ((CGPoint) -> UIView?)? {
didSet {
if self.isNodeLoaded {
self.viewImpl.customHitTest = self.customHitTest
}
}
}
override public init() {
super.init()
self.setViewBlock {
return ContextExtractedContentView(frame: CGRect())
}
}
}
public final class ContextExtractedContentView: UIView {
public var customHitTest: ((CGPoint) -> UIView?)?
override public init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let result = super.hitTest(point, with: event)
if result === self {
return nil
} else {
return result
}
}
}
public final class ContextControllerContentNode: ASDisplayNode {
public let sourceView: UIView
public let controller: ViewController
private let tapped: () -> Void
public init(sourceView: UIView, controller: ViewController, tapped: @escaping () -> Void) {
self.sourceView = sourceView
self.controller = controller
self.tapped = tapped
super.init()
self.addSubnode(controller.displayNode)
}
override public func didLoad() {
super.didLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapGesture(_:))))
}
@objc private func tapGesture(_ recognizer: UITapGestureRecognizer) {
if case .ended = recognizer.state {
self.tapped()
}
}
public func updateLayout(size: CGSize, transition: ContainedViewLayoutTransition) {
transition.updateFrame(node: self.controller.displayNode, frame: CGRect(origin: CGPoint(), size: size))
}
}
public enum ContextContentNode {
case reference(view: UIView)
case extracted(node: ContextExtractedContentContainingNode, keepInPlace: Bool)
case extractedContainer(container: ContextExtractableContainer)
case controller(ContextControllerContentNode)
}