Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of notification disappearing under cursor #28816

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

kolayne
Copy link

@kolayne kolayne commented Jan 1, 2025

Previously, notifications disappearing under cursor (e.g., because closed manually or open from another device) did not notify the manager properly, as the leaveEventHook was not triggered. This could lead to notifications staying around when not supposed to (see #28813).

This commit fixes that by explicitly notifying manager when the notification widget disappears under the cursor.

Fixes #28813.

setWindowOpacity(_a_opacity.value(_hiding ? 0. : 1.) * _manager->demoMasterOpacity());
const auto opacity = _a_opacity.value(_hiding ? 0. : 1.) * _manager->demoMasterOpacity();
if (opacity == 0. && underMouse()) {
// The notification is leaving from under the cursor, but in such case leave hook is not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't sound correct. The Qt documentation:

A leave event is sent to the widget when the mouse cursor leaves the widget.

https://doc.qt.io/qt-5/qwidget.html#leaveEvent

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose, in Qt's terminology the widget becoming invisible is not "mouse cursor leaves the widget"?..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. I guess it might be platform-dependent if it's not mentioned in documentation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, our code should not rely on that, should it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I got your idea right, just adding manager()->startAllHiding(); before manager()->removeWidget(this); in opacityAnimationCallback should be enough

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's even more logical to start hiding the rest of the notifications when this one is already destroyed (from the manager's perspective)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, personally I'm fine with either ordering

Copy link
Contributor

@ilya-fedin ilya-fedin Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although, thinking more about it, the ordering you proposed would lead to use after free, as you would access this->_manager after this is destroyed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg, does removeWidget free the notification from memory? It is then very surprising that it is called from inside a notification. Is it even defined behavior if an object is freed/deleted from within it's member method, even if it's the last statement?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, look at Manager::removeWidget, all it does is finds the notification in the vector, erases it and calls showNextFromQueue()

// The notification is leaving from under the cursor, but in such case leave hook is not
// triggered automatically. But we still want the manager to start hiding notifications
// (see #28813).
manager()->startAllHiding();
Copy link
Contributor

@ilya-fedin ilya-fedin Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see from the code, this function gets called from hide animation. As the animation is already playing, it means the hiding has already started but you call it again. This doesn't look correct.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I will try to look into this again in a few days.

@ilya-fedin
Copy link
Contributor

ilya-fedin commented Jan 1, 2025

I believe the right fix here is finding why leave event is not being emitted for you and fixing that. Otherwise there likely to be way bigger issues as the code relies on this event in lots of places.

Previously, notifications disappearing under cursor (e.g., because closed
manually or open from another device) did not notify the manager
properly, as the leaveEventHook was not triggered. This could lead to
notifications staying around when not supposed to (see telegramdesktop#28813).

This commit fixes that by explicitly notifying manager when the
notification widget disappears under the cursor.

Fixes telegramdesktop#28813.
@kolayne kolayne force-pushed the notification-fade-out-under-cursor branch from 83e0403 to 89a8c6f Compare January 2, 2025 13:43
@kolayne
Copy link
Author

kolayne commented Jan 2, 2025

@ilya-fedin, I fixed the previously discussed issues. Additionally, I added a comment at the call to Manager::removeWidget to note that it deletes the object.

Note, that I only call manager()->startAllHiding() if the disappeared notification is underMouse() (if the notification disappeared while not under cursor, we don't want any special actions about other notifications' animations, it workds fine already). The fact that the widget reports that it is underMouse() makes me think that it is the correct behavior of Qt that leaveEventHook is not triggered: from Qt's perspective, the cursor has not left, and this is consistent across different Qt interfaces (i.e., event-driven and direct).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Closing the topmost notification prevents other notifications from hiding
2 participants