Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,24 @@
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
swift_library(
name = "ContextMenuScreen",
module_name = "ContextMenuScreen",
srcs = glob([
"Sources/**/*.swift",
]),
copts = [
"-warnings-as-errors",
],
deps = [
"//submodules/AsyncDisplayKit",
"//submodules/Display",
"//submodules/TelegramPresentationData",
"//submodules/ComponentFlow",
"//submodules/Components/ViewControllerComponent",
"//submodules/AccountContext",
"//submodules/AppBundle",
],
visibility = [
"//visibility:public",
],
)
@@ -0,0 +1,217 @@
import Foundation
import UIKit
import AsyncDisplayKit
import Display
final private class ContextMenuActionButton: HighlightTrackingButton {
override func convert(_ point: CGPoint, from view: UIView?) -> CGPoint {
if view is UIWindow {
return super.convert(point, from: nil)
} else {
return super.convert(point, from: view)
}
}
}
final class ContextMenuActionNode: ASDisplayNode {
private let textNode: ImmediateTextNode?
private let subtitleNode: ImmediateTextNode?
private var textSize: CGSize?
private var subtitleSize: CGSize?
private let iconView: UIImageView?
private let action: () -> Void
private let button: ContextMenuActionButton
private let actionArea: AccessibilityAreaNode
var dismiss: (() -> Void)?
init(action: ContextMenuAction, blurred: Bool, isDark: Bool) {
self.actionArea = AccessibilityAreaNode()
self.actionArea.accessibilityTraits = .button
switch action.content {
case let .text(title, accessibilityLabel):
self.actionArea.accessibilityLabel = accessibilityLabel
let textNode = ImmediateTextNode()
textNode.isUserInteractionEnabled = false
textNode.displaysAsynchronously = false
textNode.attributedText = NSAttributedString(string: title, font: Font.regular(14.0), textColor: isDark ? .white : .black)
textNode.isAccessibilityElement = false
self.textNode = textNode
self.subtitleNode = nil
self.iconView = nil
case let .textWithIcon(title, icon):
let textNode = ImmediateTextNode()
textNode.isUserInteractionEnabled = false
textNode.displaysAsynchronously = false
textNode.attributedText = NSAttributedString(string: title, font: Font.regular(17.0), textColor: isDark ? .white : .black)
textNode.isAccessibilityElement = false
let iconView = UIImageView()
iconView.tintColor = isDark ? .white : .black
iconView.image = icon
self.textNode = textNode
self.subtitleNode = nil
self.iconView = iconView
case let .textWithSubtitleAndIcon(title, subtitle, icon):
let textNode = ImmediateTextNode()
textNode.isUserInteractionEnabled = false
textNode.displaysAsynchronously = false
textNode.attributedText = NSAttributedString(string: title, font: Font.regular(17.0), textColor: isDark ? .white : .black)
textNode.isAccessibilityElement = false
let subtitleNode = ImmediateTextNode()
subtitleNode.isUserInteractionEnabled = false
subtitleNode.displaysAsynchronously = false
subtitleNode.attributedText = NSAttributedString(string: subtitle, font: Font.regular(12.0), textColor: (isDark ? UIColor.white : UIColor.black).withAlphaComponent(0.5))
subtitleNode.isAccessibilityElement = false
let iconView = UIImageView()
iconView.tintColor = isDark ? .white : .black
iconView.image = icon
self.textNode = textNode
self.subtitleNode = subtitleNode
self.iconView = iconView
case let .icon(image):
let iconView = UIImageView()
iconView.tintColor = isDark ? .white : .black
iconView.image = image
self.iconView = iconView
self.textNode = nil
self.subtitleNode = nil
}
self.action = action.action
self.button = ContextMenuActionButton()
self.button.isAccessibilityElement = false
super.init()
if !blurred {
self.backgroundColor = isDark ? UIColor(rgb: 0x2f2f2f) : nil
}
if let textNode = self.textNode {
self.addSubnode(textNode)
}
if let subtitleNode = self.subtitleNode {
self.addSubnode(subtitleNode)
}
if let iconView = self.iconView {
self.view.addSubview(iconView)
}
self.button.highligthedChanged = { [weak self] highlighted in
if isDark {
if blurred {
self?.backgroundColor = highlighted ? UIColor(rgb: 0xffffff, alpha: 0.5) : .clear
} else {
self?.backgroundColor = highlighted ? UIColor(rgb: 0x8c8e8e) : UIColor(rgb: 0x2f2f2f)
}
} else {
self?.backgroundColor = highlighted ? UIColor(rgb: 0xDCE3DC) : .clear
}
}
self.view.addSubview(self.button)
self.addSubnode(self.actionArea)
self.actionArea.activate = { [weak self] in
self?.buttonPressed()
return true
}
}
override func didLoad() {
super.didLoad()
self.button.addTarget(self, action: #selector(self.buttonPressed), for: [.touchUpInside])
}
@objc private func buttonPressed() {
self.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
self.action()
if let dismiss = self.dismiss {
dismiss()
}
}
override func calculateSizeThatFits(_ constrainedSize: CGSize) -> CGSize {
if let textNode = self.textNode {
let constrainedSize = CGSize(width: constrainedSize.width - 36.0 - 24.0, height: constrainedSize.height)
let textSize = textNode.updateLayout(constrainedSize)
self.textSize = textSize
var totalWidth = 0.0
var totalHeight: CGFloat = 54.0
totalWidth += textSize.width
if let subtitleNode = self.subtitleNode {
let subtitleSize = subtitleNode.updateLayout(CGSize(width: constrainedSize.width * 0.75, height: constrainedSize.height))
self.subtitleSize = subtitleSize
totalWidth = max(totalWidth, subtitleSize.width)
totalHeight += 14.0
}
if let image = self.iconView?.image {
if totalWidth > 0.0 {
totalWidth += 11.0
}
totalWidth += image.size.width
totalWidth += 24.0
} else {
totalWidth += 36.0
}
return CGSize(width: totalWidth, height: totalHeight)
} else if let iconView = self.iconView, let image = iconView.image {
return CGSize(width: image.size.width + 36.0, height: 54.0)
} else {
return CGSize(width: 36.0, height: 54.0)
}
}
override func layout() {
super.layout()
self.button.frame = self.bounds
self.actionArea.frame = self.bounds
var totalWidth = 0.0
if let textSize = self.textSize {
totalWidth += textSize.width
}
if let subtitleSize = self.subtitleSize {
totalWidth = max(totalWidth, subtitleSize.width)
}
if let image = self.iconView?.image {
if totalWidth > 0.0 {
totalWidth += 11.0
}
totalWidth += image.size.width
}
var totalTextHeight: CGFloat = 0.0
if let textSize = self.textSize {
totalTextHeight += textSize.height
}
if let subtitleSize = self.subtitleSize {
totalTextHeight += subtitleSize.height
}
if let textNode = self.textNode, let textSize = self.textSize {
textNode.frame = CGRect(origin: CGPoint(x: floor((self.bounds.size.width - totalWidth) / 2.0), y: floor((self.bounds.size.height - totalTextHeight) / 2.0)), size: textSize)
}
if let subtitleNode = self.subtitleNode, let subtitleSize = self.subtitleSize {
subtitleNode.frame = CGRect(origin: CGPoint(x: floor((self.bounds.size.width - totalWidth) / 2.0), y: floor((self.bounds.size.height - totalTextHeight) / 2.0) + totalTextHeight - subtitleSize.height), size: subtitleSize)
}
if let iconView = self.iconView, let image = iconView.image {
let iconSize = image.size
iconView.frame = CGRect(origin: CGPoint(x: floor((self.bounds.size.width - totalWidth) / 2.0) + totalWidth - iconSize.width, y: floorToScreenPixels((self.bounds.size.height - iconSize.height) / 2.0)), size: iconSize)
}
}
}
@@ -0,0 +1,111 @@
import Foundation
import UIKit
import Display
public final class ContextMenuControllerImpl: ViewController, KeyShortcutResponder, ContextMenuController {
private var contextMenuNode: ContextMenuNode {
return self.displayNode as! ContextMenuNode
}
public var keyShortcuts: [KeyShortcut] {
return [KeyShortcut(input: UIKeyCommand.inputEscape, action: { [weak self] in
if let strongSelf = self {
strongSelf.dismiss()
}
})]
}
private let actions: [ContextMenuAction]
private let catchTapsOutside: Bool
private let hasHapticFeedback: Bool
private let blurred: Bool
private let skipCoordnateConversion: Bool
private let isDark: Bool
private var layout: ContainerViewLayout?
public var centerHorizontally = false
public var dismissed: (() -> Void)?
public var dismissOnTap: ((UIView, CGPoint) -> Bool)?
public init(_ arguments: ContextMenuControllerArguments) {
self.actions = arguments.actions
self.catchTapsOutside = arguments.catchTapsOutside
self.hasHapticFeedback = arguments.hasHapticFeedback
self.blurred = arguments.blurred
self.skipCoordnateConversion = arguments.skipCoordnateConversion
self.isDark = arguments.isDark
super.init(navigationBarPresentationData: nil)
self.statusBar.statusBarStyle = .Ignore
}
required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func loadDisplayNode() {
self.displayNode = ContextMenuNode(actions: self.actions, dismiss: { [weak self] in
self?.dismissed?()
self?.contextMenuNode.animateOut(bounce: (self?.presentationArguments as? ContextMenuControllerPresentationArguments)?.bounce ?? true, completion: {
self?.presentingViewController?.dismiss(animated: false)
})
}, dismissOnTap: { [weak self] view, point in
guard let self, let dismissOnTap = self.dismissOnTap else {
return false
}
return dismissOnTap(view, point)
}, catchTapsOutside: self.catchTapsOutside, hasHapticFeedback: self.hasHapticFeedback, blurred: self.blurred, isDark: self.isDark)
self.displayNodeDidLoad()
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.contextMenuNode.animateIn(bounce: (self.presentationArguments as? ContextMenuControllerPresentationArguments)?.bounce ?? true)
}
override public func dismiss(completion: (() -> Void)? = nil) {
self.dismissed?()
self.contextMenuNode.animateOut(bounce: (self.presentationArguments as? ContextMenuControllerPresentationArguments)?.bounce ?? true, completion: { [weak self] in
self?.presentingViewController?.dismiss(animated: false)
})
}
override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
super.containerLayoutUpdated(layout, transition: transition)
self.contextMenuNode.centerHorizontally = self.centerHorizontally
if self.layout != nil && self.layout! != layout {
self.dismissed?()
self.contextMenuNode.animateOut(bounce: (self.presentationArguments as? ContextMenuControllerPresentationArguments)?.bounce ?? true, completion: { [weak self] in
self?.presentingViewController?.dismiss(animated: false)
})
} else {
self.layout = layout
if let presentationArguments = self.presentationArguments as? ContextMenuControllerPresentationArguments, let (sourceNode, sourceRect, containerNode, containerRect) = presentationArguments.sourceNodeAndRect() {
if self.skipCoordnateConversion {
self.contextMenuNode.sourceRect = sourceRect
self.contextMenuNode.containerRect = containerRect
} else {
self.contextMenuNode.sourceRect = sourceNode.view.convert(sourceRect, to: nil)
self.contextMenuNode.containerRect = containerNode.view.convert(containerRect, to: nil)
}
} else {
self.contextMenuNode.sourceRect = nil
self.contextMenuNode.containerRect = nil
}
self.contextMenuNode.containerLayoutUpdated(layout, transition: transition)
}
}
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}
@@ -0,0 +1,329 @@
import Foundation
import UIKit
import AsyncDisplayKit
import AppBundle
import AsyncDisplayKit
import Display
private final class ArrowNode: HighlightTrackingButtonNode {
private let isLeft: Bool
private let iconView: UIImageView
private let separatorLayer: SimpleLayer
var action: (() -> Void)?
init(isLeft: Bool, isDark: Bool) {
self.isLeft = isLeft
self.iconView = UIImageView()
self.iconView.image = UIImage(bundleImageName: "Chat/Context Menu/Arrow")!.withRenderingMode(.alwaysTemplate)
if isLeft {
self.iconView.transform = CGAffineTransformMakeScale(-1.0, 1.0)
}
self.separatorLayer = SimpleLayer()
super.init()
self.layer.addSublayer(self.separatorLayer)
self.view.addSubview(self.iconView)
self.addTarget(self, action: #selector(self.pressed), forControlEvents: .touchUpInside)
self.highligthedChanged = { [weak self] highlighted in
guard let self else {
return
}
if isDark {
self.backgroundColor = highlighted ? UIColor(rgb: 0x8c8e8e) : nil
} else {
self.backgroundColor = highlighted ? UIColor(rgb: 0xDCE3DC) : nil
}
}
}
@objc private func pressed() {
self.action?()
}
func update(color: UIColor, separatorColor: UIColor, height: CGFloat) -> CGSize {
let size = CGSize(width: 33.0, height: height)
self.iconView.tintColor = color
if let icon = self.iconView.image {
let iconFrame = CGRect(origin: CGPoint(x: floor((size.width - icon.size.width) * 0.5), y: floor((size.height - icon.size.height) * 0.5)), size: icon.size)
self.iconView.center = CGPoint(x: iconFrame.midX, y: iconFrame.midY)
self.iconView.bounds = CGRect(origin: CGPoint(), size: iconFrame.size)
}
self.separatorLayer.backgroundColor = separatorColor.cgColor
self.separatorLayer.frame = CGRect(origin: CGPoint(x: self.isLeft ? (size.width - UIScreenPixel) : 0.0, y: 0.0), size: CGSize(width: UIScreenPixel, height: size.height))
return size
}
}
final class ContextMenuNode: ASDisplayNode {
private let blurred: Bool
private let isDark: Bool
private let actions: [ContextMenuAction]
private let dismiss: () -> Void
private let dismissOnTap: (UIView, CGPoint) -> Bool
private let containerNode: ContextMenuContainerNode
private let contentNode: ASDisplayNode
private var separatorNodes: [ASDisplayNode] = []
private let actionNodes: [ContextMenuActionNode]
private let pageLeftNode: ArrowNode
private let pageRightNode: ArrowNode
private var currentPageIndex: Int = 0
private var pageCount: Int = 0
private var validLayout: ContainerViewLayout?
var sourceRect: CGRect?
var containerRect: CGRect?
var arrowOnBottom: Bool = true
var centerHorizontally: Bool = false
private var dismissedByTouchOutside = false
private let catchTapsOutside: Bool
private let feedback: HapticFeedback?
init(actions: [ContextMenuAction], dismiss: @escaping () -> Void, dismissOnTap: @escaping (UIView, CGPoint) -> Bool, catchTapsOutside: Bool, hasHapticFeedback: Bool, blurred: Bool = false, isDark: Bool = true) {
self.blurred = blurred
self.isDark = isDark
self.actions = actions
self.dismiss = dismiss
self.dismissOnTap = dismissOnTap
self.catchTapsOutside = catchTapsOutside
self.containerNode = ContextMenuContainerNode(isBlurred: blurred, isDark: isDark)
self.contentNode = ASDisplayNode()
self.contentNode.clipsToBounds = true
self.actionNodes = actions.map { action in
return ContextMenuActionNode(action: action, blurred: blurred, isDark: isDark)
}
self.pageLeftNode = ArrowNode(isLeft: true, isDark: isDark)
self.pageRightNode = ArrowNode(isLeft: false, isDark: isDark)
if hasHapticFeedback {
self.feedback = HapticFeedback()
self.feedback?.prepareImpact(.light)
} else {
self.feedback = nil
}
super.init()
self.containerNode.containerNode.addSubnode(self.contentNode)
self.addSubnode(self.containerNode)
let dismissNode = {
dismiss()
}
for actionNode in self.actionNodes {
actionNode.dismiss = dismissNode
self.contentNode.addSubnode(actionNode)
}
self.containerNode.containerNode.addSubnode(self.pageLeftNode)
self.containerNode.containerNode.addSubnode(self.pageRightNode)
let navigatePage: (Bool) -> Void = { [weak self] isLeft in
guard let self else {
return
}
var index = self.currentPageIndex
if isLeft {
index -= 1
} else {
index += 1
}
index = max(0, min(index, self.pageCount - 1))
if self.currentPageIndex != index {
self.currentPageIndex = index
if let validLayout = self.validLayout {
self.containerLayoutUpdated(validLayout, transition: .animated(duration: 0.35, curve: .spring))
}
}
}
self.pageLeftNode.action = {
navigatePage(true)
}
self.pageRightNode.action = {
navigatePage(false)
}
}
func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
self.validLayout = layout
struct Page {
var range: Range<Int>
var width: CGFloat
var offsetX: CGFloat
}
let separatorColor = self.isDark ? UIColor(rgb: 0x8c8e8e) : UIColor(rgb: 0xDCE3DC)
var height: CGFloat = 54.0
let handleWidth: CGFloat = 33.0
let maxPageWidth = layout.size.width - 20.0 - handleWidth * 2.0
var absoluteActionOffsetX: CGFloat = 0.0
var pages: [Page] = []
for i in 0 ..< self.actionNodes.count {
if i != 0 {
absoluteActionOffsetX += UIScreenPixel
}
let actionSize = self.actionNodes[i].measure(CGSize(width: layout.size.width, height: 100.0))
height = max(height, actionSize.height)
if pages.isEmpty || (pages[pages.count - 1].width + actionSize.width) > maxPageWidth {
pages.append(Page(range: i ..< (i + 1), width: actionSize.width, offsetX: absoluteActionOffsetX))
} else {
pages[pages.count - 1].width += actionSize.width
}
let actionFrame = CGRect(origin: CGPoint(x: absoluteActionOffsetX, y: 0.0), size: actionSize)
self.actionNodes[i].frame = actionFrame
absoluteActionOffsetX += actionSize.width
let separatorNode: ASDisplayNode
if i < self.separatorNodes.count {
separatorNode = self.separatorNodes[i]
} else {
separatorNode = ASDisplayNode()
separatorNode.isUserInteractionEnabled = false
self.separatorNodes.append(separatorNode)
self.contentNode.insertSubnode(separatorNode, at: 0)
}
separatorNode.backgroundColor = separatorColor
separatorNode.frame = CGRect(origin: CGPoint(x: actionFrame.maxX, y: 0.0), size: CGSize(width: UIScreenPixel, height: height))
separatorNode.isHidden = i == self.actionNodes.count - 1
}
let pageLeftSize = self.pageLeftNode.update(color: self.isDark ? .white : .black, separatorColor: separatorColor, height: height)
let pageRightSize = self.pageRightNode.update(color: self.isDark ? .white : .black, separatorColor: separatorColor, height: height)
self.pageCount = pages.count
if !pages.isEmpty {
var leftInset: CGFloat = 0.0
if self.currentPageIndex > 0 {
leftInset = pageLeftSize.width
}
var rightInset: CGFloat = 0.0
if self.currentPageIndex < pages.count - 1 {
rightInset = pageLeftSize.width
}
let offsetX = -pages[self.currentPageIndex].offsetX
let contentWidth = leftInset + rightInset + pages[self.currentPageIndex].width
let contentNodeFrame = CGRect(origin: CGPoint(x: leftInset, y: 0.0), size: CGSize(width: pages[self.currentPageIndex].width, height: height))
transition.updatePosition(node: self.contentNode, position: CGPoint(x: contentNodeFrame.midX, y: contentNodeFrame.midY))
transition.updateBounds(node: self.contentNode, bounds: CGRect(origin: CGPoint(x: -offsetX, y: 0.0), size: contentNodeFrame.size))
transition.updateFrame(node: self.pageLeftNode, frame: CGRect(origin: CGPoint(x: leftInset - pageLeftSize.width, y: 0.0), size: pageLeftSize))
transition.updateFrame(node: self.pageRightNode, frame: CGRect(origin: CGPoint(x: contentWidth - rightInset, y: 0.0), size: pageRightSize))
let sourceRect: CGRect = self.sourceRect ?? CGRect(origin: CGPoint(x: layout.size.width / 2.0, y: layout.size.height / 2.0), size: CGSize())
let containerRect: CGRect = self.containerRect ?? CGRect(origin: CGPoint(), size: layout.size)
let insets = layout.insets(options: [.statusBar, .input])
let verticalOrigin: CGFloat
var arrowOnBottom = true
if sourceRect.minY - height > containerRect.minY + insets.top {
verticalOrigin = sourceRect.minY - height
} else {
verticalOrigin = min(containerRect.maxY - insets.bottom - height, sourceRect.maxY)
arrowOnBottom = false
}
self.arrowOnBottom = arrowOnBottom
let horizontalOrigin: CGFloat = floor(max(8.0, min(self.centerHorizontally ? sourceRect.midX - contentWidth / 2.0 : max(sourceRect.minX + 8.0, sourceRect.midX - contentWidth / 2.0), layout.size.width - contentWidth - 8.0)))
let containerFrame = CGRect(origin: CGPoint(x: horizontalOrigin, y: verticalOrigin), size: CGSize(width: contentWidth, height: height))
transition.updateFrame(node: self.containerNode, frame: containerFrame)
self.containerNode.relativeArrowPosition = (sourceRect.midX - horizontalOrigin, arrowOnBottom)
self.containerNode.updateLayout(transition: transition)
}
}
func animateIn(bounce: Bool) {
if bounce {
self.containerNode.layer.animateSpring(from: NSNumber(value: Float(0.2)), to: NSNumber(value: Float(1.0)), keyPath: "transform.scale", duration: 0.4)
let containerPosition = self.containerNode.layer.position
self.containerNode.layer.animateSpring(from: NSValue(cgPoint: CGPoint(x: containerPosition.x, y: containerPosition.y + (self.arrowOnBottom ? 1.0 : -1.0) * self.containerNode.bounds.size.height / 2.0)), to: NSValue(cgPoint: containerPosition), keyPath: "position", duration: 0.4)
}
if !(self.blurred && self.isDark) {
self.allowsGroupOpacity = true
self.layer.rasterizationScale = UIScreen.main.scale
self.layer.shouldRasterize = true
}
self.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.1, completion: { [weak self] _ in
self?.allowsGroupOpacity = false
self?.layer.shouldRasterize = false
})
if let feedback = self.feedback {
feedback.impact(.light)
}
}
func animateOut(bounce: Bool, completion: @escaping () -> Void) {
if !(self.blurred && self.isDark) {
self.allowsGroupOpacity = true
self.layer.rasterizationScale = UIScreen.main.scale
self.layer.shouldRasterize = true
}
self.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, removeOnCompletion: false, completion: { [weak self] _ in
self?.allowsGroupOpacity = false
self?.layer.shouldRasterize = false
completion()
})
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if let event = event {
var eventIsPresses = false
if #available(iOSApplicationExtension 9.0, iOS 9.0, *) {
eventIsPresses = event.type == .presses
}
if event.type == .touches || eventIsPresses {
if !self.containerNode.frame.contains(point) {
if self.dismissOnTap(self.view, point) {
self.dismiss()
if self.catchTapsOutside {
return self.view
} else {
return nil
}
}
if !self.dismissedByTouchOutside {
self.dismissedByTouchOutside = true
self.dismiss()
}
if self.catchTapsOutside {
return self.view
}
return nil
}
}
}
return super.hitTest(point, with: event)
}
}