Skip to content

[Catalyst] WebView does not display ContextFlyout on right-click - fix #30309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/Core/src/Handlers/View/ViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,58 @@ internal static void MapContextFlyout(IElementHandler handler, IContextFlyoutEle
if (contextFlyoutContainer.ContextFlyout != null)
{
if (currentInteraction == null)
uiView.AddInteraction(new MauiUIContextMenuInteraction(handler));
{
if (uiView is WebKit.WKWebView webView)
{
var existingMaskView = webView.ViewWithTag(InterceptRightClickWebViewMaskView.MaskViewTag);
existingMaskView?.RemoveFromSuperview();

// If the view is a WKWebView, we need to intercept right-clicks
// to show the context menu, so we add a mask view that intercepts
// right-clicks and passes them to the context menu interaction.
var maskView = new InterceptRightClickWebViewMaskView(uiView.Bounds);
webView.AddSubview(maskView);
maskView.AddInteraction(new MauiUIContextMenuInteraction(handler));
}
else
{
uiView.AddInteraction(new MauiUIContextMenuInteraction(handler));
}
}
}
else if (currentInteraction != null)
{
uiView.RemoveInteraction(currentInteraction);

if (uiView is WebKit.WKWebView webView)
{
var existingMaskView = webView.ViewWithTag(InterceptRightClickWebViewMaskView.MaskViewTag);
existingMaskView?.RemoveFromSuperview();
}
}
}
}

class InterceptRightClickWebViewMaskView : PlatformView
{
public const int MaskViewTag = 9999;
public InterceptRightClickWebViewMaskView(CGRect frame) : base(frame)
{
Tag = MaskViewTag;
BackgroundColor = UIColor.Clear;
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
UserInteractionEnabled = true;
}

public override PlatformView? HitTest(CGPoint point, UIEvent? uievent)
{
if (uievent is UIEvent { Type: UIEventType.Touches } touch)
{
if (touch.ButtonMask.HasFlag(UIEventButtonMask.Secondary))
return base.HitTest(point, uievent);
}

return null;
}
}
#endif
Expand Down