2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "tools/sk_app/ios/WindowContextFactory_ios.h"
9 #include "tools/sk_app/ios/Window_ios.h"
11 #if __has_feature(objc_arc)
12 #error "File should not be compiled with ARC."
15 @interface WindowViewController : UIViewController
17 - (WindowViewController*)initWithWindow:(sk_app::Window_ios*)initWindow;
21 ///////////////////////////////////////////////////////////////////////////////
27 Window_ios* Window_ios::gWindow = nullptr;
29 Window* Window::CreateNativeWindow(void*) {
30 // already have a window
31 if (Window_ios::MainWindow()) {
35 Window_ios* window = new Window_ios();
36 if (!window->initWindow()) {
44 bool Window_ios::initWindow() {
45 // we already have a window
50 // Create a view controller to track certain events
51 WindowViewController* viewController = [[WindowViewController alloc] initWithWindow:this];
52 if (nil == viewController) {
56 fWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
58 [viewController release];
61 fWindow.backgroundColor = [UIColor whiteColor];
63 viewController.view = nil;
64 [fWindow setRootViewController:viewController];
65 [fWindow makeKeyAndVisible];
72 void Window_ios::closeWindow() {
80 bool Window_ios::attach(BackendType attachType) {
83 window_context_factory::IOSWindowInfo info;
85 info.fViewController = fWindow.rootViewController;
88 case kMetal_BackendType:
89 fWindowContext = MakeMetalForIOS(info, fRequestedDisplayParams);
93 case kNativeGL_BackendType:
94 fWindowContext = MakeGLForIOS(info, fRequestedDisplayParams);
96 case kRaster_BackendType:
97 fWindowContext = MakeRasterForIOS(info, fRequestedDisplayParams);
101 SkASSERT_RELEASE(false);
103 this->onBackendCreated();
105 return (SkToBool(fWindowContext));
108 void Window_ios::PaintWindow() {
112 void Window_ios::onInval() {
113 // TODO: send expose event
116 } // namespace sk_app
118 ///////////////////////////////////////////////////////////////////////////////
120 @implementation WindowViewController {
121 sk_app::Window_ios* fWindow;
124 - (WindowViewController*)initWithWindow:(sk_app::Window_ios *)initWindow {
125 self = [super initWithNibName:nil bundle:nil];
127 fWindow = initWindow;
132 - (void)viewDidLoad {
136 - (void)didReceiveMemoryWarning {
140 - (void)viewWillTransitionToSize:(CGSize)size
141 withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
142 // handle rotations here
146 ///////////////////////////////////////////////////////////////////////////////
148 @implementation MainView {
149 sk_app::Window_ios* fWindow;
152 - (void)panGestureAction:(UIGestureRecognizer*)sender {
153 CGPoint location = [sender locationInView:self];
154 switch (sender.state) {
155 case UIGestureRecognizerStateBegan:
156 fWindow->onMouse(location.x, location.y,
157 skui::InputState::kDown, skui::ModifierKey::kNone);
159 case UIGestureRecognizerStateChanged:
160 fWindow->onMouse(location.x, location.y,
161 skui::InputState::kMove, skui::ModifierKey::kNone);
163 case UIGestureRecognizerStateEnded:
164 fWindow->onMouse(location.x, location.y,
165 skui::InputState::kUp, skui::ModifierKey::kNone);
167 case UIGestureRecognizerStateCancelled:
168 fWindow->onMouse(location.x, location.y,
169 skui::InputState::kUp, skui::ModifierKey::kNone);
176 - (void)tapGestureAction:(UIGestureRecognizer*)sender {
177 CGPoint location = [sender locationInView:self];
178 switch (sender.state) {
179 case UIGestureRecognizerStateEnded:
180 fWindow->onMouse(location.x, location.y,
181 skui::InputState::kDown, skui::ModifierKey::kNone);
182 fWindow->onMouse(location.x, location.y,
183 skui::InputState::kUp, skui::ModifierKey::kNone);
190 - (void)pinchGestureAction:(UIGestureRecognizer*)sender {
191 CGPoint location = [sender locationInView:self];
192 UIPinchGestureRecognizer* pinchGestureRecognizer = (UIPinchGestureRecognizer*) sender;
193 float scale = pinchGestureRecognizer.scale;
194 switch (sender.state) {
195 case UIGestureRecognizerStateBegan:
196 fWindow->onPinch(skui::InputState::kDown, scale, location.x, location.y);
198 case UIGestureRecognizerStateChanged:
199 fWindow->onPinch(skui::InputState::kMove, scale, location.x, location.y);
201 case UIGestureRecognizerStateEnded:
202 fWindow->onPinch(skui::InputState::kUp, scale, location.x, location.y);
204 case UIGestureRecognizerStateCancelled:
205 fWindow->onPinch(skui::InputState::kUp, scale, location.x, location.y);
212 - (void)swipeRightGestureAction:(UIGestureRecognizer*)sender {
213 if (UIGestureRecognizerStateEnded == sender.state) {
214 fWindow->onFling(skui::InputState::kRight);
218 - (void)swipeLeftGestureAction:(UIGestureRecognizer*)sender {
219 if (UIGestureRecognizerStateEnded == sender.state) {
220 fWindow->onFling(skui::InputState::kLeft);
224 - (MainView*)initWithWindow:(sk_app::Window_ios *)initWindow {
227 UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
228 panGestureRecognizer.maximumNumberOfTouches = 1;
229 [panGestureRecognizer addTarget:self action:@selector(panGestureAction:)];
230 [self addGestureRecognizer:panGestureRecognizer];
232 UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
233 [tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)];
234 [self addGestureRecognizer:tapGestureRecognizer];
236 UIPinchGestureRecognizer* pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
237 [pinchGestureRecognizer addTarget:self action:@selector(pinchGestureAction:)];
238 [self addGestureRecognizer:pinchGestureRecognizer];
240 UISwipeGestureRecognizer* swipeRightGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
241 swipeRightGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
242 [swipeRightGestureRecognizer addTarget:self action:@selector(swipeRightGestureAction:)];
243 [self addGestureRecognizer:swipeRightGestureRecognizer];
245 UISwipeGestureRecognizer* swipeLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
246 swipeLeftGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
247 [swipeLeftGestureRecognizer addTarget:self action:@selector(swipeLeftGestureAction:)];
248 [self addGestureRecognizer:swipeLeftGestureRecognizer];
250 // disable pan recognition until swipes fail
251 [panGestureRecognizer requireGestureRecognizerToFail:swipeLeftGestureRecognizer];
252 [panGestureRecognizer requireGestureRecognizerToFail:swipeRightGestureRecognizer];
254 fWindow = initWindow;