[TabDeckEditor] Generalize visibility filter and extract it to a separate file (#6526)

* create class

* use new class in old code
This commit is contained in:
RickyRister 2026-01-16 07:12:46 -08:00 committed by GitHub
parent 1b71519ec6
commit 84483c56d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67 additions and 35 deletions

View file

@ -0,0 +1,26 @@
#include "visibility_change_listener.h"
#include <QEvent>
#include <QWidget>
VisibilityChangeListener::VisibilityChangeListener(QWidget *targetWidget)
: QObject(targetWidget), targetWidget(targetWidget)
{
if (targetWidget) {
targetWidget->installEventFilter(this);
}
}
bool VisibilityChangeListener::eventFilter(QObject *o, QEvent *e)
{
if (o == targetWidget && !e->spontaneous()) {
if (e->type() == QEvent::Show) {
emit visibilityChanged(true);
}
if (e->type() == QEvent::Hide) {
emit visibilityChanged(false);
}
}
return false;
}

View file

@ -0,0 +1,35 @@
#ifndef COCKATRICE_VISIBILITY_LISTENER_H
#define COCKATRICE_VISIBILITY_LISTENER_H
#include <QObject>
/**
* @brief This filter listens to the visibility changes of a target widget, emitting signals whenever the visibility of
* that widget changes.
*/
class VisibilityChangeListener : public QObject
{
Q_OBJECT
QWidget *targetWidget;
public:
/**
* Creates a new instance of this class, watching the targetWidget.
* This class automatically installs itself as an eventFilter to the targetWidget.
*
* @param targetWidget The widget to watch. Sets that widget as this object's parent.
*/
explicit VisibilityChangeListener(QWidget *targetWidget);
bool eventFilter(QObject *o, QEvent *e) override;
signals:
/**
* Emitted whenever the target widget's visibility changes
* @param visible The widget's new visibility
*/
void visibilityChanged(bool visible);
};
#endif // COCKATRICE_VISIBILITY_LISTENER_H