[LFC][IFC] Make InlineTextItem reusable when 'segment break' behavior changes
[WebKit-https.git] / Source / WebKit / UIProcess / ios / WKDrawingCoordinator.mm
1 /*
2  * Copyright (C) 2019 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 "WKDrawingCoordinator.h"
28
29 #if HAVE(PENCILKIT)
30
31 #import "EditableImageController.h"
32 #import "WKContentViewInteraction.h"
33 #import "WKDrawingView.h"
34 #import "WKWebView.h"
35 #import <WebKitAdditions/WKDrawingCoordinatorAdditions.mm>
36 #import <wtf/RetainPtr.h>
37
38 #import "PencilKitSoftLink.h"
39
40 @interface WKDrawingCoordinator () <WKInkPickerDelegate>
41 @end
42
43 @implementation WKDrawingCoordinator {
44     __weak WKContentView *_contentView;
45
46     RetainPtr<WKInkPicker> _inkPicker;
47
48     WebCore::GraphicsLayer::EmbeddedViewID _focusedEmbeddedViewID;
49 }
50
51 - (instancetype)initWithContentView:(WKContentView *)contentView
52 {
53     self = [super init];
54     if (!self)
55         return nil;
56
57     _contentView = contentView;
58
59     return self;
60 }
61
62 - (PKInk *)currentInk
63 {
64     return [_inkPicker currentInk];
65 }
66
67 - (UIView *)rulerHostingView
68 {
69     return _contentView;
70 }
71
72 - (BOOL)rulerHostWantsSharedRuler
73 {
74     return YES;
75 }
76
77 - (WKDrawingView *)_focusedDrawingView
78 {
79     WebKit::EditableImage *focusedEditableImage = _contentView.page->editableImageController().editableImage(_focusedEmbeddedViewID);
80     if (!focusedEditableImage)
81         return nil;
82     return focusedEditableImage->drawingView.get();
83 }
84
85 - (NSUndoManager *)undoManagerForInkPicker:(WKInkPicker *)inkPicker
86 {
87     return [_contentView undoManager];
88 }
89
90 - (UIView *)containingViewForInkPicker:(WKInkPicker *)inkPicker
91 {
92     return _contentView;
93 }
94
95 - (void)inkPickerDidToggleRuler:(WKInkPicker *)inkPicker
96 {
97     _rulerEnabled = !_rulerEnabled;
98     [self._focusedDrawingView didChangeRulerState:_rulerEnabled];
99 }
100
101 - (void)inkPickerDidChangeInk:(WKInkPicker *)inkPicker
102 {
103     [self._focusedDrawingView didChangeInk:self.currentInk];
104 }
105
106 - (void)installInkPickerForDrawing:(WebCore::GraphicsLayer::EmbeddedViewID)embeddedViewID
107 {
108     _focusedEmbeddedViewID = embeddedViewID;
109
110     if (!_inkPicker)
111         _inkPicker = adoptNS([[WKInkPicker alloc] initWithDelegate:self]);
112     [_inkPicker show];
113
114     // When focused, push the ruler state down to the canvas so that it doesn't get out of sync
115     // and early-return from later changes.
116     [self._focusedDrawingView didChangeRulerState:_rulerEnabled];
117 }
118
119 - (void)uninstallInkPicker
120 {
121     [_inkPicker hide];
122     [self._focusedDrawingView didChangeRulerState:NO];
123     _focusedEmbeddedViewID = 0;
124 }
125
126 @end
127
128 #endif // HAVE(PENCILKIT)