Unregister window observer before theme manager is deallocated

Technically this probably isn't actually necessary since ThemeManager looks like it should remain allocated until the program quits, but...

Signed-off-by: Kenneth Chew <79120643+kthchew@users.noreply.github.com>
This commit is contained in:
Kenneth Chew 2025-07-16 01:10:05 -04:00
parent 3e65d3a9b5
commit 677a7d7a05
No known key found for this signature in database
3 changed files with 20 additions and 4 deletions

View file

@ -50,6 +50,11 @@ ThemeManager::ThemeManager()
initializeCatPacks(); initializeCatPacks();
} }
ThemeManager::~ThemeManager()
{
stopSettingNewWindowColorsOnMac();
}
/// @brief Adds the Theme to the list of themes /// @brief Adds the Theme to the list of themes
/// @param theme The Theme to add /// @param theme The Theme to add
/// @return Theme ID /// @return Theme ID
@ -179,6 +184,8 @@ void ThemeManager::setTitlebarColorOnMac(WId windowId, QColor color)
{} {}
void ThemeManager::setTitlebarColorOfAllWindowsOnMac(QColor color) void ThemeManager::setTitlebarColorOfAllWindowsOnMac(QColor color)
{} {}
void ThemeManager::stopSettingNewWindowColorsOnMac()
{}
#endif #endif
QList<IconTheme*> ThemeManager::getValidIconThemes() QList<IconTheme*> ThemeManager::getValidIconThemes()

View file

@ -39,6 +39,7 @@ inline auto themeWarningLog()
class ThemeManager { class ThemeManager {
public: public:
ThemeManager(); ThemeManager();
~ThemeManager();
QList<IconTheme*> getValidIconThemes(); QList<IconTheme*> getValidIconThemes();
QList<ITheme*> getValidApplicationThemes(); QList<ITheme*> getValidApplicationThemes();
@ -86,6 +87,8 @@ class ThemeManager {
// This also will set the titlebar color of newly opened windows after this method is called. // This also will set the titlebar color of newly opened windows after this method is called.
// On non-Mac systems, this is a no-op. // On non-Mac systems, this is a no-op.
void setTitlebarColorOfAllWindowsOnMac(QColor color); void setTitlebarColorOfAllWindowsOnMac(QColor color);
// On non-Mac systems, this is a no-op.
void stopSettingNewWindowColorsOnMac();
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
NSObject* m_windowTitlebarObserver = nullptr; NSObject* m_windowTitlebarObserver = nullptr;
#endif #endif

View file

@ -53,10 +53,7 @@ void ThemeManager::setTitlebarColorOfAllWindowsOnMac(QColor color)
// There's no notification for when a new window is opened, but we can set the color when a window switches // There's no notification for when a new window is opened, but we can set the color when a window switches
// from occluded to visible, which also fires on open. // from occluded to visible, which also fires on open.
NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
if (m_windowTitlebarObserver) { stopSettingNewWindowColorsOnMac();
[center removeObserver:m_windowTitlebarObserver];
m_windowTitlebarObserver = nil;
}
m_windowTitlebarObserver = [center addObserverForName:NSWindowDidChangeOcclusionStateNotification m_windowTitlebarObserver = [center addObserverForName:NSWindowDidChangeOcclusionStateNotification
object:nil object:nil
queue:[NSOperationQueue mainQueue] queue:[NSOperationQueue mainQueue]
@ -65,3 +62,12 @@ void ThemeManager::setTitlebarColorOfAllWindowsOnMac(QColor color)
setTitlebarColorOnMac((WId)window.contentView, color); setTitlebarColorOnMac((WId)window.contentView, color);
}]; }];
} }
void ThemeManager::stopSettingNewWindowColorsOnMac()
{
if (m_windowTitlebarObserver) {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:m_windowTitlebarObserver];
m_windowTitlebarObserver = nil;
}
}