ARDMainView.m (7687B)
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 15 static CGFloat const kRoomTextFieldHeight = 40; 16 static CGFloat const kRoomTextFieldMargin = 8; 17 static 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 self = [super initWithFrame:frame]; 30 if (self) { 31 _roomText = [[UITextField alloc] initWithFrame:CGRectZero]; 32 _roomText.borderStyle = UITextBorderStyleNone; 33 _roomText.font = [UIFont systemFontOfSize:12]; 34 _roomText.placeholder = @"Room name"; 35 _roomText.autocorrectionType = UITextAutocorrectionTypeNo; 36 _roomText.autocapitalizationType = UITextAutocapitalizationTypeNone; 37 _roomText.clearButtonMode = UITextFieldViewModeAlways; 38 _roomText.delegate = self; 39 [self addSubview:_roomText]; 40 41 // Give rounded corners and a light gray border. 42 self.layer.borderWidth = 1; 43 self.layer.borderColor = [[UIColor lightGrayColor] CGColor]; 44 self.layer.cornerRadius = 2; 45 } 46 return self; 47 } 48 49 - (void)layoutSubviews { 50 _roomText.frame = 51 CGRectMake(kRoomTextFieldMargin, 52 0, 53 CGRectGetWidth(self.bounds) - kRoomTextFieldMargin, 54 kRoomTextFieldHeight); 55 } 56 57 - (CGSize)sizeThatFits:(CGSize)size { 58 size.height = kRoomTextFieldHeight; 59 return size; 60 } 61 62 - (NSString *)roomText { 63 return _roomText.text; 64 } 65 66 #pragma mark - UITextFieldDelegate 67 68 - (BOOL)textFieldShouldReturn:(UITextField *)textField { 69 // There is no other control that can take focus, so manually resign focus 70 // when return (Join) is pressed to trigger `textFieldDidEndEditing`. 71 [textField resignFirstResponder]; 72 return YES; 73 } 74 75 @end 76 77 @implementation ARDMainView { 78 ARDRoomTextField *_roomText; 79 UIButton *_startRegularCallButton; 80 UIButton *_startLoopbackCallButton; 81 UIButton *_audioLoopButton; 82 } 83 84 @synthesize delegate = _delegate; 85 @synthesize isAudioLoopPlaying = _isAudioLoopPlaying; 86 87 - (instancetype)initWithFrame:(CGRect)frame { 88 self = [super initWithFrame:frame]; 89 if (self) { 90 _roomText = [[ARDRoomTextField alloc] initWithFrame:CGRectZero]; 91 [self addSubview:_roomText]; 92 93 UIFont *controlFont = [UIFont boldSystemFontOfSize:18.0]; 94 UIColor *controlFontColor = [UIColor whiteColor]; 95 96 _startRegularCallButton = [UIButton buttonWithType:UIButtonTypeSystem]; 97 _startRegularCallButton.titleLabel.font = controlFont; 98 [_startRegularCallButton setTitleColor:controlFontColor 99 forState:UIControlStateNormal]; 100 _startRegularCallButton.backgroundColor = 101 [UIColor colorWithRed:66.0 / 255.0 102 green:200.0 / 255.0 103 blue:90.0 / 255.0 104 alpha:1.0]; 105 [_startRegularCallButton setTitle:@"Call room" 106 forState:UIControlStateNormal]; 107 [_startRegularCallButton addTarget:self 108 action:@selector(onStartRegularCall:) 109 forControlEvents:UIControlEventTouchUpInside]; 110 [self addSubview:_startRegularCallButton]; 111 112 _startLoopbackCallButton = [UIButton buttonWithType:UIButtonTypeSystem]; 113 _startLoopbackCallButton.titleLabel.font = controlFont; 114 [_startLoopbackCallButton setTitleColor:controlFontColor 115 forState:UIControlStateNormal]; 116 _startLoopbackCallButton.backgroundColor = 117 [UIColor colorWithRed:0.0 green:122.0 / 255.0 blue:1.0 alpha:1.0]; 118 [_startLoopbackCallButton setTitle:@"Loopback call" 119 forState:UIControlStateNormal]; 120 [_startLoopbackCallButton addTarget:self 121 action:@selector(onStartLoopbackCall:) 122 forControlEvents:UIControlEventTouchUpInside]; 123 [self addSubview:_startLoopbackCallButton]; 124 125 // Used to test what happens to sounds when calls are in progress. 126 _audioLoopButton = [UIButton buttonWithType:UIButtonTypeSystem]; 127 _audioLoopButton.titleLabel.font = controlFont; 128 [_audioLoopButton setTitleColor:controlFontColor 129 forState:UIControlStateNormal]; 130 _audioLoopButton.backgroundColor = [UIColor colorWithRed:1.0 131 green:149.0 / 255.0 132 blue:0.0 133 alpha:1.0]; 134 [self updateAudioLoopButton]; 135 [_audioLoopButton addTarget:self 136 action:@selector(onToggleAudioLoop:) 137 forControlEvents:UIControlEventTouchUpInside]; 138 [self addSubview:_audioLoopButton]; 139 140 self.backgroundColor = [UIColor whiteColor]; 141 } 142 return self; 143 } 144 145 - (void)setIsAudioLoopPlaying:(BOOL)isAudioLoopPlaying { 146 if (_isAudioLoopPlaying == isAudioLoopPlaying) { 147 return; 148 } 149 _isAudioLoopPlaying = isAudioLoopPlaying; 150 [self updateAudioLoopButton]; 151 } 152 153 - (void)layoutSubviews { 154 CGRect bounds = self.bounds; 155 CGFloat roomTextWidth = bounds.size.width - 2 * kRoomTextFieldMargin; 156 CGFloat roomTextHeight = [_roomText sizeThatFits:bounds.size].height; 157 _roomText.frame = CGRectMake(kRoomTextFieldMargin, 158 kRoomTextFieldMargin, 159 roomTextWidth, 160 roomTextHeight); 161 162 CGFloat buttonHeight = 163 (CGRectGetMaxY(self.bounds) - CGRectGetMaxY(_roomText.frame) - 164 kCallControlMargin * 4) / 165 3; 166 167 CGFloat regularCallFrameTop = 168 CGRectGetMaxY(_roomText.frame) + kCallControlMargin; 169 CGRect regularCallFrame = 170 CGRectMake(kCallControlMargin, 171 regularCallFrameTop, 172 bounds.size.width - 2 * kCallControlMargin, 173 buttonHeight); 174 175 CGFloat loopbackCallFrameTop = 176 CGRectGetMaxY(regularCallFrame) + kCallControlMargin; 177 CGRect loopbackCallFrame = 178 CGRectMake(kCallControlMargin, 179 loopbackCallFrameTop, 180 bounds.size.width - 2 * kCallControlMargin, 181 buttonHeight); 182 183 CGFloat audioLoopTop = CGRectGetMaxY(loopbackCallFrame) + kCallControlMargin; 184 CGRect audioLoopFrame = CGRectMake(kCallControlMargin, 185 audioLoopTop, 186 bounds.size.width - 2 * kCallControlMargin, 187 buttonHeight); 188 189 _startRegularCallButton.frame = regularCallFrame; 190 _startLoopbackCallButton.frame = loopbackCallFrame; 191 _audioLoopButton.frame = audioLoopFrame; 192 } 193 194 #pragma mark - Private 195 196 - (void)updateAudioLoopButton { 197 if (_isAudioLoopPlaying) { 198 [_audioLoopButton setTitle:@"Stop sound" forState:UIControlStateNormal]; 199 } else { 200 [_audioLoopButton setTitle:@"Play sound" forState:UIControlStateNormal]; 201 } 202 } 203 204 - (void)onToggleAudioLoop:(id)sender { 205 [_delegate mainViewDidToggleAudioLoop:self]; 206 } 207 208 - (void)onStartRegularCall:(id)sender { 209 [_delegate mainView:self didInputRoom:_roomText.roomText isLoopback:NO]; 210 } 211 212 - (void)onStartLoopbackCall:(id)sender { 213 [_delegate mainView:self didInputRoom:_roomText.roomText isLoopback:YES]; 214 } 215 216 @end