Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2018 Savoir-faire Linux Inc. |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 4 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #import "ConversationVC.h" |
| 22 | |
| 23 | #import <QItemSelectionModel> |
| 24 | #import <qstring.h> |
| 25 | #import <QPixmap> |
| 26 | #import <QtMacExtras/qmacfunctions.h> |
| 27 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 28 | // LRC |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 29 | #import <globalinstances.h> |
| 30 | |
| 31 | #import "views/IconButton.h" |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 32 | #import "views/HoverButton.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 33 | #import "views/IMTableCellView.h" |
| 34 | #import "views/NSColor+RingTheme.h" |
| 35 | #import "QNSTreeController.h" |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 36 | #import "INDSequentialTextSelectionManager.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 37 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 38 | #import "PhoneDirectoryModel.h" |
| 39 | #import "account.h" |
| 40 | #import "AvailableAccountModel.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 41 | #import "MessagesVC.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 42 | #import "utils.h" |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 43 | #import "RingWindowController.h" |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 44 | #import "NSString+Extensions.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 45 | |
| 46 | #import <QuartzCore/QuartzCore.h> |
| 47 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 48 | @interface ConversationVC () { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 49 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 50 | __unsafe_unretained IBOutlet NSTextField* messageField; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 51 | NSMutableString* textSelection; |
| 52 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 53 | __unsafe_unretained IBOutlet NSView* sendPanel; |
| 54 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet NSTextField *conversationID; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 56 | __unsafe_unretained IBOutlet IconButton* sendButton; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet IconButton *sendFileButton; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 58 | __unsafe_unretained IBOutlet HoverButton *addContactButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 59 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
| 60 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 61 | IBOutlet MessagesVC* messagesViewVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 62 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 63 | IBOutlet NSLayoutConstraint *titleCenteredConstraint; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 64 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 65 | |
| 66 | std::string convUid_; |
| 67 | const lrc::api::conversation::Info* cachedConv_; |
| 68 | lrc::api::ConversationModel* convModel_; |
| 69 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 70 | RingWindowController* delegate; |
| 71 | |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 72 | // All those connections are needed to invalidate cached conversation as pointer |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 73 | // may not be referencing the same conversation anymore |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 74 | QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_; |
| 75 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 78 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 79 | @end |
| 80 | |
| 81 | @implementation ConversationVC |
| 82 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 83 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow |
| 84 | { |
| 85 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 86 | { |
| 87 | delegate = mainWindow; |
| 88 | } |
| 89 | return self; |
| 90 | } |
| 91 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 92 | - (void)setMessage:(NSString *)newValue { |
| 93 | _message = [newValue removeEmptyLinesAtBorders]; |
| 94 | } |
| 95 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 96 | -(void) clearData { |
| 97 | cachedConv_ = nil; |
| 98 | convUid_ = ""; |
| 99 | convModel_ = nil; |
| 100 | |
| 101 | [messagesViewVC clearData]; |
| 102 | QObject::disconnect(modelSortedConnection_); |
| 103 | QObject::disconnect(filterChangedConnection_); |
| 104 | QObject::disconnect(newConversationConnection_); |
| 105 | QObject::disconnect(conversationRemovedConnection_); |
| 106 | } |
| 107 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 108 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 109 | { |
| 110 | if (convModel_ == nil || convUid_.empty()) |
| 111 | return nil; |
| 112 | |
| 113 | if (cachedConv_ != nil) |
| 114 | return cachedConv_; |
| 115 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 116 | auto convQueue = convModel_->allFilteredConversations(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 117 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 118 | auto it = getConversationFromUid(convUid_, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 119 | |
| 120 | if (it != convQueue.end()) |
| 121 | cachedConv_ = &(*it); |
| 122 | |
| 123 | return cachedConv_; |
| 124 | } |
| 125 | |
| 126 | -(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model { |
| 127 | if (convUid_ == convUid && convModel_ == model) |
| 128 | return; |
| 129 | |
| 130 | cachedConv_ = nil; |
| 131 | convUid_ = convUid; |
| 132 | convModel_ = model; |
| 133 | |
| 134 | [messagesViewVC setConversationUid:convUid_ model:convModel_]; |
| 135 | |
| 136 | if (convUid_.empty() || convModel_ == nil) |
| 137 | return; |
| 138 | |
| 139 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 140 | // after a reordering. |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 141 | QObject::disconnect(modelSortedConnection_); |
| 142 | QObject::disconnect(filterChangedConnection_); |
| 143 | QObject::disconnect(newConversationConnection_); |
| 144 | QObject::disconnect(conversationRemovedConnection_); |
| 145 | modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 146 | [self](){ |
| 147 | cachedConv_ = nil; |
| 148 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 149 | filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 150 | [self](){ |
| 151 | cachedConv_ = nil; |
| 152 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 153 | newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 154 | [self](){ |
| 155 | cachedConv_ = nil; |
| 156 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 157 | conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 158 | [self](){ |
| 159 | cachedConv_ = nil; |
| 160 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 161 | |
| 162 | auto* conv = [self getCurrentConversation]; |
| 163 | |
| 164 | if (conv == nil) |
| 165 | return; |
| 166 | |
| 167 | // Setup UI elements according to new conversation |
| 168 | NSString* bestName = bestNameForConversation(*conv, *convModel_); |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 169 | NSString* bestId = bestIDForConversation(*conv, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 170 | [conversationTitle setStringValue: bestName]; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 171 | [conversationID setStringValue: bestId]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 172 | [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 173 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 174 | BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 175 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 176 | [conversationID setHidden:hideBestId]; |
| 177 | [titleCenteredConstraint setActive:hideBestId]; |
| 178 | [titleTopConstraint setActive:!hideBestId]; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 179 | auto accountType = convModel_->owner.profileInfo.type; |
| 180 | [addContactButton setHidden:((convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::TEMPORARY) || accountType == lrc::api::profile::Type::SIP)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 183 | - (void)loadView { |
| 184 | [super loadView]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 185 | // Do view setup here. |
| 186 | [self.view setWantsLayer:YES]; |
| 187 | [self.view setLayer:[CALayer layer]]; |
| 188 | [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor]; |
| 189 | [self.view.layer setCornerRadius:5.0f]; |
| 190 | |
Kateryna Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 191 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 194 | -(Account* ) chosenAccount |
| 195 | { |
| 196 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 197 | if(!index.isValid()) { |
| 198 | return nullptr; |
| 199 | } |
| 200 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 201 | return account; |
| 202 | } |
| 203 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 204 | - (void) initFrame |
| 205 | { |
| 206 | [self.view setFrame:self.view.superview.bounds]; |
| 207 | [self.view setHidden:YES]; |
| 208 | self.view.layer.position = self.view.frame.origin; |
| 209 | } |
| 210 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 211 | - (IBAction)sendMessage:(id)sender |
| 212 | { |
| 213 | /* make sure there is text to send */ |
| 214 | NSString* text = self.message; |
| 215 | if (text && text.length > 0) { |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 216 | auto* conv = [self getCurrentConversation]; |
| 217 | bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING; |
Anthony Léonard | 4b0935f | 2017-12-26 12:05:22 -0500 | [diff] [blame] | 218 | convModel_->sendMessage(convUid_, std::string([text UTF8String])); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 219 | self.message = @""; |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 220 | if (isPending) |
| 221 | [delegate currentConversationTrusted]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 225 | - (IBAction)sendFile:(id)sender |
| 226 | { |
| 227 | NSOpenPanel* filePicker = [NSOpenPanel openPanel]; |
| 228 | [filePicker setCanChooseFiles:YES]; |
| 229 | [filePicker setCanChooseDirectories:NO]; |
| 230 | [filePicker setAllowsMultipleSelection:NO]; |
| 231 | |
| 232 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 233 | if ([[filePicker URLs] count] == 1) { |
| 234 | NSURL* url = [[filePicker URLs] objectAtIndex:0]; |
| 235 | const char* fullPath = [url fileSystemRepresentation]; |
| 236 | NSString* fileName = [url lastPathComponent]; |
| 237 | if (convModel_) { |
Anthony Léonard | 0c65bd1 | 2018-02-08 16:47:19 -0500 | [diff] [blame] | 238 | auto* conv = [self getCurrentConversation]; |
| 239 | bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING; |
| 240 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 241 | convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String])); |
Anthony Léonard | 0c65bd1 | 2018-02-08 16:47:19 -0500 | [diff] [blame] | 242 | |
| 243 | if (isPending) |
| 244 | [delegate currentConversationTrusted]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 250 | - (IBAction)placeCall:(id)sender |
| 251 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 252 | auto* conv = [self getCurrentConversation]; |
| 253 | convModel_->placeCall(conv->uid); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 256 | - (IBAction)placeAudioCall:(id)sender |
| 257 | { |
| 258 | auto* conv = [self getCurrentConversation]; |
| 259 | convModel_->placeAudioOnlyCall(conv->uid); |
| 260 | } |
| 261 | |
| 262 | - (IBAction)addContact:(id)sender |
| 263 | { |
| 264 | auto* conv = [self getCurrentConversation]; |
| 265 | convModel_->makePermanent(conv->uid); |
| 266 | } |
| 267 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 268 | - (IBAction)backPressed:(id)sender { |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 269 | [delegate rightPanelClosed]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 270 | [self hideWithAnimation:false]; |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 273 | # pragma mark private IN/OUT animations |
| 274 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 275 | -(void) showWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 276 | { |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 277 | if (!animate) { |
| 278 | [self.view setHidden:NO]; |
| 279 | return; |
| 280 | } |
| 281 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 282 | CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0); |
| 283 | [self.view setHidden:NO]; |
| 284 | |
| 285 | [CATransaction begin]; |
| 286 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 287 | [animation setFromValue:[NSValue valueWithPoint:frame.origin]]; |
| 288 | [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]]; |
| 289 | [animation setDuration:0.2f]; |
| 290 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; |
| 291 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 292 | |
| 293 | [CATransaction commit]; |
| 294 | } |
| 295 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 296 | -(void) hideWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 297 | { |
| 298 | if(self.view.frame.origin.x < 0) { |
| 299 | return; |
| 300 | } |
| 301 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 302 | [self clearData]; |
| 303 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 304 | if (!animate) { |
| 305 | [self.view setHidden:YES]; |
| 306 | return; |
| 307 | } |
| 308 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 309 | CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0); |
| 310 | [CATransaction begin]; |
| 311 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 312 | [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]]; |
| 313 | [animation setToValue:[NSValue valueWithPoint:frame.origin]]; |
| 314 | [animation setDuration:0.2f]; |
| 315 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
| 316 | |
| 317 | [CATransaction setCompletionBlock:^{ |
| 318 | [self.view setHidden:YES]; |
| 319 | }]; |
| 320 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 321 | |
| 322 | [self.view.layer setPosition:frame.origin]; |
| 323 | [CATransaction commit]; |
| 324 | } |
| 325 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 326 | #pragma mark - NSTextFieldDelegate |
| 327 | |
| 328 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 329 | { |
| 330 | if (commandSelector == @selector(insertNewline:) && self.message.length > 0) { |
| 331 | [self sendMessage:nil]; |
| 332 | return YES; |
| 333 | } |
| 334 | return NO; |
| 335 | } |
| 336 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 337 | |
| 338 | @end |