impr: Add hooks to let Views to get notified when they are opened or closed (#2493)

This is a trivial change which adds virtual methods to View, `onOpen()`
and `onClose()`, which are called when the view is opened or closed.

This information is already tracked inside the View, but not exposed via
the API. There is `didWindowJustOpen()` and `didWindowJustClose()`, but
these fetch and then reset the flag, so they can't be used more than
once in a frame (and are sometimes called by the frame, meaning the flag
has already been consumed by the time the View's draw callback gets
called).

The use case here is that I have a View which needs to do some work
every time it's shown.
This commit is contained in:
David Given 2025-11-29 13:01:38 +01:00 committed by GitHub
parent b30e2bcfa4
commit c57f071f0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -110,6 +110,17 @@ namespace hex {
void trackViewState();
void setFocused(bool focused);
protected:
/**
* @brief Called when this view is opened (i.e. made visible).
*/
virtual void onOpen() {}
/**
* @brief Called when this view is closed (i.e. made invisible).
*/
virtual void onClose() {}
public:
class Window;
class Special;

View File

@ -75,9 +75,13 @@ namespace hex {
void View::trackViewState() {
if (m_windowOpen && !m_prevWindowOpen)
{
this->setWindowJustOpened(true);
else if (!m_windowOpen && m_prevWindowOpen)
this->onOpen();
} else if (!m_windowOpen && m_prevWindowOpen) {
this->setWindowJustClosed(true);
this->onClose();
}
m_prevWindowOpen = m_windowOpen;
}