From c57f071f0c8c9f52762ac3bd0e9d87369ca95f8b Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 29 Nov 2025 13:01:38 +0100 Subject: [PATCH] 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. --- lib/libimhex/include/hex/ui/view.hpp | 11 +++++++++++ lib/libimhex/source/ui/view.cpp | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/libimhex/include/hex/ui/view.hpp b/lib/libimhex/include/hex/ui/view.hpp index c125a271b..0637ecaf9 100644 --- a/lib/libimhex/include/hex/ui/view.hpp +++ b/lib/libimhex/include/hex/ui/view.hpp @@ -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; diff --git a/lib/libimhex/source/ui/view.cpp b/lib/libimhex/source/ui/view.cpp index fee0ff517..8bdc3cb97 100644 --- a/lib/libimhex/source/ui/view.cpp +++ b/lib/libimhex/source/ui/view.cpp @@ -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; }