Skip to content

Add ordered adapter preferences - #77

Open
mjcheetham wants to merge 1 commit into
AvaloniaUI:mainfrom
mjcheetham:wvadapter-prefs
Open

Add ordered adapter preferences#77
mjcheetham wants to merge 1 commit into
AvaloniaUI:mainfrom
mjcheetham:wvadapter-prefs

Conversation

@mjcheetham

Copy link
Copy Markdown

Applications may depend on capabilities that are only available from a particular WebView implementation, but backend-specific switches do not scale as more adapters and hosting modes are added.

Expose a common ordered preference list on NativeWebView and NativeWebDialog. A non-empty list is exhaustive, allowing callers to express fallback order or require a single adapter, while an empty list preserves the existing platform defaults.

Apply selection to WebView1 and WebView2 on Windows and to WPE WebKit and WebKitGTK on Linux. This also lets authentication callers select an adapter through the existing NativeWebDialogFactory without adding WebView-specific options to WebAuthenticatorOptions.

Example with NativeWebDialog

var dialog = new NativeWebDialog
{
    Source = new Uri("https://example.com"),
    AdapterPreference =
    [
        WebViewAdapterType.WebView1, // prefer WebView1 over WebView2
        WebViewAdapterType.WebView2,
    ]
};

dialog.Show();

Example with WebAuthenticationBroker

var options = new WebAuthenticatorOptions(requestUri, redirectUri)
{
    Mode = WebAuthenticatorMode.NativeWebDialog,
    NativeWebDialogFactory = () => new NativeWebDialog
    {
        AdapterPreference = [WebViewAdapterType.WebView1] // WebView1-only
    }
};

var result = await WebAuthenticationBroker.AuthenticateAsync(owner, options)

Example with NativeWebView via code+XAML

public IReadOnlyList<WebViewAdapterType> MyAdapters { get; } =
[
    WebViewAdapterType.WebView1,
    WebViewAdapterType.WebView2
];
<NativeWebView
    Source="https://example.com"
    AdapterPreference="{Binding MyAdapters}" />

Example with NativeWebView via XAML-only

xmlns:platform="clr-namespace:Avalonia.Platform;assembly=Avalonia.Controls.WebView"

<NativeWebView>
    <NativeWebView.AdapterPreference>
        <x:Array Type="platform:WebViewAdapterType">
            <platform:WebViewAdapterType>WebView1</platform:WebViewAdapterType>
            <platform:WebViewAdapterType>WebView2</platform:WebViewAdapterType>
        </x:Array>
    </NativeWebView.AdapterPreference>
</NativeWebView>

This is a proposal to address #16

Applications may depend on capabilities that are only available from a
particular WebView implementation, but backend-specific switches do not
scale as more adapters and hosting modes are added.

Expose a common ordered preference list on NativeWebView and
NativeWebDialog. A non-empty list is exhaustive, allowing callers to
express fallback order or require a single adapter, while an empty list
preserves the existing platform defaults.

Apply selection to WebView1 and WebView2 on Windows and to WPE WebKit
and WebKitGTK on Linux. This also lets authentication callers select an
adapter through the existing NativeWebDialogFactory without adding
WebView-specific options to WebAuthenticatorOptions.

Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
environmentRequested(args);
await deferralManager.WaitForDeferralsAsync();

if (hasExplicitPref || !args.PreferWebKitGtkInstead)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mark PreferWebKitGtkInstead obsolete with a hint to use new API?

var args = new WindowsWebView2EnvironmentRequestedEventArgs(deferralManager);
environmentRequested(args);
await deferralManager.WaitForDeferralsAsync();
if ((!hasExplicitPref && args.PreferWebView1Instead)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PreferWebView1Instead can be completely removed and this code simplified. It wasn't part of the Public API.

@maxkatz6

maxkatz6 commented Sep 2, 2026

Copy link
Copy Markdown
Member

XAML compiler should be able to handle these enums as in AdapterPreference="WebView1, WebView2" syntax. But I don't remember if this property needs to be a mutable list or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants