Skip to content

Commit 379211b

Browse files
authored
[desktop-webview-window] Move Webview Window (#280)
1 parent 07d1208 commit 379211b

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
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+
/// Move and Resize the webview window
60+
Future<void> moveWebviewWindow(int left, int top, int width, int height);
61+
5962
/// Activates the webview window (giving it the focus)
6063
Future<void> bringToForeground();
6164

packages/desktop_webview_window/lib/src/webview_impl.dart

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

181+
@override
182+
Future<void> moveWebviewWindow(int left, int top, int width, int height) {
183+
return channel.invokeMethod("moveWebviewWindow", {
184+
"viewId": viewId,
185+
"left": left,
186+
"top": top,
187+
"width": width,
188+
"height": height,
189+
});
190+
}
191+
181192
@override
182193
Future<void> bringToForeground() {
183194
return channel.invokeMethod("bringToForeground", {

packages/desktop_webview_window/windows/web_view_window_plugin.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,23 @@ void WebviewWindowPlugin::HandleMethodCall(
176176
}
177177
windows_[window_id]->setVisibility(visible);
178178
result->Success();
179+
} else if (method_call.method_name() == "moveWebviewWindow") {
180+
auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
181+
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();
182+
auto left = std::get<int>(arguments->at(flutter::EncodableValue("left")));
183+
auto top = std::get<int>(arguments->at(flutter::EncodableValue("top")));
184+
auto width = std::get<int>(arguments->at(flutter::EncodableValue("width")));
185+
auto height = std::get<int>(arguments->at(flutter::EncodableValue("height")));
186+
if (!windows_.count(window_id)) {
187+
result->Error("0", "can not find webview window for id");
188+
return;
189+
}
190+
if (!windows_[window_id]->GetWebView()) {
191+
result->Error("0", "webview window not ready");
192+
return;
193+
}
194+
windows_[window_id]->moveWebviewWindow(left, top, width, height);
195+
result->Success();
179196
} else if (method_call.method_name() == "bringToForeground") {
180197
auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
181198
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();

packages/desktop_webview_window/windows/webview_window.cc

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

147-
void WebviewWindow::bringToForeground()
148-
{
147+
void WebviewWindow::moveWebviewWindow(int left, int top, int width, int height) {
148+
::SetWindowPos(hwnd_.get(), nullptr, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
149+
}
150+
151+
void WebviewWindow::bringToForeground() {
149152
SetForegroundWindow(hwnd_.get());
150153
}
151154

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 moveWebviewWindow(int left, int top, int width, int height);
62+
6163
void bringToForeground();
6264

6365
[[nodiscard]] const std::unique_ptr<webview_window::WebView> &GetWebView() const {

0 commit comments

Comments
 (0)