1/* 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#import "ARDMainView.h" 12 13#import "UIImage+ARDUtilities.h" 14 15static CGFloat const kRoomTextFieldHeight = 40; 16static CGFloat const kRoomTextFieldMargin = 8; 17static CGFloat const kCallControlMargin = 8; 18 19// Helper view that contains a text field and a clear button. 20@interface ARDRoomTextField : UIView <UITextFieldDelegate> 21@property(nonatomic, readonly) NSString *roomText; 22@end 23 24@implementation ARDRoomTextField { 25 UITextField *_roomText; 26} 27 28- (instancetype)initWithFrame:(CGRect)frame { 29 if (self = [super initWithFrame:frame]) { 30 _roomText = [[UITextField alloc] initWithFrame:CGRectZero]; 31 _roomText.borderStyle = UITextBorderStyleNone; 32 _roomText.font = [UIFont systemFontOfSize:12]; 33 _roomText.placeholder = @"Room name"; 34 _roomText.autocorrectionType = UITextAutocorrectionTypeNo; 35 _roomText.autocapitalizationType = UITextAutocapitalizationTypeNone; 36 _roomText.clearButtonMode = UITextFieldViewModeAlways; 37 _roomText.delegate = self; 38 [self addSubview:_roomText]; 39 40 // Give rounded corners and a light gray border. 41 self.layer.borderWidth = 1; 42 self.layer.borderColor = [[UIColor lightGrayColor] CGColor]; 43 self.layer.cornerRadius = 2; 44 } 45 return self; 46} 47 48- (void)layoutSubviews { 49 _roomText.frame = 50 CGRectMake(kRoomTextFieldMargin, 0, CGRectGetWidth(self.bounds) - kRoomTextFieldMargin, 51 kRoomTextFieldHeight); 52} 53 54- (CGSize)sizeThatFits:(CGSize)size { 55 size.height = kRoomTextFieldHeight; 56 return size; 57} 58 59- (NSString *)roomText { 60 return _roomText.text; 61} 62 63#pragma mark - UITextFieldDelegate 64 65- (BOOL)textFieldShouldReturn:(UITextField *)textField { 66 // There is no other control that can take focus, so manually resign focus 67 // when return (Join) is pressed to trigger `textFieldDidEndEditing`. 68 [textField resignFirstResponder]; 69 return YES; 70} 71 72@end 73 74@implementation ARDMainView { 75 ARDRoomTextField *_roomText; 76 UIButton *_startRegularCallButton; 77 UIButton *_startLoopbackCallButton; 78 UIButton *_audioLoopButton; 79} 80 81@synthesize delegate = _delegate; 82@synthesize isAudioLoopPlaying = _isAudioLoopPlaying; 83 84- (instancetype)initWithFrame:(CGRect)frame { 85 if (self = [super initWithFrame:frame]) { 86 _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero]; 87 [self addSubview:_roomText]; 88 89 UIFont *controlFont = [UIFont boldSystemFontOfSize:18.0]; 90 UIColor *controlFontColor = [UIColor whiteColor]; 91 92 _startRegularCallButton = [UIButton buttonWithType:UIButtonTypeSystem]; 93 _startRegularCallButton.titleLabel.font = controlFont; 94 [_startRegularCallButton setTitleColor:controlFontColor forState:UIControlStateNormal]; 95 _startRegularCallButton.backgroundColor 96 = [UIColor colorWithRed:66.0/255.0 green:200.0/255.0 blue:90.0/255.0 alpha:1.0]; 97 [_startRegularCallButton setTitle:@"Call room" forState:UIControlStateNormal]; 98 [_startRegularCallButton addTarget:self 99 action:@selector(onStartRegularCall:) 100 forControlEvents:UIControlEventTouchUpInside]; 101 [self addSubview:_startRegularCallButton]; 102 103 _startLoopbackCallButton = [UIButton buttonWithType:UIButtonTypeSystem]; 104 _startLoopbackCallButton.titleLabel.font = controlFont; 105 [_startLoopbackCallButton setTitleColor:controlFontColor forState:UIControlStateNormal]; 106 _startLoopbackCallButton.backgroundColor = 107 [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]; 108 [_startLoopbackCallButton setTitle:@"Loopback call" forState:UIControlStateNormal]; 109 [_startLoopbackCallButton addTarget:self 110 action:@selector(onStartLoopbackCall:) 111 forControlEvents:UIControlEventTouchUpInside]; 112 [self addSubview:_startLoopbackCallButton]; 113 114 115 // Used to test what happens to sounds when calls are in progress. 116 _audioLoopButton = [UIButton buttonWithType:UIButtonTypeSystem]; 117 _audioLoopButton.titleLabel.font = controlFont; 118 [_audioLoopButton setTitleColor:controlFontColor forState:UIControlStateNormal]; 119 _audioLoopButton.backgroundColor = 120 [UIColor colorWithRed:1.0 green:149.0/255.0 blue:0.0 alpha:1.0]; 121 [self updateAudioLoopButton]; 122 [_audioLoopButton addTarget:self 123 action:@selector(onToggleAudioLoop:) 124 forControlEvents:UIControlEventTouchUpInside]; 125 [self addSubview:_audioLoopButton]; 126 127 self.backgroundColor = [UIColor whiteColor]; 128 } 129 return self; 130} 131 132- (void)setIsAudioLoopPlaying:(BOOL)isAudioLoopPlaying { 133 if (_isAudioLoopPlaying == isAudioLoopPlaying) { 134 return; 135 } 136 _isAudioLoopPlaying = isAudioLoopPlaying; 137 [self updateAudioLoopButton]; 138} 139 140- (void)layoutSubviews { 141 CGRect bounds = self.bounds; 142 CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin; 143 CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height; 144 _roomText.frame = 145 CGRectMake(kRoomTextFieldMargin, kRoomTextFieldMargin, roomTextWidth, 146 roomTextHeight); 147 148 CGFloat buttonHeight = 149 (CGRectGetMaxY(self.bounds) - CGRectGetMaxY(_roomText.frame) - kCallControlMargin * 4) / 3; 150 151 CGFloat regularCallFrameTop = CGRectGetMaxY(_roomText.frame) + kCallControlMargin; 152 CGRect regularCallFrame = CGRectMake(kCallControlMargin, 153 regularCallFrameTop, 154 bounds.size.width - 2*kCallControlMargin, 155 buttonHeight); 156 157 CGFloat loopbackCallFrameTop = CGRectGetMaxY(regularCallFrame) + kCallControlMargin; 158 CGRect loopbackCallFrame = CGRectMake(kCallControlMargin, 159 loopbackCallFrameTop, 160 bounds.size.width - 2*kCallControlMargin, 161 buttonHeight); 162 163 CGFloat audioLoopTop = CGRectGetMaxY(loopbackCallFrame) + kCallControlMargin; 164 CGRect audioLoopFrame = CGRectMake(kCallControlMargin, 165 audioLoopTop, 166 bounds.size.width - 2*kCallControlMargin, 167 buttonHeight); 168 169 _startRegularCallButton.frame = regularCallFrame; 170 _startLoopbackCallButton.frame = loopbackCallFrame; 171 _audioLoopButton.frame = audioLoopFrame; 172} 173 174#pragma mark - Private 175 176- (void)updateAudioLoopButton { 177 if (_isAudioLoopPlaying) { 178 [_audioLoopButton setTitle:@"Stop sound" forState:UIControlStateNormal]; 179 } else { 180 [_audioLoopButton setTitle:@"Play sound" forState:UIControlStateNormal]; 181 } 182} 183 184- (void)onToggleAudioLoop:(id)sender { 185 [_delegate mainViewDidToggleAudioLoop:self]; 186} 187 188- (void)onStartRegularCall:(id)sender { 189 [_delegate mainView:self didInputRoom:_roomText.roomText isLoopback:NO]; 190} 191 192- (void)onStartLoopbackCall:(id)sender { 193 [_delegate mainView:self didInputRoom:_roomText.roomText isLoopback:YES]; 194} 195 196@end 197