diff --git a/submodules/DrawingUI/Sources/DrawingScreen.swift b/submodules/DrawingUI/Sources/DrawingScreen.swift index 385984dbaa..71e366dbde 100644 --- a/submodules/DrawingUI/Sources/DrawingScreen.swift +++ b/submodules/DrawingUI/Sources/DrawingScreen.swift @@ -834,9 +834,14 @@ private final class DrawingScreenComponent: CombinedComponent { func updateColor(_ color: DrawingColor, animated: Bool = false) { self.currentColor = color - if let selectedEntity = self.selectedEntity { - selectedEntity.color = color - self.updateEntityView.invoke((selectedEntity.uuid, false)) + if let selectedEntity = self.selectedEntity, let selectedEntityView = self.entityViewForEntity(selectedEntity) { + if let textEntity = selectedEntity as? DrawingTextEntity, let textEntityView = selectedEntityView as? DrawingTextEntityView { + textEntity.setColor(color, range: textEntityView.selectedRange) + textEntityView.update(animated: false, keepSelectedRange: true) + } else { + selectedEntity.color = color + selectedEntityView.update(animated: false) + } } else { self.drawingState = self.drawingState.withUpdatedColor(color) self.updateToolState.invoke(self.drawingState.currentToolState) @@ -3497,7 +3502,7 @@ public final class DrawingToolsInteraction { return } entityView.suspendEditing() - self?.presentColorPicker(initialColor: textEntity.color, dismissed: { + self?.presentColorPicker(initialColor: textEntity.color(in: entityView.selectedRange), dismissed: { entityView.resumeEditing() }) }, diff --git a/submodules/DrawingUI/Sources/DrawingTextEntityView.swift b/submodules/DrawingUI/Sources/DrawingTextEntityView.swift index 02be4d4ebc..c062142fa0 100644 --- a/submodules/DrawingUI/Sources/DrawingTextEntityView.swift +++ b/submodules/DrawingUI/Sources/DrawingTextEntityView.swift @@ -367,6 +367,10 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate } } + public var selectedRange: NSRange { + return self.textView.selectedRange + } + public func textViewDidChange(_ textView: UITextView) { guard let updatedText = self.textView.attributedText.mutableCopy() as? NSMutableAttributedString else { return @@ -379,7 +383,7 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate self.textEntity.text = updatedText self.sizeToFit() - self.update(afterAppendingEmoji: true) + self.update(keepSelectedRange: true) self.textChanged() } @@ -398,7 +402,7 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate self.textEntity.text = updatedText - self.update(animated: false, afterAppendingEmoji: true) + self.update(animated: false, keepSelectedRange: true) self.textView.selectedRange = NSMakeRange(previousSelectedRange.location + previousSelectedRange.length + text.length, 0) } @@ -493,6 +497,9 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate if let _ = attributes[ChatTextInputAttributes.customEmoji] { text.addAttribute(.foregroundColor, value: UIColor.clear, range: subrange) visualText.addAttribute(.foregroundColor, value: UIColor.clear, range: subrange) + } else if let color = attributes[DrawingTextEntity.TextAttributes.color] { + text.addAttribute(.foregroundColor, value: color, range: subrange) + visualText.addAttribute(.foregroundColor, value: color, range: subrange) } } @@ -565,10 +572,14 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate } public override func update(animated: Bool = false) { - self.update(animated: animated, afterAppendingEmoji: false, updateEditingPosition: true) + self.update(animated: animated, keepSelectedRange: false, updateEditingPosition: true) } - func update(animated: Bool = false, afterAppendingEmoji: Bool = false, updateEditingPosition: Bool = true) { + public func update(animated: Bool = false, keepSelectedRange: Bool = false) { + self.update(animated: animated, keepSelectedRange: keepSelectedRange, updateEditingPosition: true) + } + + func update(animated: Bool = false, keepSelectedRange: Bool = false, updateEditingPosition: Bool = true) { if !self.isEditing { self.center = self.textEntity.position self.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(self.textEntity.rotation), self.textEntity.scale, self.textEntity.scale) @@ -612,7 +623,7 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate } self.textView.textAlignment = self.textEntity.alignment.alignment - self.updateText(keepSelectedRange: afterAppendingEmoji) + self.updateText(keepSelectedRange: keepSelectedRange) self.sizeToFit() diff --git a/submodules/MediaPickerUI/Sources/MediaPickerGridItem.swift b/submodules/MediaPickerUI/Sources/MediaPickerGridItem.swift index a20d645b15..3d829fecb2 100644 --- a/submodules/MediaPickerUI/Sources/MediaPickerGridItem.swift +++ b/submodules/MediaPickerUI/Sources/MediaPickerGridItem.swift @@ -260,11 +260,18 @@ final class MediaPickerGridItemNode: GridItemNode { } } + private var innerIsHidden = false func updateHiddenMedia() { - let wasHidden = self.isHidden - self.isHidden = self.interaction?.hiddenMediaId == self.identifier - if !self.isHidden && wasHidden { - self.animateFadeIn(animateCheckNode: true, animateSpoilerNode: true) + let wasHidden = self.innerIsHidden + if self.identifier == self.interaction?.hiddenMediaId { + self.isHidden = true + self.innerIsHidden = true + } else { + self.isHidden = false + self.innerIsHidden = false + if wasHidden { + self.animateFadeIn(animateCheckNode: true, animateSpoilerNode: true) + } } } diff --git a/submodules/MediaPickerUI/Sources/MediaPickerScreen.swift b/submodules/MediaPickerUI/Sources/MediaPickerScreen.swift index 5f5db9e12f..50108801b6 100644 --- a/submodules/MediaPickerUI/Sources/MediaPickerScreen.swift +++ b/submodules/MediaPickerUI/Sources/MediaPickerScreen.swift @@ -359,13 +359,11 @@ public final class MediaPickerScreen: ViewController, AttachmentContainable { |> deliverOnMainQueue).start(next: { [weak self] id in if let strongSelf = self { strongSelf.controller?.interaction?.hiddenMediaId = id - strongSelf.gridNode.forEachItemNode { itemNode in if let itemNode = itemNode as? MediaPickerGridItemNode { itemNode.updateHiddenMedia() } } - strongSelf.selectionNode?.updateHiddenMedia() } }) diff --git a/submodules/TelegramUI/Components/MediaEditor/Sources/Drawing/DrawingTextEntity.swift b/submodules/TelegramUI/Components/MediaEditor/Sources/Drawing/DrawingTextEntity.swift index 9eac4874ef..02407f5e43 100644 --- a/submodules/TelegramUI/Components/MediaEditor/Sources/Drawing/DrawingTextEntity.swift +++ b/submodules/TelegramUI/Components/MediaEditor/Sources/Drawing/DrawingTextEntity.swift @@ -96,6 +96,10 @@ public final class DrawingTextEntity: DrawingEntity, Codable { return isAnimated } + public struct TextAttributes { + public static let color = NSAttributedString.Key(rawValue: "Attribute__Color") + } + public var text: NSAttributedString public var style: Style public var animation: Animation @@ -307,3 +311,33 @@ public final class DrawingTextEntity: DrawingEntity, Codable { return true } } + +public extension DrawingTextEntity { + func setColor(_ color: DrawingColor, range: NSRange) { + if range.length == 0 { + self.color = color + + let updatedText = self.text.mutableCopy() as! NSMutableAttributedString + let range = NSMakeRange(0, updatedText.length) + updatedText.removeAttribute(DrawingTextEntity.TextAttributes.color, range: range) + self.text = updatedText + } else { + let updatedText = self.text.mutableCopy() as! NSMutableAttributedString + updatedText.removeAttribute(DrawingTextEntity.TextAttributes.color, range: range) + updatedText.addAttribute(DrawingTextEntity.TextAttributes.color, value: color.toUIColor(), range: range) + self.text = updatedText + } + } + + func color(in range: NSRange) -> DrawingColor { + if range.length == 0 { + return self.color + } else { + if let color = self.text.attribute(DrawingTextEntity.TextAttributes.color, at: range.location, effectiveRange: nil) as? UIColor { + return DrawingColor(color: color) + } else { + return self.color + } + } + } +} diff --git a/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift b/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift index f8b67f7700..9aa92302f0 100644 --- a/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift +++ b/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift @@ -2373,7 +2373,7 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate panGestureRecognizer.delegate = self panGestureRecognizer.minimumNumberOfTouches = 1 panGestureRecognizer.maximumNumberOfTouches = 2 - self.previewContainerView.addGestureRecognizer(panGestureRecognizer) + self.view.addGestureRecognizer(panGestureRecognizer) let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(_:))) pinchGestureRecognizer.delegate = self @@ -2411,8 +2411,14 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate }, updateColor: { [weak self] color in if let self, let selectedEntityView = self.entitiesView.selectedEntityView { - selectedEntityView.entity.color = color - selectedEntityView.update(animated: false) + let selectedEntity = selectedEntityView.entity + if let textEntity = selectedEntity as? DrawingTextEntity, let textEntityView = selectedEntityView as? DrawingTextEntityView, textEntityView.isEditing { + textEntity.setColor(color, range: textEntityView.selectedRange) + textEntityView.update(animated: false, keepSelectedRange: true) + } else { + selectedEntity.color = color + selectedEntityView.update(animated: false) + } } }, onInteractionUpdated: { [weak self] isInteracting in