Skip to content

Add ordered adapter preferences - #77

Merged
maxkatz6 merged 3 commits into
AvaloniaUI:mainfrom
mjcheetham:wvadapter-prefs
Sep 17, 2026
Merged

maxkatz6 merged 3 commits into
AvaloniaUI:mainfrom
mjcheetham:wvadapter-prefs

Conversation

@mjcheetham

Copy link
Copy Markdown
Contributor

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>
Comment thread src/Avalonia.Controls.WebView.Core/WebViewAdapter.cs
Comment thread src/Avalonia.Controls.WebView.Core/WebViewAdapter.cs Outdated
@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.

Now that we have a general way to specify webview adapter preferences,
we no longer need the specific WPE WebView 'PreferWebKitGtkInstead' flag
on the environment requested event args. Mark this property as Obsolete
with a hint to callers to use the new API 'AdapterPreference' on either
the NativeWebView or NativeWebDialog.

Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
Remove the internal-only API to prefer using the WebView1 adapter for
Windows (over WebView2) in favour of the generic 'AdapterPreference'
ordered preference list on either NativeWebView or NativeWebDialog.

Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
@maxkatz6
maxkatz6 merged commit d45d711 into AvaloniaUI:main Sep 17, 2026
1 check passed
@maxkatz6

Copy link
Copy Markdown
Member

Thank you!

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