Skip to content

Add setAreDevToolsEnabled Method #276

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
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ class WebviewController extends ValueNotifier<WebviewValue> {
return _methodChannel.invokeMethod('setUserAgent', userAgent);
}

/// Sets the areDevToolsEnabled value.
Future<void> setAreDevToolsEnabled(bool areDevToolsEnabled) async {
if (_isDisposed) {
return;
}
assert(value.isInitialized);
return _methodChannel.invokeMethod(
'put_AreDevToolsEnabled', areDevToolsEnabled);
}

/// Clears browser cookies.
Future<void> clearCookies() async {
if (_isDisposed) {
Expand Down
17 changes: 12 additions & 5 deletions windows/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ Webview::Webview(
webview_controller_->put_ShouldDetectMonitorScaleChanges(FALSE);
webview_controller_->put_RasterizationScale(1.0);

wil::com_ptr<ICoreWebView2Settings> settings;
if (SUCCEEDED(webview_->get_Settings(settings.put()))) {
settings2_ = settings.try_query<ICoreWebView2Settings2>();
if (SUCCEEDED(webview_->get_Settings(settings_.put()))) {
settings2_ = settings_.try_query<ICoreWebView2Settings2>();

settings->put_IsStatusBarEnabled(FALSE);
settings->put_AreDefaultContextMenusEnabled(FALSE);
settings_->put_IsStatusBarEnabled(FALSE);
settings_->put_AreDefaultContextMenusEnabled(FALSE);
}

EnableSecurityUpdates();
Expand Down Expand Up @@ -447,6 +446,14 @@ bool Webview::SetUserAgent(const std::string& user_agent) {
return false;
}

bool Webview::SetAreDevToolsEnabled(bool areDevToolsEnabled) {
if (settings_) {
return settings_->put_AreDevToolsEnabled(areDevToolsEnabled) ==
S_OK;
}
return false;
}

bool Webview::SetBackgroundColor(int32_t color) {
if (!IsValid()) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions windows/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class Webview {
bool SetCacheDisabled(bool disabled);
void SetPopupWindowPolicy(WebviewPopupWindowPolicy policy);
bool SetUserAgent(const std::string& user_agent);
bool SetAreDevToolsEnabled(bool areDevToolsEnabled);
bool OpenDevTools();
bool SetBackgroundColor(int32_t color);
bool SetZoomFactor(double factor);
Expand Down Expand Up @@ -219,6 +220,7 @@ class Webview {
wil::com_ptr<ICoreWebView2> webview_;
wil::com_ptr<ICoreWebView2DevToolsProtocolEventReceiver>
devtools_protocol_event_receiver_;
wil::com_ptr<ICoreWebView2Settings> settings_;
wil::com_ptr<ICoreWebView2Settings2> settings2_;
POINT last_cursor_pos_ = {0, 0};
VirtualKeyState virtual_keys_;
Expand Down
14 changes: 13 additions & 1 deletion windows/webview_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,19 @@ void WebviewBridge::HandleMethodCall(
}
return result->Error(kErrorInvalidArgs);
}


// setAreDevToolsEnabled: bool
if (method_name.compare(kMethodSetAreDevToolsEnabled) == 0) {
if (const auto areDevToolsEnabled = std::get_if<bool>(method_call.arguments())) {
if (webview_->SetAreDevToolsEnabled(*areDevToolsEnabled)) {
return result->Success();
}
return result->Error(kErrorNotSupported,
"Setting the setAreDevToolsEnabled failed.");
}
return result->Error(kErrorInvalidArgs);
}

// setBackgroundColor: int
if (method_name.compare(kMethodSetBackgroundColor) == 0) {
if (const auto color = std::get_if<int32_t>(method_call.arguments())) {
Expand Down