fixes for window resizing on Windows

This commit is contained in:
Nick Fisher
2023-10-26 02:10:22 +11:00
parent 0928d9d273
commit 2fd6f44785
17 changed files with 302 additions and 207 deletions

View File

@@ -132,35 +132,11 @@ void SetWindowComposition(HWND window, int32_t accent_state,
}
LRESULT NativeViewSubclassProc(HWND window, UINT message, WPARAM wparam,
LPARAM lparam, UINT_PTR subclass_id,
DWORD_PTR ref_data) noexcept {
switch (message) {
case WM_ERASEBKGND: {
// Prevent erasing of |window| when it is unfocused and minimized or
// moved out of screen etc.
return 1;
break;
}
case WM_SIZE: {
// Prevent unnecessary maxmize, minimize or restore messages for |window|.
return 1;
break;
}
default:
break;
}
return ::DefSubclassProc(window, message, wparam, lparam);
}
LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// std::cout << "FILAMENT WINDOW EVENT " << message << std::endl;
switch (message) {
case WM_MOUSEMOVE: {
std::cout << "FILAMENT MOUSE MOVE" << std::endl;
TRACKMOUSEEVENT event;
event.cbSize = sizeof(event);
event.hwndTrack = window;
@@ -168,10 +144,8 @@ LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message,
event.dwHoverTime = 200;
auto user_data = ::GetWindowLongPtr(window, GWLP_USERDATA);
if (user_data) {
std::cout << "setting foreground in filamentwindwoproc" << std::endl;
HWND flutterRootWindow = reinterpret_cast<HWND>(user_data);
::SetForegroundWindow(flutterRootWindow);
// NativeViewCore::GetInstance()->SetHitTestBehavior(0);
LONG ex_style = ::GetWindowLong(flutterRootWindow, GWL_EXSTYLE);
ex_style &= ~(WS_EX_TRANSPARENT | WS_EX_LAYERED);
::SetWindowLong(flutterRootWindow, GWL_EXSTYLE, ex_style);
@@ -179,7 +153,6 @@ LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message,
break;
}
case WM_ERASEBKGND: {
std::cout << "FILAMENT ERASE BKGND" << std::endl;
// Prevent erasing of |window| when it is unfocused and minimized or
// moved out of screen etc.
break;
@@ -189,11 +162,9 @@ LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message,
case WM_MOVING:
case WM_ACTIVATE:
case WM_WINDOWPOSCHANGED: {
// std::cout << "FILAMENT POS CHANGED" << std::endl;
// NativeViewCore::GetInstance()->SetHitTestBehavior(0);
auto user_data = ::GetWindowLongPtr(window, GWLP_USERDATA);
if (user_data) {
std::cout << "setting foreground in filamentwindwoproc" << std::endl;
HWND flutterRootWindow = reinterpret_cast<HWND>(user_data);
::SetForegroundWindow(flutterRootWindow);
// NativeViewCore::GetInstance()->SetHitTestBehavior(0);
@@ -210,7 +181,10 @@ LRESULT CALLBACK FilamentWindowProc(HWND const window, UINT const message,
}
BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
int initialWidth, int initialHeight) {
int width,
int height,
int left,
int top) : _width(width), _height(height), _left(left), _top(top) {
// a Flutter application actually has two windows - the innner window contains the FlutterView.
// although we will use the outer window for various events, we always position things relative to the inner window.
_flutterViewWindow = pluginRegistrar->GetView()->GetNativeWindow();
@@ -218,9 +192,6 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
RECT flutterChildRect;
::GetWindowRect(_flutterViewWindow, &flutterChildRect);
// ::GetClientRect(flutterWindow, &flutterChildRect);
std::cout << "child rect " << flutterChildRect.left << " " << flutterChildRect.top << " " << flutterChildRect.right << " " << flutterChildRect.bottom << std::endl;
// set composition to allow transparency
SetWindowComposition(_flutterRootWindow, 6, 0);
@@ -230,26 +201,21 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
UINT message,
WPARAM wparam,
LPARAM lparam) {
// std::cout << "TOP LEVEL EVENT " << message << std::endl;
switch (message) {
case WM_MOUSEMOVE: {
// std::cout << "FLUTTER MOUSE MOVE" << std::endl;
break;
}
case WM_ACTIVATE: {
std::cout << "WM_ACTIVATE" << std::endl;
RECT rootWindowRect;
::GetWindowRect(_flutterViewWindow, &rootWindowRect);
// Position |native_view| such that it's z order is behind |window_| &
// redraw aswell.
::SetWindowPos(_windowHandle, _flutterRootWindow, rootWindowRect.left,
rootWindowRect.top, rootWindowRect.right - rootWindowRect.left,
rootWindowRect.bottom - rootWindowRect.top, SWP_NOACTIVATE);
::SetWindowPos(_windowHandle, _flutterRootWindow, rootWindowRect.left + _left,
rootWindowRect.top + _top, _width,
_height, SWP_NOACTIVATE);
break;
}
case WM_SIZE: {
std::cout << "WM_SIZE" << std::endl;
// Handle Windows's minimize & maximize animations properly.
// Since |SetWindowPos| & other Win32 APIs on |native_view_container_|
// do not re-produce the same DWM animations like actual user
@@ -289,6 +255,12 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
if (wparam != SIZE_MINIMIZED) {
::ShowWindow(_windowHandle, SW_SHOWNOACTIVATE);
}
RECT flutterViewRect;
::GetWindowRect(_flutterViewWindow, &flutterViewRect);
::SetWindowPos(_windowHandle, _flutterRootWindow, flutterViewRect.left + _left,
flutterViewRect.top + _top, _width, _height,
SWP_NOACTIVATE);
},
last_thread_time_)
.detach();
@@ -302,12 +274,11 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
case WM_WINDOWPOSCHANGED: {
RECT rootWindowRect;
::GetWindowRect(_flutterViewWindow, &rootWindowRect);
// std::cout << "FLUTTER WINDOWPOSCHANGED TO " << rootWindowRect.left << " " << rootWindowRect.top << " " << rootWindowRect.right << " " << rootWindowRect.bottom << std::endl;
if (rootWindowRect.right - rootWindowRect.left > 0 &&
rootWindowRect.bottom - rootWindowRect.top > 0) {
::SetWindowPos(_windowHandle, _flutterRootWindow, rootWindowRect.left,
rootWindowRect.top, rootWindowRect.right - rootWindowRect.left,
rootWindowRect.bottom - rootWindowRect.top, SWP_NOACTIVATE);
::SetWindowPos(_windowHandle, _flutterRootWindow, rootWindowRect.left + _left,
rootWindowRect.top + _top, _width,
_height, SWP_NOACTIVATE);
// |window_| is minimized.
if (rootWindowRect.left < 0 && rootWindowRect.top < 0 &&
rootWindowRect.right < 0 && rootWindowRect.bottom < 0) {
@@ -343,7 +314,7 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
window_class.hbrBackground = ::CreateSolidBrush(0);
::RegisterClassExW(&window_class);
_windowHandle = ::CreateWindow(kClassName, kWindowName, WS_OVERLAPPEDWINDOW,
0, 0, initialWidth, initialHeight, nullptr,
0, 0, _width, _height, nullptr,
nullptr, GetModuleHandle(nullptr), nullptr);
// Disable DWM animations
@@ -352,22 +323,19 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
&disable_window_transitions,
sizeof(disable_window_transitions));
::SetWindowSubclass(_windowHandle, NativeViewSubclassProc, 69420,
NULL); // what does this do?
auto style = ::GetWindowLongPtr(_windowHandle, GWL_STYLE);
auto style = ::GetWindowLong(_windowHandle, GWL_STYLE);
style &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
WS_EX_APPWINDOW);
::SetWindowLongPtr(_windowHandle, GWL_STYLE, style);
RECT flutterViewRect;
::GetClientRect(_flutterViewWindow, &flutterViewRect);
::SetWindowLong(_windowHandle, GWL_STYLE, style);
::SetWindowLongPtr(_windowHandle, GWLP_USERDATA,
reinterpret_cast<LONG>(_flutterRootWindow));
::SetWindowPos(_windowHandle, _flutterRootWindow, flutterViewRect.left,
flutterViewRect.top, flutterViewRect.right - flutterViewRect.left, flutterViewRect.bottom - flutterViewRect.top,
RECT flutterViewRect;
::GetWindowRect(_flutterViewWindow, &flutterViewRect);
::SetWindowPos(_windowHandle, _flutterRootWindow, flutterViewRect.left + _left,
flutterViewRect.top + _top, _width, _height,
SWP_NOACTIVATE);
// remove taskbar entry for the window we created
@@ -378,8 +346,28 @@ BackingWindow::BackingWindow(flutter::PluginRegistrarWindows *pluginRegistrar,
taskbar->Release();
::ShowWindow(_windowHandle, SW_SHOW);
::ShowWindow(_flutterRootWindow, SW_SHOW);
::SetFocus(_flutterRootWindow);
::ShowWindow(_flutterViewWindow, SW_SHOW);
::SetForegroundWindow(_flutterViewWindow);
::SetFocus(_flutterViewWindow);
LONG ex_style = ::GetWindowLong(_flutterRootWindow, GWL_EXSTYLE);
ex_style &= ~(WS_EX_TRANSPARENT | WS_EX_LAYERED);
::SetWindowLong(_flutterRootWindow, GWL_EXSTYLE, ex_style);
}
void BackingWindow::Resize(int width, int height, int left, int top) {
_width = width;
_height = height;
_left = left;
_top = top;
RECT flutterViewRect;
::ShowWindow(_windowHandle, SW_HIDE);
::GetWindowRect(_flutterViewWindow, &flutterViewRect);
std::cout << "Resizing to " << _width << " x " << _height << " with LT" << _left << " " << _top << " flutter view rect" << flutterViewRect.left << " " << flutterViewRect.top << " " << flutterViewRect.right << " " << flutterViewRect.bottom << std::endl;
::SetWindowPos(_windowHandle, _flutterRootWindow, flutterViewRect.left + _left,
flutterViewRect.top + _top, _width, _height,
SWP_NOACTIVATE);
::ShowWindow(_windowHandle, SW_SHOWNOACTIVATE);
}
HWND BackingWindow::GetHandle() { return _windowHandle; }

View File

@@ -11,13 +11,20 @@ class BackingWindow {
public:
BackingWindow(
flutter::PluginRegistrarWindows *pluginRegistrar,
int initialWidth,
int initialHeight);
int width,
int height,
int left,
int top);
HWND GetHandle();
void Resize(int width, int height, int left, int top);
private:
HWND _windowHandle;
HWND _flutterRootWindow;
HWND _flutterViewWindow;
uint32_t _width = 0;
uint32_t _height = 0;
uint32_t _left = 0;
uint32_t _top = 0;
};
}

View File

@@ -137,11 +137,19 @@ EGLContext::EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter
}
}
EGLContext::CreateTexture(
EGLContext::CreateRenderingSurface(
uint32_t width, uint32_t height,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result,
uint32_t left, uint32_t top
) {
importGLESExtensionsEntryPoints();
if(left != 0 || top != 0) {
result->Error("ERROR",
"Rendering with EGL uses a Texture render target/Flutter widget and does not need a window offset.");
return false;
}
if (_active.get()) {
result->Error("ERROR",
"Texture already exists. You must call destroyTexture before "

View File

@@ -9,9 +9,6 @@ namespace polyvox_filament {
class EGLContext : public FlutterRenderingContext {
public:
EGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar);
void CreateTexture(
uint32_t width, uint32_t height,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
private:
EGLContext _context = NULL;
EGLConfig _eglConfig = NULL;

View File

@@ -12,17 +12,14 @@ namespace polyvox_filament {
class FlutterRenderContext {
public:
void CreateRenderingSurface(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top );
void DestroyTexture(std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (!_active) {
result->Success("Texture has already been detroyed, ignoring");
return;
}
// if (_active->flutterTextureId != *flutterTextureId) {
// result->Error("TEXTURE_MISMATCH", "Specified texture ID is not active");
// return;
// }
auto sh = std::make_shared<
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>>>(
std::move(result));

View File

@@ -167,9 +167,18 @@ void PolyvoxFilamentPlugin::CreateTexture(
const auto *args =
std::get_if<flutter::EncodableList>(methodCall.arguments());
const auto width = (uint32_t)round(*(std::get_if<double>(&(args->at(0)))));
const auto height = (uint32_t)round(*(std::get_if<double>(&(args->at(1)))));
double dWidth = *(std::get_if<double>(&(args->at(0))));
double dHeight = *(std::get_if<double>(&(args->at(1))));
double pixelRatio = *(std::get_if<double>(&(args->at(2))));
double dLeft = *(std::get_if<double>(&(args->at(3))));
double dTop = *(std::get_if<double>(&(args->at(4))));
auto width = (uint32_t)round(dWidth * pixelRatio);
auto height = (uint32_t)round(dHeight * pixelRatio);
auto left = (uint32_t)round(dLeft * pixelRatio);
auto top = (uint32_t)round(dTop * pixelRatio);
std::cout << "Using " << width << "x" << height << " (pixel ratio " << pixelRatio << ")" << std::endl;
// create a single shared context for the life of the application
// this will be used to create a backing texture and passed to Filament
if (!_context) {
@@ -179,7 +188,7 @@ void PolyvoxFilamentPlugin::CreateTexture(
_context = std::make_unique<WGLContext>(_pluginRegistrar, _textureRegistrar);
#endif
}
_context->CreateTexture(width, height, std::move(result));
_context->CreateRenderingSurface(width, height, std::move(result), left, top);
}
void PolyvoxFilamentPlugin::DestroyTexture(
@@ -207,7 +216,7 @@ void PolyvoxFilamentPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &methodCall,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (methodCall.method_name() == "useBackingWindow") {
if (methodCall.method_name() == "usesBackingWindow") {
result->Success(flutter::EncodableValue(
#ifdef WGL_USE_BACKING_WINDOW
true
@@ -215,12 +224,28 @@ void PolyvoxFilamentPlugin::HandleMethodCall(
false
#endif
));
} else if (methodCall.method_name() == "getSharedContext") {
result->Success(flutter::EncodableValue((int64_t)_context->sharedContext));
} else if (methodCall.method_name() == "getResourceLoaderWrapper") {
const ResourceLoaderWrapper *const resourceLoader =
new ResourceLoaderWrapper(_loadResource, _freeResource, this);
result->Success(flutter::EncodableValue((int64_t)resourceLoader));
} else if (methodCall.method_name() == "resizeWindow") {
#if WGL_USE_BACKING_WINDOW
const auto *args =
std::get_if<flutter::EncodableList>(methodCall.arguments());
double dWidth = *(std::get_if<double>(&(args->at(0))));
double dHeight = *(std::get_if<double>(&(args->at(1))));
double pixelRatio = *(std::get_if<double>(&(args->at(2))));
double dLeft = *(std::get_if<double>(&(args->at(3))));
double dTop = *(std::get_if<double>(&(args->at(4))));
auto width = (uint32_t)round(dWidth * pixelRatio);
auto height = (uint32_t)round(dHeight * pixelRatio);
auto left = (uint32_t)round(dLeft * pixelRatio);
auto top = (uint32_t)round(dTop * pixelRatio);
_context->ResizeRenderingSurface(width, height, left, top);
result->Success();
#else
result->Error("ERROR", "resizeWindow is only available when using a backing window");
#endif
} else if (methodCall.method_name() == "createTexture") {
CreateTexture(methodCall, std::move(result));
} else if (methodCall.method_name() == "destroyTexture") {

View File

@@ -96,24 +96,37 @@ WGLContext::WGLContext(flutter::PluginRegistrarWindows *pluginRegistrar,
}
}
void WGLContext::CreateTexture(
void WGLContext::ResizeRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top) {
_backingWindow->Resize(width, height, left, top);
}
void WGLContext::CreateRenderingSurface(
uint32_t width, uint32_t height,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top) {
#if WGL_USE_BACKING_WINDOW
_backingWindow = std::make_unique<BackingWindow>(
_pluginRegistrar, static_cast<int>(width), static_cast<int>(height));
if(!_backingWindow) {
_backingWindow = std::make_unique<BackingWindow>(
_pluginRegistrar, static_cast<int>(width), static_cast<int>(height), static_cast<int>(left), static_cast<int>(top));
} else {
ResizeRenderingSurface(width, height, left, top);
}
std::vector<flutter::EncodableValue> resultList;
resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(
flutter::EncodableValue((int64_t)_backingWindow->GetHandle()));
resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(flutter::EncodableValue((int64_t)sharedContext));
result->Success(resultList);
#else
if (_active.get()) {
if(left != 0 || top != 0) {
result->Error("ERROR",
"When WGL_USE_BACKING_WINDOW is false, rendering with WGL uses a Texture render target/Flutter widget and does not need a window offset.");
} else if (_active.get()) {
result->Error("ERROR",
"Texture already exists. You must call destroyTexture before "
"attempting to create a new one.");
} else {
_active = std::make_unique<OpenGLTextureBuffer>(
_pluginRegistrar, _textureRegistrar, std::move(result), width, height,
@@ -124,9 +137,10 @@ void WGLContext::CreateTexture(
resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(flutter::EncodableValue((int64_t) nullptr));
resultList.push_back(flutter::EncodableValue((int64_t)sharedContext));
result->Success(resultList);
} else {
result->Error("FOO", "ERROR", nullptr);
result->Error("NO_FLUTTER_TEXTURE", "Unknown error registering texture with Flutter.", nullptr);
}
}
#endif

View File

@@ -12,9 +12,15 @@ namespace polyvox_filament {
class WGLContext : public FlutterRenderContext {
public:
WGLContext(flutter::PluginRegistrarWindows* pluginRegistrar, flutter::TextureRegistrar* textureRegistrar);
void CreateTexture(uint32_t width, uint32_t height, std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
void* GetSharedContext();
void CreateRenderingSurface(
uint32_t width, uint32_t height,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result, uint32_t left, uint32_t top);
void ResizeRenderingSurface(
uint32_t width, uint32_t height, uint32_t left, uint32_t top
);
private:
flutter::PluginRegistrarWindows* _pluginRegistrar = nullptr;
flutter::TextureRegistrar* _textureRegistrar = nullptr;
HGLRC _context = NULL;