Make '[un]registerLeaveSubscription' work in all windows.

This commit is contained in:
John Preston 2021-05-27 09:34:33 +04:00
parent deecf80f20
commit 3709714339
5 changed files with 60 additions and 26 deletions

View File

@ -1948,11 +1948,26 @@ void Panel::setupControlsBackgroundWide() {
template <typename WidgetPointer>
void Panel::trackControl(WidgetPointer &widget, rpl::lifetime &lifetime) {
if (widget) {
widget->events(
const auto raw = &*widget;
raw->events(
) | rpl::start_with_next([=](not_null<QEvent*> e) {
using Type = std::remove_cvref_t<decltype(*raw)>;
constexpr auto mute = std::is_same_v<Type, Ui::CallMuteButton>;
if (e->type() == QEvent::Enter) {
auto &integration = Ui::Integration::Instance();
if constexpr (mute) {
integration.registerLeaveSubscription(raw->outer());
} else {
integration.registerLeaveSubscription(raw);
}
toggleWideControls(true);
} else if (e->type() == QEvent::Leave) {
auto &integration = Ui::Integration::Instance();
if constexpr (mute) {
integration.unregisterLeaveSubscription(raw->outer());
} else {
integration.unregisterLeaveSubscription(raw);
}
toggleWideControls(false);
}
}, lifetime);

View File

@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_session.h"
#include "data/data_user.h"
#include "base/timer.h"
#include "base/event_filter.h"
#include "base/concurrent_timer.h"
#include "base/qt_signal_producer.h"
#include "base/unixtime.h"
@ -997,30 +998,49 @@ QPoint Application::getPointForCallPanelCenter() const {
// macOS Qt bug workaround, sometimes no leaveEvent() gets to the nested widgets.
void Application::registerLeaveSubscription(not_null<QWidget*> widget) {
#ifdef Q_OS_MAC
if (const auto topLevel = widget->window()) {
if (topLevel == _window->widget()) {
auto weak = Ui::MakeWeak(widget);
auto subscription = _window->widget()->leaveEvents(
) | rpl::start_with_next([weak] {
if (const auto window = weak.data()) {
QEvent ev(QEvent::Leave);
QGuiApplication::sendEvent(window, &ev);
if (const auto window = widget->window()) {
auto i = _leaveFilters.find(window);
if (i == end(_leaveFilters)) {
const auto check = [=](not_null<QEvent*> e) {
if (e->type() == QEvent::Leave) {
if (const auto taken = _leaveFilters.take(window)) {
for (const auto weak : taken->registered) {
if (const auto widget = weak.data()) {
QEvent ev(QEvent::Leave);
QCoreApplication::sendEvent(widget, &ev);
}
}
delete taken->filter.data();
}
}
return base::EventFilterResult::Continue;
};
const auto filter = base::install_event_filter(window, check);
QObject::connect(filter, &QObject::destroyed, [=] {
_leaveFilters.remove(window);
});
_leaveSubscriptions.emplace_back(weak, std::move(subscription));
i = _leaveFilters.emplace(
window,
LeaveFilter{ .filter = filter.get() }).first;
}
i->second.registered.push_back(widget.get());
}
#endif // Q_OS_MAC
}
void Application::unregisterLeaveSubscription(not_null<QWidget*> widget) {
#ifdef Q_OS_MAC
_leaveSubscriptions = std::move(
_leaveSubscriptions
) | ranges::actions::remove_if([&](const LeaveSubscription &subscription) {
auto pointer = subscription.pointer.data();
return !pointer || (pointer == widget);
});
if (const auto topLevel = widget->window()) {
const auto i = _leaveFilters.find(topLevel);
if (i != end(_leaveFilters)) {
i->second.registered = std::move(
i->second.registered
) | ranges::actions::remove_if([&](QPointer<QWidget> widget) {
const auto pointer = widget.data();
return !pointer || (pointer == widget);
});
}
}
#endif // Q_OS_MAC
}

View File

@ -369,17 +369,11 @@ private:
std::optional<base::Timer> _saveSettingsTimer;
struct LeaveSubscription {
LeaveSubscription(
QPointer<QWidget> pointer,
rpl::lifetime &&subscription)
: pointer(pointer), subscription(std::move(subscription)) {
}
QPointer<QWidget> pointer;
rpl::lifetime subscription;
struct LeaveFilter {
std::vector<QPointer<QWidget>> registered;
QPointer<QObject> filter;
};
std::vector<LeaveSubscription> _leaveSubscriptions;
base::flat_map<not_null<QWidget*>, LeaveFilter> _leaveFilters;
rpl::lifetime _lifetime;

View File

@ -1140,6 +1140,10 @@ rpl::producer<CallButtonColors> CallMuteButton::colorOverrides() const {
return _colorOverrides.events();
}
not_null<QWidget*> CallMuteButton::outer() const {
return _content.get();
}
rpl::lifetime &CallMuteButton::lifetime() {
return _blobs->lifetime();
}

View File

@ -81,6 +81,7 @@ public:
void raise();
void lower();
[[nodiscard]] not_null<QWidget*> outer() const;
[[nodiscard]] rpl::producer<CallButtonColors> colorOverrides() const;
[[nodiscard]] rpl::lifetime &lifetime();