1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Windows: Use timer to update VBlank registration for embedded HWNDComponentPeer

This commit is contained in:
attila 2022-12-16 18:57:27 +01:00
parent b5c775210f
commit a93d0a7ed0

View file

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