Skip to content

Commit 7208d8f

Browse files
authored
[desktop-webview-window] Show and Hide Webview window (#268)
* Show Webview Window Commit * include new runner generated by dart automatically * change function name to setWebviewWindowVisibility * change also messageCallName
1 parent 3214026 commit 7208d8f

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

packages/desktop_webview_window/example/windows/runner/Runner.rc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ IDI_APP_ICON ICON "resources\\app_icon.ico"
6060
// Version
6161
//
6262

63-
#ifdef FLUTTER_BUILD_NUMBER
64-
#define VERSION_AS_NUMBER FLUTTER_BUILD_NUMBER
63+
#if defined(FLUTTER_VERSION_MAJOR) && defined(FLUTTER_VERSION_MINOR) && defined(FLUTTER_VERSION_PATCH) && defined(FLUTTER_VERSION_BUILD)
64+
#define VERSION_AS_NUMBER FLUTTER_VERSION_MAJOR,FLUTTER_VERSION_MINOR,FLUTTER_VERSION_PATCH,FLUTTER_VERSION_BUILD
6565
#else
66-
#define VERSION_AS_NUMBER 1,0,0
66+
#define VERSION_AS_NUMBER 1,0,0,0
6767
#endif
6868

69-
#ifdef FLUTTER_BUILD_NAME
70-
#define VERSION_AS_STRING #FLUTTER_BUILD_NAME
69+
#if defined(FLUTTER_VERSION)
70+
#define VERSION_AS_STRING FLUTTER_VERSION
7171
#else
7272
#define VERSION_AS_STRING "1.0.0"
7373
#endif

packages/desktop_webview_window/lib/src/webview.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ abstract class Webview {
5353
/// Navigate to the next page in the history.
5454
Future<void> forward();
5555

56+
/// Show or hide webview window
57+
Future<void> setWebviewWindowVisibility(bool visible);
58+
5659
/// Reload the current page.
5760
Future<void> reload();
5861

packages/desktop_webview_window/lib/src/webview_impl.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ class WebviewImpl extends Webview {
170170
return channel.invokeMethod("forward", {"viewId": viewId});
171171
}
172172

173+
@override
174+
Future<void> setWebviewWindowVisibility(bool visible) {
175+
return channel.invokeMethod("setWebviewWindowVisibility", {
176+
"viewId": viewId,
177+
"visible": visible,
178+
});
179+
}
180+
173181
@override
174182
Future<void> back() {
175183
return channel.invokeMethod("back", {"viewId": viewId});

packages/desktop_webview_window/windows/web_view_window_plugin.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ void WebviewWindowPlugin::HandleMethodCall(
162162
}
163163
windows_[window_id]->GetWebView()->GoForward();
164164
result->Success();
165+
} else if (method_call.method_name() == "setWebviewWindowVisibility") {
166+
auto* arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
167+
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();
168+
auto visible = std::get<bool>(arguments->at(flutter::EncodableValue("visible")));
169+
if (!windows_.count(window_id)) {
170+
result->Error("0", "can not find webview window for id");
171+
return;
172+
}
173+
if (!windows_[window_id]->GetWebView()) {
174+
result->Error("0", "webview window not ready");
175+
return;
176+
}
177+
windows_[window_id]->setVisibility(visible);
178+
result->Success();
165179
} else if (method_call.method_name() == "reload") {
166180
auto *arguments = std::get_if<flutter::EncodableMap>(method_call.arguments());
167181
auto window_id = arguments->at(flutter::EncodableValue("viewId")).LongValue();

packages/desktop_webview_window/windows/webview_window.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ void WebviewWindow::CreateAndShow(const std::wstring &title, int height, int wid
136136
void WebviewWindow::SetBrightness(int brightness) {
137137
}
138138

139+
void WebviewWindow::setVisibility(bool visible)
140+
{
141+
if(visible)
142+
::ShowWindow(hwnd_.get(), SW_SHOW);
143+
else
144+
::ShowWindow(hwnd_.get(), SW_HIDE);
145+
}
146+
139147
// static
140148
LRESULT CALLBACK
141149
WebviewWindow::WndProc(

packages/desktop_webview_window/windows/webview_window.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class WebviewWindow {
4343
int windowPosX, int windowPosY, bool useWindowPositionAndSize,
4444
bool openMaximized, CreateCallback callback);
4545

46+
4647
// OS callback called by message pump. Handles the WM_NCCREATE message which
4748
// is passed when the non-client area is being created and enables automatic
4849
// non-client DPI scaling so that the non-client area automatically
@@ -55,6 +56,8 @@ class WebviewWindow {
5556

5657
void SetBrightness(int brightness);
5758

59+
void setVisibility(bool visible);
60+
5861
[[nodiscard]] const std::unique_ptr<webview_window::WebView> &GetWebView() const {
5962
return web_view_;
6063
}

0 commit comments

Comments
 (0)