-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVChatSectionView.m
497 lines (435 loc) · 17.4 KB
/
MVChatSectionView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#import "MVChatSectionView.h"
#import "MVDiscussionView.h"
#import "MVBottomBarView.h"
#import "MVRoundedTextView.h"
#import "MVChatBlankslateView.h"
#import "MVActivityIndicatorView.h"
#import "NSObject+PerformBlockAfterDelay.h"
static NSGradient *backgroundGradient;
@interface MVChatSectionView () <MVRoundedTextViewDelegate,
MVDiscussionViewDelegate>
@property (strong, readwrite) MVBottomBarView *bottomBarView;
@property (strong, readwrite) MVRoundedTextView *textView;
@property (strong, readwrite) TUIButton *offlineButton;
@property (strong, readwrite) MVActivityIndicatorView *connectingSpinner;
@property (strong, readwrite) MVDiscussionView *discussionView;
@property (strong, readwrite) MVChatBlankslateView *blankslateView;
@property (readwrite) BOOL blankslateDisplayed;
- (void)updateFromOnline;
- (void)updateBottomBarViewFrame;
- (void)updateDiscussionViewFrame;
- (void)setDiscussionViewFront:(BOOL)front;
- (void)updateBlankslateVisibility:(BOOL)animated;
@end
@implementation MVChatSectionView
@synthesize bottomBarView = bottomBarView_,
textView = textView_,
offlineButton = offlineButton_,
connectingSpinner = connectingSpinner_,
discussionView = discussionView_,
blankslateView = blankslateView_,
blankslateDisplayed = blankslateDisplayed_,
state = state_,
delegate = delegate_;
+ (void)initialize
{
if(!backgroundGradient)
{
NSColor *bottomColor = [NSColor colorWithDeviceRed:0.8863
green:0.9059
blue:0.9529
alpha:1.0000];
NSColor *topColor = [NSColor colorWithDeviceRed:0.9216
green:0.9373
blue:0.9686
alpha:1.0000];
backgroundGradient = [[NSGradient alloc] initWithStartingColor:bottomColor
endingColor:topColor];
}
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self) {
self.opaque = NO;
self.backgroundColor = [TUIColor clearColor];
discussionView_ = nil;
textView_ = nil;
state_ = kMVChatSectionViewStateOnline;
bottomBarView_ = [[MVBottomBarView alloc] initWithFrame:
CGRectMake(0, 0, self.frame.size.width, 30)];
bottomBarView_.layer.zPosition = 2;
[self addSubview:bottomBarView_];
offlineButton_ = [[TUIButton alloc] initWithFrame:CGRectMake(0, 7, 16, 15)];
offlineButton_.autoresizingMask = TUIViewAutoresizingFlexibleLeftMargin |
TUIViewAutoresizingFlexibleRightMargin;
offlineButton_.dimsInBackground = NO;
[offlineButton_ setImage:[TUIImage imageNamed:@"icon_offline.png" cache:YES]
forState:TUIControlStateNormal];
[offlineButton_ setImage:[TUIImage imageNamed:@"icon_offline_active.png" cache:YES]
forState:TUIControlStateHighlighted];
[offlineButton_ addTarget:self
action:@selector(offlineButtonAction)
forControlEvents:TUIControlEventTouchUpInside];
[offlineButton_ setToolTip:NSLocalizedString(@"Try to reconnect",
@"Offline button tooltip")];
connectingSpinner_ = [[MVActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 7, 0, 0)];
connectingSpinner_.userInteractionEnabled = NO;
connectingSpinner_.style = kMVActivityIndicatorStyleBottomBar;
connectingSpinner_.autoresizingMask = TUIViewAutoresizingFlexibleLeftMargin |
TUIViewAutoresizingFlexibleRightMargin;
discussionView_ = [[MVDiscussionView alloc] initWithFrame:
CGRectMake(0, 30,
self.frame.size.width, self.frame.size.height)];
discussionView_.autoresizingMask = TUIViewAutoresizingFlexibleWidth |
TUIViewAutoresizingFlexibleHeight;
discussionView_.delegate = self;
textView_ = [[MVRoundedTextView alloc] initWithFrame:
CGRectMake(5, 0,
self.bottomBarView.bounds.size.width - 10, 29)];
textView_.autoresizingMask = TUIViewAutoresizingFlexibleWidth;
textView_.delegate = self;
textView_.layer.zPosition = 2;
[textView_ registerForDraggedTypes:[NSArray arrayWithObjects:
NSPasteboardTypeTIFF,
NSPasteboardTypePNG,
NSFilenamesPboardType,
nil]];
[self addSubview:self.discussionView];
[self setDiscussionViewFront:NO];
[self.discussionView addObserver:self forKeyPath:@"countItems" options:0 context:NULL];
[self.discussionView addObserver:self forKeyPath:@"allowsBlankslate" options:0 context:NULL];
[self addSubview:self.textView];
blankslateView_ = [[MVChatBlankslateView alloc] initWithFrame:CGRectMake(0, 0, 226, 130)];
blankslateView_.label = NSLocalizedString(@"Start discussing", @"Chat blankslate label");
blankslateView_.layer.opacity = 0.0;
__block __weak MVChatSectionView *weakSelf = self;
blankslateView_.drawBackground = ^(TUIView *view, CGRect rect)
{
float p1 = NSMinY(view.frame) / weakSelf.bounds.size.height;
float p2 = NSMaxY(view.frame) / weakSelf.bounds.size.height;
NSColor *startBackgroundColor = [NSColor colorWithDeviceRed:(0.8863 + (0.9216 - 0.8863) * p1)
green:(0.9059 + (0.9373 - 0.9059) * p1)
blue:(0.9529 + (0.9686 - 0.9529) * p1)
alpha:1.0000];
NSColor *endBackgroundColor = [NSColor colorWithDeviceRed:(0.8863 + (0.9216 - 0.8863) * p2)
green:(0.9059 + (0.9373 - 0.9059) * p2)
blue:(0.9529 + (0.9686 - 0.9529) * p2)
alpha:1.0000];
NSGradient *backgroundGradient = [[NSGradient alloc] initWithColorsAndLocations:
startBackgroundColor, 0.0,
endBackgroundColor, 1.0,
nil];
[backgroundGradient drawInRect:view.bounds angle:90];
};
blankslateView_.layout = ^(TUIView *view)
{
CGRect frame = weakSelf.blankslateView.frame;
frame.origin.x = round((weakSelf.bounds.size.width - frame.size.width) / 2);
float height = weakSelf.bounds.size.height - weakSelf.bottomBarView.frame.size.height;
frame.origin.y = round((height - frame.size.height) / 5 * 2.7);
return frame;
};
blankslateDisplayed_ = NO;
delegate_ = nil;
[self updateDiscussionViewFrame];
}
return self;
}
- (void)dealloc
{
if(self.discussionView)
{
[self.discussionView removeObserver:self forKeyPath:@"countItems"];
[self.discussionView removeObserver:self forKeyPath:@"allowsBlankslate"];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
if(!self.textView)
{
[self updateBottomBarViewFrame];
}
}
- (void)forwardKeyDownEventToTextView:(NSEvent*)event
{
if(self.textView)
{
[self.nsWindow tui_makeFirstResponder:self.textView];
CGPoint location = CGPointMake(10, 5);
location = [self.textView convertPoint:location toView:nil];
location = [self.nsView convertPoint:location toView:nil];
NSEvent *keyEvent = [NSEvent keyEventWithType:event.type
location:location
modifierFlags:event.modifierFlags
timestamp:event.timestamp
windowNumber:event.windowNumber
context:[NSGraphicsContext currentContext]
characters:event.characters
charactersIgnoringModifiers:event.charactersIgnoringModifiers
isARepeat:event.isARepeat
keyCode:event.keyCode];
[self.nsWindow postEvent:keyEvent atStart:YES];
}
}
#pragma mark -
#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if(([keyPath isEqualToString:@"countItems"] ||
[keyPath isEqualToString:@"allowsBlankslate"])
&& object == self.discussionView)
{
[self updateBlankslateVisibility:YES];
}
else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
#pragma mark -
#pragma mark Properties
- (void)setState:(int)state
{
if(state_ == state)
return;
state_ = state;
[self updateFromOnline];
}
#pragma mark -
#pragma mark Button Actions
- (void)offlineButtonAction
{
if([self.delegate respondsToSelector:@selector(chatSectionViewShouldTryReconnection:)])
[self.delegate chatSectionViewShouldTryReconnection:self];
}
#pragma mark -
#pragma mark MVRoundedTextViewDelegate Methods
- (void)roundedTextViewDidResize:(MVRoundedTextView *)chatTextView
animated:(BOOL)animated
{
[TUIView animateWithDuration:0.2 animations:^{
[TUIView setAnimationsEnabled:animated];
[self updateBottomBarViewFrame];
[TUIView setAnimationsEnabled:YES];
}];
}
- (BOOL)roundedTextView:(MVRoundedTextView*)chatTextView
sendString:(NSString*)string
{
if([self.delegate respondsToSelector:@selector(chatSectionView:sendString:)])
return [self.delegate chatSectionView:self sendString:string];
return NO;
}
- (void)roundedTextViewMoveUp:(MVRoundedTextView*)chatTextView
{
if(self.discussionView)
{
[self.nsWindow tui_makeFirstResponder:self.discussionView];
[self.discussionView resetSelectedItem];
[self.discussionView selectUp];
}
}
- (void)roundedTextViewDidBecomeFirstResponder:(MVRoundedTextView *)roundedTextView
{
if(self.discussionView)
{
[self.discussionView resetSelectedItem];
}
}
- (BOOL)roundedTextView:(MVRoundedTextView *)roundedTextView
pastePasteboard:(NSPasteboard *)pasteboard
{
if([self.delegate respondsToSelector:@selector(chatSectionView:pastePasteboard:)])
return [self.delegate chatSectionView:self pastePasteboard:pasteboard];
return NO;
}
- (BOOL)roundedTextView:(MVRoundedTextView *)roundedTextView
didDropPasteboard:(NSPasteboard *)pboard
{
if([self.delegate respondsToSelector:@selector(chatSectionView:dropPasteboard:)])
return [self.delegate chatSectionView:self dropPasteboard:pboard];
return NO;
}
- (void)roundedTextViewTextDidChange:(MVRoundedTextView *)roundedTextView
{
if([self.delegate respondsToSelector:@selector(chatSectionViewTextViewTextDidChange:
discussionView:)])
[self.delegate chatSectionViewTextViewTextDidChange:self
discussionView:self.discussionView];
}
#pragma mark -
#pragma mark MVDiscussionViewDelegate
- (void)discussionView:(MVDiscussionView*)discussionView
keyDown:(NSEvent*)event
{
[self forwardKeyDownEventToTextView:event];
}
- (void)discussionViewShouldBeFront:(MVDiscussionView*)discussionView
{
if(discussionView == self.discussionView)
[self setDiscussionViewFront:YES];
}
- (void)discussionViewShouldNotBeFront:(MVDiscussionView*)discussionView
{
if(discussionView == self.discussionView)
[self setDiscussionViewFront:NO];
}
- (void)discussionViewShouldGiveFocusToTextField:(MVDiscussionView*)discussionView
{
if(self.textView)
[self.nsWindow tui_makeFirstResponder:self.textView];
}
- (void)discussionViewShouldLoadPreviousItems:(MVDiscussionView*)discussionView
{
if([self.delegate respondsToSelector:@selector(chatSectionViewShouldLoadPreviousItems:
discussionView:)])
[self.delegate chatSectionViewShouldLoadPreviousItems:self
discussionView:discussionView];
}
- (void)discussionViewShouldLoadNextItems:(MVDiscussionView *)discussionView
{
if([self.delegate respondsToSelector:@selector(chatSectionViewShouldLoadNextItems:
discussionView:)])
[self.delegate chatSectionViewShouldLoadNextItems:self
discussionView:discussionView];
}
- (BOOL)discussionView:(MVDiscussionView *)discussionView
didDropPasteboard:(NSPasteboard*)pboard
{
if([self.delegate respondsToSelector:@selector(chatSectionView:dropPasteboard:)])
return [self.delegate chatSectionView:self dropPasteboard:pboard];
return NO;
}
- (void)discussionView:(MVDiscussionView *)discussionView
didClickNotification:(MVDiscussionMessageItem*)discussionItem
{
if([self.delegate respondsToSelector:@selector(chatSectionView:didClickNotification:)])
[self.delegate chatSectionView:self didClickNotification:discussionItem];
}
- (void)discussionView:(MVDiscussionView *)discussionView
shouldRetryFileTransfer:(MVDiscussionMessageItem*)discussionItem
{
if([self.delegate respondsToSelector:@selector(chatSectionView:shouldRetryFileTransfer:)])
[self.delegate chatSectionView:self shouldRetryFileTransfer:discussionItem];
}
- (void)discussionView:(MVDiscussionView *)discussionView
shouldRetrySendingMessage:(MVDiscussionMessageItem*)discussionItem
{
if([self.delegate respondsToSelector:@selector(chatSectionView:shouldRetrySendingMessage:)])
[self.delegate chatSectionView:self shouldRetrySendingMessage:discussionItem];
}
#pragma mark -
#pragma mark TUIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(TUIScrollView *)scrollView
{
if([self.delegate respondsToSelector:@selector(chatSectionViewDiscussionViewDidScroll:
discussionView:)])
[self.delegate chatSectionViewDiscussionViewDidScroll:self
discussionView:self.discussionView];
}
#pragma mark -
#pragma mark Private Methods
- (void)updateBlankslateVisibility:(BOOL)animated
{
BOOL showsBlankslate = (self.discussionView.countItems == 0 &&
self.discussionView.allowsBlankslate);
if(showsBlankslate != self.blankslateDisplayed)
{
if(showsBlankslate)
{
if(!self.blankslateView.superview)
{
[self.blankslateView setNeedsDisplay];
[self insertSubview:self.blankslateView belowSubview:self.discussionView];
}
[self.blankslateView removeAllAnimations];
[TUIView setAnimationsEnabled:animated block:^{
[TUIView animateWithDuration:0.4 animations:^{
self.blankslateView.layer.opacity = 1.0;
}];
}];
}
else
{
[self.blankslateView removeAllAnimations];
[TUIView setAnimationsEnabled:animated block:^{
[TUIView animateWithDuration:0.4 animations:^{
self.blankslateView.layer.opacity = 0.0;
}];
}];
[self mv_performBlock:^{
if(!self.blankslateDisplayed)
{
[self.blankslateView removeFromSuperview];
}
} afterDelay:(animated ? 0.4 : 0)];
}
self.blankslateDisplayed = showsBlankslate;
}
}
- (void)updateFromOnline
{
if(self.textView)
{
self.textView.editable = (self.state == kMVChatSectionViewStateOnline);
self.textView.hidden = (self.state != kMVChatSectionViewStateOnline);
}
if(self.state == kMVChatSectionViewStateOffline)
{
CGRect frame = self.offlineButton.frame;
frame.origin.x = round((self.bottomBarView.frame.size.width - frame.size.width) / 2);
self.offlineButton.frame = frame;
if(!self.offlineButton.superview)
[self.bottomBarView addSubview:self.offlineButton];
}
else
{
[self.offlineButton removeFromSuperview];
}
if(self.state == kMVChatSectionViewStateConnecting)
{
CGRect frame = self.connectingSpinner.frame;
frame.origin.x = round((self.bottomBarView.frame.size.width - frame.size.width) / 2);
self.connectingSpinner.frame = frame;
if(!self.connectingSpinner.superview)
{
[self.bottomBarView addSubview:self.connectingSpinner];
[self mv_performBlock:^{
[self.connectingSpinner startAnimating];
} afterDelay:0.01];
}
}
else
{
[self.connectingSpinner stopAnimating];
[self.connectingSpinner removeFromSuperview];
}
[self updateBottomBarViewFrame];
}
- (void)updateBottomBarViewFrame
{
[self.bottomBarView removeAllAnimations];
CGRect rect = CGRectMake(0, 0, self.frame.size.width, 30);
if(self.textView && self.state == kMVChatSectionViewStateOnline)
{
rect.size.height = self.textView.frame.size.height + 1;
}
[self.bottomBarView setFrame:rect];
[self updateDiscussionViewFrame];
}
- (void)updateDiscussionViewFrame
{
self.discussionView.frame = CGRectMake(0, self.bottomBarView.frame.size.height,
self.frame.size.width,
self.frame.size.height -
self.bottomBarView.frame.size.height);
}
- (void)setDiscussionViewFront:(BOOL)front
{
if(self.discussionView)
self.discussionView.layer.zPosition = (front ? 3 : 1);
}
@end