7 #import <WebKit/WebKit.h>
9 #include "flutter/display_list/effects/dl_image_filter.h"
10 #include "flutter/fml/platform/darwin/cf_utils.h"
17 return CGRectMake(clipDlRect.GetX(),
19 clipDlRect.GetWidth(),
20 clipDlRect.GetHeight());
24 CATransform3D transform = CATransform3DIdentity;
25 transform.m11 = matrix.m[0];
26 transform.m12 = matrix.m[1];
27 transform.m13 = matrix.m[2];
28 transform.m14 = matrix.m[3];
30 transform.m21 = matrix.m[4];
31 transform.m22 = matrix.m[5];
32 transform.m23 = matrix.m[6];
33 transform.m24 = matrix.m[7];
35 transform.m31 = matrix.m[8];
36 transform.m32 = matrix.m[9];
37 transform.m33 = matrix.m[10];
38 transform.m34 = matrix.m[11];
40 transform.m41 = matrix.m[12];
41 transform.m42 = matrix.m[13];
42 transform.m43 = matrix.m[14];
43 transform.m44 = matrix.m[15];
49 void MoveTo(
const flutter::DlPoint& p2,
bool will_be_closed)
override {
50 CGPathMoveToPoint(path_ref_, nil, p2.x, p2.y);
52 void LineTo(
const flutter::DlPoint& p2)
override {
53 CGPathAddLineToPoint(path_ref_, nil, p2.x, p2.y);
55 void QuadTo(
const flutter::DlPoint& cp,
const flutter::DlPoint& p2)
override {
56 CGPathAddQuadCurveToPoint(path_ref_, nil, cp.x, cp.y, p2.x, p2.y);
60 const flutter::DlPoint& cp2,
61 const flutter::DlPoint& p2)
override {
62 CGPathAddCurveToPoint(path_ref_, nil,
63 cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
65 void Close()
override { CGPathCloseSubpath(path_ref_); }
67 CGMutablePathRef
TakePath()
const {
return path_ref_; }
70 CGMutablePathRef path_ref_ = CGPathCreateMutable();
77 @property(nonatomic) BOOL backdropFilterViewConfigured;
82 - (void)updateVisualEffectView:(UIVisualEffectView*)visualEffectView;
96 blurRadius:(CGFloat)blurRadius
97 visualEffectView:(UIVisualEffectView*)visualEffectView {
98 if (
self = [super init]) {
103 FML_DLOG(ERROR) <<
"Apple's API for UIVisualEffectView changed. Update the implementation to "
104 "access the gaussianBlur CAFilter.";
107 _backdropFilterView = visualEffectView;
108 _backdropFilterViewConfigured = NO;
120 + (void)prepareOnce:(UIVisualEffectView*)visualEffectView {
124 for (NSUInteger i = 0; i < visualEffectView.subviews.count; i++) {
125 UIView* view = visualEffectView.subviews[i];
126 if ([NSStringFromClass([view
class]) hasSuffix:
@"BackdropView"]) {
128 for (NSObject* filter in view.layer.filters) {
129 if ([[filter valueForKey:
@"name"] isEqual:
@"gaussianBlur"] &&
130 [[filter valueForKey:
@"inputRadius"] isKindOfClass:[NSNumber class]]) {
131 _gaussianBlurFilter = filter;
135 }
else if ([NSStringFromClass([view
class]) hasSuffix:
@"VisualEffectSubview"]) {
142 + (BOOL)isUIVisualEffectViewImplementationValid {
147 FML_DCHECK(_backdropFilterView);
148 if (!
self.backdropFilterViewConfigured) {
149 [
self updateVisualEffectView:_backdropFilterView];
150 self.backdropFilterViewConfigured = YES;
152 return _backdropFilterView;
155 - (void)updateVisualEffectView:(UIVisualEffectView*)visualEffectView {
156 NSObject* gaussianBlurFilter = [_gaussianBlurFilter copy];
157 FML_DCHECK(gaussianBlurFilter);
158 UIView* backdropView = visualEffectView.subviews[_indexOfBackdropView];
159 [gaussianBlurFilter setValue:@(_blurRadius) forKey:@"inputRadius"];
160 backdropView.layer.filters = @[ gaussianBlurFilter ];
162 UIView* visualEffectSubview = visualEffectView.subviews[_indexOfVisualEffectSubview];
163 visualEffectSubview.layer.backgroundColor = UIColor.clearColor.CGColor;
164 visualEffectView.frame = _frame;
166 self.backdropFilterView = visualEffectView;
173 @property(nonatomic, copy) NSArray<PlatformViewFilter*>* filters;
183 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
184 for (UIView* view in
self.subviews) {
185 if ([view pointInside:[
self convertPoint:point toView:view] withEvent:event]) {
193 FML_DCHECK(
self.filters.count ==
self.backdropFilterSubviews.count);
194 if (
self.filters.count == 0 && filters.count == 0) {
197 self.filters = filters;
198 NSUInteger index = 0;
199 for (index = 0; index <
self.filters.count; index++) {
200 UIVisualEffectView* backdropFilterView;
203 backdropFilterView = filter.backdropFilterView;
204 [self addSubview:backdropFilterView];
205 [self.backdropFilterSubviews addObject:backdropFilterView];
207 [filter updateVisualEffectView:self.backdropFilterSubviews[index]];
211 [self.backdropFilterSubviews[i - 1] removeFromSuperview];
212 [self.backdropFilterSubviews removeLastObject];
217 if (!_backdropFilterSubviews) {
218 _backdropFilterSubviews = [[NSMutableArray alloc] init];
220 return _backdropFilterSubviews;
235 @property(nonatomic) CATransform3D reverseScreenScale;
237 - (
fml::CFRef<CGPathRef>)getTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix;
242 std::vector<fml::CFRef<CGPathRef>> paths_;
248 return [
self initWithFrame:frame screenScale:[UIScreen mainScreen].scale];
251 - (instancetype)
initWithFrame:(CGRect)frame screenScale:(CGFloat)screenScale {
253 self.backgroundColor = UIColor.clearColor;
254 _reverseScreenScale = CATransform3DMakeScale(1 / screenScale, 1 / screenScale, 1);
261 + (Class)layerClass {
262 return [CAShapeLayer class];
265 - (CAShapeLayer*)shapeLayer {
266 return (CAShapeLayer*)
self.layer;
273 [
self shapeLayer].path = nil;
274 [
self setNeedsDisplay];
282 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
286 - (void)drawRect:(CGRect)rect {
290 CGContextRef context = UIGraphicsGetCurrentContext();
291 CGContextSaveGState(context);
294 CGContextSetAlpha(context, 1);
296 for (
size_t i = 0; i < paths_.size(); i++) {
297 CGContextAddPath(context, paths_.at(i));
298 CGContextClip(context);
300 CGContextFillRect(context, rect);
301 CGContextRestoreGState(context);
305 [
super drawRect:rect];
306 if (![
self shapeLayer].path) {
307 if (paths_.size() == 1) {
309 [
self shapeLayer].path = paths_.at(0);
312 CGPathRef pathSoFar = CGPathCreateWithRect(
rectSoFar_, nil);
313 [
self shapeLayer].path = pathSoFar;
314 CGPathRelease(pathSoFar);
320 - (void)clipRect:(const
flutter::DlRect&)clipDlRect matrix:(const
flutter::DlMatrix&)matrix {
322 CGPathRef path = CGPathCreateWithRect(clipRect, nil);
324 CATransform3D matrixInPoints =
326 paths_.push_back([
self getTransformedPath:path matrix:matrixInPoints]);
327 CGAffineTransform affine = [
self affineWithMatrix:matrixInPoints];
329 if (affine.b == 0 && affine.c == 0) {
336 - (void)clipRRect:(const
flutter::DlRoundRect&)clipDlRRect matrix:(const
flutter::DlMatrix&)matrix {
337 if (clipDlRRect.IsEmpty()) {
339 }
else if (clipDlRRect.IsRect()) {
340 [
self clipRect:clipDlRRect.GetBounds() matrix:matrix];
343 CGPathRef pathRef =
nullptr;
346 if (clipDlRRect.GetRadii().AreAllCornersSame()) {
348 auto radii = clipDlRRect.GetRadii();
350 CGPathCreateWithRoundedRect(clipRect, radii.top_left.width, radii.top_left.height, nil);
352 CGMutablePathRef mutablePathRef = CGPathCreateMutable();
354 flutter::DlRect clipDlRect = clipDlRRect.GetBounds();
355 auto left = clipDlRect.GetLeft();
356 auto top = clipDlRect.GetTop();
357 auto right = clipDlRect.GetRight();
358 auto bottom = clipDlRect.GetBottom();
359 flutter::DlRoundingRadii radii = clipDlRRect.GetRadii();
360 auto& top_left = radii.top_left;
361 auto& top_right = radii.top_right;
362 auto& bottom_left = radii.bottom_left;
363 auto& bottom_right = radii.bottom_right;
370 CGPathMoveToPoint(mutablePathRef, nil,
371 left + top_left.width, top);
373 CGPathAddLineToPoint(mutablePathRef, nil,
374 right - top_right.width, top);
375 CGPathAddCurveToPoint(mutablePathRef, nil,
377 right, top + top_right.height,
378 right, top + top_right.height);
380 CGPathAddLineToPoint(mutablePathRef, nil,
381 right, bottom - bottom_right.height);
382 CGPathAddCurveToPoint(mutablePathRef, nil,
384 right - bottom_right.width, bottom,
385 right - bottom_right.width, bottom);
387 CGPathAddLineToPoint(mutablePathRef, nil,
388 left + bottom_left.width, bottom);
389 CGPathAddCurveToPoint(mutablePathRef, nil,
391 left, bottom - bottom_left.height,
392 left, bottom - bottom_left.height);
394 CGPathAddLineToPoint(mutablePathRef, nil,
395 left, top + top_left.height);
396 CGPathAddCurveToPoint(mutablePathRef, nil,
398 left + top_left.width, top,
399 left + top_left.width, top);
400 CGPathCloseSubpath(mutablePathRef);
401 pathRef = mutablePathRef;
404 CATransform3D matrixInPoints =
409 paths_.push_back([
self getTransformedPath:pathRef matrix:matrixInPoints]);
413 - (void)clipPath:(const
flutter::DlPath&)dlPath matrix:(const
flutter::DlMatrix&)matrix {
416 CGPathReceiver receiver;
421 dlPath.Dispatch(receiver);
424 CATransform3D matrixInPoints =
426 paths_.push_back([
self getTransformedPath:receiver.TakePath() matrix:matrixInPoints]);
429 - (CGAffineTransform)affineWithMatrix:(CATransform3D)matrix {
430 return CGAffineTransformMake(matrix.m11, matrix.m12, matrix.m21, matrix.m22, matrix.m41,
434 - (
fml::CFRef<CGPathRef>)getTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix {
435 CGAffineTransform affine = [
self affineWithMatrix:matrix];
436 CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(path, &affine);
439 return fml::CFRef<CGPathRef>(transformedPath);
448 @property(nonatomic) NSUInteger capacity;
452 @property(nonatomic) NSMutableSet<FlutterClippingMaskView*>* pool;
458 - (instancetype)initWithCapacity:(NSInteger)capacity {
459 if (
self = [super init]) {
462 _pool = [[NSMutableSet alloc] initWithCapacity:1];
463 _capacity = capacity;
469 FML_DCHECK(
self.pool.count <=
self.capacity);
470 if (
self.pool.count == 0) {
473 screenScale:UIScreen.mainScreen.scale];
476 maskView.frame = frame;
478 [
self.pool removeObject:maskView];
483 FML_DCHECK(![
self.pool containsObject:maskView]);
484 FML_DCHECK(
self.pool.count <=
self.capacity);
485 if (
self.pool.count ==
self.capacity) {
488 [
self.pool addObject:maskView];
495 if (
self.isFirstResponder) {
498 for (UIView* subview in
self.subviews) {
499 if (subview.flt_hasFirstResponderInViewHierarchySubtree) {
514 - (instancetype)initWithEmbeddedView:(UIView*)embeddedView
516 gestureRecognizersBlockingPolicy:
518 self = [
super initWithFrame:embeddedView.frame];
520 self.multipleTouchEnabled = YES;
523 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
525 [
self addSubview:embeddedView];
529 platformViewsController:platformViewsController];
531 _delayingRecognizer =
534 forwardingRecognizer:forwardingRecognizer];
535 _blockingPolicy = blockingPolicy;
537 [
self addGestureRecognizer:_delayingRecognizer];
538 [
self addGestureRecognizer:forwardingRecognizer];
543 - (void)forceResetForwardingGestureRecognizerState {
551 [oldForwardingRecognizer recreateRecognizerWithTarget:
self];
552 self.delayingRecognizer.forwardingRecognizer = newForwardingRecognizer;
553 [
self removeGestureRecognizer:oldForwardingRecognizer];
554 [
self addGestureRecognizer:newForwardingRecognizer];
558 self.delayingRecognizer.state = UIGestureRecognizerStateFailed;
561 - (BOOL)containsWebView:(UIView*)view remainingSubviewDepth:(
int)remainingSubviewDepth {
562 if (remainingSubviewDepth < 0) {
565 if ([view isKindOfClass:[WKWebView
class]]) {
568 for (UIView* subview in view.subviews) {
569 if ([
self containsWebView:subview remainingSubviewDepth:remainingSubviewDepth - 1]) {
577 switch (_blockingPolicy) {
580 self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
590 if (@available(iOS 18.2, *)) {
597 if ([
self containsWebView:
self.
embeddedView remainingSubviewDepth:1]) {
598 [
self removeGestureRecognizer:self.delayingRecognizer];
599 [
self addGestureRecognizer:self.delayingRecognizer];
605 if (
self.delayingRecognizer.touchedEndedWithoutBlocking) {
609 self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
614 self.delayingRecognizer.shouldEndInNextTouchesEnded = YES;
625 - (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
628 - (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
631 - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
634 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
638 return self.flutterAccessibilityContainer;
645 - (instancetype)initWithTarget:(
id)target
647 forwardingRecognizer:(UIGestureRecognizer*)forwardingRecognizer {
648 self = [
super initWithTarget:target action:action];
650 self.delaysTouchesBegan = YES;
651 self.delaysTouchesEnded = YES;
652 self.delegate =
self;
653 _shouldEndInNextTouchesEnded = NO;
654 _touchedEndedWithoutBlocking = NO;
660 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
661 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
664 return otherGestureRecognizer != _forwardingRecognizer && otherGestureRecognizer !=
self;
667 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
668 shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
669 return otherGestureRecognizer ==
self;
672 - (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
673 self.touchedEndedWithoutBlocking = NO;
674 [
super touchesBegan:touches withEvent:event];
677 - (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
679 self.state = UIGestureRecognizerStateEnded;
680 self.shouldEndInNextTouchesEnded = NO;
682 self.touchedEndedWithoutBlocking = YES;
684 [
super touchesEnded:touches withEvent:event];
687 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
688 self.state = UIGestureRecognizerStateFailed;
710 - (instancetype)initWithTarget:(
id)target
712 self = [
super initWithTarget:target action:nil];
714 self.delegate =
self;
715 FML_DCHECK(platformViewsController);
716 _platformViewsController = platformViewsController;
724 platformViewsController:_platformViewsController];
727 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
735 [_flutterViewController touchesBegan:touches withEvent:event];
739 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
740 [_flutterViewController touchesMoved:touches withEvent:event];
743 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
744 [_flutterViewController touchesEnded:touches withEvent:event];
751 self.state = UIGestureRecognizerStateFailed;
753 [
self forceResetStateIfNeeded];
757 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
763 [_flutterViewController forceTouchesCancelled:touches];
766 self.state = UIGestureRecognizerStateFailed;
768 [
self forceResetStateIfNeeded];
772 - (void)forceResetStateIfNeeded {
774 dispatch_async(dispatch_get_main_queue(), ^{
779 if (strongSelf.state != UIGestureRecognizerStatePossible) {
780 [(FlutterTouchInterceptingView*)strongSelf.view forceResetForwardingGestureRecognizerState];
785 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
786 shouldRecognizeSimultaneouslyWithGestureRecognizer:
787 (UIGestureRecognizer*)otherGestureRecognizer {
FlutterPlatformViewGestureRecognizersBlockingPolicy
@ FlutterPlatformViewGestureRecognizersBlockingPolicyEager
@ FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded
instancetype initWithFrame
id accessibilityContainer()
BOOL flt_hasFirstResponderInViewHierarchySubtree
NSMutableArray * backdropFilterSubviews()
void CubicTo(const flutter::DlPoint &cp1, const flutter::DlPoint &cp2, const flutter::DlPoint &p2) override
void MoveTo(const flutter::DlPoint &p2, bool will_be_closed) override
void LineTo(const flutter::DlPoint &p2) override
void QuadTo(const flutter::DlPoint &cp, const flutter::DlPoint &p2) override
CGMutablePathRef TakePath() const
UIGestureRecognizer * forwardingRecognizer
BOOL shouldEndInNextTouchesEnded