Skip to content

Add environment on Edge Webview#1223

Open
noaroussiere wants to merge 18 commits into
webview:masterfrom
noaroussiere:master
Open

Add environment on Edge Webview#1223
noaroussiere wants to merge 18 commits into
webview:masterfrom
noaroussiere:master

Conversation

@noaroussiere
Copy link
Copy Markdown
Contributor

@noaroussiere noaroussiere commented Nov 26, 2024

Allow ICoreWebView2EnvironmentOptions to be Passed to Constructor

Work only for Windows.

Linux or Mac didn't need this feature

Description

This pull request introduces a modification to the WebView library, allowing the ICoreWebView2EnvironmentOptions to be passed directly to the constructor. This enhancement provides greater flexibility in configuring WebView2 environments with custom options, such as disabling features or setting specific browser arguments.


Changes

  • Updated the constructor to accept an ICoreWebView2EnvironmentOptions parameter.
  • Modified relevant parts of the initialization process to utilize the provided environment options.

Implementation Details

Constructor Modification

The constructor is now capable of receiving an ICoreWebView2EnvironmentOptions parameter.

Example Windows:

#ifdef _MSC_VER
#include <WebView2EnvironmentOptions.h>
#endif
#include <iostream>
#include "webview/webview.h"


#ifdef _WIN32
int WINAPI WinMain(HINSTANCE /*hInst*/, HINSTANCE /*hPrevInst*/,
                   LPSTR /*lpCmdLine*/, int /*nCmdShow*/) {
#else
int main() {
#endif
  try {
#ifdef _MSC_VER
    auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
    options->put_AdditionalBrowserArguments(L"--flag-switches-begin --disable-features=ElasticOverscroll,OverscrollHistoryNavigation --flag-switches-end");
    webview::webview w(false, nullptr, options.Get());
#else
    webview::webview w(false, nullptr, nullptr);
#endif
    w.set_title("Basic Example");
    w.set_size(480, 320, WEBVIEW_HINT_NONE);
    w.set_html("Thanks for using webview!");
    w.run();
  } catch (const webview::exception &e) {
    std::cerr << e.what() << '\n';
    return 1;
  }

  return 0;
}

@SteffenL
Copy link
Copy Markdown
Collaborator

SteffenL commented Dec 3, 2024

Thanks for this PR.

I wonder if it might be a good idea to add a new constructor that allows passing in a versioned struct with options. The struct could perhaps have the same members as the existing constructor's parameters and other things such as a window title, initial size, and also backend-specific things such as a pointer directly to CoreWebView2EnvironmentOptions or an abstraction that can be shared by all of the backends.

The way the current webview class concerns itself with pretty much everything makes it a bit complicated when introducing new features.

For example, it isn't clear to me how options for other backends would be passed in the future and who would be responsible for the memory. If we need to pass any other type of options than CoreWebView2EnvironmentOptions for WebView2 or other backends, we would need to add another defaulted parameter for that to avoid breaking existing code.

When it comes to the C API, I'm afraid we can't change webview_create(). There's currently no way for the caller to let the library know which versions of the header its using. They could be calling webview_create() with 2 or 3 arguments and the library might crash due to expecting a different number of arguments.

Especially here it would help to have a versioned struct so that the function signature doesn't have to change in the future. We would have to add a new function though such as webview_create_with_options() because we can't overload webview_create().

I made an attempt to improve things in PR #766 some time ago, and I sometimes take bits of code from it, but it's pretty much abandoned.

The route with an options struct seems like a good path to me, at least for the C API.

In the long run, the main API would ideally not be one god class called webview, but something that has more clear scopes. That's an idea at least for the future.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants