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 cornerRadius:(CGFloat)cornerRadius
98 isRoundedSuperellipse:(BOOL)isRoundedSuperellipse
99 visualEffectView:(UIVisualEffectView*)visualEffectView {
100 if (
self = [super init]) {
107 FML_DLOG(ERROR) <<
"Apple's API for UIVisualEffectView changed. Update the implementation to "
108 "access the gaussianBlur CAFilter.";
111 _backdropFilterView = visualEffectView;
112 _backdropFilterViewConfigured = NO;
124 + (void)prepareOnce:(UIVisualEffectView*)visualEffectView {
128 for (NSUInteger i = 0; i < visualEffectView.subviews.count; i++) {
129 UIView* view = visualEffectView.subviews[i];
130 if ([NSStringFromClass([view
class]) hasSuffix:
@"BackdropView"]) {
132 for (NSObject* filter in view.layer.filters) {
133 if ([[filter valueForKey:
@"name"] isEqual:
@"gaussianBlur"] &&
134 [[filter valueForKey:
@"inputRadius"] isKindOfClass:[NSNumber class]]) {
135 _gaussianBlurFilter = filter;
139 }
else if ([NSStringFromClass([view
class]) hasSuffix:
@"VisualEffectSubview"]) {
146 + (BOOL)isUIVisualEffectViewImplementationValid {
151 FML_DCHECK(_backdropFilterView);
152 if (!
self.backdropFilterViewConfigured) {
153 [
self updateVisualEffectView:_backdropFilterView];
154 self.backdropFilterViewConfigured = YES;
156 return _backdropFilterView;
159 - (void)updateVisualEffectView:(UIVisualEffectView*)visualEffectView {
160 NSObject* gaussianBlurFilter = [_gaussianBlurFilter copy];
161 FML_DCHECK(gaussianBlurFilter);
162 UIView* backdropView = visualEffectView.subviews[_indexOfBackdropView];
163 [gaussianBlurFilter setValue:@(_blurRadius) forKey:@"inputRadius"];
164 backdropView.layer.filters = @[ gaussianBlurFilter ];
166 UIView* visualEffectSubview = visualEffectView.subviews[_indexOfVisualEffectSubview];
167 visualEffectSubview.layer.backgroundColor = UIColor.clearColor.CGColor;
168 visualEffectView.frame = _frame;
170 visualEffectView.layer.cornerRadius = _cornerRadius;
171 if (@available(iOS 13.0, *)) {
172 visualEffectView.layer.cornerCurve =
173 _isRoundedSuperellipse ? kCACornerCurveContinuous : kCACornerCurveCircular;
175 visualEffectView.clipsToBounds = YES;
177 self.backdropFilterView = visualEffectView;
184 @property(nonatomic, copy) NSArray<PlatformViewFilter*>* filters;
194 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
195 for (UIView* view in
self.subviews) {
196 if ([view pointInside:[
self convertPoint:point toView:view] withEvent:event]) {
204 FML_DCHECK(
self.filters.count ==
self.backdropFilterSubviews.count);
205 if (
self.filters.count == 0 && filters.count == 0) {
208 self.filters = filters;
209 NSUInteger index = 0;
210 for (index = 0; index <
self.filters.count; index++) {
211 UIVisualEffectView* backdropFilterView;
214 backdropFilterView = filter.backdropFilterView;
215 [self addSubview:backdropFilterView];
216 [self.backdropFilterSubviews addObject:backdropFilterView];
218 [filter updateVisualEffectView:self.backdropFilterSubviews[index]];
222 [self.backdropFilterSubviews[i - 1] removeFromSuperview];
223 [self.backdropFilterSubviews removeLastObject];
228 if (!_backdropFilterSubviews) {
229 _backdropFilterSubviews = [[NSMutableArray alloc] init];
231 return _backdropFilterSubviews;
246 @property(nonatomic) CATransform3D reverseScreenScale;
248 - (
fml::CFRef<CGPathRef>)getTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix;
253 std::vector<fml::CFRef<CGPathRef>> paths_;
259 return [
self initWithFrame:frame screenScale:[UIScreen mainScreen].scale];
262 - (instancetype)
initWithFrame:(CGRect)frame screenScale:(CGFloat)screenScale {
264 self.backgroundColor = UIColor.clearColor;
265 _reverseScreenScale = CATransform3DMakeScale(1 / screenScale, 1 / screenScale, 1);
272 + (Class)layerClass {
273 return [CAShapeLayer class];
276 - (CAShapeLayer*)shapeLayer {
277 return (CAShapeLayer*)
self.layer;
284 [
self shapeLayer].path = nil;
285 [
self setNeedsDisplay];
293 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event {
297 - (void)drawRect:(CGRect)rect {
301 CGContextRef context = UIGraphicsGetCurrentContext();
302 CGContextSaveGState(context);
305 CGContextSetAlpha(context, 1);
307 for (
size_t i = 0; i < paths_.size(); i++) {
308 CGContextAddPath(context, paths_.at(i));
309 CGContextClip(context);
311 CGContextFillRect(context, rect);
312 CGContextRestoreGState(context);
316 [
super drawRect:rect];
317 if (![
self shapeLayer].path) {
318 if (paths_.size() == 1) {
320 [
self shapeLayer].path = paths_.at(0);
323 CGPathRef pathSoFar = CGPathCreateWithRect(
rectSoFar_, nil);
324 [
self shapeLayer].path = pathSoFar;
325 CGPathRelease(pathSoFar);
331 - (void)clipRect:(const
flutter::DlRect&)clipDlRect matrix:(const
flutter::DlMatrix&)matrix {
333 CGPathRef path = CGPathCreateWithRect(clipRect, nil);
335 CATransform3D matrixInPoints =
337 paths_.push_back([
self getTransformedPath:path matrix:matrixInPoints]);
338 CGAffineTransform affine = [
self affineWithMatrix:matrixInPoints];
340 if (affine.b == 0 && affine.c == 0) {
347 - (void)clipRRect:(const
flutter::DlRoundRect&)clipDlRRect matrix:(const
flutter::DlMatrix&)matrix {
348 if (clipDlRRect.IsEmpty()) {
350 }
else if (clipDlRRect.IsRect()) {
351 [
self clipRect:clipDlRRect.GetBounds() matrix:matrix];
354 CGPathRef pathRef =
nullptr;
357 if (clipDlRRect.GetRadii().AreAllCornersSame()) {
359 auto radii = clipDlRRect.GetRadii();
361 CGPathCreateWithRoundedRect(clipRect, radii.top_left.width, radii.top_left.height, nil);
363 CGMutablePathRef mutablePathRef = CGPathCreateMutable();
365 flutter::DlRect clipDlRect = clipDlRRect.GetBounds();
366 auto left = clipDlRect.GetLeft();
367 auto top = clipDlRect.GetTop();
368 auto right = clipDlRect.GetRight();
369 auto bottom = clipDlRect.GetBottom();
370 flutter::DlRoundingRadii radii = clipDlRRect.GetRadii();
371 auto& top_left = radii.top_left;
372 auto& top_right = radii.top_right;
373 auto& bottom_left = radii.bottom_left;
374 auto& bottom_right = radii.bottom_right;
381 CGPathMoveToPoint(mutablePathRef, nil,
382 left + top_left.width, top);
384 CGPathAddLineToPoint(mutablePathRef, nil,
385 right - top_right.width, top);
386 CGPathAddCurveToPoint(mutablePathRef, nil,
388 right, top + top_right.height,
389 right, top + top_right.height);
391 CGPathAddLineToPoint(mutablePathRef, nil,
392 right, bottom - bottom_right.height);
393 CGPathAddCurveToPoint(mutablePathRef, nil,
395 right - bottom_right.width, bottom,
396 right - bottom_right.width, bottom);
398 CGPathAddLineToPoint(mutablePathRef, nil,
399 left + bottom_left.width, bottom);
400 CGPathAddCurveToPoint(mutablePathRef, nil,
402 left, bottom - bottom_left.height,
403 left, bottom - bottom_left.height);
405 CGPathAddLineToPoint(mutablePathRef, nil,
406 left, top + top_left.height);
407 CGPathAddCurveToPoint(mutablePathRef, nil,
409 left + top_left.width, top,
410 left + top_left.width, top);
411 CGPathCloseSubpath(mutablePathRef);
412 pathRef = mutablePathRef;
415 CATransform3D matrixInPoints =
420 paths_.push_back([
self getTransformedPath:pathRef matrix:matrixInPoints]);
424 - (void)clipPath:(const
flutter::DlPath&)dlPath matrix:(const
flutter::DlMatrix&)matrix {
427 CGPathReceiver receiver;
432 dlPath.Dispatch(receiver);
435 CATransform3D matrixInPoints =
437 paths_.push_back([
self getTransformedPath:receiver.TakePath() matrix:matrixInPoints]);
440 - (CGAffineTransform)affineWithMatrix:(CATransform3D)matrix {
441 return CGAffineTransformMake(matrix.m11, matrix.m12, matrix.m21, matrix.m22, matrix.m41,
445 - (
fml::CFRef<CGPathRef>)getTransformedPath:(CGPathRef)path matrix:(CATransform3D)matrix {
446 CGAffineTransform affine = [
self affineWithMatrix:matrix];
447 CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(path, &affine);
450 return fml::CFRef<CGPathRef>(transformedPath);
459 @property(nonatomic) NSUInteger capacity;
463 @property(nonatomic) NSMutableSet<FlutterClippingMaskView*>* pool;
469 - (instancetype)initWithCapacity:(NSInteger)capacity {
470 if (
self = [super init]) {
473 _pool = [[NSMutableSet alloc] initWithCapacity:1];
474 _capacity = capacity;
480 FML_DCHECK(
self.pool.count <=
self.capacity);
481 if (
self.pool.count == 0) {
484 screenScale:UIScreen.mainScreen.scale];
487 maskView.frame = frame;
489 [
self.pool removeObject:maskView];
494 FML_DCHECK(![
self.pool containsObject:maskView]);
495 FML_DCHECK(
self.pool.count <=
self.capacity);
496 if (
self.pool.count ==
self.capacity) {
499 [
self.pool addObject:maskView];
506 if (
self.isFirstResponder) {
509 for (UIView* subview in
self.subviews) {
510 if (subview.flt_hasFirstResponderInViewHierarchySubtree) {
525 - (instancetype)initWithEmbeddedView:(UIView*)embeddedView
527 gestureRecognizersBlockingPolicy:
529 self = [
super initWithFrame:embeddedView.frame];
531 self.multipleTouchEnabled = YES;
534 (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
536 [
self addSubview:embeddedView];
540 platformViewsController:platformViewsController];
542 _delayingRecognizer =
545 forwardingRecognizer:forwardingRecognizer];
546 _blockingPolicy = blockingPolicy;
548 [
self addGestureRecognizer:_delayingRecognizer];
549 [
self addGestureRecognizer:forwardingRecognizer];
554 - (void)forceResetForwardingGestureRecognizerState {
562 [oldForwardingRecognizer recreateRecognizerWithTarget:
self];
563 self.delayingRecognizer.forwardingRecognizer = newForwardingRecognizer;
564 [
self removeGestureRecognizer:oldForwardingRecognizer];
565 [
self addGestureRecognizer:newForwardingRecognizer];
569 self.delayingRecognizer.state = UIGestureRecognizerStateFailed;
572 - (BOOL)containsWebView:(UIView*)view {
573 if ([view isKindOfClass:[WKWebView
class]]) {
576 for (UIView* subview in view.subviews) {
577 if ([
self containsWebView:subview]) {
584 - (void)searchAndFixWebView:(UIView*)view {
585 if ([view isKindOfClass:[WKWebView
class]]) {
586 return [
self searchAndFixWebViewGestureRecognzier:view];
588 for (UIView* subview in view.subviews) {
589 [
self searchAndFixWebView:subview];
594 - (void)searchAndFixWebViewGestureRecognzier:(UIView*)view {
595 for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
606 if (recognizer.enabled &&
607 [NSStringFromClass([recognizer
class]) hasSuffix:
@"TouchEventsGestureRecognizer"]) {
608 recognizer.enabled = NO;
609 recognizer.enabled = YES;
612 for (UIView* subview in view.subviews) {
613 [
self searchAndFixWebViewGestureRecognzier:subview];
618 switch (_blockingPolicy) {
621 self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
631 if (@available(iOS 26.0, *)) {
636 NSNumber* isWorkaroundDisabled =
637 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTDisableWebViewGestureReset"];
638 if (!isWorkaroundDisabled.boolValue) {
639 [
self searchAndFixWebView:self.embeddedView];
641 }
else if (@available(iOS 18.2, *)) {
646 [
self removeGestureRecognizer:self.delayingRecognizer];
647 [
self addGestureRecognizer:self.delayingRecognizer];
653 if (
self.delayingRecognizer.touchedEndedWithoutBlocking) {
657 self.delayingRecognizer.state = UIGestureRecognizerStateEnded;
662 self.delayingRecognizer.shouldEndInNextTouchesEnded = YES;
673 - (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
676 - (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
679 - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
682 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
686 return self.flutterAccessibilityContainer;
693 - (instancetype)initWithTarget:(
id)target
695 forwardingRecognizer:(UIGestureRecognizer*)forwardingRecognizer {
696 self = [
super initWithTarget:target action:action];
698 self.delaysTouchesBegan = YES;
699 self.delaysTouchesEnded = YES;
700 self.delegate =
self;
701 _shouldEndInNextTouchesEnded = NO;
702 _touchedEndedWithoutBlocking = NO;
708 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
709 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
712 return otherGestureRecognizer != _forwardingRecognizer && otherGestureRecognizer !=
self;
715 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
716 shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer {
717 return otherGestureRecognizer ==
self;
720 - (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
721 self.touchedEndedWithoutBlocking = NO;
722 [
super touchesBegan:touches withEvent:event];
725 - (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event {
727 self.state = UIGestureRecognizerStateEnded;
728 self.shouldEndInNextTouchesEnded = NO;
730 self.touchedEndedWithoutBlocking = YES;
732 [
super touchesEnded:touches withEvent:event];
735 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
736 self.state = UIGestureRecognizerStateFailed;
758 - (instancetype)initWithTarget:(
id)target
760 self = [
super initWithTarget:target action:nil];
762 self.delegate =
self;
763 FML_DCHECK(platformViewsController);
764 _platformViewsController = platformViewsController;
772 platformViewsController:_platformViewsController];
775 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
783 [_flutterViewController touchesBegan:touches withEvent:event];
787 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
788 [_flutterViewController touchesMoved:touches withEvent:event];
791 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
792 [_flutterViewController touchesEnded:touches withEvent:event];
799 self.state = UIGestureRecognizerStateFailed;
801 [
self forceResetStateIfNeeded];
805 - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
811 [_flutterViewController forceTouchesCancelled:touches];
814 self.state = UIGestureRecognizerStateFailed;
816 [
self forceResetStateIfNeeded];
820 - (void)forceResetStateIfNeeded {
824 if (@available(iOS 26.0, *)) {
828 dispatch_async(dispatch_get_main_queue(), ^{
833 if (strongSelf.state != UIGestureRecognizerStatePossible) {
834 [(FlutterTouchInterceptingView*)strongSelf.view forceResetForwardingGestureRecognizerState];
839 - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
840 shouldRecognizeSimultaneouslyWithGestureRecognizer:
841 (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