Add undo/redo buttons to image editor.

This commit is contained in:
Matthew Chen 2018-12-17 17:05:43 -05:00
parent 967da78f29
commit 9378ab2192
3 changed files with 72 additions and 3 deletions

View File

@ -311,9 +311,15 @@
/* Label for generic done button. */
"BUTTON_DONE" = "Done";
/* Label for redo button. */
"BUTTON_REDO" = "Redo";
/* Button text to enable batch selection mode */
"BUTTON_SELECT" = "Select";
/* Label for undo button. */
"BUTTON_UNDO" = "Undo";
/* Label for button that lets users call a contact again. */
"CALL_AGAIN_BUTTON_TITLE" = "Call Again";

View File

@ -857,8 +857,6 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
private(set) var contentContainer: UIView!
private(set) var playVideoButton: UIView?
private var imageEditorView: ImageEditorView?
// MARK: - Initializers
init(attachmentItem: SignalAttachmentItem) {
@ -954,7 +952,8 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD
imageMediaView.isUserInteractionEnabled = true
imageMediaView.addSubview(imageEditorView)
imageEditorView.autoPinEdgesToSuperviewEdges()
self.imageEditorView = imageEditorView
imageEditorView.addControls(to: self.mediaMessageView)
}
#endif

View File

@ -29,8 +29,70 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate {
notImplemented()
}
// MARK: - Buttons
private let undoButton = UIButton(type: .custom)
private let redoButton = UIButton(type: .custom)
@objc
public func addControls(to containerView: UIView) {
configure(button: undoButton,
label: NSLocalizedString("BUTTON_UNDO", comment: "Label for undo button."),
selector: #selector(didTapUndo(sender:)))
configure(button: redoButton,
label: NSLocalizedString("BUTTON_REDO", comment: "Label for redo button."),
selector: #selector(didTapRedo(sender:)))
let stackView = UIStackView(arrangedSubviews: [undoButton, redoButton])
stackView.axis = .vertical
stackView.alignment = .center
stackView.spacing = 10
containerView.addSubview(stackView)
stackView.autoAlignAxis(toSuperviewAxis: .horizontal)
stackView.autoPinTrailingToSuperviewMargin(withInset: 10)
updateButtons()
}
private func configure(button: UIButton,
label: String,
selector: Selector) {
button.setTitle(label, for: .normal)
button.setTitleColor(.white,
for: .normal)
button.setTitleColor(.gray,
for: .disabled)
button.titleLabel?.font = UIFont.ows_dynamicTypeBody.ows_mediumWeight()
button.addTarget(self, action: selector, for: .touchUpInside)
}
private func updateButtons() {
undoButton.isEnabled = model.canUndo()
redoButton.isEnabled = model.canRedo()
}
// MARK: - Actions
@objc func didTapUndo(sender: UIButton) {
Logger.verbose("")
guard model.canUndo() else {
owsFailDebug("Can't undo.")
return
}
model.undo()
}
@objc func didTapRedo(sender: UIButton) {
Logger.verbose("")
guard model.canRedo() else {
owsFailDebug("Can't redo.")
return
}
model.redo()
}
// These properties are non-empty while drawing a stroke.
private var currentStroke: ImageEditorStrokeItem?
private var currentStrokeSamples = [ImageEditorStrokeItem.StrokeSample]()
@ -103,6 +165,8 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate {
// TODO: We eventually want to narrow our change events
// to reflect the specific item(s) which changed.
updateAllContent()
updateButtons()
}
// MARK: - Accessor Overrides