Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flutter_inappwebview_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
- Updated Microsoft.Windows.ImplementationLibrary to `1.0.260126.7` and
Microsoft.Web.WebView2 to `1.0.4078.44` for current MSVC toolchains.
- Fixed internal WebView host windows stealing focus from the Flutter window.
- Fixed owned popups (`<select>` dropdowns, print preview pickers, context
menus) opening away from the WebView after the host windows became
`WS_CHILD`: positioning now uses parent-client coordinates.
- Added profile-scoped WebView2 preferred color-scheme control.
- Updated flutter_inappwebview_platform_interface version to ^1.4.0-beta.3
- Updated Microsoft.Web.WebView2 SDK version from `1.0.2849.39` to `1.0.3650.58`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';

/// The WebView host HWNDs are WS_CHILD of the Flutter window (see
/// native_host_window_style_test.dart), so SetWindowPos takes parent-client
/// coordinates. Positioning them with screen coordinates leaves every owned
/// popup - <select> dropdowns, the print preview pickers, context menus -
/// offset by the window origin.
void main() {
final source =
File('windows/in_app_webview/in_app_webview.cpp')
.readAsStringSync()
.replaceAll('\r\n', '\n');
final setPosition = source.substring(
source.indexOf('void InAppWebView::setPosition('),
source.indexOf('void InAppWebView::setCursorPos('),
);

test('setPosition uses parent-client coordinates', () {
expect(setPosition, contains('SetWindowPos'));
expect(setPosition, contains('scaled_x,'));
expect(setPosition, contains('scaled_y,'));
});

test('setPosition does not reintroduce screen-coordinate math', () {
for (final banned in const [
'GetWindowRect',
'GetSystemMetrics',
'SM_CYCAPTION',
'SM_CXPADDEDBORDER',
'ClientToScreen',
]) {
expect(
setPosition,
isNot(contains(banned)),
reason: '$banned belongs to screen-space positioning, which is wrong '
'for a WS_CHILD host window',
);
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3900,32 +3900,25 @@ namespace flutter_inappwebview_plugin

void InAppWebView::setPosition(size_t x, size_t y, float scale_factor)
{
if (!webViewController || !plugin || !plugin->registrar) {
if (!webViewController) {
return;
}

if (x >= 0 && y >= 0) {
scaleFactor_ = scale_factor;
auto scaled_x = static_cast<int>(x * scale_factor);
auto scaled_y = static_cast<int>(y * scale_factor);
widgetOffset_ = { scaled_x, scaled_y };

auto titleBarHeight = ((GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)) * scale_factor) + GetSystemMetrics(SM_CXPADDEDBORDER);
auto borderWidth = (GetSystemMetrics(SM_CXBORDER) + GetSystemMetrics(SM_CXPADDEDBORDER)) * scale_factor;

RECT flutterWindowRect;
HWND flutterWindowHWnd = plugin->registrar->GetView()->GetNativeWindow();
GetWindowRect(flutterWindowHWnd, &flutterWindowRect);

HWND webViewHWnd;
if (succeededOrLog(webViewController->get_ParentWindow(&webViewHWnd))) {
::SetWindowPos(webViewHWnd,
nullptr,
static_cast<int>(flutterWindowRect.left + scaled_x - borderWidth),
static_cast<int>(flutterWindowRect.top + scaled_y - titleBarHeight),
0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
scaleFactor_ = scale_factor;
auto scaled_x = static_cast<int>(x * scale_factor);
auto scaled_y = static_cast<int>(y * scale_factor);
widgetOffset_ = { scaled_x, scaled_y };

HWND webViewHWnd;
if (succeededOrLog(webViewController->get_ParentWindow(&webViewHWnd))) {
// The host window is a WS_CHILD of the Flutter window, so SetWindowPos
// takes parent-client coordinates - exactly the widget offset.
::SetWindowPos(webViewHWnd,
nullptr,
scaled_x,
scaled_y,
0, 0,
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
}

Expand Down
Loading