diff --git a/frameworks/core/pipeline_ng/pipeline_context.cpp b/frameworks/core/pipeline_ng/pipeline_context.cpp index deafafe05700ec7cb4a057cffe0f3c24c5518f7a..8cb8ded84732c9b122dca3b61f4d8cbad97d0289 100755 --- a/frameworks/core/pipeline_ng/pipeline_context.cpp +++ b/frameworks/core/pipeline_ng/pipeline_context.cpp @@ -1845,6 +1845,11 @@ void PipelineContext::FlushWindowFocusChangedCallback(bool isFocus) ++iter; } } + + // 当窗口失焦时,发送触摸取消事件来清理所有按下状态的控件 + if (!isFocus) { + SendTouchCancelEventOnWindowUnfocused(); + } } void PipelineContext::AddWindowSizeChangeCallback(int32_t nodeId) @@ -2084,4 +2089,35 @@ void PipelineContext::SetCloseButtonStatus(bool isEnabled) CHECK_NULL_VOID(containerPattern); containerPattern->SetCloseButtonStatus(isEnabled); } + +void PipelineContext::SendTouchCancelEventOnWindowUnfocused() +{ + CHECK_RUN_ON(UI); + + // 遍历所有活跃的触摸事件,发送取消事件 + if (!touchEvents_.empty()) { + std::vector cancelEvents; + for (const auto& touchEvent : touchEvents_) { + // 创建取消事件 + TouchEvent cancelEvent = touchEvent; + cancelEvent.type = TouchType::CANCEL; + cancelEvents.push_back(cancelEvent); + } + + // 清空当前触摸事件列表 + touchEvents_.clear(); + + // 发送所有取消事件 + for (const auto& cancelEvent : cancelEvents) { + auto scalePoint = cancelEvent.CreateScalePoint(GetViewScale()); + eventManager_->DispatchTouchEvent(scalePoint); + } + + // 清理插件管道上下文 + touchPluginPipelineContext_.clear(); + + LOGI("Sent touch cancel events on window unfocused, count: %zu", cancelEvents.size()); + } +} + } // namespace OHOS::Ace::NG diff --git a/frameworks/core/pipeline_ng/pipeline_context.h b/frameworks/core/pipeline_ng/pipeline_context.h index 74462f4ffc0e1422a62bf6b9d7d95f6f559cdcd5..2f9fccc14774425bc271f0d64bdac0ae03c9b66e 100644 --- a/frameworks/core/pipeline_ng/pipeline_context.h +++ b/frameworks/core/pipeline_ng/pipeline_context.h @@ -535,6 +535,8 @@ private: std::list delayedTasks_; ACE_DISALLOW_COPY_AND_MOVE(PipelineContext); + + void SendTouchCancelEventOnWindowUnfocused(); }; } // namespace OHOS::Ace::NG