mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 14:20:20 +00:00
Sticker input rewrite continued
This commit is contained in:
@@ -11,9 +11,14 @@ import BundleIconComponent
|
||||
|
||||
public final class EntityKeyboardChildEnvironment: Equatable {
|
||||
public let theme: PresentationTheme
|
||||
public let getContentActiveItemUpdated: (AnyHashable) -> ActionSlot<(AnyHashable, Transition)>?
|
||||
|
||||
public init(theme: PresentationTheme) {
|
||||
public init(
|
||||
theme: PresentationTheme,
|
||||
getContentActiveItemUpdated: @escaping (AnyHashable) -> ActionSlot<(AnyHashable, Transition)>?
|
||||
) {
|
||||
self.theme = theme
|
||||
self.getContentActiveItemUpdated = getContentActiveItemUpdated
|
||||
}
|
||||
|
||||
public static func ==(lhs: EntityKeyboardChildEnvironment, rhs: EntityKeyboardChildEnvironment) -> Bool {
|
||||
@@ -25,7 +30,17 @@ public final class EntityKeyboardChildEnvironment: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
public enum EntitySearchContentType {
|
||||
case stickers
|
||||
case gifs
|
||||
}
|
||||
|
||||
public final class EntityKeyboardComponent: Component {
|
||||
public final class MarkInputCollapsed {
|
||||
public init() {
|
||||
}
|
||||
}
|
||||
|
||||
public let theme: PresentationTheme
|
||||
public let bottomInset: CGFloat
|
||||
public let emojiContent: EmojiPagerContentComponent
|
||||
@@ -34,6 +49,9 @@ public final class EntityKeyboardComponent: Component {
|
||||
public let defaultToEmojiTab: Bool
|
||||
public let externalTopPanelContainer: UIView?
|
||||
public let topPanelExtensionUpdated: (CGFloat, Transition) -> Void
|
||||
public let hideInputUpdated: (Bool, Transition) -> Void
|
||||
public let makeSearchContainerNode: (EntitySearchContentType) -> EntitySearchContainerNode
|
||||
public let deviceMetrics: DeviceMetrics
|
||||
|
||||
public init(
|
||||
theme: PresentationTheme,
|
||||
@@ -43,7 +61,10 @@ public final class EntityKeyboardComponent: Component {
|
||||
gifContent: GifPagerContentComponent,
|
||||
defaultToEmojiTab: Bool,
|
||||
externalTopPanelContainer: UIView?,
|
||||
topPanelExtensionUpdated: @escaping (CGFloat, Transition) -> Void
|
||||
topPanelExtensionUpdated: @escaping (CGFloat, Transition) -> Void,
|
||||
hideInputUpdated: @escaping (Bool, Transition) -> Void,
|
||||
makeSearchContainerNode: @escaping (EntitySearchContentType) -> EntitySearchContainerNode,
|
||||
deviceMetrics: DeviceMetrics
|
||||
) {
|
||||
self.theme = theme
|
||||
self.bottomInset = bottomInset
|
||||
@@ -53,6 +74,9 @@ public final class EntityKeyboardComponent: Component {
|
||||
self.defaultToEmojiTab = defaultToEmojiTab
|
||||
self.externalTopPanelContainer = externalTopPanelContainer
|
||||
self.topPanelExtensionUpdated = topPanelExtensionUpdated
|
||||
self.hideInputUpdated = hideInputUpdated
|
||||
self.makeSearchContainerNode = makeSearchContainerNode
|
||||
self.deviceMetrics = deviceMetrics
|
||||
}
|
||||
|
||||
public static func ==(lhs: EntityKeyboardComponent, rhs: EntityKeyboardComponent) -> Bool {
|
||||
@@ -77,6 +101,9 @@ public final class EntityKeyboardComponent: Component {
|
||||
if lhs.externalTopPanelContainer != rhs.externalTopPanelContainer {
|
||||
return false
|
||||
}
|
||||
if lhs.deviceMetrics != rhs.deviceMetrics {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -85,6 +112,12 @@ public final class EntityKeyboardComponent: Component {
|
||||
private let pagerView: ComponentHostView<EntityKeyboardChildEnvironment>
|
||||
|
||||
private var component: EntityKeyboardComponent?
|
||||
private weak var state: EmptyComponentState?
|
||||
|
||||
private var searchView: ComponentHostView<EntitySearchContentEnvironment>?
|
||||
private var searchComponent: EntitySearchContentComponent?
|
||||
|
||||
private var topPanelExtension: CGFloat?
|
||||
|
||||
override init(frame: CGRect) {
|
||||
self.pagerView = ComponentHostView<EntityKeyboardChildEnvironment>()
|
||||
@@ -92,6 +125,7 @@ public final class EntityKeyboardComponent: Component {
|
||||
super.init(frame: frame)
|
||||
|
||||
self.clipsToBounds = true
|
||||
self.disablesInteractiveTransitionGestureRecognizer = true
|
||||
|
||||
self.addSubview(self.pagerView)
|
||||
}
|
||||
@@ -101,11 +135,15 @@ public final class EntityKeyboardComponent: Component {
|
||||
}
|
||||
|
||||
func update(component: EntityKeyboardComponent, availableSize: CGSize, state: EmptyComponentState, environment: Environment<Empty>, transition: Transition) -> CGSize {
|
||||
self.state = state
|
||||
|
||||
var contents: [AnyComponentWithIdentity<(EntityKeyboardChildEnvironment, PagerComponentChildEnvironment)>] = []
|
||||
var contentTopPanels: [AnyComponentWithIdentity<Empty>] = []
|
||||
var contentTopPanels: [AnyComponentWithIdentity<EntityKeyboardTopContainerPanelEnvironment>] = []
|
||||
var contentIcons: [AnyComponentWithIdentity<Empty>] = []
|
||||
var contentAccessoryLeftButtons: [AnyComponentWithIdentity<Empty>] = []
|
||||
var contentAccessoryRightButtons: [AnyComponentWithIdentity<Empty>] = []
|
||||
|
||||
let gifsContentItemIdUpdated = ActionSlot<(AnyHashable, Transition)>()
|
||||
contents.append(AnyComponentWithIdentity(id: "gifs", component: AnyComponent(component.gifContent)))
|
||||
var topGifItems: [EntityKeyboardTopPanelComponent.Item] = []
|
||||
topGifItems.append(EntityKeyboardTopPanelComponent.Item(
|
||||
@@ -126,13 +164,24 @@ public final class EntityKeyboardComponent: Component {
|
||||
))
|
||||
contentTopPanels.append(AnyComponentWithIdentity(id: "gifs", component: AnyComponent(EntityKeyboardTopPanelComponent(
|
||||
theme: component.theme,
|
||||
items: topGifItems
|
||||
items: topGifItems,
|
||||
activeContentItemIdUpdated: gifsContentItemIdUpdated
|
||||
))))
|
||||
contentIcons.append(AnyComponentWithIdentity(id: "gifs", component: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputGifsIcon",
|
||||
tintColor: component.theme.chat.inputMediaPanel.panelIconColor,
|
||||
maxSize: nil
|
||||
))))
|
||||
contentAccessoryLeftButtons.append(AnyComponentWithIdentity(id: "gifs", component: AnyComponent(Button(
|
||||
content: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputSearchIcon",
|
||||
tintColor: component.theme.chat.inputMediaPanel.panelIconColor,
|
||||
maxSize: nil
|
||||
)),
|
||||
action: { [weak self] in
|
||||
self?.openSearch()
|
||||
}
|
||||
).minSize(CGSize(width: 38.0, height: 38.0)))))
|
||||
/*contentAccessoryRightButtons.append(AnyComponentWithIdentity(id: "gifs", component: AnyComponent(Button(
|
||||
content: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputSettingsIcon",
|
||||
@@ -152,38 +201,59 @@ public final class EntityKeyboardComponent: Component {
|
||||
]
|
||||
if let iconName = iconMapping[id] {
|
||||
topStickerItems.append(EntityKeyboardTopPanelComponent.Item(
|
||||
id: id,
|
||||
content: AnyComponent(BundleIconComponent(
|
||||
name: iconName,
|
||||
tintColor: component.theme.chat.inputMediaPanel.panelIconColor,
|
||||
maxSize: CGSize(width: 30.0, height: 30.0))
|
||||
id: itemGroup.id,
|
||||
content: AnyComponent(Button(
|
||||
content: AnyComponent(BundleIconComponent(
|
||||
name: iconName,
|
||||
tintColor: component.theme.chat.inputMediaPanel.panelIconColor,
|
||||
maxSize: CGSize(width: 30.0, height: 30.0)
|
||||
)),
|
||||
action: { [weak self] in
|
||||
self?.scrollToItemGroup(contentId: "stickers", groupId: itemGroup.id)
|
||||
}
|
||||
).minSize(CGSize(width: 30.0, height: 30.0))
|
||||
)
|
||||
))
|
||||
}
|
||||
} else {
|
||||
if !itemGroup.items.isEmpty {
|
||||
topStickerItems.append(EntityKeyboardTopPanelComponent.Item(
|
||||
id: AnyHashable(itemGroup.items[0].file.fileId),
|
||||
id: itemGroup.id,
|
||||
content: AnyComponent(EntityKeyboardAnimationTopPanelComponent(
|
||||
context: component.stickerContent.context,
|
||||
file: itemGroup.items[0].file,
|
||||
animationCache: component.stickerContent.animationCache,
|
||||
animationRenderer: component.stickerContent.animationRenderer
|
||||
animationRenderer: component.stickerContent.animationRenderer,
|
||||
pressed: { [weak self] in
|
||||
self?.scrollToItemGroup(contentId: "stickers", groupId: itemGroup.id)
|
||||
}
|
||||
))
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
let stickersContentItemIdUpdated = ActionSlot<(AnyHashable, Transition)>()
|
||||
contents.append(AnyComponentWithIdentity(id: "stickers", component: AnyComponent(component.stickerContent)))
|
||||
contentTopPanels.append(AnyComponentWithIdentity(id: "stickers", component: AnyComponent(EntityKeyboardTopPanelComponent(
|
||||
theme: component.theme,
|
||||
items: topStickerItems
|
||||
items: topStickerItems,
|
||||
activeContentItemIdUpdated: stickersContentItemIdUpdated
|
||||
))))
|
||||
contentIcons.append(AnyComponentWithIdentity(id: "stickers", component: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputStickersIcon",
|
||||
tintColor: component.theme.chat.inputMediaPanel.panelIconColor,
|
||||
maxSize: nil
|
||||
))))
|
||||
contentAccessoryLeftButtons.append(AnyComponentWithIdentity(id: "stickers", component: AnyComponent(Button(
|
||||
content: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputSearchIcon",
|
||||
tintColor: component.theme.chat.inputMediaPanel.panelIconColor,
|
||||
maxSize: nil
|
||||
)),
|
||||
action: { [weak self] in
|
||||
self?.openSearch()
|
||||
}
|
||||
).minSize(CGSize(width: 38.0, height: 38.0)))))
|
||||
contentAccessoryRightButtons.append(AnyComponentWithIdentity(id: "stickers", component: AnyComponent(Button(
|
||||
content: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputSettingsIcon",
|
||||
@@ -195,24 +265,29 @@ public final class EntityKeyboardComponent: Component {
|
||||
}
|
||||
).minSize(CGSize(width: 38.0, height: 38.0)))))
|
||||
|
||||
let emojiContentItemIdUpdated = ActionSlot<(AnyHashable, Transition)>()
|
||||
contents.append(AnyComponentWithIdentity(id: "emoji", component: AnyComponent(component.emojiContent)))
|
||||
var topEmojiItems: [EntityKeyboardTopPanelComponent.Item] = []
|
||||
for itemGroup in component.emojiContent.itemGroups {
|
||||
if !itemGroup.items.isEmpty {
|
||||
topEmojiItems.append(EntityKeyboardTopPanelComponent.Item(
|
||||
id: AnyHashable(itemGroup.items[0].file.fileId),
|
||||
id: itemGroup.id,
|
||||
content: AnyComponent(EntityKeyboardAnimationTopPanelComponent(
|
||||
context: component.emojiContent.context,
|
||||
file: itemGroup.items[0].file,
|
||||
animationCache: component.emojiContent.animationCache,
|
||||
animationRenderer: component.emojiContent.animationRenderer
|
||||
animationRenderer: component.emojiContent.animationRenderer,
|
||||
pressed: { [weak self] in
|
||||
self?.scrollToItemGroup(contentId: "emoji", groupId: itemGroup.id)
|
||||
}
|
||||
))
|
||||
))
|
||||
}
|
||||
}
|
||||
contentTopPanels.append(AnyComponentWithIdentity(id: "emoji", component: AnyComponent(EntityKeyboardTopPanelComponent(
|
||||
theme: component.theme,
|
||||
items: topEmojiItems
|
||||
items: topEmojiItems,
|
||||
activeContentItemIdUpdated: emojiContentItemIdUpdated
|
||||
))))
|
||||
contentIcons.append(AnyComponentWithIdentity(id: "emoji", component: AnyComponent(BundleIconComponent(
|
||||
name: "Chat/Input/Media/EntityInputEmojiIcon",
|
||||
@@ -237,6 +312,7 @@ public final class EntityKeyboardComponent: Component {
|
||||
contents: contents,
|
||||
contentTopPanels: contentTopPanels,
|
||||
contentIcons: contentIcons,
|
||||
contentAccessoryLeftButtons: contentAccessoryLeftButtons,
|
||||
contentAccessoryRightButtons: contentAccessoryRightButtons,
|
||||
defaultId: component.defaultToEmojiTab ? "emoji" : "stickers",
|
||||
contentBackground: AnyComponent(BlurredBackgroundComponent(
|
||||
@@ -253,21 +329,149 @@ public final class EntityKeyboardComponent: Component {
|
||||
self?.component?.emojiContent.inputInteraction.deleteBackwards()
|
||||
}
|
||||
)),
|
||||
panelStateUpdated: { panelState, transition in
|
||||
component.topPanelExtensionUpdated(panelState.topPanelHeight, transition)
|
||||
}
|
||||
panelStateUpdated: { [weak self] panelState, transition in
|
||||
guard let strongSelf = self else {
|
||||
return
|
||||
}
|
||||
strongSelf.topPanelExtensionUpdated(height: panelState.topPanelHeight, transition: transition)
|
||||
},
|
||||
hidePanels: self.searchComponent != nil
|
||||
)),
|
||||
environment: {
|
||||
EntityKeyboardChildEnvironment(theme: component.theme)
|
||||
EntityKeyboardChildEnvironment(
|
||||
theme: component.theme,
|
||||
getContentActiveItemUpdated: { id in
|
||||
if id == AnyHashable("gifs") {
|
||||
return gifsContentItemIdUpdated
|
||||
} else if id == AnyHashable("stickers") {
|
||||
return stickersContentItemIdUpdated
|
||||
} else if id == AnyHashable("emoji") {
|
||||
return emojiContentItemIdUpdated
|
||||
}
|
||||
return nil
|
||||
}
|
||||
)
|
||||
},
|
||||
containerSize: availableSize
|
||||
)
|
||||
transition.setFrame(view: self.pagerView, frame: CGRect(origin: CGPoint(), size: pagerSize))
|
||||
|
||||
if transition.userData(MarkInputCollapsed.self) != nil {
|
||||
self.searchComponent = nil
|
||||
}
|
||||
|
||||
if let searchComponent = self.searchComponent {
|
||||
var animateIn = false
|
||||
let searchView: ComponentHostView<EntitySearchContentEnvironment>
|
||||
var searchViewTransition = transition
|
||||
if let current = self.searchView {
|
||||
searchView = current
|
||||
} else {
|
||||
searchViewTransition = .immediate
|
||||
searchView = ComponentHostView<EntitySearchContentEnvironment>()
|
||||
self.searchView = searchView
|
||||
self.addSubview(searchView)
|
||||
|
||||
animateIn = true
|
||||
component.topPanelExtensionUpdated(0.0, transition)
|
||||
}
|
||||
|
||||
let _ = searchView.update(
|
||||
transition: searchViewTransition,
|
||||
component: AnyComponent(searchComponent),
|
||||
environment: {
|
||||
EntitySearchContentEnvironment(
|
||||
context: component.stickerContent.context,
|
||||
theme: component.theme,
|
||||
deviceMetrics: component.deviceMetrics
|
||||
)
|
||||
},
|
||||
containerSize: availableSize
|
||||
)
|
||||
searchViewTransition.setFrame(view: searchView, frame: CGRect(origin: CGPoint(), size: availableSize))
|
||||
|
||||
if animateIn {
|
||||
transition.animateAlpha(view: searchView, from: 0.0, to: 1.0)
|
||||
transition.animatePosition(view: searchView, from: CGPoint(x: 0.0, y: self.topPanelExtension ?? 0.0), to: CGPoint(), additive: true, completion: nil)
|
||||
}
|
||||
} else {
|
||||
if let searchView = self.searchView {
|
||||
self.searchView = nil
|
||||
|
||||
transition.setFrame(view: searchView, frame: CGRect(origin: CGPoint(x: 0.0, y: self.topPanelExtension ?? 0.0), size: availableSize))
|
||||
transition.setAlpha(view: searchView, alpha: 0.0, completion: { [weak searchView] _ in
|
||||
searchView?.removeFromSuperview()
|
||||
})
|
||||
|
||||
if let topPanelExtension = self.topPanelExtension {
|
||||
component.topPanelExtensionUpdated(topPanelExtension, transition)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.component = component
|
||||
|
||||
return availableSize
|
||||
}
|
||||
|
||||
private func topPanelExtensionUpdated(height: CGFloat, transition: Transition) {
|
||||
guard let component = self.component else {
|
||||
return
|
||||
}
|
||||
if self.topPanelExtension != height {
|
||||
self.topPanelExtension = height
|
||||
}
|
||||
if self.searchComponent == nil {
|
||||
component.topPanelExtensionUpdated(height, transition)
|
||||
}
|
||||
}
|
||||
|
||||
private func openSearch() {
|
||||
guard let component = self.component else {
|
||||
return
|
||||
}
|
||||
if self.searchComponent != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if let pagerView = self.pagerView.findTaggedView(tag: PagerComponentViewTag()) as? PagerComponent<EntityKeyboardChildEnvironment, EntityKeyboardTopContainerPanelEnvironment>.View, let centralId = pagerView.centralId {
|
||||
let contentType: EntitySearchContentType
|
||||
if centralId == AnyHashable("gifs") {
|
||||
contentType = .gifs
|
||||
} else {
|
||||
contentType = .stickers
|
||||
}
|
||||
|
||||
self.searchComponent = EntitySearchContentComponent(
|
||||
makeContainerNode: {
|
||||
return component.makeSearchContainerNode(contentType)
|
||||
},
|
||||
dismissSearch: { [weak self] in
|
||||
self?.closeSearch()
|
||||
}
|
||||
)
|
||||
//self.state?.updated(transition: Transition(animation: .curve(duration: 0.3, curve: .spring)))
|
||||
component.hideInputUpdated(true, Transition(animation: .curve(duration: 0.3, curve: .spring)))
|
||||
}
|
||||
}
|
||||
|
||||
private func closeSearch() {
|
||||
guard let component = self.component else {
|
||||
return
|
||||
}
|
||||
if self.searchComponent == nil {
|
||||
return
|
||||
}
|
||||
self.searchComponent = nil
|
||||
//self.state?.updated(transition: Transition(animation: .curve(duration: 0.4, curve: .spring)))
|
||||
component.hideInputUpdated(false, Transition(animation: .curve(duration: 0.4, curve: .spring)))
|
||||
}
|
||||
|
||||
private func scrollToItemGroup(contentId: String, groupId: AnyHashable) {
|
||||
if let pagerView = self.pagerView.findTaggedView(tag: EmojiPagerContentComponent.Tag(id: contentId)) as? EmojiPagerContentComponent.View {
|
||||
pagerView.scrollToItemGroup(groupId: groupId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func makeView() -> View {
|
||||
|
||||
Reference in New Issue
Block a user