Skip to content

Commit

Permalink
[desktop-webview-window] Get positional parameters for webview window (
Browse files Browse the repository at this point in the history
…#291)

* bringToForeground Maximized

* add get positional parameters
  • Loading branch information
Iri-Hor committed Dec 6, 2023
1 parent af28658 commit 5becd52
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/desktop_webview_window/lib/src/webview.dart
Expand Up @@ -62,6 +62,9 @@ abstract class Webview {
/// Activates the webview window (giving it the focus)
Future<void> bringToForeground({bool maximized = false});

/// get position, extents and maximization info of the webview window
Future<Map<dynamic,dynamic>?> getPositionalParameters();

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

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

@override
Future<Map<dynamic,dynamic>?> getPositionalParameters() async {
return await channel.invokeMethod("getPositionalParameters", {
"viewId": viewId,
});
}

@override
Future<void> back() {
return channel.invokeMethod("back", {"viewId": viewId});
Expand Down
12 changes: 12 additions & 0 deletions packages/desktop_webview_window/windows/web_view_window_plugin.cc
Expand Up @@ -209,6 +209,18 @@ void WebviewWindowPlugin::HandleMethodCall(
}
windows_[window_id]->bringToForeground(maximized);
result->Success();
} else if (method_call.method_name() == "getPositionalParameters") {
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]->getPositionalParameters(std::move(result));
} 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
15 changes: 15 additions & 0 deletions packages/desktop_webview_window/windows/webview_window.cc
Expand Up @@ -155,6 +155,21 @@ void WebviewWindow::bringToForeground(bool maximized) {
}
}

void WebviewWindow::getPositionalParameters(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> completer) {
RECT rc;
GetWindowRect(hwnd_.get(), &rc);

std::unique_ptr<WINDOWPLACEMENT> wp(new WINDOWPLACEMENT);
GetWindowPlacement(hwnd_.get(), wp.get());
std::map<flutter::EncodableValue, flutter::EncodableValue> m{
{"left", rc.left},
{"top", rc.top},
{"width", rc.right-rc.left},
{"height", rc.bottom-rc.top},
{"maximized", wp->showCmd==SW_MAXIMIZE}};
completer->Success(flutter::EncodableValue(m));
}

// static
LRESULT CALLBACK
WebviewWindow::WndProc(
Expand Down
2 changes: 2 additions & 0 deletions packages/desktop_webview_window/windows/webview_window.h
Expand Up @@ -62,6 +62,8 @@ class WebviewWindow {

void bringToForeground(bool maximized);

void getPositionalParameters(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> completer);

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

0 comments on commit 5becd52

Please sign in to comment.