Skip to content

Commit

Permalink
Move off of UIKit SPI: UIURLDragPreviewView
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=260448
rdar://114165890

Reviewed by Aditya Keerthi and Tim Horton.

Replace usages of `UIURLDragPreviewView` in WebKit with `+[UIDragPreview previewForURL:title:]`.

* Source/WebKit/Platform/spi/ios/UIKitSPI.h:

Remove the (now-unused) SPI declaration and header include.

* Source/WebKit/UIProcess/ios/DragDropInteractionState.h:
* Source/WebKit/UIProcess/ios/DragDropInteractionState.mm:
(WebKit::DragDropInteractionState::stageDragItem):

We no longer need to plumb the `center` point into the preview provider block, so we can also remove
the `adjustedOrigin` member while we're here.

(WebKit::DragDropInteractionState::updatePreviewsForActiveDragSources):

Use `+[UIDragPreview previewForURL:title:]` instead of directly creating a `UIURLDragPreviewView`.
There is a caveat here, which is that on visionOS (where we need to override the background color of
the preview by setting `-backgroundColor` on the preview parameters), we need to first use
`+previewForURL:title:` to make UIKit instantiate a `UIURLDragPreviewView`, which we can then use to
initialize a `UIDragPreview`.

Canonical link: https://commits.webkit.org/267102@main
  • Loading branch information
whsieh committed Aug 21, 2023
1 parent 1003bbe commit 9a057d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
6 changes: 0 additions & 6 deletions Source/WebKit/Platform/spi/ios/UIKitSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@
#import <UIKit/UIDragPreview_Private.h>
#import <UIKit/UIDragSession.h>
#import <UIKit/UIDropInteraction.h>
#import <UIKit/UIPreviewInteraction.h>
#import <UIKit/UIURLDragPreviewView.h>
#import <UIKit/_UITextDragCaretView.h>
#endif

Expand Down Expand Up @@ -1103,10 +1101,6 @@ WTF_EXTERN_C_END
-(void)remove;
@end

@interface UIURLDragPreviewView : UIView
+ (instancetype)viewWithTitle:(NSString *)title URL:(NSURL *)url;
@end

@interface _UIParallaxTransitionPanGestureRecognizer : UIScreenEdgePanGestureRecognizer
@end

Expand Down
1 change: 0 additions & 1 deletion Source/WebKit/UIProcess/ios/DragDropInteractionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace WebKit {

struct DragSourceState {
OptionSet<WebCore::DragSourceAction> action;
CGPoint adjustedOrigin { CGPointZero };
CGRect dragPreviewFrameInRootViewCoordinates { CGRectZero };
RetainPtr<UIImage> image;
std::optional<WebCore::TextIndicatorData> indicatorData;
Expand Down
18 changes: 11 additions & 7 deletions Source/WebKit/UIProcess/ios/DragDropInteractionState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ static bool canUpdatePreviewForActiveDragSource(const DragSourceState& source)
m_adjustedPositionForDragEnd = item.eventPositionInContentCoordinates;
m_stagedDragSource = {{
item.sourceAction,
item.eventPositionInContentCoordinates,
item.dragPreviewFrameInRootViewCoordinates,
dragImage,
item.image.indicatorData(),
Expand Down Expand Up @@ -403,15 +402,20 @@ static bool canUpdatePreviewForActiveDragSource(const DragSourceState& source)
continue;

if (source.action.contains(DragSourceAction::Link)) {
dragItem.previewProvider = [title = retainPtr((NSString *)source.linkTitle), url = retainPtr((NSURL *)source.linkURL), center = source.adjustedOrigin] () -> UIDragPreview * {
UIURLDragPreviewView *previewView = [UIURLDragPreviewView viewWithTitle:title.get() URL:url.get()];
previewView.center = center;
auto parameters = adoptNS([[UIDragPreviewParameters alloc] initWithTextLineRects:@[ [NSValue valueWithCGRect:previewView.bounds] ]]);
[parameters setBackgroundColor:[UIColor colorWithDynamicProvider:[] (UITraitCollection *traitCollection) -> UIColor * {
dragItem.previewProvider = [title = retainPtr((NSString *)source.linkTitle), url = retainPtr((NSURL *)source.linkURL)] () -> UIDragPreview * {
RetainPtr preview = [UIDragPreview previewForURL:url.get() title:title.get()];
#if PLATFORM(VISION)
// FIXME: This is a slightly unfortunate since we end up copying the preview parameters,
// and also create an extra `UIDragPreview` on visionOS. We can remove this workaround
// once UIKit addresses <rdar://114204432>.
auto adjustedParameters = [preview parameters];
adjustedParameters.backgroundColor = [UIColor colorWithDynamicProvider:[] (UITraitCollection *traitCollection) -> UIColor * {
WebCore::LocalCurrentTraitCollection localCurrentTraitCollection(traitCollection);
return [UIColor.systemBackgroundColor resolvedColorWithTraitCollection:UITraitCollection.currentTraitCollection];
}]];
return adoptNS([[UIDragPreview alloc] initWithView:previewView parameters:parameters.get()]).autorelease();
preview = adoptNS([[UIDragPreview alloc] initWithView:[preview view] parameters:adjustedParameters]);
#endif // PLATFORM(VISION)
return preview.autorelease();
};
}
#if ENABLE(INPUT_TYPE_COLOR)
Expand Down

0 comments on commit 9a057d2

Please sign in to comment.