onItemDrag适配appSubWindow

Signed-off-by: tomkl123 <wangyansong11@huawei.com>
This commit is contained in:
tomkl123 2024-07-15 23:19:50 +08:00
parent 651bc21b83
commit 50f529227c
8 changed files with 96 additions and 49 deletions

View File

@ -221,51 +221,36 @@ void DrawPixelMapInner(RSCanvas* canvas, const RefPtr<PixelMap>& pixmap, int32_t
RefPtr<DragWindow> DragWindow::CreateDragWindow( RefPtr<DragWindow> DragWindow::CreateDragWindow(
const std::string& windowName, int32_t x, int32_t y, uint32_t width, uint32_t height) const std::string& windowName, int32_t x, int32_t y, uint32_t width, uint32_t height)
{ {
int32_t halfWidth = static_cast<int32_t>(width) / 2; return CreateDragWindow({ windowName, x, y, width, height });
int32_t halfHeight = static_cast<int32_t>(height) / 2;
OHOS::sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
option->SetWindowRect({ x - halfWidth, y - halfHeight, width, height });
option->SetHitOffset(halfWidth, halfHeight);
option->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_DRAGGING_EFFECT);
option->SetWindowMode(OHOS::Rosen::WindowMode::WINDOW_MODE_FLOATING);
option->SetFocusable(false);
OHOS::sptr<OHOS::Rosen::Window> dragWindow = OHOS::Rosen::Window::Create(windowName, option);
CHECK_NULL_RETURN(dragWindow, nullptr);
OHOS::Rosen::WMError ret = dragWindow->Show();
if (ret != OHOS::Rosen::WMError::WM_OK) {
TAG_LOGE(AceLogTag::ACE_DRAG, "DragWindow CreateDragWindow, drag window Show() failed, ret: %d", ret);
}
auto window = AceType::MakeRefPtr<DragWindowOhos>(dragWindow);
window->SetSize(width, height);
return window;
} }
RefPtr<DragWindow> DragWindow::CreateDragWindow( RefPtr<DragWindow> DragWindow::CreateDragWindow(const DragWindowParams& params)
const std::string& windowName, int32_t parentWindowId, int32_t x, int32_t y, uint32_t width, uint32_t height)
{ {
int32_t halfWidth = static_cast<int32_t>(width) / 2; int32_t halfWidth = static_cast<int32_t>(params.width) / 2;
int32_t halfHeight = static_cast<int32_t>(height) / 2; int32_t halfHeight = static_cast<int32_t>(params.height) / 2;
OHOS::sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption(); OHOS::sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
option->SetWindowRect({ x - halfWidth, y - halfHeight, width, height }); option->SetWindowRect({ params.x - halfWidth, params.y - halfHeight, params.width, params.height });
option->SetHitOffset(halfWidth, halfHeight); option->SetHitOffset(halfWidth, halfHeight);
option->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW); if (params.parentWindowId == -1) {
option->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_DRAGGING_EFFECT);
} else {
option->SetParentId(params.parentWindowId);
option->SetWindowType(OHOS::Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
}
option->SetWindowMode(OHOS::Rosen::WindowMode::WINDOW_MODE_FLOATING); option->SetWindowMode(OHOS::Rosen::WindowMode::WINDOW_MODE_FLOATING);
option->SetParentId(parentWindowId);
option->SetFocusable(false); option->SetFocusable(false);
OHOS::sptr<OHOS::Rosen::Window> dragWindow = OHOS::Rosen::Window::Create(windowName, option); OHOS::sptr<OHOS::Rosen::Window> dragWindow = OHOS::Rosen::Window::Create(params.windowName, option);
CHECK_NULL_RETURN(dragWindow, nullptr); CHECK_NULL_RETURN(dragWindow, nullptr);
OHOS::Rosen::WMError ret = dragWindow->Show(); OHOS::Rosen::WMError ret = dragWindow->Show();
if (ret != OHOS::Rosen::WMError::WM_OK) { if (ret != OHOS::Rosen::WMError::WM_OK) {
TAG_LOGE(AceLogTag::ACE_DRAG, "DragWindow CreateDragWindow, drag window Show() failed, ret: %d", ret); TAG_LOGE(AceLogTag::ACE_DRAG, "DragWindow CreateDragWindow, drag window Show() failed, ret: %d", ret);
return nullptr;
} }
auto window = AceType::MakeRefPtr<DragWindowOhos>(dragWindow); auto window = AceType::MakeRefPtr<DragWindowOhos>(dragWindow);
window->SetSize(width, height); window->SetSize(params.width, params.height);
return window; return window;
} }

