Revert "Simplify Windows native event filter."

With that commit the window frame on Windows 7 is still bugged :(
This commit is contained in:
John Preston 2022-06-24 20:33:02 +04:00
parent b90d7ee27a
commit f40dc661b3
6 changed files with 63 additions and 60 deletions

View file

@ -70,13 +70,6 @@ rpl::producer<HitTestResult> BasicWindowHelper::systemButtonDown() const {
return rpl::never<HitTestResult>(); return rpl::never<HitTestResult>();
} }
bool BasicWindowHelper::nativeEvent(
const QByteArray &eventType,
void *message,
base::NativeEventResult *result) {
return false;
}
void BasicWindowHelper::setTitle(const QString &title) { void BasicWindowHelper::setTitle(const QString &title) {
_window->setWindowTitle(title); _window->setWindowTitle(title);
} }

View file

@ -8,7 +8,6 @@
#include "base/flags.h" #include "base/flags.h"
#include "base/object_ptr.h" #include "base/object_ptr.h"
#include "base/qt/qt_common_adapters.h"
#include "ui/round_rect.h" #include "ui/round_rect.h"
namespace style { namespace style {
@ -49,10 +48,6 @@ public:
-> rpl::producer<HitTestResult>; -> rpl::producer<HitTestResult>;
[[nodiscard]] virtual auto systemButtonDown() const [[nodiscard]] virtual auto systemButtonDown() const
-> rpl::producer<HitTestResult>; -> rpl::producer<HitTestResult>;
[[nodiscard]] virtual bool nativeEvent(
const QByteArray &eventType,
void *message,
base::NativeEventResult *result);
virtual void setTitle(const QString &title); virtual void setTitle(const QString &title);
virtual void setTitleStyle(const style::WindowTitle &st); virtual void setTitleStyle(const style::WindowTitle &st);
virtual void setNativeFrame(bool enabled); virtual void setNativeFrame(bool enabled);

View file

@ -17,6 +17,7 @@
#include "styles/palette.h" #include "styles/palette.h"
#include "styles/style_widgets.h" #include "styles/style_widgets.h"
#include <QtCore/QAbstractNativeEventFilter>
#include <QtGui/QWindow> #include <QtGui/QWindow>
#include <QtWidgets/QStyleFactory> #include <QtWidgets/QStyleFactory>
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
@ -128,6 +129,50 @@ void FixAeroSnap(HWND handle) {
} // namespace } // namespace
class WindowHelper::NativeFilter final : public QAbstractNativeEventFilter {
public:
void registerWindow(HWND handle, not_null<WindowHelper*> helper);
void unregisterWindow(HWND handle);
bool nativeEventFilter(
const QByteArray &eventType,
void *message,
long *result) override;
private:
base::flat_map<HWND, not_null<WindowHelper*>> _windowByHandle;
};
void WindowHelper::NativeFilter::registerWindow(
HWND handle,
not_null<WindowHelper*> helper) {
_windowByHandle.emplace(handle, helper);
}
void WindowHelper::NativeFilter::unregisterWindow(HWND handle) {
_windowByHandle.remove(handle);
}
bool WindowHelper::NativeFilter::nativeEventFilter(
const QByteArray &eventType,
void *message,
long *result) {
auto filtered = false;
const auto msg = static_cast<MSG*>(message);
const auto i = _windowByHandle.find(msg->hwnd);
if (i != end(_windowByHandle)) {
base::Integration::Instance().enterFromEventLoop([&] {
filtered = i->second->handleNativeEvent(
msg->message,
msg->wParam,
msg->lParam,
reinterpret_cast<LRESULT*>(result));
});
}
return filtered;
}
WindowHelper::WindowHelper(not_null<RpWidget*> window) WindowHelper::WindowHelper(not_null<RpWidget*> window)
: BasicWindowHelper(window) : BasicWindowHelper(window)
, _handle(ResolveWindowHandle(window)) , _handle(ResolveWindowHandle(window))
@ -141,6 +186,7 @@ WindowHelper::WindowHelper(not_null<RpWidget*> window)
} }
WindowHelper::~WindowHelper() { WindowHelper::~WindowHelper() {
GetNativeFilter()->unregisterWindow(_handle);
} }
void WindowHelper::initInWindow(not_null<RpWindow*> window) { void WindowHelper::initInWindow(not_null<RpWindow*> window) {
@ -272,6 +318,7 @@ rpl::producer<HitTestResult> WindowHelper::systemButtonDown() const {
void WindowHelper::init() { void WindowHelper::init() {
_title->show(); _title->show();
GetNativeFilter()->registerWindow(_handle, this);
style::PaletteChanged( style::PaletteChanged(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
@ -321,26 +368,6 @@ void WindowHelper::init() {
initialShadowUpdate(); initialShadowUpdate();
} }
bool WindowHelper::nativeEvent(
const QByteArray &eventType,
void *message,
base::NativeEventResult *result) {
const auto msg = static_cast<MSG*>(message);
auto lresult = LRESULT(*result);
const auto guard = gsl::finally([&] {
*result = base::NativeEventResult(lresult);
});
auto filtered = false;
base::Integration::Instance().enterFromEventLoop([&] {
filtered = handleNativeEvent(
msg->message,
msg->wParam,
msg->lParam,
&lresult);
});
return filtered;
}
bool WindowHelper::handleNativeEvent( bool WindowHelper::handleNativeEvent(
UINT msg, UINT msg,
WPARAM wParam, WPARAM wParam,
@ -762,6 +789,18 @@ void WindowHelper::fixMaximizedWindow() {
} }
} }
not_null<WindowHelper::NativeFilter*> WindowHelper::GetNativeFilter() {
Expects(QCoreApplication::instance() != nullptr);
static const auto GlobalFilter = [&] {
const auto application = QCoreApplication::instance();
const auto filter = Ui::CreateChild<NativeFilter>(application);
application->installNativeEventFilter(filter);
return filter;
}();
return GlobalFilter;
}
HWND GetWindowHandle(not_null<QWidget*> widget) { HWND GetWindowHandle(not_null<QWidget*> widget) {
const auto toplevel = widget->window(); const auto toplevel = widget->window();
toplevel->createWinId(); toplevel->createWinId();

View file

@ -44,12 +44,10 @@ public:
[[nodiscard]] auto systemButtonDown() const [[nodiscard]] auto systemButtonDown() const
-> rpl::producer<HitTestResult> override; -> rpl::producer<HitTestResult> override;
[[nodiscard]] bool nativeEvent(
const QByteArray &eventType,
void *message,
base::NativeEventResult *result) override;
private: private:
class NativeFilter;
friend class NativeFilter;
void init(); void init();
void updateMargins(); void updateMargins();
void updateWindowFrameColors(); void updateWindowFrameColors();
@ -72,6 +70,7 @@ private:
[[nodiscard]] HitTestResult systemButtonHitTest(int result) const; [[nodiscard]] HitTestResult systemButtonHitTest(int result) const;
[[nodiscard]] int titleHeight() const; [[nodiscard]] int titleHeight() const;
static not_null<NativeFilter*> GetNativeFilter();
const HWND _handle = nullptr; const HWND _handle = nullptr;
const not_null<TitleWidget*> _title; const not_null<TitleWidget*> _title;

View file

@ -17,8 +17,6 @@ RpWindow::RpWindow(QWidget *parent)
_helper->initInWindow(this); _helper->initInWindow(this);
hide(); hide();
_initialized = true;
} }
RpWindow::~RpWindow() = default; RpWindow::~RpWindow() = default;
@ -101,17 +99,4 @@ void RpWindow::setBodyTitleArea(
_helper->setBodyTitleArea(std::move(testMethod)); _helper->setBodyTitleArea(std::move(testMethod));
} }
bool RpWindow::nativeEvent(
const QByteArray &eventType,
void *message,
base::NativeEventResult *result) {
if (_initialized && _helper->nativeEvent(
eventType,
message,
result)) {
return true;
}
return RpWidget::nativeEvent(eventType, message, result);
}
} // namespace Ui } // namespace Ui

View file

@ -8,7 +8,6 @@
#include "ui/rp_widget.h" #include "ui/rp_widget.h"
#include "base/flags.h" #include "base/flags.h"
#include "base/qt/qt_common_adapters.h"
namespace style { namespace style {
struct WindowTitle; struct WindowTitle;
@ -68,14 +67,7 @@ public:
void close(); void close();
void setBodyTitleArea(Fn<WindowTitleHitTestFlags(QPoint)> testMethod); void setBodyTitleArea(Fn<WindowTitleHitTestFlags(QPoint)> testMethod);
protected:
bool nativeEvent(
const QByteArray &eventType,
void *message,
base::NativeEventResult *result) override;
private: private:
bool _initialized = false;
const std::unique_ptr<Platform::BasicWindowHelper> _helper; const std::unique_ptr<Platform::BasicWindowHelper> _helper;
}; };