Various improvements

This commit is contained in:
Ilya Laktyushin 2023-11-29 19:28:49 +04:00
parent 97e3a66cfb
commit 25dcb36761
6 changed files with 79 additions and 18 deletions

View File

@ -834,9 +834,14 @@ private final class DrawingScreenComponent: CombinedComponent {
func updateColor(_ color: DrawingColor, animated: Bool = false) { func updateColor(_ color: DrawingColor, animated: Bool = false) {
self.currentColor = color self.currentColor = color
if let selectedEntity = self.selectedEntity { 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 selectedEntity.color = color
self.updateEntityView.invoke((selectedEntity.uuid, false)) selectedEntityView.update(animated: false)
}
} else { } else {
self.drawingState = self.drawingState.withUpdatedColor(color) self.drawingState = self.drawingState.withUpdatedColor(color)
self.updateToolState.invoke(self.drawingState.currentToolState) self.updateToolState.invoke(self.drawingState.currentToolState)
@ -3497,7 +3502,7 @@ public final class DrawingToolsInteraction {
return return
} }
entityView.suspendEditing() entityView.suspendEditing()
self?.presentColorPicker(initialColor: textEntity.color, dismissed: { self?.presentColorPicker(initialColor: textEntity.color(in: entityView.selectedRange), dismissed: {
entityView.resumeEditing() entityView.resumeEditing()
}) })
}, },

View File

@ -367,6 +367,10 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate
} }
} }
public var selectedRange: NSRange {
return self.textView.selectedRange
}
public func textViewDidChange(_ textView: UITextView) { public func textViewDidChange(_ textView: UITextView) {
guard let updatedText = self.textView.attributedText.mutableCopy() as? NSMutableAttributedString else { guard let updatedText = self.textView.attributedText.mutableCopy() as? NSMutableAttributedString else {
return return
@ -379,7 +383,7 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate
self.textEntity.text = updatedText self.textEntity.text = updatedText
self.sizeToFit() self.sizeToFit()
self.update(afterAppendingEmoji: true) self.update(keepSelectedRange: true)
self.textChanged() self.textChanged()
} }
@ -398,7 +402,7 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate
self.textEntity.text = updatedText 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) 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] { if let _ = attributes[ChatTextInputAttributes.customEmoji] {
text.addAttribute(.foregroundColor, value: UIColor.clear, range: subrange) text.addAttribute(.foregroundColor, value: UIColor.clear, range: subrange)
visualText.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) { 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 { if !self.isEditing {
self.center = self.textEntity.position self.center = self.textEntity.position
self.transform = CGAffineTransformScale(CGAffineTransformMakeRotation(self.textEntity.rotation), self.textEntity.scale, self.textEntity.scale) 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.textView.textAlignment = self.textEntity.alignment.alignment
self.updateText(keepSelectedRange: afterAppendingEmoji) self.updateText(keepSelectedRange: keepSelectedRange)
self.sizeToFit() self.sizeToFit()

View File

@ -260,13 +260,20 @@ final class MediaPickerGridItemNode: GridItemNode {
} }
} }
private var innerIsHidden = false
func updateHiddenMedia() { func updateHiddenMedia() {
let wasHidden = self.isHidden let wasHidden = self.innerIsHidden
self.isHidden = self.interaction?.hiddenMediaId == self.identifier if self.identifier == self.interaction?.hiddenMediaId {
if !self.isHidden && wasHidden { self.isHidden = true
self.innerIsHidden = true
} else {
self.isHidden = false
self.innerIsHidden = false
if wasHidden {
self.animateFadeIn(animateCheckNode: true, animateSpoilerNode: true) self.animateFadeIn(animateCheckNode: true, animateSpoilerNode: true)
} }
} }
}
func animateFadeIn(animateCheckNode: Bool, animateSpoilerNode: Bool) { func animateFadeIn(animateCheckNode: Bool, animateSpoilerNode: Bool) {
if animateCheckNode { if animateCheckNode {

View File

@ -359,13 +359,11 @@ public final class MediaPickerScreen: ViewController, AttachmentContainable {
|> deliverOnMainQueue).start(next: { [weak self] id in |> deliverOnMainQueue).start(next: { [weak self] id in
if let strongSelf = self { if let strongSelf = self {
strongSelf.controller?.interaction?.hiddenMediaId = id strongSelf.controller?.interaction?.hiddenMediaId = id
strongSelf.gridNode.forEachItemNode { itemNode in strongSelf.gridNode.forEachItemNode { itemNode in
if let itemNode = itemNode as? MediaPickerGridItemNode { if let itemNode = itemNode as? MediaPickerGridItemNode {
itemNode.updateHiddenMedia() itemNode.updateHiddenMedia()
} }
} }
strongSelf.selectionNode?.updateHiddenMedia() strongSelf.selectionNode?.updateHiddenMedia()
} }
}) })

View File

@ -96,6 +96,10 @@ public final class DrawingTextEntity: DrawingEntity, Codable {
return isAnimated return isAnimated
} }
public struct TextAttributes {
public static let color = NSAttributedString.Key(rawValue: "Attribute__Color")
}
public var text: NSAttributedString public var text: NSAttributedString
public var style: Style public var style: Style
public var animation: Animation public var animation: Animation
@ -307,3 +311,33 @@ public final class DrawingTextEntity: DrawingEntity, Codable {
return true 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
}
}
}
}

View File

@ -2373,7 +2373,7 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate
panGestureRecognizer.delegate = self panGestureRecognizer.delegate = self
panGestureRecognizer.minimumNumberOfTouches = 1 panGestureRecognizer.minimumNumberOfTouches = 1
panGestureRecognizer.maximumNumberOfTouches = 2 panGestureRecognizer.maximumNumberOfTouches = 2
self.previewContainerView.addGestureRecognizer(panGestureRecognizer) self.view.addGestureRecognizer(panGestureRecognizer)
let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(_:))) let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(_:)))
pinchGestureRecognizer.delegate = self pinchGestureRecognizer.delegate = self
@ -2411,9 +2411,15 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate
}, },
updateColor: { [weak self] color in updateColor: { [weak self] color in
if let self, let selectedEntityView = self.entitiesView.selectedEntityView { if let self, let selectedEntityView = self.entitiesView.selectedEntityView {
selectedEntityView.entity.color = color 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) selectedEntityView.update(animated: false)
} }
}
}, },
onInteractionUpdated: { [weak self] isInteracting in onInteractionUpdated: { [weak self] isInteracting in
if let self { if let self {