mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2026-01-24 04:58:59 +00:00
61 lines
2.1 KiB
Swift
61 lines
2.1 KiB
Swift
import Foundation
|
|
import UIKit
|
|
import Display
|
|
|
|
private let titleFont = Font.regular(17.0)
|
|
|
|
class ListControllerButtonItem: ListControllerGroupableItem {
|
|
private let title: String
|
|
private let action: () -> ()
|
|
private let color: UIColor
|
|
|
|
let selectable: Bool = true
|
|
|
|
init(title: String, action: () -> (), color: UIColor = .blue) {
|
|
self.title = title
|
|
self.action = action
|
|
self.color = color
|
|
}
|
|
|
|
func setupNode(async: (() -> Void) -> Void, completion: (ListControllerGroupableItemNode) -> Void) {
|
|
let node = ListControllerButtonItemNode()
|
|
completion(node)
|
|
}
|
|
|
|
func selected() {
|
|
self.action()
|
|
}
|
|
}
|
|
|
|
class ListControllerButtonItemNode: ListControllerGroupableItemNode {
|
|
let label: TextNode
|
|
|
|
override init() {
|
|
self.label = TextNode()
|
|
|
|
super.init()
|
|
|
|
self.label.isLayerBacked = true
|
|
self.addSubnode(self.label)
|
|
}
|
|
|
|
override func asyncLayoutContent() -> (item: ListControllerGroupableItem, width: CGFloat) -> (CGSize, () -> Void) {
|
|
let layoutLabel = TextNode.asyncLayout(self.label)
|
|
return { item, width in
|
|
if let item = item as? ListControllerButtonItem {
|
|
let (labelLayout, labelApply) = layoutLabel(attributedString: NSAttributedString(string: item.title, font: titleFont, textColor: item.color), backgroundColor: nil, maximumNumberOfLines: 1, truncationType: .end, constrainedSize: CGSize(width: width - 20, height: CGFloat.greatestFiniteMagnitude), cutout: nil)
|
|
return (CGSize(width: width, height: 44.0), { [weak self] in
|
|
if let strongSelf = self {
|
|
let _ = labelApply()
|
|
|
|
strongSelf.label.frame = CGRect(origin: CGPoint(x: 16.0, y: floorToScreenPixels((44.0 - labelLayout.size.height) / 2.0)), size: labelLayout.size)
|
|
}
|
|
})
|
|
} else {
|
|
return (CGSize(width: width, height: 0.0), {
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|