Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: remoting/ios/app/host_view_controller.mm

Issue 2869303003: Adding hooks to add the keyboard on screen and be able to dismiss it. (Closed)
Patch Set: Adding kKeyboardAnimationTime. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/ios/BUILD.gn ('k') | remoting/ios/client_gestures.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #if !defined(__has_feature) || !__has_feature(objc_arc) 5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support." 6 #error "This file requires ARC support."
7 #endif 7 #endif
8 8
9 #import "remoting/ios/app/host_view_controller.h" 9 #import "remoting/ios/app/host_view_controller.h"
10 10
11 #include <memory> 11 #include <memory>
12 12
13 #import <GLKit/GLKit.h> 13 #import <GLKit/GLKit.h>
14 14
15 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate rialButtons.h" 15 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate rialButtons.h"
16 #import "remoting/ios/client_gestures.h" 16 #import "remoting/ios/client_gestures.h"
17 #import "remoting/ios/client_keyboard.h"
17 #import "remoting/ios/session/remoting_client.h" 18 #import "remoting/ios/session/remoting_client.h"
18 19
19 #include "remoting/client/gesture_interpreter.h" 20 #include "remoting/client/gesture_interpreter.h"
20 21
21 static const CGFloat kFabInset = 15.f; 22 static const CGFloat kFabInset = 15.f;
23 static const CGFloat kKeyboardAnimationTime = 0.3;
22 24
23 @interface HostViewController () { 25 @interface HostViewController () {
24 RemotingClient* _client; 26 RemotingClient* _client;
25 MDCFloatingButton* _floatingButton; 27 MDCFloatingButton* _floatingButton;
26
27 ClientGestures* _clientGestures; 28 ClientGestures* _clientGestures;
29 ClientKeyboard* _clientKeyboard;
30 CGSize _keyboardSize;
28 } 31 }
29 @end 32 @end
30 33
31 @implementation HostViewController 34 @implementation HostViewController
32 35
33 - (id)initWithClient:(RemotingClient*)client { 36 - (id)initWithClient:(RemotingClient*)client {
34 self = [super init]; 37 self = [super init];
35 if (self) { 38 if (self) {
36 _client = client; 39 _client = client;
40 _keyboardSize = CGSizeZero;
37 } 41 }
38 return self; 42 return self;
39 } 43 }
40 44
41 #pragma mark - UIViewController 45 #pragma mark - UIViewController
42 46
43 - (void)loadView { 47 - (void)loadView {
44 self.view = [[GLKView alloc] initWithFrame:CGRectZero]; 48 self.view = [[GLKView alloc] initWithFrame:CGRectZero];
45 } 49 }
46 50
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 // the surface is not ready and onSurfaceChanged will be no-op. 82 // the surface is not ready and onSurfaceChanged will be no-op.
79 // Call onSurfaceChanged here to cover that case. 83 // Call onSurfaceChanged here to cover that case.
80 [_client surfaceChanged:self.view.frame]; 84 [_client surfaceChanged:self.view.frame];
81 } 85 }
82 86
83 - (void)viewWillAppear:(BOOL)animated { 87 - (void)viewWillAppear:(BOOL)animated {
84 [super viewWillAppear:animated]; 88 [super viewWillAppear:animated];
85 89
86 _clientGestures = 90 _clientGestures =
87 [[ClientGestures alloc] initWithView:self.view client:_client]; 91 [[ClientGestures alloc] initWithView:self.view client:_client];
92 [[NSNotificationCenter defaultCenter]
93 addObserver:self
94 selector:@selector(keyboardWillShow:)
95 name:UIKeyboardWillShowNotification
96 object:nil];
97
98 [[NSNotificationCenter defaultCenter]
99 addObserver:self
100 selector:@selector(keyboardWillHide:)
101 name:UIKeyboardWillHideNotification
102 object:nil];
88 } 103 }
89 104
90 - (void)viewWillDisappear:(BOOL)animated { 105 - (void)viewWillDisappear:(BOOL)animated {
91 [super viewWillDisappear:animated]; 106 [super viewWillDisappear:animated];
92 107
93 _clientGestures = nil; 108 _clientGestures = nil;
109 [[NSNotificationCenter defaultCenter] removeObserver:self];
94 } 110 }
95 111
96 - (void)viewDidLayoutSubviews { 112 - (void)viewDidLayoutSubviews {
97 [super viewDidLayoutSubviews]; 113 [super viewDidLayoutSubviews];
98 114
99 [_client surfaceChanged:self.view.frame]; 115 [_client surfaceChanged:self.view.frame];
100 116
101 CGSize btnSize = _floatingButton.frame.size; 117 CGSize btnSize = _floatingButton.frame.size;
102 _floatingButton.frame = 118 _floatingButton.frame =
103 CGRectMake(self.view.frame.size.width - btnSize.width - kFabInset, 119 CGRectMake(self.view.frame.size.width - btnSize.width - kFabInset,
104 self.view.frame.size.height - btnSize.height - kFabInset, 120 self.view.frame.size.height - btnSize.height - kFabInset,
105 btnSize.width, btnSize.height); 121 btnSize.width, btnSize.height);
106 } 122 }
107 123
124 #pragma mark - Keyboard
125
126 - (BOOL)isKeyboardActive {
127 if (_clientKeyboard) {
128 return [_clientKeyboard isFirstResponder];
129 }
130 return NO;
131 }
132
133 - (void)showKeyboard {
134 if (!_clientKeyboard) {
135 _clientKeyboard = [[ClientKeyboard alloc] init];
136 [self.view addSubview:_clientKeyboard];
137 // TODO(nicholss): need to pass some keyboard injection interface here.
138 }
139 [_clientKeyboard becomeFirstResponder];
140 }
141
142 - (void)hideKeyboard {
143 [_clientKeyboard resignFirstResponder];
144 }
145
146 #pragma mark - Keyboard Notifications
147
148 - (void)keyboardWillShow:(NSNotification*)notification {
149 CGSize keyboardSize =
150 [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey]
Yuwei 2017/05/13 01:58:57 You should use UIKeyboardFrameEndUserInfoKey inste
151 CGRectValue]
152 .size;
153 if (_keyboardSize.height != keyboardSize.height) {
154 CGFloat deltaHeight = keyboardSize.height - _keyboardSize.height;
155 [UIView animateWithDuration:0.3
Yuwei 2017/05/13 01:58:57 This should use kKeyboardAnimationTime
nicholss 2017/05/15 15:34:13 Done.
156 animations:^{
157 CGRect f = self.view.frame;
158 f.size.height += deltaHeight;
Yuwei 2017/05/13 01:58:57 Should be -=
nicholss 2017/05/15 15:34:13 Delta can be negative. So I think the math works o
Yuwei 2017/05/15 18:20:53 I think the problem here is this is the delta of t
159 self.view.frame = f;
160 }];
161 _keyboardSize = keyboardSize;
162 }
163 }
164
165 - (void)keyboardWillHide:(NSNotification*)notification {
166 [UIView animateWithDuration:kKeyboardAnimationTime
167 animations:^{
168 CGRect f = self.view.frame;
169 f.size.height += _keyboardSize.height;
170 self.view.frame = f;
171 }];
172 _keyboardSize = CGSizeZero;
173 }
174
108 #pragma mark - Private 175 #pragma mark - Private
109 176
110 - (void)didTap:(id)sender { 177 - (void)didTap:(id)sender {
111 // TODO(nicholss): The FAB is being used to close the window at the moment 178 // TODO(nicholss): The FAB is being used to launch an alert window with
112 // just as a demo as the integration continues. This will not be the case 179 // more options. This is not ideal but it gets us an easy way to make a
113 // in the final app. 180 // modal window option selector. Replace this with a real menu later.
114 [_client disconnectFromHost]; 181
115 [self dismissViewControllerAnimated:YES completion:nil]; 182 UIAlertController* alert =
183 [UIAlertController alertControllerWithTitle:@"Remote Settings"
184 message:nil
185 preferredStyle:UIAlertControllerStyleAlert];
186
187 if ([self isKeyboardActive]) {
188 void (^hideKeyboardHandler)(UIAlertAction*) = ^(UIAlertAction*) {
189 NSLog(@"Will hide keyboard.");
190 [self hideKeyboard];
191 };
192 [alert addAction:[UIAlertAction actionWithTitle:@"Hide Keyboard"
193 style:UIAlertActionStyleDefault
194 handler:hideKeyboardHandler]];
195 } else {
196 void (^showKeyboardHandler)(UIAlertAction*) = ^(UIAlertAction*) {
197 NSLog(@"Will show keyboard.");
198 [self showKeyboard];
199 };
200 [alert addAction:[UIAlertAction actionWithTitle:@"Show Keyboard"
201 style:UIAlertActionStyleDefault
202 handler:showKeyboardHandler]];
203 }
204 void (^disconnectHandler)(UIAlertAction*) = ^(UIAlertAction*) {
205 [_client disconnectFromHost];
206 [self dismissViewControllerAnimated:YES completion:nil];
207 };
208 [alert addAction:[UIAlertAction actionWithTitle:@"Disconnect"
209 style:UIAlertActionStyleCancel
210 handler:disconnectHandler]];
211
212 [self presentViewController:alert animated:YES completion:nil];
116 } 213 }
117 214
118 @end 215 @end
OLDNEW
« no previous file with comments | « remoting/ios/BUILD.gn ('k') | remoting/ios/client_gestures.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698