View File

@ -44,10 +44,18 @@ class ACE_EXPORT DragWindow : public AceType {
DECLARE_ACE_TYPE(DragWindow, AceType); DECLARE_ACE_TYPE(DragWindow, AceType);
public: public:
struct DragWindowParams {
std::string windowName;
int32_t x = 0;
int32_t y = 0;
uint32_t width = 0;
uint32_t height = 0;
int32_t parentWindowId = -1;
};
static RefPtr<DragWindow> CreateDragWindow( static RefPtr<DragWindow> CreateDragWindow(
const std::string& windowName, int32_t x, int32_t y, uint32_t width, uint32_t height); const std::string& windowName, int32_t x, int32_t y, uint32_t width, uint32_t height);
static RefPtr<DragWindow> CreateDragWindow( static RefPtr<DragWindow> CreateDragWindow(const DragWindowParams& params);
const std::string& windowName, int32_t parentWindowId, int32_t x, int32_t y, uint32_t width, uint32_t height);
static RefPtr<DragWindow> CreateTextDragWindow( static RefPtr<DragWindow> CreateTextDragWindow(
const std::string& windowName, int32_t x, int32_t y, uint32_t width, uint32_t height); const std::string& windowName, int32_t x, int32_t y, uint32_t width, uint32_t height);

View File

@ -61,6 +61,10 @@ RefPtr<DragDropProxy> DragDropManager::CreateAndShowDragWindow(
SetIsDragged(true); SetIsDragged(true);
isDragCancel_ = false; isDragCancel_ = false;
#if !defined(PREVIEW) #if !defined(PREVIEW)
if (GetWindowId() == -1) {
const float windowScale = GetWindowScale();
pixelMap->Scale(windowScale, windowScale, AceAntiAliasingOption::HIGH);
}
CreateDragWindow(info, pixelMap->GetWidth(), pixelMap->GetHeight()); CreateDragWindow(info, pixelMap->GetWidth(), pixelMap->GetHeight());
CHECK_NULL_RETURN(dragWindow_, nullptr); CHECK_NULL_RETURN(dragWindow_, nullptr);
dragWindow_->DrawPixelMap(pixelMap); dragWindow_->DrawPixelMap(pixelMap);
@ -104,20 +108,57 @@ RefPtr<DragDropProxy> DragDropManager::CreateTextDragDropProxy()
void DragDropManager::CreateDragWindow(const GestureEvent& info, uint32_t width, uint32_t height) void DragDropManager::CreateDragWindow(const GestureEvent& info, uint32_t width, uint32_t height)
{ {
#if !defined(PREVIEW) #if !defined(PREVIEW)
// The window manager currently does not support creating child windows within child windows, const int32_t windowId = GetWindowId();
// so the main window is used here auto pipeline = PipelineContext::GetCurrentContext();
auto container = Container::GetContainer(CONTAINER_ID_DIVIDE_SIZE); CHECK_NULL_VOID(pipeline);
CHECK_NULL_VOID(container); float windowScale = GetWindowScale();
int32_t windowX = static_cast<int32_t>(info.GetGlobalPoint().GetX()); Rect rect = Rect(0, 0, 0, 0);
int32_t windowY = static_cast<int32_t>(info.GetGlobalPoint().GetY()); if (!isDragWindowSubWindow_ || NearEqual(windowScale, 1.0f)) {
rect = pipeline->GetDisplayWindowRectInfo();
}
if (isDragWindowShow_) {
windowScale = 1.0f;
}
const int32_t windowY = static_cast<int32_t>(info.GetGlobalPoint().GetY() * windowScale);
const int32_t windowX = static_cast<int32_t>(info.GetGlobalPoint().GetX() * windowScale);
dragWindow_ = DragWindow::CreateDragWindow( dragWindow_ = DragWindow::CreateDragWindow(
"APP_DRAG_WINDOW", container->GetWindowId(), windowX, windowY, width, height); { "APP_DRAG_WINDOW", windowX + rect.Left(), windowY + rect.Top(), width, height, windowId });
if (!dragWindow_) { if (dragWindow_) {
dragWindow_->SetOffset(rect.Left(), rect.Top());
} else {
TAG_LOGW(AceLogTag::ACE_DRAG, "Create drag window failed!"); TAG_LOGW(AceLogTag::ACE_DRAG, "Create drag window failed!");
} }
#endif #endif
} }
int32_t DragDropManager::GetWindowId()
{
auto windowId = -1;
auto container = Container::Current();
CHECK_NULL_RETURN(container, windowId);
if (!container->IsSceneBoardEnabled()) {
isDragWindowSubWindow_ = false;
return windowId;
}
if (!container->IsMainWindow()) {
// The window manager currently does not support creating child windows within child windows,
// so the main window is used here
container = Container::GetContainer(CONTAINER_ID_DIVIDE_SIZE);
CHECK_NULL_RETURN(container, windowId);
if (!container->IsMainWindow()) {
isDragWindowSubWindow_ = false;
return windowId;
}
}
windowId = container->GetWindowId();
isDragWindowSubWindow_ = true;
return windowId;
}
RefPtr<FrameNode> DragDropManager::CreateDragRootNode(const RefPtr<UINode>& customNode) RefPtr<FrameNode> DragDropManager::CreateDragRootNode(const RefPtr<UINode>& customNode)
{ {
auto pipeline = PipelineContext::GetCurrentContext(); auto pipeline = PipelineContext::GetCurrentContext();
@ -1069,11 +1110,14 @@ void DragDropManager::OnItemDragMove(float globalX, float globalY, int32_t dragg
auto container = Container::Current(); auto container = Container::Current();
CHECK_NULL_VOID(container); CHECK_NULL_VOID(container);
UpdateDragWindowPosition(static_cast<int32_t>(globalX), static_cast<int32_t>(globalY)); const float windowScale = isDragWindowSubWindow_ ? 1.0f : GetWindowScale();
const float windowX = globalX * windowScale;
const float windowY = globalY * windowScale;
UpdateDragWindowPosition(static_cast<int32_t>(windowX), static_cast<int32_t>(windowY));
OHOS::Ace::ItemDragInfo itemDragInfo; OHOS::Ace::ItemDragInfo itemDragInfo;
itemDragInfo.SetX(pipeline->ConvertPxToVp(Dimension(globalX, DimensionUnit::PX))); itemDragInfo.SetX(pipeline->ConvertPxToVp(Dimension(windowX, DimensionUnit::PX)));
itemDragInfo.SetY(pipeline->ConvertPxToVp(Dimension(globalY, DimensionUnit::PX))); itemDragInfo.SetY(pipeline->ConvertPxToVp(Dimension(windowY, DimensionUnit::PX)));
// use -1 for grid item not in eventGrid // use -1 for grid item not in eventGrid
auto getDraggedIndex = [draggedGrid = draggedGridFrameNode_, draggedIndex, dragType]( auto getDraggedIndex = [draggedGrid = draggedGridFrameNode_, draggedIndex, dragType](
@ -1127,10 +1171,13 @@ void DragDropManager::OnItemDragEnd(float globalX, float globalY, int32_t dragge
dragDropState_ = DragDropMgrState::IDLE; dragDropState_ = DragDropMgrState::IDLE;
auto pipeline = PipelineContext::GetCurrentContext(); auto pipeline = PipelineContext::GetCurrentContext();
CHECK_NULL_VOID(pipeline); CHECK_NULL_VOID(pipeline);
const float windowScale = isDragWindowSubWindow_ ? 1.0f : GetWindowScale();
const float windowX = globalX * windowScale;
const float windowY = globalY * windowScale;
OHOS::Ace::ItemDragInfo itemDragInfo; OHOS::Ace::ItemDragInfo itemDragInfo;
itemDragInfo.SetX(pipeline->ConvertPxToVp(Dimension(globalX, DimensionUnit::PX))); itemDragInfo.SetX(pipeline->ConvertPxToVp(Dimension(windowX, DimensionUnit::PX)));
itemDragInfo.SetY(pipeline->ConvertPxToVp(Dimension(globalY, DimensionUnit::PX))); itemDragInfo.SetY(pipeline->ConvertPxToVp(Dimension(windowY, DimensionUnit::PX)));
auto dragFrameNode = FindDragFrameNodeByPosition(globalX, globalY); auto dragFrameNode = FindDragFrameNodeByPosition(globalX, globalY);
if (!dragFrameNode) { if (!dragFrameNode) {

View File

@ -481,6 +481,7 @@ private:
bool ReachMoveLimit(const PointerEvent& pointerEvent, const Point& point); bool ReachMoveLimit(const PointerEvent& pointerEvent, const Point& point);
bool IsUIExtensionShowPlaceholder(const RefPtr<NG::UINode>& node); bool IsUIExtensionShowPlaceholder(const RefPtr<NG::UINode>& node);
bool IsUIExtensionComponent(const RefPtr<NG::UINode>& node); bool IsUIExtensionComponent(const RefPtr<NG::UINode>& node);
int32_t GetWindowId();
std::map<int32_t, WeakPtr<FrameNode>> dragFrameNodes_; std::map<int32_t, WeakPtr<FrameNode>> dragFrameNodes_;
std::map<int32_t, WeakPtr<FrameNode>> gridDragFrameNodes_; std::map<int32_t, WeakPtr<FrameNode>> gridDragFrameNodes_;
@ -519,6 +520,7 @@ private:
bool isDragWindowShow_ = false; bool isDragWindowShow_ = false;
bool hasNotifiedTransformation_ = false; bool hasNotifiedTransformation_ = false;
bool isPullMoveReceivedForCurrentDrag_ = false; bool isPullMoveReceivedForCurrentDrag_ = false;
bool isDragWindowSubWindow_ = false;
VelocityTracker velocityTracker_; VelocityTracker velocityTracker_;
DragDropMgrState dragDropState_ = DragDropMgrState::IDLE; DragDropMgrState dragDropState_ = DragDropMgrState::IDLE;
PreDragStatus preDragStatus_ = PreDragStatus::ACTION_DETECTING_STATUS; PreDragStatus preDragStatus_ = PreDragStatus::ACTION_DETECTING_STATUS;

View File

@ -24,8 +24,7 @@ RefPtr<DragWindow> DragWindow::CreateDragWindow(
return dragWindow; return dragWindow;
} }
RefPtr<DragWindow> DragWindow::CreateDragWindow(const std::string& /* windowName */, int32_t /* parentWindowId */, RefPtr<DragWindow> DragWindow::CreateDragWindow(const DragWindowParams& /* params */)
int32_t /* x */, int32_t /* y */, uint32_t /* width */, uint32_t /* height */)
{ {
static RefPtr<DragWindow> dragWindow = AceType::MakeRefPtr<MockDragWindow>(); static RefPtr<DragWindow> dragWindow = AceType::MakeRefPtr<MockDragWindow>();
return dragWindow; return dragWindow;

View File

@ -23,7 +23,7 @@ void DragDropManagerTestNg::SetUpTestCase()
{ {
MockPipelineContext::SetUp(); MockPipelineContext::SetUp();
MockContainer::SetUp(); MockContainer::SetUp();
MOCK_DRAG_WINDOW = DragWindow::CreateDragWindow("", 0, 0, 0, 0, 0); MOCK_DRAG_WINDOW = DragWindow::CreateDragWindow({"", 0, 0, 0, 0, 0});
} }
void DragDropManagerTestNg::TearDownTestCase() void DragDropManagerTestNg::TearDownTestCase()

View File

@ -16,6 +16,7 @@
#include "grid_test_ng.h" #include "grid_test_ng.h"
#include "test/mock/base/mock_drag_window.h" #include "test/mock/base/mock_drag_window.h"
#include "test/mock/core/common/mock_container.h"
#include "test/mock/core/common/mock_theme_manager.h" #include "test/mock/core/common/mock_theme_manager.h"
#include "test/mock/core/pipeline/mock_pipeline_context.h" #include "test/mock/core/pipeline/mock_pipeline_context.h"
@ -41,10 +42,12 @@ void GridTestNg::SetUpTestSuite()
auto themeConstants = CreateThemeConstants(THEME_PATTERN_GRID); auto themeConstants = CreateThemeConstants(THEME_PATTERN_GRID);
auto gridItemTheme = GridItemTheme::Builder().Build(themeConstants); auto gridItemTheme = GridItemTheme::Builder().Build(themeConstants);
EXPECT_CALL(*themeManager, GetTheme(GridItemTheme::TypeId())).WillRepeatedly(Return(gridItemTheme)); EXPECT_CALL(*themeManager, GetTheme(GridItemTheme::TypeId())).WillRepeatedly(Return(gridItemTheme));
RefPtr<DragWindow> dragWindow = DragWindow::CreateDragWindow("", 0, 0, 0, 0, 0); RefPtr<DragWindow> dragWindow = DragWindow::CreateDragWindow({ "", 0, 0, 0, 0, 0 });
EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), DrawFrameNode(_)).Times(AnyNumber()); EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), DrawFrameNode(_)).Times(AnyNumber());
EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), MoveTo(_, _)).Times(AnyNumber()); EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), MoveTo(_, _)).Times(AnyNumber());
EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), Destroy()).Times(AnyNumber()); EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(dragWindow)), Destroy()).Times(AnyNumber());
auto container = Container::GetContainer(CONTAINER_ID_DIVIDE_SIZE);
EXPECT_CALL(*(AceType::DynamicCast<MockContainer>(container)), GetWindowId()).Times(AnyNumber());
EXPECT_CALL(*MockPipelineContext::pipeline_, FlushUITasks).Times(AnyNumber()); EXPECT_CALL(*MockPipelineContext::pipeline_, FlushUITasks).Times(AnyNumber());
#ifndef TEST_IRREGULAR_GRID #ifndef TEST_IRREGULAR_GRID

