Skip to content
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

[desktop-webview-window] Bring Webview Window to Foreground #274

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/desktop_webview_window/lib/src/webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ abstract class Webview {
/// Show or hide webview window
Future<void> setWebviewWindowVisibility(bool visible);

/// Activates the webview window (giving it the focus)
Future<void> bringToForeground();

/// Reload the current page.
Future<void> reload();

Expand Down
7 changes: 7 additions & 0 deletions packages/desktop_webview_window/lib/src/webview_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ class WebviewImpl extends Webview {
});
}

@override
Future<void> bringToForeground() {
return channel.invokeMethod("bringToForeground", {
"viewId": viewId,
});
}

@override
Future<void> back() {
return channel.invokeMethod("back", {"viewId": viewId});
Expand Down
13 changes: 13 additions & 0 deletions packages/desktop_webview_window/windows/web_view_window_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ void WebviewWindowPlugin::HandleMethodCall(
}
windows_[window_id]->setVisibility(visible);
result->Success();
} else if (method_call.method_name() == "bringToForeground") {
auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();
if (!windows_.count(window_id)) {
result->Error("0", "can not find webview window for id");
return;
}
if (!windows_[window_id]->GetWebView()) {
result->Error("0", "webview window not ready");
return;
}
windows_[window_id]->bringToForeground();
result->Success();
} else if (method_call.method_name() == "reload") {
auto *arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();
Expand Down
5 changes: 5 additions & 0 deletions packages/desktop_webview_window/windows/webview_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ void WebviewWindow::setVisibility(bool visible)
::ShowWindow(hwnd_.get(), SW_HIDE);
}

void WebviewWindow::bringToForeground()
{
SetForegroundWindow(hwnd_.get());
}

// static
LRESULT CALLBACK
WebviewWindow::WndProc(
Expand Down
2 changes: 2 additions & 0 deletions packages/desktop_webview_window/windows/webview_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class WebviewWindow {

void setVisibility(bool visible);

void bringToForeground();

[[nodiscard]] const std::unique_ptr<webview_window::WebView> &GetWebView() const {
return web_view_;
}
Expand Down