diff options
Diffstat (limited to 'desktop/inc/lib/init.hxx')
-rw-r--r-- | desktop/inc/lib/init.hxx | 112 |
1 files changed, 107 insertions, 5 deletions
diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx index 5b386e4820d7..5c987bec57ab 100644 --- a/desktop/inc/lib/init.hxx +++ b/desktop/inc/lib/init.hxx @@ -10,24 +10,126 @@ #ifndef INCLUDED_DESKTOP_INC_LIB_INIT_HXX #define INCLUDED_DESKTOP_INC_LIB_INIT_HXX +#include <map> +#include <memory> +#include <mutex> + +#include <osl/thread.h> +#include <vcl/idle.hxx> #include <LibreOfficeKit/LibreOfficeKit.h> #include <LibreOfficeKit/LibreOfficeKitEnums.h> #include <com/sun/star/frame/XStorable.hpp> #include <com/sun/star/lang/XComponent.hpp> -#include <memory> -#include <map> + #include <desktop/dllapi.h> -#include <osl/thread.h> class LOKInteractionHandler; namespace desktop { + + class CallbackFlushHandler : public Idle + { + public: + explicit CallbackFlushHandler(LibreOfficeKitCallback pCallback, void* pData) + : Idle( "lokit timer callback" ), + m_pCallback(pCallback), + m_pData(pData) + { + SetPriority(SchedulerPriority::POST_PAINT); + + // Add the states that are safe to skip duplicates on, + // even when not consequent. + m_states.emplace(LOK_CALLBACK_TEXT_SELECTION, "NIL"); + m_states.emplace(LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR, "NIL"); + m_states.emplace(LOK_CALLBACK_STATE_CHANGED, "NIL"); + + Start(); + } + + virtual ~CallbackFlushHandler() + { + Stop(); + + // We might have important notification (.uno:save?). + flush(); + } + + virtual void Invoke() override + { + flush(); + } + + static + void callback(const int type, const char* payload, void* data) + { + CallbackFlushHandler* self = reinterpret_cast<CallbackFlushHandler*>(data); + if (self) + { + self->queue(type, payload); + } + } + + void queue(const int type, const char* data) + { + const std::string payload(data ? data : "(nil)"); + std::unique_lock<std::mutex> lock(m_mutex); + + const auto stateIt = m_states.find(type); + if (stateIt != m_states.end()) + { + // If the state didn't change, it's safe to ignore. + if (stateIt->second == payload) + { + return; + } + + stateIt->second = payload; + } + + if (type == LOK_CALLBACK_INVALIDATE_TILES && + !m_queue.empty() && std::get<0>(m_queue.back()) == type && std::get<1>(m_queue.back()) == payload) + { + // Supress duplicate invalidation only when they are in sequence. + return; + } + + m_queue.emplace_back(type, payload); + + lock.unlock(); + if (!IsActive()) + { + Start(); + } + } + + private: + void flush() + { + if (m_pCallback) + { + std::unique_lock<std::mutex> lock(m_mutex); + for (auto& pair : m_queue) + { + m_pCallback(std::get<0>(pair), std::get<1>(pair).c_str(), m_pData); + } + + m_queue.clear(); + } + } + + private: + std::vector<std::tuple<int, std::string>> m_queue; + std::map<int, std::string> m_states; + LibreOfficeKitCallback m_pCallback; + void *m_pData; + std::mutex m_mutex; + }; + struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument { css::uno::Reference<css::lang::XComponent> mxComponent; std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass; - LibreOfficeKitCallback mpCallback; - void *mpCallbackData; + std::shared_ptr<CallbackFlushHandler> mpCallbackFlushHandler; explicit LibLODocument_Impl(const css::uno::Reference <css::lang::XComponent> &xComponent); ~LibLODocument_Impl(); |