View File

@ -15,6 +15,7 @@
#include "list_test_ng.h" #include "list_test_ng.h"
#include "test/mock/base/mock_drag_window.h" #include "test/mock/base/mock_drag_window.h"
#include "test/mock/core/common/mock_container.h"
#include "core/components/common/properties/shadow_config.h" #include "core/components/common/properties/shadow_config.h"
#include "core/components_ng/pattern/button/button_layout_property.h" #include "core/components_ng/pattern/button/button_layout_property.h"
#include "core/components_ng/pattern/button/button_model_ng.h" #include "core/components_ng/pattern/button/button_model_ng.h"
@ -989,10 +990,12 @@ HWTEST_F(ListCommonTestNg, EventHub001, TestSize.Level1)
/** /**
* @tc.steps: step1. EXPECT_CALL DrawFrameNode, HandleOnItemDragStart will trigger it * @tc.steps: step1. EXPECT_CALL DrawFrameNode, HandleOnItemDragStart will trigger it
*/ */
auto mockDragWindow = MockDragWindow::CreateDragWindow("", 0, 0, 0, 0, 0); auto mockDragWindow = MockDragWindow::CreateDragWindow({ "", 0, 0, 0, 0, 0 });
EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(mockDragWindow)), DrawFrameNode(_)).Times(2); EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(mockDragWindow)), DrawFrameNode(_)).Times(2);
EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(mockDragWindow)), MoveTo).Times(AnyNumber()); EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(mockDragWindow)), MoveTo).Times(AnyNumber());
EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(mockDragWindow)), Destroy).Times(AnyNumber()); EXPECT_CALL(*(AceType::DynamicCast<MockDragWindow>(mockDragWindow)), Destroy).Times(AnyNumber());
auto container = Container::GetContainer(CONTAINER_ID_DIVIDE_SIZE);
EXPECT_CALL(*(AceType::DynamicCast<MockContainer>(container)), GetWindowId()).Times(AnyNumber());
/** /**
* @tc.steps: step2. Run List GetDragExtraParams func. * @tc.steps: step2. Run List GetDragExtraParams func.