WebView2 with caret-browsing mode enabled

Viewed 88

Is it possible to programmatically enable caret-browsing mode in the Microsoft Edge WebView2 control?

I have tried setting the chromium command-line argument, to no avail (other arguments, such as --diagnostics, work fine).

auto wv2opts = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();

// https://peter.sh/experiments/chromium-command-line-switches/
wv2opts->put_AdditionalBrowserArguments(L"--enable-caret-browsing"); 

hr = CreateCoreWebView2EnvironmentWithOptions(nullptr,nullptr,wv2opts.Get(),
         Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
         this,&AppWindow::OnCreateEnvironmentCompleted).Get());
1 Answers

Found the answer, by reading the caret navigation design document, the available blink options and the Chromium command-line arguments.

The short version is that you set this in the blink settings of Chromium:

auto wv2opts = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
wv2opts->put_AdditionalBrowserArguments(L"--blink-settings=caretBrowsingEnabled"); 
hr = CreateCoreWebView2EnvironmentWithOptions(nullptr,nullptr,
         wv2opts.Get(),
         Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
         this,&AppWindow::OnCreateEnvironmentCompleted).Get());

Note that blink settings are global to the WebView2 process group, and apply to all WebView2 controls created in the WebView2 process group. To get separate WebView2 process groups, we can supply unique data folder locations, like this:

   hr = CreateCoreWebView2EnvironmentWithOptions(nullptr,this->GetUniqueDataPath(),
             wv2opts.Get(),
             Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
             this,&AppWindow::OnCreateEnvironmentCompleted).Get());

...and voilá!, we get to pick and choose which ones have caret-mode enabled or not.

Related