diff --git a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp index f51b50acfd..c7a78b54c3 100644 --- a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp @@ -1641,6 +1641,31 @@ private: JUCE_IMPLEMENT_SINGLETON (VBlankDispatcher) +//============================================================================== +class SimpleTimer : private Timer +{ +public: + SimpleTimer (int intervalMs, std::function callbackIn) + : callback (std::move (callbackIn)) + { + jassert (callback); + startTimer (intervalMs); + } + + ~SimpleTimer() override + { + stopTimer(); + } + +private: + void timerCallback() override + { + callback(); + } + + std::function callback; +}; + //============================================================================== class HWNDComponentPeer : public ComponentPeer, private VBlankListener, @@ -1682,8 +1707,10 @@ public: return ModifierKeys::currentModifiers; }; - if (updateCurrentMonitor()) - VBlankDispatcher::getInstance()->updateDisplay (*this, currentMonitor); + updateCurrentMonitorAndRefreshVBlankDispatcher(); + + if (parentToAddTo != nullptr) + monitorUpdateTimer.emplace (1000, [this] { updateCurrentMonitorAndRefreshVBlankDispatcher(); }); suspendResumeRegistration = ScopedSuspendResumeNotificationRegistration { hwnd }; } @@ -3675,10 +3702,18 @@ private: return 0; } - bool updateCurrentMonitor() + enum class ForceRefreshDispatcher + { + no, + yes + }; + + void updateCurrentMonitorAndRefreshVBlankDispatcher (ForceRefreshDispatcher force = ForceRefreshDispatcher::no) { auto monitor = MonitorFromWindow (hwnd, MONITOR_DEFAULTTONULL); - return std::exchange (currentMonitor, monitor) != monitor; + + if (std::exchange (currentMonitor, monitor) != monitor || force == ForceRefreshDispatcher::yes) + VBlankDispatcher::getInstance()->updateDisplay (*this, currentMonitor); } bool handlePositionChanged() @@ -3697,9 +3732,7 @@ private: } handleMovedOrResized(); - - if (updateCurrentMonitor()) - VBlankDispatcher::getInstance()->updateDisplay (*this, currentMonitor); + updateCurrentMonitorAndRefreshVBlankDispatcher(); return ! dontRepaint; // to allow non-accelerated openGL windows to draw themselves correctly. } @@ -3857,8 +3890,7 @@ private: auto* dispatcher = VBlankDispatcher::getInstance(); dispatcher->reconfigureDisplays(); - updateCurrentMonitor(); - dispatcher->updateDisplay (*this, currentMonitor); + updateCurrentMonitorAndRefreshVBlankDispatcher (ForceRefreshDispatcher::yes); } //============================================================================== @@ -4625,6 +4657,7 @@ private: RectangleList deferredRepaints; ScopedSuspendResumeNotificationRegistration suspendResumeRegistration; + std::optional monitorUpdateTimer; //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HWNDComponentPeer)