From 2294a7299701f9bc36366429504bccbd55c5bdcc Mon Sep 17 00:00:00 2001 From: Sam Steele Date: Thu, 17 Jun 2021 07:46:09 -0400 Subject: [PATCH] Reduce layout passes while typing when the text area height hasn't changed --- IRCCloud/Classes/MainViewController.m | 82 ++++++++++++++++----------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/IRCCloud/Classes/MainViewController.m b/IRCCloud/Classes/MainViewController.m index caa810e2..ca07e160 100644 --- a/IRCCloud/Classes/MainViewController.m +++ b/IRCCloud/Classes/MainViewController.m @@ -2484,39 +2484,58 @@ -(void)cancelSuggestionsTimer { -(void)_updateMessageWidth { self->_message.animateHeightChange = NO; + BOOL dirty = NO; + CGFloat messageWidth; + if(self->_message.text.length > 0) { - CGFloat c = self->_eventsViewWidthConstraint.constant - _sendBtn.frame.size.width - _message.frame.origin.x - 16; - c -= self.slidingViewController.view.safeAreaInsets.right; - if(self->_messageWidthConstraint.constant != c) - self->_messageWidthConstraint.constant = c; - [self.view layoutIfNeeded]; - self->_sendBtn.enabled = YES; - self->_sendBtn.alpha = 1; - self->_settingsBtn.enabled = NO; - self->_settingsBtn.alpha = 0; + messageWidth = self->_eventsViewWidthConstraint.constant - _sendBtn.frame.size.width - _message.frame.origin.x - 16; } else { - self->_messageWidthConstraint.constant = self->_eventsViewWidthConstraint.constant - _settingsBtn.frame.size.width - _message.frame.origin.x - 16; - self->_messageWidthConstraint.constant -= self.slidingViewController.view.safeAreaInsets.right; - [self.view layoutIfNeeded]; - self->_sendBtn.enabled = NO; - self->_sendBtn.alpha = 0; - self->_settingsBtn.enabled = YES; - self->_settingsBtn.alpha = 1; - self->_message.delegate = nil; - [self->_message clearText]; - self->_message.delegate = self; + messageWidth = self->_eventsViewWidthConstraint.constant - _settingsBtn.frame.size.width - _message.frame.origin.x - 16; + } + messageWidth -= self.slidingViewController.view.safeAreaInsets.right; + + if(self->_message.text.length > 0) { + if(self->_messageWidthConstraint.constant != messageWidth) { + self->_messageWidthConstraint.constant = messageWidth; + [self.view layoutIfNeeded]; + dirty = YES; + } + if(self->_sendBtn.alpha != 1) { + self->_sendBtn.enabled = YES; + self->_sendBtn.alpha = 1; + self->_settingsBtn.enabled = NO; + self->_settingsBtn.alpha = 0; + } + } else { + if(self->_messageWidthConstraint.constant != messageWidth) { + self->_messageWidthConstraint.constant = messageWidth; + [self.view layoutIfNeeded]; + dirty = YES; + } + if(self->_sendBtn.alpha != 0) { + self->_sendBtn.enabled = NO; + self->_sendBtn.alpha = 0; + self->_settingsBtn.enabled = YES; + self->_settingsBtn.alpha = 1; + self->_message.delegate = nil; + [self->_message clearText]; + self->_message.delegate = self; + } } self->_message.animateHeightChange = YES; - self->_messageHeightConstraint.constant = self->_message.frame.size.height; - self->_bottomBarHeightConstraint.constant = self->_message.frame.size.height + 8; - CGRect frame = self->_settingsBtn.frame; - frame.origin.x = self->_eventsViewWidthConstraint.constant - _settingsBtn.frame.size.width - 10 - self.slidingViewController.view.safeAreaInsets.right; - self->_settingsBtn.frame = frame; - frame = self->_sendBtn.frame; - frame.origin.x = self->_eventsViewWidthConstraint.constant - _sendBtn.frame.size.width - 8 - self.slidingViewController.view.safeAreaInsets.right; - self->_sendBtn.frame = frame; - [self _updateEventsInsets]; - [self.view layoutIfNeeded]; + + if(dirty || self->_messageHeightConstraint.constant != self->_message.frame.size.height) { + self->_messageHeightConstraint.constant = self->_message.frame.size.height; + self->_bottomBarHeightConstraint.constant = self->_message.frame.size.height + 8; + CGRect frame = self->_settingsBtn.frame; + frame.origin.x = self->_eventsViewWidthConstraint.constant - _settingsBtn.frame.size.width - 10 - self.slidingViewController.view.safeAreaInsets.right; + self->_settingsBtn.frame = frame; + frame = self->_sendBtn.frame; + frame.origin.x = self->_eventsViewWidthConstraint.constant - _sendBtn.frame.size.width - 8 - self.slidingViewController.view.safeAreaInsets.right; + self->_sendBtn.frame = frame; + [self _updateEventsInsets]; + [self.view layoutIfNeeded]; + } } -(BOOL)expandingTextView:(UIExpandingTextView *)expandingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { @@ -2550,9 +2569,8 @@ -(void)expandingTextViewDidChange:(UIExpandingTextView *)expandingTextView { [self->_sendBtn setTitleColor:c forState:UIControlStateDisabled]; [self->_sendBtn setTitleColor:c forState:UIControlStateHighlighted]; - if(self->_handoffTimer) - [self->_handoffTimer invalidate]; - self->_handoffTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(_updateHandoffTimer) userInfo:nil repeats:NO]; + if(!self->_handoffTimer) + self->_handoffTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(_updateHandoffTimer) userInfo:nil repeats:NO]; } -(void)_cancelHandoffTimer {