Skip to content

Commit ef7924c

Browse files
authored
[desktop_webview_window] More control over webview position and size under windows (#206)
* OpenDevTools * Control Webview Size and Position and Maximization * clear non plugin stuff * clear non plugin stuff 2
1 parent 1ac0384 commit ef7924c

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

packages/desktop_webview_window/lib/src/create_configuration.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class CreateConfiguration {
44
final int windowWidth;
55
final int windowHeight;
66

7+
/// Position of the top left point of the webview window
8+
final int windowPosX;
9+
final int windowPosY;
10+
711
/// the title of window
812
final String title;
913

@@ -13,13 +17,20 @@ class CreateConfiguration {
1317

1418
final String userDataFolderWindows;
1519

20+
final bool useWindowPositionAndSize;
21+
final bool openMaximized;
22+
1623
const CreateConfiguration({
1724
this.windowWidth = 1280,
1825
this.windowHeight = 720,
26+
this.windowPosX = 0,
27+
this.windowPosY = 0,
1928
this.title = "",
2029
this.titleBarHeight = 40,
2130
this.titleBarTopPadding = 0,
2231
this.userDataFolderWindows = 'webview_window_WebView2',
32+
this.useWindowPositionAndSize = false,
33+
this.openMaximized = false,
2334
});
2435

2536
factory CreateConfiguration.platform() {
@@ -31,9 +42,13 @@ class CreateConfiguration {
3142
Map toMap() => {
3243
"windowWidth": windowWidth,
3344
"windowHeight": windowHeight,
45+
"windowPosX": windowPosX,
46+
"windowPosY": windowPosY,
3447
"title": title,
3548
"titleBarHeight": titleBarHeight,
3649
"titleBarTopPadding": titleBarTopPadding,
3750
"userDataFolderWindows": userDataFolderWindows,
51+
"useWindowPositionAndSize": useWindowPositionAndSize,
52+
"openMaximized": openMaximized,
3853
};
3954
}

packages/desktop_webview_window/windows/web_view.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <windows.h>
66
#include <tchar.h>
77
#include <cassert>
8-
#include <iostream>
98
#include <utility>
109
#include <thread>
1110

packages/desktop_webview_window/windows/web_view_window_plugin.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ void WebviewWindowPlugin::HandleMethodCall(
5656
auto title = std::get<std::string>(arguments->at(flutter::EncodableValue("title")));
5757
auto titleBarHeight = arguments->at(flutter::EncodableValue("titleBarHeight")).LongValue();
5858
auto userDataFolder = std::get<std::string>(arguments->at(flutter::EncodableValue("userDataFolderWindows")));
59+
auto useWindowPositionAndSize = std::get<bool>(arguments->at(flutter::EncodableValue("useWindowPositionAndSize")));
60+
auto openMaximized = std::get<bool>(arguments->at(flutter::EncodableValue("openMaximized")));
61+
auto windowPosX = arguments->at(flutter::EncodableValue("windowPosX")).LongValue();
62+
auto windowPosY = arguments->at(flutter::EncodableValue("windowPosY")).LongValue();
5963

6064
auto window_id = next_window_id_;
6165
auto window = std::make_unique<WebviewWindow>(
@@ -66,6 +70,7 @@ void WebviewWindowPlugin::HandleMethodCall(
6670
std::shared_ptr<flutter::MethodResult<flutter::EncodableValue>> result2(std::move(result));
6771
window->CreateAndShow(
6872
utf8_to_wide(title), int(height), int(width), utf8_to_wide(userDataFolder),
73+
int(windowPosX), int(windowPosY), useWindowPositionAndSize, openMaximized,
6974
[this, window_id, result(result2)](bool succeed) mutable {
7075
if (!succeed) {
7176
result->Error("0", "failed to show window");

packages/desktop_webview_window/windows/webview_window.cc

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ WebviewWindow::~WebviewWindow() {
5252

5353
void WebviewWindow::CreateAndShow(const std::wstring &title, int height, int width,
5454
const std::wstring &userDataFolder,
55-
CreateCallback callback) {
55+
int windowPosX, int windowPosY, bool useWindowPositionAndSize,
56+
bool openMaximized, CreateCallback callback) {
5657

5758
RegisterWindowClass(kWebViewWindowClassName, WebviewWindow::WndProc);
5859

@@ -64,12 +65,25 @@ void WebviewWindow::CreateAndShow(const std::wstring &title, int height, int wid
6465
UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
6566
double scale_factor = dpi / 96.0;
6667

67-
hwnd_ = wil::unique_hwnd(::CreateWindow(
68+
DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
69+
if (openMaximized)
70+
dwStyle |= WS_MAXIMIZE;
71+
72+
if (useWindowPositionAndSize) {
73+
hwnd_ = wil::unique_hwnd(::CreateWindow(
74+
kWebViewWindowClassName, title.c_str(),
75+
dwStyle,
76+
windowPosX, windowPosY,
77+
width, height,
78+
nullptr, nullptr, GetModuleHandle(nullptr), this));
79+
} else {
80+
hwnd_ = wil::unique_hwnd(::CreateWindow(
6881
kWebViewWindowClassName, title.c_str(),
69-
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
82+
dwStyle,
7083
CW_USEDEFAULT, CW_USEDEFAULT,
7184
Scale(width, scale_factor), Scale(height, scale_factor),
7285
nullptr, nullptr, GetModuleHandle(nullptr), this));
86+
}
7387
if (!hwnd_) {
7488
callback(false);
7589
return;
@@ -78,8 +92,10 @@ void WebviewWindow::CreateAndShow(const std::wstring &title, int height, int wid
7892
// Centered window on screen.
7993
RECT rc;
8094
GetClientRect(hwnd_.get(), &rc);
81-
ClipOrCenterRectToMonitor(&rc, MONITOR_CENTER);
82-
SetWindowPos(hwnd_.get(), nullptr, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
95+
if (!useWindowPositionAndSize && !openMaximized) {
96+
ClipOrCenterRectToMonitor(&rc, MONITOR_CENTER);
97+
SetWindowPos(hwnd_.get(), nullptr, rc.left, rc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
98+
}
8399

84100
auto title_bar_height = Scale(title_bar_height_, scale_factor);
85101

packages/desktop_webview_window/windows/webview_window.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class WebviewWindow {
4040

4141
void CreateAndShow(const std::wstring &title, int height, int width,
4242
const std::wstring &userDataFolder,
43-
CreateCallback callback);
43+
int windowPosX, int windowPosY, bool useWindowPositionAndSize,
44+
bool openMaximized, CreateCallback callback);
4445

4546
// OS callback called by message pump. Handles the WM_NCCREATE message which
4647
// is passed when the non-client area is being created and enables automatic

0 commit comments

Comments
 (0)