[iOS] Replace "node assistance" terminology in WebKit with "focused element"
[WebKit-https.git] / Source / WebKit / UIProcess / WKImagePreviewViewController.mm
1 /*
2  * Copyright (C) 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "WKImagePreviewViewController.h"
28
29 #if PLATFORM(IOS_FAMILY)
30
31 #import <UIKitSPI.h>
32 #import <WebCore/IntSize.h>
33 #import <_WKElementAction.h>
34
35 @implementation WKImagePreviewViewController {
36     RetainPtr<CGImageRef> _image;
37     RetainPtr<UIImageView> _imageView;
38 }
39
40 - (void)loadView
41 {
42     [super loadView];
43     self.view.backgroundColor = [UIColor whiteColor];
44     [self.view addSubview:_imageView.get()];
45 }
46
47 - (id)initWithCGImage:(RetainPtr<CGImageRef>)image defaultActions:(RetainPtr<NSArray>)actions elementInfo:(RetainPtr<_WKActivatedElementInfo>)elementInfo
48 {
49     self = [super initWithNibName:nil bundle:nil];
50     if (!self)
51         return nil;
52
53     _image = image;
54
55     _imageView = adoptNS([[UIImageView alloc] initWithFrame:CGRectZero]);
56     RetainPtr<UIImage> uiImage = adoptNS([[UIImage alloc] initWithCGImage:_image.get()]);
57     [_imageView setImage:uiImage.get()];
58
59     CGSize screenSize = [UIScreen mainScreen].bounds.size;
60     CGSize imageSize = _scaleSizeWithinSize(CGSizeMake(CGImageGetWidth(_image.get()), CGImageGetHeight(_image.get())), screenSize);
61     [_imageView setFrame:CGRectMake([_imageView frame].origin.x, [_imageView frame].origin.y, imageSize.width, imageSize.height)];
62     [self setPreferredContentSize:imageSize];
63
64     _imageActions = actions;
65     _activatedElementInfo = elementInfo;
66
67     return self;
68 }
69
70 - (void)viewDidLayoutSubviews
71 {
72     [super viewDidLayoutSubviews];
73
74     [_imageView setFrame:self.view.bounds];
75 }
76
77 static CGSize _scaleSizeWithinSize(CGSize source, CGSize destination)
78 {
79     CGSize size = destination;
80     CGFloat sourceAspectRatio = (source.width / source.height);
81     CGFloat destinationAspectRatio = (destination.width / destination.height);
82     
83     if (sourceAspectRatio > destinationAspectRatio) {
84         size.width = destination.width;
85         size.height = (source.height * (destination.width / source.width));
86     } else if (sourceAspectRatio < destinationAspectRatio) {
87         size.width = (source.width * (destination.height / source.height));
88         size.height = destination.height;
89     }
90     
91     return size;
92 }
93
94 #if HAVE(LINK_PREVIEW)
95 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
96 - (NSArray <UIViewControllerPreviewAction *> *)previewActions
97 IGNORE_WARNINGS_END
98 {
99     NSMutableArray<UIViewControllerPreviewAction *> *previewActions = [NSMutableArray array];
100     for (_WKElementAction *imageAction in _imageActions.get()) {
101         UIViewControllerPreviewAction *previewAction = [UIViewControllerPreviewAction actionWithTitle:imageAction.title handler:^(UIViewControllerPreviewAction *action, UIViewController *previewViewController) {
102             [imageAction runActionWithElementInfo:_activatedElementInfo.get()];
103         }];
104
105         [previewActions addObject:previewAction];
106     }
107
108     return previewActions;
109 }
110 #endif
111
112 @end
113
114 #endif