Skip to content

Commit

Permalink
[desktop-webview-window] Move Webview Window (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iri-Hor committed Aug 28, 2023
1 parent 07d1208 commit 379211b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/desktop_webview_window/lib/src/webview.dart
Expand Up @@ -56,6 +56,9 @@ abstract class Webview {
/// Show or hide webview window
Future<void> setWebviewWindowVisibility(bool visible);

/// Move and Resize the webview window
Future<void> moveWebviewWindow(int left, int top, int width, int height);

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

Expand Down
11 changes: 11 additions & 0 deletions packages/desktop_webview_window/lib/src/webview_impl.dart
Expand Up @@ -178,6 +178,17 @@ class WebviewImpl extends Webview {
});
}

@override
Future<void> moveWebviewWindow(int left, int top, int width, int height) {
return channel.invokeMethod("moveWebviewWindow", {
"viewId": viewId,
"left": left,
"top": top,
"width": width,
"height": height,
});
}

@override
Future<void> bringToForeground() {
return channel.invokeMethod("bringToForeground", {
Expand Down
17 changes: 17 additions & 0 deletions packages/desktop_webview_window/windows/web_view_window_plugin.cc
Expand Up @@ -176,6 +176,23 @@ void WebviewWindowPlugin::HandleMethodCall(
}
windows_[window_id]->setVisibility(visible);
result->Success();
} else if (method_call.method_name() == "moveWebviewWindow") {
auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();
auto left = std::get<int>(arguments->at(flutter::EncodableValue("left")));
auto top = std::get<int>(arguments->at(flutter::EncodableValue("top")));
auto width = std::get<int>(arguments->at(flutter::EncodableValue("width")));
auto height = std::get<int>(arguments->at(flutter::EncodableValue("height")));
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]->moveWebviewWindow(left, top, width, height);
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();
Expand Down
7 changes: 5 additions & 2 deletions packages/desktop_webview_window/windows/webview_window.cc
Expand Up @@ -144,8 +144,11 @@ void WebviewWindow::setVisibility(bool visible)
::ShowWindow(hwnd_.get(), SW_HIDE);
}

void WebviewWindow::bringToForeground()
{
void WebviewWindow::moveWebviewWindow(int left, int top, int width, int height) {
::SetWindowPos(hwnd_.get(), nullptr, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
}

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

Expand Down
2 changes: 2 additions & 0 deletions packages/desktop_webview_window/windows/webview_window.h
Expand Up @@ -58,6 +58,8 @@ class WebviewWindow {

void setVisibility(bool visible);

void moveWebviewWindow(int left, int top, int width, int height);

void bringToForeground();

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

0 comments on commit 379211b

Please sign in to comment.