blob: 14d8267d1b465538bfa1bfd3cb9bcef97f2765c8 [file] [log] [blame]
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -04001/*
2* Copyright (C) 2020 Savoir-faire Linux Inc.
3* Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
4*
5* This program is free software; you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation; either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program; if not, write to the Free Software
17* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#import "ConferenceOverlayView.h"
kkostiuk3a2c9f72021-01-27 10:30:49 -050021#import "CustomBackgroundView.h"
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -040022
23@implementation ConferenceOverlayView
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -040024
kkostiuk3a2c9f72021-01-27 10:30:49 -050025CGFloat const margin = 2;
26CGFloat const controlSize = 25;
27CGFloat const minWidth = 140;
28CGFloat const minHeight = 80;
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -040029
30- (instancetype)initWithFrame:(NSRect)frame
31{
32 self = [super initWithFrame:frame];
33 if (self) {
34 self.translatesAutoresizingMaskIntoConstraints = false;
35 [self setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable | NSViewMinYMargin | NSViewMaxYMargin | NSViewMinXMargin | NSViewMaxXMargin];
36 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sizeChanged) name:NSWindowDidResizeNotification object:nil];
kkostiuk3a2c9f72021-01-27 10:30:49 -050037 self.wantsLayer = true;
38 self.layer.masksToBounds = false;
39 [self addViews];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -040040 }
41 return self;
42}
43
kkostiuk3a2c9f72021-01-27 10:30:49 -050044- (void)addViews {
45 self.increasedBackgroundView = [[NSView alloc] init];
46 [self.increasedBackgroundView setWantsLayer: YES];
47 self.increasedBackgroundView.layer.backgroundColor = [[NSColor colorWithCalibratedRed: 0 green: 0 blue: 0 alpha: 0.8] CGColor];
48 self.increasedBackgroundView.translatesAutoresizingMaskIntoConstraints = false;
49 [self addSubview: self.increasedBackgroundView];
50 self.increasedBackgroundView.hidden = true;
51 self.increasedBackgroundView.layer.masksToBounds = true;
52 self.increasedBackgroundView.layer.cornerRadius = 6;
53
54 self.backgroundView = [[NSView alloc] init];
55 [self.backgroundView setWantsLayer: YES];
56 self.backgroundView.layer.backgroundColor = [[NSColor colorWithCalibratedRed: 0 green: 0 blue: 0 alpha: 0.6] CGColor];
57 self.backgroundView.translatesAutoresizingMaskIntoConstraints = false;
58 [self addSubview: self.backgroundView];
59 self.backgroundView.hidden = true;
60
61 //participat state
62 self.audioState = [[CustomBackgroundView alloc] init];
63 self.audioState.backgroundType = RECTANGLE_WITH_ROUNDED_RIGHT_CORNER;
64 [self.audioState.widthAnchor constraintEqualToConstant: controlSize].active = true;
65 [self.audioState.heightAnchor constraintEqualToConstant: controlSize].active = true;
66 NSImage* audioImage = [NSImage imageNamed: @"ic_moderator_audio_muted.png"];
67 self.audioState.image = audioImage;
68
69 self.moderatorState = [[CustomBackgroundView alloc] init];
70 self.moderatorState.backgroundType = RECTANGLE;
71 [self.moderatorState.widthAnchor constraintEqualToConstant: controlSize].active = true;
72 [self.moderatorState.heightAnchor constraintEqualToConstant: controlSize].active = true;
73 NSImage* moderatorImage = [NSImage imageNamed: @"ic_moderator.png"];
74 self.moderatorState.image = moderatorImage;
75
76 self.hostState = [[CustomBackgroundView alloc] init];
77 self.hostState.backgroundType = RECTANGLE;
78 [self.hostState.widthAnchor constraintEqualToConstant: controlSize].active = true;
79 [self.hostState.heightAnchor constraintEqualToConstant: controlSize].active = true;
80 NSImage* hostImage = [NSImage imageNamed: @"ic_star.png"];
81 self.hostState.image = hostImage;
82
83 self.cusp = [[CustomBackgroundView alloc] init];
84 self.cusp.backgroundType = CUSP;
85 [self.cusp.widthAnchor constraintEqualToConstant: 6].active = true;
86 [self.cusp.heightAnchor constraintEqualToConstant: controlSize].active = true;
87
88 NSArray *statesViews = [NSArray arrayWithObjects: self.hostState, self.moderatorState, self.audioState, self.cusp, nil];
89 self.states = [NSStackView stackViewWithViews: statesViews];
90 self.states.spacing = 0;
91 [self addSubview: self.states];
92
93 //actions
94 self.maximize = [self getActionbutton];
95 NSImage* maximizeImage = [NSImage imageNamed: @"ic_moderator_maximize.png"];
96 [self.maximize setImage: maximizeImage];
97 [self.maximize setAction:@selector(maximize:)];
98 [self.maximize setTarget:self];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -040099
kkostiuk3a2c9f72021-01-27 10:30:49 -0500100 self.minimize = [self getActionbutton];
101 NSImage* minimizeImage = [NSImage imageNamed: @"ic_moderator_minimize.png"];
102 [self.minimize setImage: minimizeImage];
103 [self.minimize setAction:@selector(minimize:)];
104 [self.minimize setTarget:self];
105
106 self.hangup = [self getActionbutton];
107 NSImage* hangupImage = [NSImage imageNamed: @"ic_moderator_hangup.png"];
108 [self.hangup setImage: hangupImage];
109 [self.hangup setAction:@selector(finishCall:)];
110 [self.hangup setTarget:self];
111
112 self.setModerator = [self getActionbutton];
113 NSImage* setModeratorImage = [NSImage imageNamed: @"ic_moderator.png"];
114 [self.setModerator setImage: setModeratorImage];
115 [self.setModerator setAction:@selector(setModerator:)];
116 [self.setModerator setTarget:self];
117
118 self.muteAudio = [self getActionbutton];
119 NSImage* muteAudioImage = [NSImage imageNamed: @"ic_moderator_audio_muted.png"];
120 [self.muteAudio setImage: muteAudioImage];
121 [self.muteAudio setAction:@selector(muteAudio:)];
122 [self.muteAudio setTarget:self];
123
124 NSArray *actions = [NSArray arrayWithObjects: self.setModerator, self.muteAudio, self.maximize, self.minimize, self.hangup, nil];
125 self.buttonsContainer = [NSStackView stackViewWithViews: actions];
126 self.buttonsContainer.orientation = NSUserInterfaceLayoutOrientationHorizontal;
127 self.buttonsContainer.spacing = 5;
128
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400129 self.usernameLabel = [[NSTextView alloc] init];
kkostiuk3a2c9f72021-01-27 10:30:49 -0500130 self.usernameLabel.alignment = NSTextAlignmentCenter;
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400131 self.usernameLabel.textColor = [NSColor whiteColor];
kkostiuk3a2c9f72021-01-27 10:30:49 -0500132 self.usernameLabel.editable = false;
133 self.usernameLabel.drawsBackground = false;
134 self.usernameLabel.font = [NSFont userFontOfSize: 13.0];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400135 self.usernameLabel.translatesAutoresizingMaskIntoConstraints = false;
kkostiuk3a2c9f72021-01-27 10:30:49 -0500136 [self.usernameLabel.heightAnchor constraintEqualToConstant: 20].active = true;
137 [self.usernameLabel.widthAnchor constraintGreaterThanOrEqualToConstant: 60].active = true;
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400138 self.usernameLabel.textContainer.maximumNumberOfLines = 1;
kkostiuk3a2c9f72021-01-27 10:30:49 -0500139
140 NSArray* infoItems = [NSArray arrayWithObjects: self.usernameLabel, self.buttonsContainer, nil];
141
142 self.infoContainer = [NSStackView stackViewWithViews: infoItems];
143 self.infoContainer.orientation = NSUserInterfaceLayoutOrientationVertical;
144 self.infoContainer.spacing = 0;
145 self.infoContainer.distribution = NSStackViewDistributionFillEqually;
146 self.infoContainer.alignment = NSLayoutAttributeCenterX;
147 [self.backgroundView addSubview: self.infoContainer];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400148}
149
kkostiuk3a2c9f72021-01-27 10:30:49 -0500150- (IconButton*) getActionbutton {
151 IconButton* button = [[IconButton alloc] init];
152 button.transparent = true;
153 [button.widthAnchor constraintEqualToConstant: controlSize].active = true;
154 [button.heightAnchor constraintEqualToConstant: controlSize].active = true;
155 button.title = @"";
156 button.buttonDisableColor = [NSColor lightGrayColor];
157 button.bgColor = [NSColor clearColor];
158 button.imageColor = [NSColor whiteColor];
159 button.imagePressedColor = [NSColor lightGrayColor];
160 button.imageInsets = margin;
161 button.translatesAutoresizingMaskIntoConstraints = false;
162 return button;
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400163}
164
kkostiuk3a2c9f72021-01-27 10:30:49 -0500165- (void)configureView {
166 [self.backgroundView.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier: 1].active = TRUE;
167 [self.backgroundView.topAnchor constraintEqualToAnchor:self.topAnchor].active = true;
168 [self.backgroundView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = true;
169 [self.backgroundView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = true;
170
171 [self.states.topAnchor constraintEqualToAnchor:self.topAnchor].active = true;
172 [self.states.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = true;
173
174 [self.infoContainer.centerYAnchor constraintEqualToAnchor:self.centerYAnchor constant:1].active = true;
175 [self.infoContainer.centerXAnchor constraintEqualToAnchor:self.centerXAnchor constant:1].active = true;
176
177 [self.increasedBackgroundView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor constant:1].active = true;
178 [self.increasedBackgroundView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor constant:1].active = true;
179 [self.increasedBackgroundView.widthAnchor constraintEqualToConstant:minWidth].active = true;
180 [self.increasedBackgroundView.heightAnchor constraintEqualToConstant:minHeight].active = true;
181}
182
183- (IBAction)minimize:(id) sender {
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400184 [self.delegate minimizeParticipant];
185}
186
kkostiuk3a2c9f72021-01-27 10:30:49 -0500187- (IBAction)maximize:(id) sender {
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400188 [self.delegate maximizeParticipant:self.participant.uri active: self.participant.active];
189}
190
kkostiuk3a2c9f72021-01-27 10:30:49 -0500191- (IBAction)finishCall:(id) sender {
kkostiukc04f4472021-01-25 09:32:18 -0500192 [self.delegate hangUpParticipant: self.participant.uri];
193}
194
kkostiuk3a2c9f72021-01-27 10:30:49 -0500195- (IBAction)muteAudio:(id) sender {
kkostiukc04f4472021-01-25 09:32:18 -0500196 [self.delegate muteParticipantAudio: self.participant.uri state: !self.participant.audioModeratorMuted];
197}
198
kkostiuk3a2c9f72021-01-27 10:30:49 -0500199- (IBAction)setModerator:(id) sender {
kkostiukc04f4472021-01-25 09:32:18 -0500200 [self.delegate setModerator:self.participant.uri state: !self.participant.isModerator];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400201}
202
203- (void)sizeChanged {
Kateryna Kostiuk8f073862020-09-30 09:28:15 -0400204 if (self.superview == nil) {
205 return;
206 }
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400207 NSArray* constraints = [NSArray arrayWithObjects: self.widthConstraint, self.heightConstraint, self.centerXConstraint, self.centerYConstraint, nil];
208 [self.superview removeConstraints: constraints];
209 [self.superview layoutSubtreeIfNeeded];
210 CGSize viewSize = self.superview.frame.size;
211 if (viewSize.width == 0 || viewSize.height == 0 || self.framesize.width == 0 || self.framesize.height == 0 || self.participant.width == 0 || self.participant.hight == 0) {
Kateryna Kostiuka9908a32020-09-10 10:10:58 -0400212 self.frame = CGRectZero;
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400213 return;
214 }
215 CGFloat viewRatio = viewSize.width / viewSize.height;
216 CGFloat frameRatio = self.framesize.width / self.framesize.height;
217 CGFloat ratio = viewRatio * (1/frameRatio);
218 // calculate size for all participants
219 CGFloat allViewsWidth = viewSize.width;
220 CGFloat allViewsHeight = viewSize.height;
221 if (ratio < 1) {
222 allViewsHeight = allViewsHeight * ratio;
223 } else {
224 allViewsWidth = allViewsWidth / ratio;
225 }
226 CGFloat widthRatio = self.participant.width / self.framesize.width;
227 CGFloat heightRatio = self.participant.hight / self.framesize.height;
228
229 CGFloat overlayWidth = allViewsWidth * widthRatio;
230 CGFloat overlayHeight = allViewsHeight * heightRatio;
231 CGFloat ratioX = overlayWidth / viewSize.width;
232 CGFloat ratioY = overlayHeight / viewSize.height;
233 CGFloat offsetx = (viewSize.width - allViewsWidth) * 0.5;
234 CGFloat offsety = (viewSize.height - allViewsHeight) * 0.5;
235 CGFloat centerX = (offsetx + (self.participant.x * (overlayWidth / self.participant.width))+ overlayWidth * 0.5) / (viewSize.width * 0.5);
236 CGFloat centerY = (offsety + (self.participant.y * (overlayHeight / self.participant.hight)) + overlayHeight * 0.5) / (viewSize.height * 0.5);
237
238 self.centerXConstraint = [NSLayoutConstraint constraintWithItem:self
239 attribute:NSLayoutAttributeCenterX
240 relatedBy:NSLayoutRelationEqual toItem:self.superview
241 attribute:NSLayoutAttributeCenterX
242 multiplier:centerX constant:0];
243 self.centerYConstraint = [NSLayoutConstraint constraintWithItem:self
244 attribute:NSLayoutAttributeCenterY
245 relatedBy:NSLayoutRelationEqual toItem:self.superview
246 attribute:NSLayoutAttributeCenterY
247 multiplier:centerY constant:0];
248 self.widthConstraint =
249 [self.widthAnchor constraintEqualToAnchor:self.superview.widthAnchor multiplier: ratioX];
250 self.heightConstraint = [self.heightAnchor constraintEqualToAnchor:self.superview.heightAnchor multiplier: ratioY];
251 self.widthConstraint.active = YES;
252 self.heightConstraint.active = YES;
253 self.centerXConstraint.active = YES;
254 self.centerYConstraint.active = YES;
255 [self layoutSubtreeIfNeeded];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400256}
257
258- (void)updateViewWithParticipant:(ConferenceParticipant) participant {
kkostiuk3a2c9f72021-01-27 10:30:49 -0500259 bool sizeChanged = self.participant.width != participant.width || self.participant.hight != participant.hight
260 || self.participant.x != participant.x || self.participant.y != participant.y;
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400261 self.participant = participant;
kkostiuk3a2c9f72021-01-27 10:30:49 -0500262 [self updateButtonsState];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400263 [self sizeChanged];
264 self.usernameLabel.string = self.participant.bestName;
265}
266
kkostiuk3a2c9f72021-01-27 10:30:49 -0500267-(void) updateButtonsState {
kkostiukddfad892021-02-17 15:26:37 -0500268 bool audioMuted = self.participant.audioModeratorMuted || self.participant.audioLocalMuted;
269 self.audioState.hidden = !audioMuted;
kkostiuk3a2c9f72021-01-27 10:30:49 -0500270 self.moderatorState.hidden = !self.participant.isModerator || [self.delegate isParticipantHost: self.participant.uri];
271 self.hostState.hidden = ![self.delegate isParticipantHost: self.participant.uri];
272 self.cusp.hidden = (self.audioState.hidden && self.moderatorState.hidden && self.hostState.hidden);
273 BackgroundType type = self.audioState.hidden ? RECTANGLE_WITH_ROUNDED_RIGHT_CORNER : RECTANGLE;
274 if (!self.moderatorState.hidden && self.moderatorState.backgroundType != type) {
275 self.moderatorState.backgroundType = type;
276 [self.moderatorState setNeedsDisplay:YES];
277 }
278 if (!self.hostState.hidden && self.hostState.backgroundType != type) {
279 self.hostState.backgroundType = type;
280 [self.hostState setNeedsDisplay:YES];
281 }
282 bool couldManageConference = [self.delegate isMasterCall] || [self.delegate isCallModerator];
283 self.buttonsContainer.hidden = !couldManageConference;
284 if (!couldManageConference) {
285 return;
286 }
287 int layout = [self.delegate getCurrentLayout];
288 if (layout < 0)
289 return;
290 BOOL showConferenceHostOnly = !self.participant.isLocal && [self.delegate isMasterCall];
291 BOOL hangupEnabled = ![self.delegate isParticipantHost: self.participant.uri];
292 BOOL showMaximized = layout != 2;
293 BOOL showMinimized = !(layout == 0 || (layout == 1 && !self.participant.active));
294 self.setModerator.enabled = showConferenceHostOnly;
295 self.hangup.enabled = hangupEnabled;
296 self.minimize.hidden = !showMinimized;
297 self.maximize.hidden = !showMaximized;
kkostiukddfad892021-02-17 15:26:37 -0500298 NSImage* muteAudioImage = audioMuted ? [NSImage imageNamed: @"ic_moderator_audio_muted.png"] :
kkostiuk3a2c9f72021-01-27 10:30:49 -0500299 [NSImage imageNamed: @"ic_moderator_audio_unmuted.png"];
300 [self.muteAudio setImage: muteAudioImage];
kkostiukddfad892021-02-17 15:26:37 -0500301 self.muteAudio.enabled = !self.participant.audioLocalMuted;
kkostiuk3a2c9f72021-01-27 10:30:49 -0500302}
303
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400304-(void)mouseEntered:(NSEvent *)theEvent {
kkostiuk3a2c9f72021-01-27 10:30:49 -0500305 self.backgroundView.hidden = NO;
306 auto size1 = self.frame.size;
307 if (size1.width < minWidth && size1.height < minHeight) {
308 self.increasedBackgroundView.hidden = false;
309 self.backgroundView.layer.backgroundColor = [[NSColor clearColor] CGColor];
310 }
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400311 [super mouseEntered:theEvent];
312}
313
314-(void)mouseExited:(NSEvent *)theEvent {
kkostiuk3a2c9f72021-01-27 10:30:49 -0500315 self.backgroundView.hidden = YES;
316 self.increasedBackgroundView.hidden = true;
317 self.backgroundView.layer.backgroundColor = [[NSColor colorWithCalibratedRed: 0 green: 0 blue: 0 alpha: 0.6] CGColor];
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400318 [super mouseExited:theEvent];
319}
320
Kateryna Kostiukcd027882020-09-08 10:59:31 -0400321-(void)mouseUp:(NSEvent *)theEvent
322{
323 [super mouseUp:theEvent];
324 if ([theEvent clickCount] == 1) {
325 [self performSelector:@selector(singleTap) withObject:nil afterDelay:[NSEvent doubleClickInterval]];
326 } else if (theEvent.clickCount == 2) {
327 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
328 }
329}
330
331- (void)singleTap {
332 [self.delegate maximizeParticipant:self.participant.uri active: self.participant.active];
333}
334
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400335- (void)ensureTrackingArea {
336 if (trackingArea == nil) {
337 trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
338 options:NSTrackingInVisibleRect
339 | NSTrackingActiveAlways
340 | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
341 }
342}
343
344- (void)updateTrackingAreas {
345 [super updateTrackingAreas];
346 [self ensureTrackingArea];
347 if (![[self trackingAreas] containsObject:trackingArea]) {
348 [self addTrackingArea:trackingArea];
349 }
350}
351
352
353@end