mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2026-01-16 09:26:59 +00:00
no message
This commit is contained in:
91
Display/PeekControllerMenuItemNode.swift
Normal file
91
Display/PeekControllerMenuItemNode.swift
Normal file
@@ -0,0 +1,91 @@
|
||||
import Foundation
|
||||
import AsyncDisplayKit
|
||||
|
||||
public enum PeekControllerMenuItemColor {
|
||||
case accent
|
||||
case destructive
|
||||
}
|
||||
|
||||
public struct PeekControllerMenuItem {
|
||||
public let title: String
|
||||
public let color: PeekControllerMenuItemColor
|
||||
public let action: () -> Void
|
||||
|
||||
public init(title: String, color: PeekControllerMenuItemColor, action: @escaping () -> Void) {
|
||||
self.title = title
|
||||
self.color = color
|
||||
self.action = action
|
||||
}
|
||||
}
|
||||
|
||||
final class PeekControllerMenuItemNode: HighlightTrackingButtonNode {
|
||||
private let item: PeekControllerMenuItem
|
||||
private let activatedAction: () -> Void
|
||||
|
||||
private let separatorNode: ASDisplayNode
|
||||
private let highlightedBackgroundNode: ASDisplayNode
|
||||
private let textNode: ASTextNode
|
||||
|
||||
init(theme: PeekControllerTheme, item: PeekControllerMenuItem, activatedAction: @escaping () -> Void) {
|
||||
self.item = item
|
||||
self.activatedAction = activatedAction
|
||||
|
||||
self.separatorNode = ASDisplayNode()
|
||||
self.separatorNode.isLayerBacked = true
|
||||
self.separatorNode.backgroundColor = theme.menuItemSeparatorColor
|
||||
|
||||
self.highlightedBackgroundNode = ASDisplayNode()
|
||||
self.highlightedBackgroundNode.isLayerBacked = true
|
||||
self.highlightedBackgroundNode.backgroundColor = theme.menuItemHighligtedColor
|
||||
self.highlightedBackgroundNode.alpha = 0.0
|
||||
|
||||
self.textNode = ASTextNode()
|
||||
self.textNode.isLayerBacked = true
|
||||
self.textNode.displaysAsynchronously = false
|
||||
|
||||
let textColor: UIColor
|
||||
switch item.color {
|
||||
case .accent:
|
||||
textColor = theme.accentColor
|
||||
case .destructive:
|
||||
textColor = theme.destructiveColor
|
||||
}
|
||||
self.textNode.attributedText = NSAttributedString(string: item.title, font: Font.regular(20.0), textColor: textColor)
|
||||
|
||||
super.init()
|
||||
|
||||
self.addSubnode(self.separatorNode)
|
||||
self.addSubnode(self.highlightedBackgroundNode)
|
||||
self.addSubnode(self.textNode)
|
||||
|
||||
self.highligthedChanged = { [weak self] highlighted in
|
||||
if let strongSelf = self {
|
||||
if highlighted {
|
||||
strongSelf.view.superview?.bringSubview(toFront: strongSelf.view)
|
||||
strongSelf.highlightedBackgroundNode.alpha = 1.0
|
||||
} else {
|
||||
strongSelf.highlightedBackgroundNode.alpha = 0.0
|
||||
strongSelf.highlightedBackgroundNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addTarget(self, action: #selector(self.buttonPressed), forControlEvents: .touchUpInside)
|
||||
}
|
||||
|
||||
func updateLayout(width: CGFloat, transition: ContainedViewLayoutTransition) -> CGFloat {
|
||||
let height: CGFloat = 57.0
|
||||
transition.updateFrame(node: self.highlightedBackgroundNode, frame: CGRect(origin: CGPoint(), size: CGSize(width: width, height: height)))
|
||||
transition.updateFrame(node: self.separatorNode, frame: CGRect(origin: CGPoint(x: 0.0, y: height), size: CGSize(width: width, height: UIScreenPixel)))
|
||||
|
||||
let textSize = self.textNode.measure(CGSize(width: width - 10.0, height: height))
|
||||
transition.updateFrame(node: self.textNode, frame: CGRect(origin: CGPoint(x: floor((width - textSize.width) / 2.0), y: floor((height - textSize.height) / 2.0)), size: textSize))
|
||||
|
||||
return height
|
||||
}
|
||||
|
||||
@objc func buttonPressed() {
|
||||
self.activatedAction()
|
||||
self.item.action()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user