Skip to content

Commit 4220524

Browse files
authored
[desktop-webview-window] Bring Webview Window to Foreground (#274)
1 parent 0edbcd0 commit 4220524

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

packages/desktop_webview_window/lib/src/webview.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ abstract class Webview {
5656
/// Show or hide webview window
5757
Future<void> setWebviewWindowVisibility(bool visible);
5858

59+
/// Activates the webview window (giving it the focus)
60+
Future<void> bringToForeground();
61+
5962
/// Reload the current page.
6063
Future<void> reload();
6164

packages/desktop_webview_window/lib/src/webview_impl.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ class WebviewImpl extends Webview {
178178
});
179179
}
180180

181+
@override
182+
Future<void> bringToForeground() {
183+
return channel.invokeMethod("bringToForeground", {
184+
"viewId": viewId,
185+
});
186+
}
187+
181188
@override
182189
Future<void> back() {
183190
return channel.invokeMethod("back", {"viewId": viewId});

packages/desktop_webview_window/windows/web_view_window_plugin.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ void WebviewWindowPlugin::HandleMethodCall(
176176
}
177177
windows_[window_id]->setVisibility(visible);
178178
result->Success();
179+
} else if (method_call.method_name() == "bringToForeground") {
180+
auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
181+
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();
182+
if (!windows_.count(window_id)) {
183+
result->Error("0", "can not find webview window for id");
184+
return;
185+
}
186+
if (!windows_[window_id]->GetWebView()) {
187+
result->Error("0", "webview window not ready");
188+
return;
189+
}
190+
windows_[window_id]->bringToForeground();
191+
result->Success();
179192
} else if (method_call.method_name() == "reload") {
180193
auto *arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
181194
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();

packages/desktop_webview_window/windows/webview_window.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ void WebviewWindow::setVisibility(bool visible)
144144
::ShowWindow(hwnd_.get(), SW_HIDE);
145145
}
146146

147+
void WebviewWindow::bringToForeground()
148+
{
149+
SetForegroundWindow(hwnd_.get());
150+
}
151+
147152
// static
148153
LRESULT CALLBACK
149154
WebviewWindow::WndProc(

packages/desktop_webview_window/windows/webview_window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class WebviewWindow {
5858

5959
void setVisibility(bool visible);
6060

61+
void bringToForeground();
62+
6163
[[nodiscard]] const std::unique_ptr<webview_window::WebView> &GetWebView() const {
6264
return web_view_;
6365
}

0 commit comments

Comments
 (0)