session-ios/Signal/src/ViewControllers/ConversationView/ConversationInputTextView.m

236 lines
6.7 KiB
Mathematica
Raw Normal View History

2017-10-10 22:13:54 +02:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2017-10-10 22:13:54 +02:00
//
#import "ConversationInputTextView.h"
#import "Signal-Swift.h"
2017-12-19 03:50:51 +01:00
#import <SignalMessaging/NSString+OWS.h>
2017-10-10 22:13:54 +02:00
NS_ASSUME_NONNULL_BEGIN
@interface ConversationInputTextView () <UITextViewDelegate>
@property (nonatomic) UILabel *placeholderView;
@property (nonatomic) NSArray<NSLayoutConstraint *> *placeholderConstraints;
@end
#pragma mark -
2017-10-10 22:13:54 +02:00
@implementation ConversationInputTextView
- (instancetype)init
{
self = [super init];
if (self) {
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
self.delegate = self;
2018-07-03 02:17:45 +02:00
// round edge with dynamic type.
CGFloat cornerRadius = 16.0f;
2017-10-10 22:13:54 +02:00
2018-07-03 02:17:45 +02:00
self.backgroundColor = [UIColor ows_light02Color];
self.layer.borderColor = [UIColor.ows_blackColor colorWithAlphaComponent:0.12f].CGColor;
2017-10-10 22:13:54 +02:00
self.layer.borderWidth = 0.5f;
self.layer.cornerRadius = cornerRadius;
2018-07-03 02:17:45 +02:00
self.scrollIndicatorInsets = UIEdgeInsetsMake(4, 4, 4, 4);
2017-10-10 22:13:54 +02:00
self.scrollEnabled = YES;
self.scrollsToTop = NO;
self.userInteractionEnabled = YES;
2018-07-03 02:17:45 +02:00
self.font = [UIFont ows_dynamicTypeBodyFont];
2017-10-10 22:13:54 +02:00
self.textColor = [UIColor blackColor];
self.textAlignment = NSTextAlignmentNatural;
self.contentMode = UIViewContentModeRedraw;
self.dataDetectorTypes = UIDataDetectorTypeNone;
self.text = nil;
self.placeholderView = [UILabel new];
self.placeholderView.text = NSLocalizedString(@"new_message", @"");
self.placeholderView.textColor = [UIColor lightGrayColor];
2017-10-18 21:21:17 +02:00
self.placeholderView.userInteractionEnabled = NO;
[self addSubview:self.placeholderView];
// We need to do these steps _after_ placeholderView is configured.
self.font = [UIFont ows_dynamicTypeBodyFont];
2018-07-03 02:17:45 +02:00
self.textContainerInset = UIEdgeInsetsMake(4.0f, 8.0, 4.0f, 2.0f);
self.contentInset = UIEdgeInsetsMake(1.0f, 0.0f, 1.0f, 0.0f);
[self ensurePlaceholderConstraints];
[self updatePlaceholderVisibility];
2017-10-10 22:13:54 +02:00
}
return self;
}
- (void)setFont:(UIFont *_Nullable)font
{
[super setFont:font];
self.placeholderView.font = font;
}
- (void)setContentInset:(UIEdgeInsets)contentInset
{
[super setContentInset:contentInset];
[self ensurePlaceholderConstraints];
}
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
{
[super setTextContainerInset:textContainerInset];
[self ensurePlaceholderConstraints];
}
- (void)ensurePlaceholderConstraints
{
OWSAssert(self.placeholderView);
if (self.placeholderConstraints) {
[NSLayoutConstraint deactivateConstraints:self.placeholderConstraints];
}
// We align the location of our placeholder with the text content of
// this view. The only safe way to do that is by measuring the
// beginning position.
UITextRange *beginningTextRange =
[self textRangeFromPosition:self.beginningOfDocument toPosition:self.beginningOfDocument];
CGRect beginningTextRect = [self firstRectForRange:beginningTextRange];
CGFloat topInset = beginningTextRect.origin.y;
2018-07-03 02:17:45 +02:00
CGFloat leftInset = beginningTextRect.origin.x;
2018-07-03 02:17:45 +02:00
// we use Left instead of Leading, since it's based on the prior CGRect offset
self.placeholderConstraints = @[
2018-07-03 02:17:45 +02:00
[self.placeholderView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:leftInset],
[self.placeholderView autoPinEdgeToSuperviewEdge:ALEdgeRight],
[self.placeholderView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:topInset],
];
}
- (void)updatePlaceholderVisibility
{
2017-10-24 17:43:42 +02:00
self.placeholderView.hidden = self.text.length > 0;
}
- (void)setText:(NSString *_Nullable)text
{
[super setText:text];
[self updatePlaceholderVisibility];
}
2017-10-10 22:13:54 +02:00
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)pasteboardHasPossibleAttachment
{
// We don't want to load/convert images more than once so we
// only do a cursory validation pass at this time.
return ([SignalAttachment pasteboardHasPossibleAttachment] && ![SignalAttachment pasteboardHasText]);
}
- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender
{
if (action == @selector(paste:)) {
if ([self pasteboardHasPossibleAttachment]) {
return YES;
}
}
return [super canPerformAction:action withSender:sender];
}
- (void)paste:(nullable id)sender
{
if ([self pasteboardHasPossibleAttachment]) {
SignalAttachment *attachment = [SignalAttachment attachmentFromPasteboard];
// Note: attachment might be nil or have an error at this point; that's fine.
[self.inputTextViewDelegate didPasteAttachment:attachment];
return;
}
[super paste:sender];
}
- (NSString *)trimmedText
{
2017-10-18 20:53:31 +02:00
return [self.text ows_stripped];
2017-10-10 22:13:54 +02:00
}
#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// TODO: Is this necessary?
[textView becomeFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView
{
OWSAssert(self.textViewToolbarDelegate);
[self updatePlaceholderVisibility];
[self.textViewToolbarDelegate textViewDidChange];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[textView resignFirstResponder];
}
#pragma mark - Key Commands
- (nullable NSArray<UIKeyCommand *> *)keyCommands
{
// We're permissive about what modifier key we accept for the "send message" hotkey.
2017-11-01 17:48:07 +01:00
// We accept command-return, option-return.
//
// We don't support control-return because it doesn't work.
//
// We don't support shift-return because it is often used for "newline" in other
// messaging apps.
return @[
2017-11-29 18:31:02 +01:00
[self keyCommandWithInput:@"\r"
modifierFlags:UIKeyModifierCommand
action:@selector(modifiedReturnPressed:)
discoverabilityTitle:@"Send Message"],
// "Alternate" is option.
2017-11-29 18:31:02 +01:00
[self keyCommandWithInput:@"\r"
modifierFlags:UIKeyModifierAlternate
action:@selector(modifiedReturnPressed:)
discoverabilityTitle:@"Send Message"],
];
}
2017-11-29 18:31:02 +01:00
- (UIKeyCommand *)keyCommandWithInput:(NSString *)input
modifierFlags:(UIKeyModifierFlags)modifierFlags
action:(SEL)action
discoverabilityTitle:(NSString *)discoverabilityTitle
{
return [UIKeyCommand keyCommandWithInput:input
modifierFlags:modifierFlags
action:action
discoverabilityTitle:discoverabilityTitle];
2017-11-29 18:31:02 +01:00
}
- (void)modifiedReturnPressed:(UIKeyCommand *)sender
{
DDLogInfo(@"%@ modifiedReturnPressed: %@", self.logTag, sender.input);
[self.inputTextViewDelegate inputTextViewSendMessagePressed];
}
2017-10-10 22:13:54 +02:00
@end
NS_ASSUME_NONNULL_END