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] Get positional parameters for webview window #291

Merged
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 @@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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