Add editor list, scroll, and property grid widgets

This commit is contained in:
2026-04-07 16:57:04 +08:00
parent 0308be1483
commit e22ce763c2
32 changed files with 6057 additions and 0 deletions

View File

@@ -53,6 +53,16 @@ if(TARGET editor_ui_tree_view_basic_validation)
editor_ui_tree_view_basic_validation)
endif()
if(TARGET editor_ui_property_grid_basic_validation)
list(APPEND EDITOR_UI_INTEGRATION_TARGETS
editor_ui_property_grid_basic_validation)
endif()
if(TARGET editor_ui_list_view_basic_validation)
list(APPEND EDITOR_UI_INTEGRATION_TARGETS
editor_ui_list_view_basic_validation)
endif()
add_custom_target(editor_ui_integration_tests
DEPENDS
${EDITOR_UI_INTEGRATION_TARGETS}

View File

@@ -22,8 +22,11 @@ Layout:
- `shell/menu_bar_basic/`: menu bar open/close/hover/dispatch only
- `shell/context_menu_basic/`: context menu root/submenu/dismiss/dispatch only
- `shell/panel_frame_basic/`: panel frame layout/state/hit-test only
- `shell/scroll_view_basic/`: ScrollView viewport, clip, thumb drag, wheel offset only
- `shell/property_grid_basic/`: PropertyGrid section toggle, field selection, value edit, keyboard navigation only
- `shell/status_bar_basic/`: status bar slot/segment/hit-test only
- `shell/tree_view_basic/`: TreeView row layout, indent, disclosure, selection, focus, hit-test only
- `shell/list_view_basic/`: ListView row layout, selection, focus, keyboard navigation, hit-test only
- `shell/tab_strip_basic/`: tab strip layout/state/hit-test/close/navigation only
- `shell/viewport_slot_basic/`: viewport slot chrome/surface/status only
- `shell/viewport_shell_basic/`: viewport shell request/state compose only
@@ -77,6 +80,16 @@ Scenarios:
Executable: `XCUIEditorPanelFrameBasicValidation.exe`
Scope: panel frame header/body/footer layout, focus/active/hover chrome, pin/close hit target only
- `editor.shell.scroll_view_basic`
Build target: `editor_ui_scroll_view_basic_validation`
Executable: `XCUIEditorScrollViewBasicValidation.exe`
Scope: ScrollView viewport clip, wheel scrolling, thumb drag, focus, and hit-test only
- `editor.shell.property_grid_basic`
Build target: `editor_ui_property_grid_basic_validation`
Executable: `XCUIEditorPropertyGridBasicValidation.exe`
Scope: PropertyGrid 基础控件验证;只检查 section toggle、field selection、value edit、Enter/Esc、keyboard navigation不涉及业务 Inspector
- `editor.shell.status_bar_basic`
Build target: `editor_ui_status_bar_basic_validation`
Executable: `XCUIEditorStatusBarBasicValidation.exe`
@@ -87,6 +100,11 @@ Scenarios:
Executable: `XCUIEditorTreeViewBasicValidation.exe`
Scope: TreeView 基础控件验证只检查行缩进、disclosure 展开/折叠、selection、hover/focus 和 hit-test不涉及业务面板
- `editor.shell.list_view_basic`
Build target: `editor_ui_list_view_basic_validation`
Executable: `XCUIEditorListViewBasicValidation.exe`
Scope: ListView 基础控件验证;只检查 row 排列、selection、hover/focus、Up/Down/Home/End 键盘导航和 hit-test不涉及业务面板
- `editor.shell.tab_strip_basic`
Build target: `editor_ui_tab_strip_basic_validation`
Executable: `XCUIEditorTabStripBasicValidation.exe`
@@ -174,12 +192,17 @@ Selected controls:
- `shell/panel_frame_basic/`
Move the mouse over the preview panel, click `Body / Pin / Close`, toggle `Active / Focus / Closable / Footer`, press `F12`.
- `shell/scroll_view_basic/`
把鼠标移到右侧日志区内滚轮滚动,拖拽 scrollbar thumb检查 `Hover / Focused / Thumb Dragging / Offset / Has Scrollbar / Result`,按 `重置``截图(F12)` 或直接按 `F12`
- `shell/status_bar_basic/`
Move the mouse across leading/trailing segments, click interactive segments, toggle focus/active, press `F12`.
- `shell/tree_view_basic/`
先看顶部中文说明“这个测试在验证什么功能”,再点击 disclosure 和树节点行,检查 `Hover / Focused / Selected / Expanded / Visible / Result`,按 `重置``截图(F12)` 或直接按 `F12`
- `shell/list_view_basic/`
先看顶部中文说明“这个测试在验证什么功能”,再点击列表行,并在列表获得 focus 后按 `Up / Down / Home / End`,检查 `Hover / Focused / Selected / Current / Result`,按 `重置``截图(F12)` 或直接按 `F12`
- `shell/tab_strip_basic/`
Click `Document A / B / C`, click `X` on closable tabs, click content to focus, press `Left / Right / Home / End`, press `Reset`, press `F12`.

View File

@@ -16,9 +16,18 @@ endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/panel_content_host_basic/CMakeLists.txt")
add_subdirectory(panel_content_host_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/property_grid_basic/CMakeLists.txt")
add_subdirectory(property_grid_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tree_view_basic/CMakeLists.txt")
add_subdirectory(tree_view_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/list_view_basic/CMakeLists.txt")
add_subdirectory(list_view_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/scroll_view_basic/CMakeLists.txt")
add_subdirectory(scroll_view_basic)
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/status_bar_basic/CMakeLists.txt")
add_subdirectory(status_bar_basic)
endif()

View File

@@ -0,0 +1,30 @@
add_executable(editor_ui_list_view_basic_validation WIN32
main.cpp
)
target_include_directories(editor_ui_list_view_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include
)
target_compile_definitions(editor_ui_list_view_basic_validation PRIVATE
UNICODE
_UNICODE
XCENGINE_EDITOR_UI_TESTS_REPO_ROOT="${XCENGINE_EDITOR_UI_TESTS_REPO_ROOT_PATH}"
)
if(MSVC)
target_compile_options(editor_ui_list_view_basic_validation PRIVATE /utf-8 /FS)
set_property(TARGET editor_ui_list_view_basic_validation PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(editor_ui_list_view_basic_validation PRIVATE
XCUIEditorLib
XCUIEditorHost
)
set_target_properties(editor_ui_list_view_basic_validation PROPERTIES
OUTPUT_NAME "XCUIEditorListViewBasicValidation"
)

View File

@@ -0,0 +1,767 @@
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <XCEditor/Core/UIEditorListViewInteraction.h>
#include <XCEditor/Widgets/UIEditorListView.h>
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
#include <XCEngine/Input/InputTypes.h>
#include <XCEngine/UI/DrawData.h>
#include <XCEngine/UI/Widgets/UISelectionModel.h>
#include <windows.h>
#include <windowsx.h>
#include <algorithm>
#include <filesystem>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#ifndef XCENGINE_EDITOR_UI_TESTS_REPO_ROOT
#define XCENGINE_EDITOR_UI_TESTS_REPO_ROOT "."
#endif
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIColor;
using XCEngine::UI::UIDrawData;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Widgets::UISelectionModel;
using XCEngine::UI::Editor::Host::AutoScreenshotController;
using XCEngine::UI::Editor::Host::NativeRenderer;
using XCEngine::UI::Editor::UIEditorListViewInteractionFrame;
using XCEngine::UI::Editor::UIEditorListViewInteractionResult;
using XCEngine::UI::Editor::UIEditorListViewInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorListViewInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorListViewBackground;
using XCEngine::UI::Editor::Widgets::AppendUIEditorListViewForeground;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorListView;
using XCEngine::UI::Editor::Widgets::UIEditorListViewHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorListViewHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorListViewItem;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorListViewBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | ListView Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kTextSuccess(0.63f, 0.76f, 0.63f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
};
struct ButtonLayout {
ActionId action = ActionId::Reset;
const char* label = "";
UIRect rect = {};
};
struct ScenarioLayout {
UIRect introRect = {};
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect listRect = {};
std::vector<ButtonLayout> buttons = {};
};
std::filesystem::path ResolveRepoRootPath() {
std::string root = XCENGINE_EDITOR_UI_TESTS_REPO_ROOT;
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height;
}
std::int32_t MapListNavigationKey(UINT keyCode) {
switch (keyCode) {
case VK_UP:
return static_cast<std::int32_t>(KeyCode::Up);
case VK_DOWN:
return static_cast<std::int32_t>(KeyCode::Down);
case VK_HOME:
return static_cast<std::int32_t>(KeyCode::Home);
case VK_END:
return static_cast<std::int32_t>(KeyCode::End);
default:
return static_cast<std::int32_t>(KeyCode::None);
}
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 214.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 92.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(200.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.listRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 64.0f,
layout.previewRect.width - 36.0f,
layout.previewRect.height - 84.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 40.0f;
layout.buttons = {
{ ActionId::Reset, "重置", UIRect(layout.controlRect.x + 14.0f, buttonY, buttonWidth, 36.0f) },
{ ActionId::Capture, "截图(F12)", UIRect(layout.controlRect.x + 26.0f + buttonWidth, buttonY, buttonWidth, 36.0f) }
};
return layout;
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
kTextPrimary,
12.0f);
}
std::vector<UIEditorListViewItem> BuildListItems() {
return {
{ "scene", "Scene.unity", "Scene | 最近修改: 1 分钟前", 0.0f },
{ "material", "Metal.mat", "Material | 4 个属性", 0.0f },
{ "script", "PlayerController.cs", "C# Script | 1.8 KB", 0.0f },
{ "texture", "Checker.png", "Texture2D | 1024x1024", 0.0f },
{ "prefab", "Robot.prefab", "Prefab | 7 个组件", 0.0f }
};
}
std::string JoinItems(const std::vector<UIEditorListViewItem>& items) {
std::ostringstream stream = {};
for (std::size_t index = 0u; index < items.size(); ++index) {
if (index > 0u) {
stream << " | ";
}
stream << items[index].primaryText;
}
return stream.str();
}
std::string DescribeHitTarget(
const UIEditorListViewHitTarget& hitTarget,
const std::vector<UIEditorListViewItem>& items) {
if (hitTarget.itemIndex >= items.size()) {
return "";
}
return "row: " + items[hitTarget.itemIndex].primaryText;
}
UIInputEvent MakePointerEvent(
UIInputEventType type,
const UIPoint& position,
UIPointerButton button = UIPointerButton::None) {
UIInputEvent event = {};
event.type = type;
event.position = position;
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyEvent(std::int32_t keyCode) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = keyCode;
return event;
}
class ScenarioApp {
public:
int Run(HINSTANCE hInstance, int nCmdShow) {
if (!Initialize(hInstance, nCmdShow)) {
Shutdown();
return 1;
}
MSG message = {};
while (message.message != WM_QUIT) {
if (PeekMessageW(&message, nullptr, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessageW(&message);
continue;
}
RenderFrame();
Sleep(8);
}
Shutdown();
return static_cast<int>(message.wParam);
}
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
const auto* createStruct = reinterpret_cast<CREATESTRUCTW*>(lParam);
auto* app = reinterpret_cast<ScenarioApp*>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
return TRUE;
}
auto* app = reinterpret_cast<ScenarioApp*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_SIZE:
if (app != nullptr && wParam != SIZE_MINIMIZED) {
app->OnResize(static_cast<UINT>(LOWORD(lParam)), static_cast<UINT>(HIWORD(lParam)));
}
return 0;
case WM_MOUSEMOVE:
if (app != nullptr) {
app->HandleMouseMove(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_MOUSELEAVE:
if (app != nullptr) {
app->HandleMouseLeave();
return 0;
}
break;
case WM_LBUTTONDOWN:
if (app != nullptr) {
app->HandleLeftButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_LBUTTONUP:
if (app != nullptr) {
app->HandleLeftButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_RBUTTONDOWN:
if (app != nullptr) {
app->HandleRightButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_RBUTTONUP:
if (app != nullptr) {
app->HandleRightButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (app != nullptr) {
if (wParam == VK_F12) {
app->m_autoScreenshot.RequestCapture("manual_f12");
app->m_lastResult = "已请求截图,输出到 captures/latest.png";
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
}
const std::int32_t keyCode = MapListNavigationKey(static_cast<UINT>(wParam));
if (keyCode != static_cast<std::int32_t>(KeyCode::None)) {
app->HandleNavigationKey(keyCode);
return 0;
}
}
break;
case WM_PAINT:
if (app != nullptr) {
PAINTSTRUCT paintStruct = {};
BeginPaint(hwnd, &paintStruct);
app->RenderFrame();
EndPaint(hwnd, &paintStruct);
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (app != nullptr) {
app->m_hwnd = nullptr;
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
bool Initialize(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &ScenarioApp::WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
return false;
}
m_hwnd = CreateWindowExW(
0,
kWindowClassName,
kWindowTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1480,
920,
nullptr,
nullptr,
hInstance,
this);
if (m_hwnd == nullptr) {
return false;
}
ShowWindow(m_hwnd, nCmdShow);
UpdateWindow(m_hwnd);
if (!m_renderer.Initialize(m_hwnd)) {
return false;
}
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/list_view_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
ResetScenario();
return true;
}
void Shutdown() {
m_autoScreenshot.Shutdown();
m_renderer.Shutdown();
if (m_hwnd != nullptr && IsWindow(m_hwnd)) {
DestroyWindow(m_hwnd);
}
m_hwnd = nullptr;
if (m_windowClassAtom != 0) {
UnregisterClassW(kWindowClassName, GetModuleHandleW(nullptr));
m_windowClassAtom = 0;
}
}
ScenarioLayout GetLayout() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
}
void ResetScenario() {
m_items = BuildListItems();
m_selectionModel = {};
m_selectionModel.SetSelection("material");
m_interactionState = {};
m_interactionState.listViewState.focused = true;
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hoveredAction = ActionId::Reset;
m_hasHoveredAction = false;
m_lastResult = "已重置到默认列表状态";
RefreshListFrame();
}
void RefreshListFrame() {
if (m_hwnd == nullptr) {
return;
}
const ScenarioLayout layout = GetLayout();
m_listFrame =
UpdateUIEditorListViewInteraction(
m_interactionState,
m_selectionModel,
layout.listRect,
m_items,
{});
}
void OnResize(UINT width, UINT height) {
if (width == 0u || height == 0u) {
return;
}
m_renderer.Resize(width, height);
RefreshListFrame();
}
void HandleMouseMove(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
UpdateHoveredAction(layout, x, y);
TRACKMOUSEEVENT trackEvent = {};
trackEvent.cbSize = sizeof(trackEvent);
trackEvent.dwFlags = TME_LEAVE;
trackEvent.hwndTrack = m_hwnd;
TrackMouseEvent(&trackEvent);
PumpListEvents({ MakePointerEvent(UIInputEventType::PointerMove, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseLeave() {
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hasHoveredAction = false;
PumpListEvents({ MakePointerEvent(UIInputEventType::PointerLeave, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
if (HitTestAction(layout, x, y) != nullptr) {
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
PumpListEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Left) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button != nullptr) {
ExecuteAction(button->action);
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const bool wasFocused = m_interactionState.listViewState.focused;
const bool insideList = ContainsPoint(layout.listRect, x, y);
const UIEditorListViewInteractionResult result =
PumpListEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result, wasFocused, insideList);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleRightButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
PumpListEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Right) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleRightButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const UIEditorListViewInteractionResult result =
PumpListEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Right) });
UpdateResultText(result, m_interactionState.listViewState.focused, true);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleNavigationKey(std::int32_t keyCode) {
const UIEditorListViewInteractionResult result =
PumpListEvents({ MakeKeyEvent(keyCode) });
UpdateResultText(result, m_interactionState.listViewState.focused, true);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void UpdateHoveredAction(const ScenarioLayout& layout, float x, float y) {
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button == nullptr) {
m_hasHoveredAction = false;
return;
}
m_hoveredAction = button->action;
m_hasHoveredAction = true;
}
const ButtonLayout* HitTestAction(const ScenarioLayout& layout, float x, float y) const {
for (const ButtonLayout& button : layout.buttons) {
if (ContainsPoint(button.rect, x, y)) {
return &button;
}
}
return nullptr;
}
UIEditorListViewInteractionResult PumpListEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
m_listFrame =
UpdateUIEditorListViewInteraction(
m_interactionState,
m_selectionModel,
layout.listRect,
m_items,
events);
return m_listFrame.result;
}
void UpdateResultText(
const UIEditorListViewInteractionResult& result,
bool wasFocused,
bool insideList) {
if (result.keyboardNavigated && !result.selectedItemId.empty()) {
m_lastResult = "键盘导航选择: " + result.selectedItemId;
return;
}
if (result.secondaryClicked && !result.selectedItemId.empty()) {
m_lastResult = "右键命中行: " + result.selectedItemId;
return;
}
if (result.selectionChanged && !result.selectedItemId.empty()) {
m_lastResult = "选中行: " + result.selectedItemId;
return;
}
if (!insideList && wasFocused && !m_interactionState.listViewState.focused) {
m_lastResult = "点击列表外空白: focus 已清除selection 保留";
return;
}
if (insideList) {
m_lastResult = "点击列表内空白: 只更新 focus / hover";
return;
}
m_lastResult = "等待交互";
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
ResetScenario();
break;
case ActionId::Capture:
m_autoScreenshot.RequestCapture("manual_button");
m_lastResult = "已请求截图,输出到 captures/latest.png";
break;
}
}
void RenderFrame() {
if (m_hwnd == nullptr) {
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
RefreshListFrame();
const UIEditorListViewHitTarget currentHit =
HitTestUIEditorListView(m_listFrame.layout, m_mousePosition);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorListViewBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
DrawCard(
drawList,
layout.introRect,
"这个测试在验证什么功能",
"只验证 Editor ListView 基础控件,不涉及任何业务面板。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 验证列表行的垂直排列是否稳定:主标题和次标题不能互相挤压。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 点击 row 只切换 selectionhover、selected、focused 三种状态要能区分。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 当列表获得 focus 后,按 Up / Down / Home / End 应稳定移动当前选择。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 点击列表外空白后focus 应清除,但 selection 不应丢失。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 按 F12 手动截图;设置 XCUI_AUTO_CAPTURE_ON_STARTUP=1 可自动截图。",
kTextPrimary,
12.0f);
DrawCard(drawList, layout.controlRect, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要", "重点检查 hit / focus / selection / current / items。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit, m_items),
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.listViewState.focused ? "" : ""),
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
"Selected: " +
(m_selectionModel.HasSelection() ? m_selectionModel.GetSelectedId() : std::string("(none)")),
kTextSuccess,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
std::string("Current: ") +
(m_interactionState.keyboardNavigation.HasCurrentIndex()
? std::to_string(m_interactionState.keyboardNavigation.GetCurrentIndex())
: std::string("(none)")),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Items(" + std::to_string(m_items.size()) + "): " + JoinItems(m_items),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
? "截图排队中..."
: (m_autoScreenshot.GetLastCaptureSummary().empty()
? std::string("F12 -> tests/UI/Editor/integration/shell/list_view_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 216.0f),
captureSummary,
kTextWeak,
12.0f);
DrawCard(drawList, layout.previewRect, "ListView 预览", "这里只放一个 ListView不混入 Project/Console 等业务内容。");
AppendUIEditorListViewBackground(
drawList,
m_listFrame.layout,
m_items,
m_selectionModel,
m_interactionState.listViewState);
AppendUIEditorListViewForeground(drawList, m_listFrame.layout, m_items);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
framePresented);
}
HWND m_hwnd = nullptr;
ATOM m_windowClassAtom = 0;
NativeRenderer m_renderer = {};
AutoScreenshotController m_autoScreenshot = {};
std::filesystem::path m_captureRoot = {};
std::vector<UIEditorListViewItem> m_items = {};
UISelectionModel m_selectionModel = {};
UIEditorListViewInteractionState m_interactionState = {};
UIEditorListViewInteractionFrame m_listFrame = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
};
} // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow) {
return ScenarioApp().Run(hInstance, nCmdShow);
}

View File

@@ -0,0 +1,30 @@
add_executable(editor_ui_property_grid_basic_validation WIN32
main.cpp
)
target_include_directories(editor_ui_property_grid_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include
)
target_compile_definitions(editor_ui_property_grid_basic_validation PRIVATE
UNICODE
_UNICODE
XCENGINE_EDITOR_UI_TESTS_REPO_ROOT="${XCENGINE_EDITOR_UI_TESTS_REPO_ROOT_PATH}"
)
if(MSVC)
target_compile_options(editor_ui_property_grid_basic_validation PRIVATE /utf-8 /FS)
set_property(TARGET editor_ui_property_grid_basic_validation PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(editor_ui_property_grid_basic_validation PRIVATE
XCUIEditorLib
XCUIEditorHost
)
set_target_properties(editor_ui_property_grid_basic_validation PROPERTIES
OUTPUT_NAME "XCUIEditorPropertyGridBasicValidation"
)

View File

@@ -0,0 +1,907 @@
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <XCEditor/Core/UIEditorPropertyGridInteraction.h>
#include <XCEditor/Widgets/UIEditorPropertyGrid.h>
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
#include <XCEngine/Input/InputTypes.h>
#include <XCEngine/UI/DrawData.h>
#include <windows.h>
#include <windowsx.h>
#include <algorithm>
#include <filesystem>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#ifndef XCENGINE_EDITOR_UI_TESTS_REPO_ROOT
#define XCENGINE_EDITOR_UI_TESTS_REPO_ROOT "."
#endif
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIColor;
using XCEngine::UI::UIDrawData;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Widgets::UIExpansionModel;
using XCEngine::UI::Widgets::UIPropertyEditModel;
using XCEngine::UI::Widgets::UISelectionModel;
using XCEngine::UI::Editor::Host::AutoScreenshotController;
using XCEngine::UI::Editor::Host::NativeRenderer;
using XCEngine::UI::Editor::UIEditorPropertyGridInteractionFrame;
using XCEngine::UI::Editor::UIEditorPropertyGridInteractionResult;
using XCEngine::UI::Editor::UIEditorPropertyGridInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorPropertyGridInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorPropertyGridBackground;
using XCEngine::UI::Editor::Widgets::AppendUIEditorPropertyGridForeground;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorPropertyGrid;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridField;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridSection;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorPropertyGridBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | PropertyGrid Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kTextSuccess(0.63f, 0.76f, 0.63f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
};
struct ButtonLayout {
ActionId action = ActionId::Reset;
const char* label = "";
UIRect rect = {};
};
struct ScenarioLayout {
UIRect introRect = {};
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect gridRect = {};
std::vector<ButtonLayout> buttons = {};
};
std::filesystem::path ResolveRepoRootPath() {
std::string root = XCENGINE_EDITOR_UI_TESTS_REPO_ROOT;
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height;
}
std::int32_t MapEditorKey(UINT keyCode) {
switch (keyCode) {
case VK_UP:
return static_cast<std::int32_t>(KeyCode::Up);
case VK_DOWN:
return static_cast<std::int32_t>(KeyCode::Down);
case VK_HOME:
return static_cast<std::int32_t>(KeyCode::Home);
case VK_END:
return static_cast<std::int32_t>(KeyCode::End);
case VK_LEFT:
return static_cast<std::int32_t>(KeyCode::Left);
case VK_RIGHT:
return static_cast<std::int32_t>(KeyCode::Right);
case VK_RETURN:
return static_cast<std::int32_t>(KeyCode::Enter);
case VK_ESCAPE:
return static_cast<std::int32_t>(KeyCode::Escape);
case VK_BACK:
return static_cast<std::int32_t>(KeyCode::Backspace);
case VK_DELETE:
return static_cast<std::int32_t>(KeyCode::Delete);
default:
return static_cast<std::int32_t>(KeyCode::None);
}
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 214.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 92.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(220.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.gridRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 64.0f,
layout.previewRect.width - 36.0f,
layout.previewRect.height - 84.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 40.0f;
layout.buttons = {
{ ActionId::Reset, "重置", UIRect(layout.controlRect.x + 14.0f, buttonY, buttonWidth, 36.0f) },
{ ActionId::Capture, "截图(F12)", UIRect(layout.controlRect.x + 26.0f + buttonWidth, buttonY, buttonWidth, 36.0f) }
};
return layout;
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
kTextPrimary,
12.0f);
}
std::vector<UIEditorPropertyGridSection> BuildSections() {
return {
{
"transform",
"Transform",
{
{ "position", "Position", "0, 0, 0", false, 0.0f },
{ "rotation", "Rotation", "0, 45, 0", false, 0.0f },
{ "scale", "Scale", "1, 1, 1", false, 0.0f }
},
0.0f
},
{
"material",
"Material",
{
{ "shader", "Shader", "Standard/Lit", false, 0.0f },
{ "queue", "Render Queue", "2000", false, 0.0f },
{ "guid", "GUID", "asset-guid-001", true, 0.0f }
},
0.0f
},
{
"metadata",
"Metadata",
{
{ "tag", "Tag", "", false, 0.0f },
{ "layer", "Layer", "Default", false, 0.0f }
},
0.0f
}
};
}
std::string DescribeHitTarget(
const UIEditorPropertyGridHitTarget& hitTarget,
const std::vector<UIEditorPropertyGridSection>& sections) {
if (hitTarget.kind == UIEditorPropertyGridHitTargetKind::SectionHeader &&
hitTarget.sectionIndex < sections.size()) {
return "section: " + sections[hitTarget.sectionIndex].title;
}
if ((hitTarget.kind == UIEditorPropertyGridHitTargetKind::FieldRow ||
hitTarget.kind == UIEditorPropertyGridHitTargetKind::ValueBox) &&
hitTarget.sectionIndex < sections.size() &&
hitTarget.fieldIndex < sections[hitTarget.sectionIndex].fields.size()) {
const UIEditorPropertyGridField& field =
sections[hitTarget.sectionIndex].fields[hitTarget.fieldIndex];
return (hitTarget.kind == UIEditorPropertyGridHitTargetKind::ValueBox ? "value: " : "field: ") +
field.label;
}
return "(none)";
}
std::string BuildExpandedSummary(const UIExpansionModel& expansionModel) {
std::ostringstream stream = {};
stream << (expansionModel.IsExpanded("transform") ? "Transform" : "-");
stream << " / " << (expansionModel.IsExpanded("material") ? "Material" : "-");
stream << " / " << (expansionModel.IsExpanded("metadata") ? "Metadata" : "-");
return stream.str();
}
UIInputEvent MakePointerEvent(
UIInputEventType type,
const UIPoint& position,
UIPointerButton button = UIPointerButton::None) {
UIInputEvent event = {};
event.type = type;
event.position = position;
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyEvent(std::int32_t keyCode) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = keyCode;
return event;
}
UIInputEvent MakeCharacterEvent(std::uint32_t character) {
UIInputEvent event = {};
event.type = UIInputEventType::Character;
event.character = character;
return event;
}
class ScenarioApp {
public:
int Run(HINSTANCE hInstance, int nCmdShow) {
if (!Initialize(hInstance, nCmdShow)) {
Shutdown();
return 1;
}
MSG message = {};
while (message.message != WM_QUIT) {
if (PeekMessageW(&message, nullptr, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessageW(&message);
continue;
}
RenderFrame();
Sleep(8);
}
Shutdown();
return static_cast<int>(message.wParam);
}
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
const auto* createStruct = reinterpret_cast<CREATESTRUCTW*>(lParam);
auto* app = reinterpret_cast<ScenarioApp*>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
return TRUE;
}
auto* app = reinterpret_cast<ScenarioApp*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_SIZE:
if (app != nullptr && wParam != SIZE_MINIMIZED) {
app->OnResize(static_cast<UINT>(LOWORD(lParam)), static_cast<UINT>(HIWORD(lParam)));
}
return 0;
case WM_MOUSEMOVE:
if (app != nullptr) {
app->HandleMouseMove(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_MOUSELEAVE:
if (app != nullptr) {
app->HandleMouseLeave();
return 0;
}
break;
case WM_LBUTTONDOWN:
if (app != nullptr) {
app->HandleLeftButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_LBUTTONUP:
if (app != nullptr) {
app->HandleLeftButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_RBUTTONDOWN:
if (app != nullptr) {
app->HandleRightButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_RBUTTONUP:
if (app != nullptr) {
app->HandleRightButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (app != nullptr) {
if (wParam == VK_F12) {
app->m_autoScreenshot.RequestCapture("manual_f12");
app->m_lastResult = "已请求截图,输出到 captures/latest.png";
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
}
const std::int32_t keyCode = MapEditorKey(static_cast<UINT>(wParam));
if (keyCode != static_cast<std::int32_t>(KeyCode::None)) {
app->HandleKeyDown(keyCode);
return 0;
}
}
break;
case WM_CHAR:
if (app != nullptr &&
wParam >= 32 &&
wParam != VK_RETURN &&
wParam != VK_ESCAPE &&
wParam != VK_TAB) {
app->HandleCharacter(static_cast<std::uint32_t>(wParam));
return 0;
}
break;
case WM_PAINT:
if (app != nullptr) {
PAINTSTRUCT paintStruct = {};
BeginPaint(hwnd, &paintStruct);
app->RenderFrame();
EndPaint(hwnd, &paintStruct);
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (app != nullptr) {
app->m_hwnd = nullptr;
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
bool Initialize(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &ScenarioApp::WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
return false;
}
m_hwnd = CreateWindowExW(
0,
kWindowClassName,
kWindowTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1480,
920,
nullptr,
nullptr,
hInstance,
this);
if (m_hwnd == nullptr) {
return false;
}
ShowWindow(m_hwnd, nCmdShow);
UpdateWindow(m_hwnd);
if (!m_renderer.Initialize(m_hwnd)) {
return false;
}
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/property_grid_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
ResetScenario();
return true;
}
void Shutdown() {
m_autoScreenshot.Shutdown();
m_renderer.Shutdown();
if (m_hwnd != nullptr && IsWindow(m_hwnd)) {
DestroyWindow(m_hwnd);
}
m_hwnd = nullptr;
if (m_windowClassAtom != 0) {
UnregisterClassW(kWindowClassName, GetModuleHandleW(nullptr));
m_windowClassAtom = 0;
}
}
ScenarioLayout GetLayout() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
}
void ResetScenario() {
m_sections = BuildSections();
m_selectionModel = {};
m_selectionModel.SetSelection("rotation");
m_expansionModel = {};
m_expansionModel.Expand("transform");
m_expansionModel.Expand("material");
m_expansionModel.Expand("metadata");
m_propertyEditModel = {};
m_interactionState = {};
m_interactionState.propertyGridState.focused = true;
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hoveredAction = ActionId::Reset;
m_hasHoveredAction = false;
m_lastResult = "已重置到默认 PropertyGrid 状态";
m_lastCommit = "(none)";
RefreshGridFrame();
}
void RefreshGridFrame() {
if (m_hwnd == nullptr) {
return;
}
const ScenarioLayout layout = GetLayout();
m_gridFrame =
UpdateUIEditorPropertyGridInteraction(
m_interactionState,
m_selectionModel,
m_expansionModel,
m_propertyEditModel,
layout.gridRect,
m_sections,
{});
}
void ApplyCommittedValue(const UIEditorPropertyGridInteractionResult& result) {
if (!result.editCommitted || result.committedFieldId.empty()) {
return;
}
for (UIEditorPropertyGridSection& section : m_sections) {
for (UIEditorPropertyGridField& field : section.fields) {
if (field.fieldId == result.committedFieldId) {
field.valueText = result.committedValue;
m_lastCommit = result.committedFieldId + " = " + result.committedValue;
return;
}
}
}
}
void OnResize(UINT width, UINT height) {
if (width == 0u || height == 0u) {
return;
}
m_renderer.Resize(width, height);
RefreshGridFrame();
}
void HandleMouseMove(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
UpdateHoveredAction(layout, x, y);
TRACKMOUSEEVENT trackEvent = {};
trackEvent.cbSize = sizeof(trackEvent);
trackEvent.dwFlags = TME_LEAVE;
trackEvent.hwndTrack = m_hwnd;
TrackMouseEvent(&trackEvent);
PumpGridEvents({ MakePointerEvent(UIInputEventType::PointerMove, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseLeave() {
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hasHoveredAction = false;
PumpGridEvents({ MakePointerEvent(UIInputEventType::PointerLeave, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
if (HitTestAction(layout, x, y) != nullptr) {
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
PumpGridEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Left) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button != nullptr) {
ExecuteAction(button->action);
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const bool wasFocused = m_interactionState.propertyGridState.focused;
const bool insideGrid = ContainsPoint(layout.gridRect, x, y);
const UIEditorPropertyGridInteractionResult result =
PumpGridEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Left) });
UpdateResultText(result, wasFocused, insideGrid);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleRightButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
PumpGridEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Right) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleRightButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const UIEditorPropertyGridInteractionResult result =
PumpGridEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Right) });
UpdateResultText(result, m_interactionState.propertyGridState.focused, true);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleKeyDown(std::int32_t keyCode) {
const UIEditorPropertyGridInteractionResult result =
PumpGridEvents({ MakeKeyEvent(keyCode) });
UpdateResultText(result, m_interactionState.propertyGridState.focused, true);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleCharacter(std::uint32_t character) {
const UIEditorPropertyGridInteractionResult result =
PumpGridEvents({ MakeCharacterEvent(character) });
UpdateResultText(result, m_interactionState.propertyGridState.focused, true);
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void UpdateHoveredAction(const ScenarioLayout& layout, float x, float y) {
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button == nullptr) {
m_hasHoveredAction = false;
return;
}
m_hoveredAction = button->action;
m_hasHoveredAction = true;
}
const ButtonLayout* HitTestAction(const ScenarioLayout& layout, float x, float y) const {
for (const ButtonLayout& button : layout.buttons) {
if (ContainsPoint(button.rect, x, y)) {
return &button;
}
}
return nullptr;
}
UIEditorPropertyGridInteractionResult PumpGridEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
m_gridFrame =
UpdateUIEditorPropertyGridInteraction(
m_interactionState,
m_selectionModel,
m_expansionModel,
m_propertyEditModel,
layout.gridRect,
m_sections,
std::move(events));
ApplyCommittedValue(m_gridFrame.result);
return m_gridFrame.result;
}
void UpdateResultText(
const UIEditorPropertyGridInteractionResult& result,
bool wasFocused,
bool insideGrid) {
if (result.editCommitted) {
m_lastResult = "提交字段: " + result.committedFieldId + " = " + result.committedValue;
return;
}
if (result.editCanceled) {
m_lastResult = "取消编辑: " + result.activeFieldId;
return;
}
if (result.editValueChanged) {
m_lastResult = "编辑中: " + result.activeFieldId;
return;
}
if (result.editStarted) {
m_lastResult = "开始编辑: " + result.activeFieldId;
return;
}
if (result.sectionToggled) {
m_lastResult = "切换分组: " + result.toggledSectionId;
return;
}
if (result.keyboardNavigated && !result.selectedFieldId.empty()) {
m_lastResult = "键盘选中: " + result.selectedFieldId;
return;
}
if (result.selectionChanged && !result.selectedFieldId.empty()) {
m_lastResult = "选中字段: " + result.selectedFieldId;
return;
}
if (result.secondaryClicked && !result.selectedFieldId.empty()) {
m_lastResult = "右键命中字段: " + result.selectedFieldId;
return;
}
if (!insideGrid && wasFocused && !m_interactionState.propertyGridState.focused) {
m_lastResult = "点击外部后 focus 已清除";
return;
}
if (insideGrid) {
m_lastResult = "Grid focus / hover 已更新";
return;
}
m_lastResult = "等待交互";
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
ResetScenario();
break;
case ActionId::Capture:
m_autoScreenshot.RequestCapture("manual_button");
m_lastResult = "已请求截图,输出到 captures/latest.png";
break;
}
}
void RenderFrame() {
if (m_hwnd == nullptr) {
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
RefreshGridFrame();
const UIEditorPropertyGridHitTarget currentHit =
HitTestUIEditorPropertyGrid(m_gridFrame.layout, m_mousePosition);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorPropertyGridBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
DrawCard(
drawList,
layout.introRect,
"这个测试在验证什么功能",
"只验证 Editor PropertyGrid 基础控件,不涉及任何 Inspector 业务逻辑。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 点击 section header检查展开/折叠是否稳定,字段布局不能歪。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 点击 field row 只切换 selection点击 value box 才进入编辑态。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 编辑态输入字符,按 Enter commit按 Esc cancelread-only 字段不能进编辑。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. Grid 获得 focus 后按 Up / Down / Home / End检查字段导航和 selection 同步。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 按 F12 手动截图;设置 XCUI_AUTO_CAPTURE_ON_STARTUP=1 可自动截图。",
kTextPrimary,
12.0f);
DrawCard(drawList, layout.controlRect, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要", "重点检查 hit / focus / selection / edit / commit。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit, m_sections),
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.propertyGridState.focused ? "" : ""),
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
"Selected: " +
(m_selectionModel.HasSelection() ? m_selectionModel.GetSelectedId() : std::string("(none)")),
kTextSuccess,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
"Active Edit: " +
(m_propertyEditModel.HasActiveEdit() ? m_propertyEditModel.GetActiveFieldId() : std::string("(none)")),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
"Staged: " +
(m_propertyEditModel.HasActiveEdit() ? m_propertyEditModel.GetStagedValue() : std::string("(none)")),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Expanded: " + BuildExpandedSummary(m_expansionModel),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
"Last Commit: " + m_lastCommit,
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
? "截图排队中..."
: (m_autoScreenshot.GetLastCaptureSummary().empty()
? std::string("F12 -> tests/UI/Editor/integration/shell/property_grid_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 262.0f),
captureSummary,
kTextWeak,
12.0f);
DrawCard(drawList, layout.previewRect, "PropertyGrid 预览", "这里只放一个 PropertyGrid不混入任何业务面板。");
AppendUIEditorPropertyGridBackground(
drawList,
m_gridFrame.layout,
m_sections,
m_selectionModel,
m_propertyEditModel,
m_interactionState.propertyGridState);
AppendUIEditorPropertyGridForeground(
drawList,
m_gridFrame.layout,
m_sections,
m_propertyEditModel);
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
framePresented);
}
HWND m_hwnd = nullptr;
ATOM m_windowClassAtom = 0;
NativeRenderer m_renderer = {};
AutoScreenshotController m_autoScreenshot = {};
std::filesystem::path m_captureRoot = {};
std::vector<UIEditorPropertyGridSection> m_sections = {};
UISelectionModel m_selectionModel = {};
UIExpansionModel m_expansionModel = {};
UIPropertyEditModel m_propertyEditModel = {};
UIEditorPropertyGridInteractionState m_interactionState = {};
UIEditorPropertyGridInteractionFrame m_gridFrame = {};
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
std::string m_lastCommit = {};
};
} // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow) {
return ScenarioApp().Run(hInstance, nCmdShow);
}

View File

@@ -0,0 +1,30 @@
add_executable(editor_ui_scroll_view_basic_validation WIN32
main.cpp
)
target_include_directories(editor_ui_scroll_view_basic_validation PRIVATE
${CMAKE_SOURCE_DIR}/new_editor/include
${CMAKE_SOURCE_DIR}/new_editor/app
${CMAKE_SOURCE_DIR}/engine/include
)
target_compile_definitions(editor_ui_scroll_view_basic_validation PRIVATE
UNICODE
_UNICODE
XCENGINE_EDITOR_UI_TESTS_REPO_ROOT="${XCENGINE_EDITOR_UI_TESTS_REPO_ROOT_PATH}"
)
if(MSVC)
target_compile_options(editor_ui_scroll_view_basic_validation PRIVATE /utf-8 /FS)
set_property(TARGET editor_ui_scroll_view_basic_validation PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(editor_ui_scroll_view_basic_validation PRIVATE
XCUIEditorLib
XCUIEditorHost
)
set_target_properties(editor_ui_scroll_view_basic_validation PROPERTIES
OUTPUT_NAME "XCUIEditorScrollViewBasicValidation"
)

View File

@@ -0,0 +1,759 @@
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <XCEditor/Core/UIEditorScrollViewInteraction.h>
#include <XCEditor/Widgets/UIEditorScrollView.h>
#include "Host/AutoScreenshot.h"
#include "Host/NativeRenderer.h"
#include <XCEngine/UI/DrawData.h>
#include <windows.h>
#include <windowsx.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#ifndef XCENGINE_EDITOR_UI_TESTS_REPO_ROOT
#define XCENGINE_EDITOR_UI_TESTS_REPO_ROOT "."
#endif
namespace {
using XCEngine::UI::UIColor;
using XCEngine::UI::UIDrawData;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Editor::Host::AutoScreenshotController;
using XCEngine::UI::Editor::Host::NativeRenderer;
using XCEngine::UI::Editor::UIEditorScrollViewInteractionFrame;
using XCEngine::UI::Editor::UIEditorScrollViewInteractionResult;
using XCEngine::UI::Editor::UIEditorScrollViewInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorScrollViewInteraction;
using XCEngine::UI::Editor::Widgets::AppendUIEditorScrollViewBackground;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorScrollView;
using XCEngine::UI::Editor::Widgets::ResolveUIEditorScrollViewContentOrigin;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewHitTarget;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewHitTargetKind;
constexpr const wchar_t* kWindowClassName = L"XCUIEditorScrollViewBasicValidation";
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | ScrollView Basic";
constexpr UIColor kWindowBg(0.13f, 0.13f, 0.13f, 1.0f);
constexpr UIColor kCardBg(0.18f, 0.18f, 0.18f, 1.0f);
constexpr UIColor kCardBorder(0.29f, 0.29f, 0.29f, 1.0f);
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
constexpr UIColor kTextMuted(0.72f, 0.72f, 0.72f, 1.0f);
constexpr UIColor kTextWeak(0.56f, 0.56f, 0.56f, 1.0f);
constexpr UIColor kTextSuccess(0.63f, 0.76f, 0.63f, 1.0f);
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
constexpr UIColor kButtonHoverBg(0.32f, 0.32f, 0.32f, 1.0f);
enum class ActionId : unsigned char {
Reset = 0,
Capture
};
struct ButtonLayout {
ActionId action = ActionId::Reset;
const char* label = "";
UIRect rect = {};
};
struct ScenarioLayout {
UIRect introRect = {};
UIRect controlRect = {};
UIRect stateRect = {};
UIRect previewRect = {};
UIRect scrollRect = {};
std::vector<ButtonLayout> buttons = {};
};
std::filesystem::path ResolveRepoRootPath() {
std::string root = XCENGINE_EDITOR_UI_TESTS_REPO_ROOT;
if (root.size() >= 2u && root.front() == '"' && root.back() == '"') {
root = root.substr(1u, root.size() - 2u);
}
return std::filesystem::path(root).lexically_normal();
}
bool ContainsPoint(const UIRect& rect, float x, float y) {
return x >= rect.x &&
x <= rect.x + rect.width &&
y >= rect.y &&
y <= rect.y + rect.height;
}
ScenarioLayout BuildScenarioLayout(float width, float height) {
constexpr float margin = 20.0f;
constexpr float leftWidth = 430.0f;
constexpr float gap = 16.0f;
ScenarioLayout layout = {};
layout.introRect = UIRect(margin, margin, leftWidth, 214.0f);
layout.controlRect = UIRect(margin, layout.introRect.y + layout.introRect.height + gap, leftWidth, 84.0f);
layout.stateRect = UIRect(
margin,
layout.controlRect.y + layout.controlRect.height + gap,
leftWidth,
(std::max)(200.0f, height - (layout.controlRect.y + layout.controlRect.height + gap) - margin));
layout.previewRect = UIRect(
leftWidth + margin * 2.0f,
margin,
(std::max)(420.0f, width - leftWidth - margin * 3.0f),
height - margin * 2.0f);
layout.scrollRect = UIRect(
layout.previewRect.x + 18.0f,
layout.previewRect.y + 64.0f,
layout.previewRect.width - 36.0f,
layout.previewRect.height - 84.0f);
const float buttonWidth = (layout.controlRect.width - 44.0f) * 0.5f;
const float buttonY = layout.controlRect.y + 32.0f;
layout.buttons = {
{ ActionId::Reset, "重置", UIRect(layout.controlRect.x + 14.0f, buttonY, buttonWidth, 36.0f) },
{ ActionId::Capture, "截图(F12)", UIRect(layout.controlRect.x + 26.0f + buttonWidth, buttonY, buttonWidth, 36.0f) }
};
return layout;
}
void DrawCard(
UIDrawList& drawList,
const UIRect& rect,
std::string_view title,
std::string_view subtitle = {}) {
drawList.AddFilledRect(rect, kCardBg, 10.0f);
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 10.0f);
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 14.0f), std::string(title), kTextPrimary, 17.0f);
if (!subtitle.empty()) {
drawList.AddText(UIPoint(rect.x + 16.0f, rect.y + 40.0f), std::string(subtitle), kTextMuted, 12.0f);
}
}
void DrawButton(
UIDrawList& drawList,
const ButtonLayout& button,
bool hovered) {
drawList.AddFilledRect(button.rect, hovered ? kButtonHoverBg : kButtonBg, 8.0f);
drawList.AddRectOutline(button.rect, kCardBorder, 1.0f, 8.0f);
drawList.AddText(
UIPoint(button.rect.x + 16.0f, button.rect.y + 10.0f),
button.label,
kTextPrimary,
12.0f);
}
std::vector<std::string> BuildLogLines() {
std::vector<std::string> lines = {
"Line 01 - ScrollView validation log",
"Line 02 - Hover inside the viewport, then use wheel",
"Line 03 - Offset should grow when wheel scrolls down",
"Line 04 - Offset should clamp at the bottom boundary",
"Line 05 - Drag the thumb to verify direct scrollbar control",
"Line 06 - Clicking empty content should only change focus",
"Line 07 - ScrollView is Editor foundation, not any business panel",
"Line 08 - PropertyGrid and ListView will reuse this viewport contract",
"Line 09 - Line spacing should remain clipped inside viewport",
"Line 10 - The right thumb should stay aligned to overflow",
"Line 11 - Extra row",
"Line 12 - Extra row",
"Line 13 - Extra row",
"Line 14 - Extra row",
"Line 15 - Extra row",
"Line 16 - Extra row",
};
for (int index = 11; index <= 40; ++index) {
lines.push_back(
"Line " +
std::string(index < 10 ? "0" : "") +
std::to_string(index) +
" - Extra row");
}
return lines;
}
std::string DescribeHitTarget(const UIEditorScrollViewHitTarget& hitTarget) {
switch (hitTarget.kind) {
case UIEditorScrollViewHitTargetKind::Content:
return "content";
case UIEditorScrollViewHitTargetKind::ScrollbarTrack:
return "scrollbar-track";
case UIEditorScrollViewHitTargetKind::ScrollbarThumb:
return "scrollbar-thumb";
case UIEditorScrollViewHitTargetKind::None:
default:
return "";
}
}
UIInputEvent MakePointerEvent(
UIInputEventType type,
const UIPoint& position,
UIPointerButton button = UIPointerButton::None) {
UIInputEvent event = {};
event.type = type;
event.position = position;
event.pointerButton = button;
return event;
}
UIInputEvent MakeWheelEvent(const UIPoint& position, float wheelDelta) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerWheel;
event.position = position;
event.wheelDelta = wheelDelta;
return event;
}
class ScenarioApp {
public:
int Run(HINSTANCE hInstance, int nCmdShow) {
if (!Initialize(hInstance, nCmdShow)) {
Shutdown();
return 1;
}
MSG message = {};
while (message.message != WM_QUIT) {
if (PeekMessageW(&message, nullptr, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessageW(&message);
continue;
}
RenderFrame();
Sleep(8);
}
Shutdown();
return static_cast<int>(message.wParam);
}
private:
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (message == WM_NCCREATE) {
const auto* createStruct = reinterpret_cast<CREATESTRUCTW*>(lParam);
auto* app = reinterpret_cast<ScenarioApp*>(createStruct->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
return TRUE;
}
auto* app = reinterpret_cast<ScenarioApp*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_SIZE:
if (app != nullptr && wParam != SIZE_MINIMIZED) {
app->OnResize(static_cast<UINT>(LOWORD(lParam)), static_cast<UINT>(HIWORD(lParam)));
}
return 0;
case WM_MOUSEMOVE:
if (app != nullptr) {
app->HandleMouseMove(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_MOUSELEAVE:
if (app != nullptr) {
app->HandleMouseLeave();
return 0;
}
break;
case WM_MOUSEWHEEL:
if (app != nullptr) {
POINT point = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
ScreenToClient(hwnd, &point);
app->HandleMouseWheel(
static_cast<float>(point.x),
static_cast<float>(point.y),
static_cast<float>(GET_WHEEL_DELTA_WPARAM(wParam)) / static_cast<float>(WHEEL_DELTA));
return 0;
}
break;
case WM_LBUTTONDOWN:
if (app != nullptr) {
app->HandleLeftButtonDown(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_LBUTTONUP:
if (app != nullptr) {
app->HandleLeftButtonUp(
static_cast<float>(GET_X_LPARAM(lParam)),
static_cast<float>(GET_Y_LPARAM(lParam)));
return 0;
}
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if (app != nullptr && wParam == VK_F12) {
app->m_autoScreenshot.RequestCapture("manual_f12");
app->m_lastResult = "已请求截图,输出到 captures/latest.png";
InvalidateRect(hwnd, nullptr, FALSE);
return 0;
}
break;
case WM_PAINT:
if (app != nullptr) {
PAINTSTRUCT paintStruct = {};
BeginPaint(hwnd, &paintStruct);
app->RenderFrame();
EndPaint(hwnd, &paintStruct);
return 0;
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
if (app != nullptr) {
app->m_hwnd = nullptr;
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
bool Initialize(HINSTANCE hInstance, int nCmdShow) {
WNDCLASSEXW windowClass = {};
windowClass.cbSize = sizeof(windowClass);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = &ScenarioApp::WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.lpszClassName = kWindowClassName;
m_windowClassAtom = RegisterClassExW(&windowClass);
if (m_windowClassAtom == 0) {
return false;
}
m_hwnd = CreateWindowExW(
0,
kWindowClassName,
kWindowTitle,
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1480,
920,
nullptr,
nullptr,
hInstance,
this);
if (m_hwnd == nullptr) {
return false;
}
ShowWindow(m_hwnd, nCmdShow);
UpdateWindow(m_hwnd);
if (!m_renderer.Initialize(m_hwnd)) {
return false;
}
m_captureRoot =
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/scroll_view_basic/captures";
m_autoScreenshot.Initialize(m_captureRoot);
ResetScenario();
return true;
}
void Shutdown() {
m_autoScreenshot.Shutdown();
m_renderer.Shutdown();
if (m_hwnd != nullptr && IsWindow(m_hwnd)) {
DestroyWindow(m_hwnd);
}
m_hwnd = nullptr;
if (m_windowClassAtom != 0) {
UnregisterClassW(kWindowClassName, GetModuleHandleW(nullptr));
m_windowClassAtom = 0;
}
}
ScenarioLayout GetLayout() const {
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
return BuildScenarioLayout(width, height);
}
float ResolveContentHeight() const {
constexpr float lineHeight = 28.0f;
constexpr float topPadding = 12.0f;
return topPadding * 2.0f + static_cast<float>(m_logLines.size()) * lineHeight;
}
void ResetScenario() {
m_logLines = BuildLogLines();
m_verticalOffset = 0.0f;
m_interactionState = {};
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hoveredAction = ActionId::Reset;
m_hasHoveredAction = false;
m_lastResult = "已重置到 " + std::to_string(m_logLines.size()) + " 行默认滚动位置";
RefreshScrollFrame();
}
void RefreshScrollFrame() {
if (m_hwnd == nullptr) {
return;
}
const ScenarioLayout layout = GetLayout();
m_scrollFrame =
UpdateUIEditorScrollViewInteraction(
m_interactionState,
m_verticalOffset,
layout.scrollRect,
ResolveContentHeight(),
{});
}
void OnResize(UINT width, UINT height) {
if (width == 0u || height == 0u) {
return;
}
m_renderer.Resize(width, height);
RefreshScrollFrame();
}
void HandleMouseMove(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
UpdateHoveredAction(layout, x, y);
TRACKMOUSEEVENT trackEvent = {};
trackEvent.cbSize = sizeof(trackEvent);
trackEvent.dwFlags = TME_LEAVE;
trackEvent.hwndTrack = m_hwnd;
TrackMouseEvent(&trackEvent);
PumpScrollEvents({ MakePointerEvent(UIInputEventType::PointerMove, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseLeave() {
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
m_hasHoveredAction = false;
PumpScrollEvents({ MakePointerEvent(UIInputEventType::PointerLeave, m_mousePosition) });
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleMouseWheel(float x, float y, float wheelDelta) {
m_mousePosition = UIPoint(x, y);
const UIEditorScrollViewInteractionResult result =
PumpScrollEvents({ MakeWheelEvent(m_mousePosition, wheelDelta) });
if (result.offsetChanged) {
m_lastResult = "滚轮滚动: offset 已更新";
}
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonDown(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
if (HitTestAction(layout, x, y) != nullptr) {
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorScrollViewInteractionResult result =
PumpScrollEvents({ MakePointerEvent(UIInputEventType::PointerButtonDown, m_mousePosition, UIPointerButton::Left) });
if (result.startedThumbDrag) {
SetCapture(m_hwnd);
}
UpdateResultText(result, ContainsPoint(layout.scrollRect, x, y));
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void HandleLeftButtonUp(float x, float y) {
m_mousePosition = UIPoint(x, y);
const ScenarioLayout layout = GetLayout();
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button != nullptr) {
ExecuteAction(button->action);
UpdateHoveredAction(layout, x, y);
InvalidateRect(m_hwnd, nullptr, FALSE);
return;
}
const UIEditorScrollViewInteractionResult result =
PumpScrollEvents({ MakePointerEvent(UIInputEventType::PointerButtonUp, m_mousePosition, UIPointerButton::Left) });
if (!m_interactionState.scrollViewState.draggingScrollbarThumb && GetCapture() == m_hwnd) {
ReleaseCapture();
}
UpdateResultText(result, ContainsPoint(layout.scrollRect, x, y));
InvalidateRect(m_hwnd, nullptr, FALSE);
}
void UpdateHoveredAction(const ScenarioLayout& layout, float x, float y) {
const ButtonLayout* button = HitTestAction(layout, x, y);
if (button == nullptr) {
m_hasHoveredAction = false;
return;
}
m_hoveredAction = button->action;
m_hasHoveredAction = true;
}
const ButtonLayout* HitTestAction(const ScenarioLayout& layout, float x, float y) const {
for (const ButtonLayout& button : layout.buttons) {
if (ContainsPoint(button.rect, x, y)) {
return &button;
}
}
return nullptr;
}
UIEditorScrollViewInteractionResult PumpScrollEvents(std::vector<UIInputEvent> events) {
const ScenarioLayout layout = GetLayout();
m_scrollFrame =
UpdateUIEditorScrollViewInteraction(
m_interactionState,
m_verticalOffset,
layout.scrollRect,
ResolveContentHeight(),
std::move(events));
return m_scrollFrame.result;
}
void UpdateResultText(
const UIEditorScrollViewInteractionResult& result,
bool insideScrollView) {
if (result.startedThumbDrag) {
m_lastResult = "开始拖拽 scrollbar thumb";
return;
}
if (result.endedThumbDrag) {
m_lastResult = "结束拖拽 scrollbar thumb";
return;
}
if (result.offsetChanged) {
m_lastResult = "滚动位置变化";
return;
}
if (result.focusChanged) {
m_lastResult = m_interactionState.scrollViewState.focused
? "ScrollView 获得 focus"
: "ScrollView focus 已清除";
return;
}
if (insideScrollView) {
m_lastResult = "点击内容区域: 只验证 focus / hover / scrollbar";
return;
}
m_lastResult = "等待交互";
}
void ExecuteAction(ActionId action) {
switch (action) {
case ActionId::Reset:
ResetScenario();
break;
case ActionId::Capture:
m_autoScreenshot.RequestCapture("manual_button");
m_lastResult = "已请求截图,输出到 captures/latest.png";
break;
}
}
void RenderFrame() {
if (m_hwnd == nullptr) {
return;
}
RECT clientRect = {};
GetClientRect(m_hwnd, &clientRect);
const float width = static_cast<float>((std::max)(1L, clientRect.right - clientRect.left));
const float height = static_cast<float>((std::max)(1L, clientRect.bottom - clientRect.top));
const ScenarioLayout layout = BuildScenarioLayout(width, height);
RefreshScrollFrame();
const UIEditorScrollViewHitTarget currentHit =
HitTestUIEditorScrollView(m_scrollFrame.layout, m_mousePosition);
UIDrawData drawData = {};
UIDrawList& drawList = drawData.EmplaceDrawList("EditorScrollViewBasic");
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
DrawCard(
drawList,
layout.introRect,
"这个测试在验证什么功能",
"只验证 Editor ScrollView 基础控件,不涉及任何业务面板。");
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 72.0f),
"1. 把鼠标放到右侧日志区内滚动滚轮内容应上下移动offset 应变化。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 94.0f),
"2. 继续滚到边界后 offset 要被 clamp不能越界。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 116.0f),
"3. 拖拽 scrollbar thumb内容位置应同步变化。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 138.0f),
"4. 点击内容区只改变 focus点击外部空白后 focus 应清除。",
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.introRect.x + 16.0f, layout.introRect.y + 160.0f),
"5. 按 F12 手动截图;设置 XCUI_AUTO_CAPTURE_ON_STARTUP=1 可自动截图。",
kTextPrimary,
12.0f);
DrawCard(drawList, layout.controlRect, "操作");
for (const ButtonLayout& button : layout.buttons) {
DrawButton(
drawList,
button,
m_hasHoveredAction && m_hoveredAction == button.action);
}
DrawCard(drawList, layout.stateRect, "状态摘要", "重点检查 hit / focus / thumb-drag / offset / overflow。");
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 70.0f),
"Hover: " + DescribeHitTarget(currentHit),
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 94.0f),
std::string("Focused: ") + (m_interactionState.scrollViewState.focused ? "" : ""),
kTextPrimary,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 118.0f),
std::string("Thumb Dragging: ") +
(m_interactionState.scrollViewState.draggingScrollbarThumb ? "" : ""),
kTextSuccess,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 142.0f),
"Offset: " + std::to_string(static_cast<int>(m_verticalOffset)) +
" / " + std::to_string(static_cast<int>(m_scrollFrame.layout.maxOffset)),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 166.0f),
std::string("Has Scrollbar: ") + (m_scrollFrame.layout.hasScrollbar ? "" : ""),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 190.0f),
"Lines: " + std::to_string(m_logLines.size()),
kTextMuted,
12.0f);
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 214.0f),
"Result: " + m_lastResult,
kTextPrimary,
12.0f);
const std::string captureSummary =
m_autoScreenshot.HasPendingCapture()
? "截图排队中..."
: (m_autoScreenshot.GetLastCaptureSummary().empty()
? std::string("F12 -> tests/UI/Editor/integration/shell/scroll_view_basic/captures/")
: m_autoScreenshot.GetLastCaptureSummary());
drawList.AddText(
UIPoint(layout.stateRect.x + 16.0f, layout.stateRect.y + 238.0f),
captureSummary,
kTextWeak,
12.0f);
DrawCard(drawList, layout.previewRect, "ScrollView 预览", "这里只放一个 ScrollView不混入任何上层业务内容。");
AppendUIEditorScrollViewBackground(
drawList,
m_scrollFrame.layout,
m_interactionState.scrollViewState);
const UIPoint contentOrigin = ResolveUIEditorScrollViewContentOrigin(m_scrollFrame.layout);
drawList.PushClipRect(m_scrollFrame.layout.contentRect);
for (std::size_t index = 0; index < m_logLines.size(); ++index) {
drawList.AddText(
UIPoint(
contentOrigin.x + 16.0f,
contentOrigin.y + 12.0f + static_cast<float>(index) * 28.0f),
m_logLines[index],
kTextPrimary,
12.0f);
}
drawList.PopClipRect();
const bool framePresented = m_renderer.Render(drawData);
m_autoScreenshot.CaptureIfRequested(
m_renderer,
drawData,
static_cast<unsigned int>(width),
static_cast<unsigned int>(height),
framePresented);
}
HWND m_hwnd = nullptr;
ATOM m_windowClassAtom = 0;
NativeRenderer m_renderer = {};
AutoScreenshotController m_autoScreenshot = {};
std::filesystem::path m_captureRoot = {};
std::vector<std::string> m_logLines = {};
UIEditorScrollViewInteractionState m_interactionState = {};
UIEditorScrollViewInteractionFrame m_scrollFrame = {};
float m_verticalOffset = 0.0f;
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
ActionId m_hoveredAction = ActionId::Reset;
bool m_hasHoveredAction = false;
std::string m_lastResult = {};
};
} // namespace
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow) {
return ScenarioApp().Run(hInstance, nCmdShow);
}

View File

@@ -12,12 +12,18 @@ set(EDITOR_UI_UNIT_TEST_SOURCES
test_ui_editor_panel_content_host.cpp
test_ui_editor_panel_host_lifecycle.cpp
test_ui_editor_panel_registry.cpp
test_ui_editor_property_grid.cpp
test_ui_editor_property_grid_interaction.cpp
test_ui_editor_shell_compose.cpp
test_ui_editor_shell_interaction.cpp
test_ui_editor_collection_primitives.cpp
test_ui_editor_dock_host.cpp
test_ui_editor_list_view.cpp
test_ui_editor_list_view_interaction.cpp
test_ui_editor_panel_chrome.cpp
test_ui_editor_panel_frame.cpp
test_ui_editor_scroll_view.cpp
test_ui_editor_scroll_view_interaction.cpp
test_ui_editor_status_bar.cpp
test_ui_editor_tab_strip.cpp
test_ui_editor_tree_view.cpp

View File

@@ -0,0 +1,113 @@
#include <gtest/gtest.h>
#include <XCEditor/Widgets/UIEditorListView.h>
namespace {
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIRect;
using XCEngine::UI::Widgets::UISelectionModel;
using XCEngine::UI::Editor::Widgets::AppendUIEditorListViewBackground;
using XCEngine::UI::Editor::Widgets::AppendUIEditorListViewForeground;
using XCEngine::UI::Editor::Widgets::BuildUIEditorListViewLayout;
using XCEngine::UI::Editor::Widgets::FindUIEditorListViewItemIndex;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorListView;
using XCEngine::UI::Editor::Widgets::UIEditorListViewHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorListViewItem;
using XCEngine::UI::Editor::Widgets::UIEditorListViewState;
bool ContainsTextCommand(
const XCEngine::UI::UIDrawData& drawData,
std::string_view text) {
for (const auto& drawList : drawData.GetDrawLists()) {
for (const auto& command : drawList.GetCommands()) {
if (command.type == XCEngine::UI::UIDrawCommandType::Text &&
command.text == text) {
return true;
}
}
}
return false;
}
std::vector<UIEditorListViewItem> BuildListItems() {
return {
{ "scene", "Scene.unity", "最近修改: 1 分钟前", 0.0f },
{ "material", "Metal.mat", "Material | 4 个属性", 0.0f },
{ "script", "PlayerController.cs", "", 0.0f }
};
}
} // namespace
TEST(UIEditorListViewTest, FindItemIndexReturnsMatchingStableIndex) {
const auto items = BuildListItems();
EXPECT_EQ(FindUIEditorListViewItemIndex(items, "scene"), 0u);
EXPECT_EQ(FindUIEditorListViewItemIndex(items, "material"), 1u);
EXPECT_EQ(FindUIEditorListViewItemIndex(items, "missing"), static_cast<std::size_t>(-1));
}
TEST(UIEditorListViewTest, LayoutBuildsRowsAndSecondaryTextRects) {
const auto items = BuildListItems();
const auto layout = BuildUIEditorListViewLayout(
UIRect(10.0f, 20.0f, 320.0f, 240.0f),
items);
ASSERT_EQ(layout.rowRects.size(), items.size());
EXPECT_EQ(layout.itemIndices[0], 0u);
EXPECT_EQ(layout.itemIndices[2], 2u);
EXPECT_FLOAT_EQ(layout.rowRects[0].x, 10.0f);
EXPECT_FLOAT_EQ(layout.rowRects[0].y, 20.0f);
EXPECT_FLOAT_EQ(layout.rowRects[1].y, 66.0f);
EXPECT_FLOAT_EQ(layout.primaryTextRects[0].x, 20.0f);
EXPECT_TRUE(layout.hasSecondaryText[0]);
EXPECT_TRUE(layout.hasSecondaryText[1]);
EXPECT_FALSE(layout.hasSecondaryText[2]);
EXPECT_GT(layout.secondaryTextRects[0].height, 0.0f);
EXPECT_FLOAT_EQ(layout.secondaryTextRects[2].height, 0.0f);
}
TEST(UIEditorListViewTest, HitTestResolvesRowsAndReturnsNoneOutsideBounds) {
const auto items = BuildListItems();
const auto layout = BuildUIEditorListViewLayout(
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items);
const auto hit = HitTestUIEditorListView(layout, UIPoint(80.0f, 72.0f));
EXPECT_EQ(hit.kind, UIEditorListViewHitTargetKind::Row);
EXPECT_EQ(hit.visibleIndex, 1u);
EXPECT_EQ(hit.itemIndex, 1u);
const auto miss = HitTestUIEditorListView(layout, UIPoint(360.0f, 20.0f));
EXPECT_EQ(miss.kind, UIEditorListViewHitTargetKind::None);
}
TEST(UIEditorListViewTest, BackgroundAndForegroundEmitStableCommands) {
const auto items = BuildListItems();
UISelectionModel selectionModel = {};
selectionModel.SetSelection("material");
UIEditorListViewState state = {};
state.focused = true;
state.hoveredItemId = "script";
const auto layout = BuildUIEditorListViewLayout(
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items);
XCEngine::UI::UIDrawData drawData = {};
auto& drawList = drawData.EmplaceDrawList("ListView");
AppendUIEditorListViewBackground(drawList, layout, items, selectionModel, state);
AppendUIEditorListViewForeground(drawList, layout, items);
const auto& commands = drawList.GetCommands();
ASSERT_GE(commands.size(), 7u);
EXPECT_EQ(commands[0].type, XCEngine::UI::UIDrawCommandType::FilledRect);
EXPECT_EQ(commands[1].type, XCEngine::UI::UIDrawCommandType::RectOutline);
EXPECT_EQ(commands[2].type, XCEngine::UI::UIDrawCommandType::FilledRect);
EXPECT_EQ(commands[3].type, XCEngine::UI::UIDrawCommandType::FilledRect);
EXPECT_TRUE(ContainsTextCommand(drawData, "Scene.unity"));
EXPECT_TRUE(ContainsTextCommand(drawData, "最近修改: 1 分钟前"));
}

View File

@@ -0,0 +1,232 @@
#include <gtest/gtest.h>
#include <XCEditor/Core/UIEditorListViewInteraction.h>
#include <XCEngine/Input/InputTypes.h>
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Widgets::UISelectionModel;
using XCEngine::UI::Editor::UIEditorListViewInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorListViewInteraction;
using XCEngine::UI::Editor::Widgets::UIEditorListViewHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorListViewItem;
std::vector<UIEditorListViewItem> BuildListItems() {
return {
{ "scene", "Scene.unity", "最近修改: 1 分钟前", 0.0f },
{ "material", "Metal.mat", "Material | 4 个属性", 0.0f },
{ "script", "PlayerController.cs", "", 0.0f },
{ "texture", "Checker.png", "Texture2D | 1024x1024", 0.0f }
};
}
UIInputEvent MakePointerMove(float x, float y) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerMove;
event.position = UIPoint(x, y);
return event;
}
UIInputEvent MakePointerDown(float x, float y, UIPointerButton button = UIPointerButton::Left) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerButtonDown;
event.position = UIPoint(x, y);
event.pointerButton = button;
return event;
}
UIInputEvent MakePointerUp(float x, float y, UIPointerButton button = UIPointerButton::Left) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerButtonUp;
event.position = UIPoint(x, y);
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyDown(KeyCode keyCode) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = static_cast<std::int32_t>(keyCode);
return event;
}
UIInputEvent MakePointerLeave() {
UIInputEvent event = {};
event.type = UIInputEventType::PointerLeave;
return event;
}
UIInputEvent MakeFocusLost() {
UIInputEvent event = {};
event.type = UIInputEventType::FocusLost;
return event;
}
UIPoint RectCenter(const XCEngine::UI::UIRect& rect) {
return UIPoint(rect.x + rect.width * 0.5f, rect.y + rect.height * 0.5f);
}
} // namespace
TEST(UIEditorListViewInteractionTest, PointerMoveUpdatesHoveredItemAndHitTarget) {
const auto items = BuildListItems();
UISelectionModel selectionModel = {};
UIEditorListViewInteractionState state = {};
const auto initialFrame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{});
const UIPoint materialCenter = RectCenter(initialFrame.layout.rowRects[1]);
const auto frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{ MakePointerMove(materialCenter.x, materialCenter.y) });
EXPECT_EQ(frame.result.hitTarget.kind, UIEditorListViewHitTargetKind::Row);
EXPECT_EQ(frame.result.hitTarget.itemIndex, 1u);
EXPECT_EQ(state.listViewState.hoveredItemId, "material");
}
TEST(UIEditorListViewInteractionTest, LeftClickRowSelectsItemAndFocusesList) {
const auto items = BuildListItems();
UISelectionModel selectionModel = {};
UIEditorListViewInteractionState state = {};
const auto initialFrame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{});
const UIPoint scriptCenter = RectCenter(initialFrame.layout.rowRects[2]);
const auto frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{
MakePointerDown(scriptCenter.x, scriptCenter.y),
MakePointerUp(scriptCenter.x, scriptCenter.y)
});
EXPECT_TRUE(frame.result.consumed);
EXPECT_TRUE(frame.result.selectionChanged);
EXPECT_EQ(frame.result.selectedItemId, "script");
EXPECT_EQ(frame.result.selectedIndex, 2u);
EXPECT_TRUE(selectionModel.IsSelected("script"));
EXPECT_TRUE(state.listViewState.focused);
EXPECT_TRUE(state.keyboardNavigation.HasCurrentIndex());
EXPECT_EQ(state.keyboardNavigation.GetCurrentIndex(), 2u);
}
TEST(UIEditorListViewInteractionTest, RightClickRowSelectsItemAndMarksSecondaryClick) {
const auto items = BuildListItems();
UISelectionModel selectionModel = {};
UIEditorListViewInteractionState state = {};
const auto initialFrame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{});
const UIPoint textureCenter = RectCenter(initialFrame.layout.rowRects[3]);
const auto frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{
MakePointerDown(textureCenter.x, textureCenter.y, UIPointerButton::Right),
MakePointerUp(textureCenter.x, textureCenter.y, UIPointerButton::Right)
});
EXPECT_TRUE(frame.result.consumed);
EXPECT_TRUE(frame.result.secondaryClicked);
EXPECT_EQ(frame.result.selectedItemId, "texture");
EXPECT_TRUE(selectionModel.IsSelected("texture"));
EXPECT_TRUE(state.listViewState.focused);
}
TEST(UIEditorListViewInteractionTest, ArrowAndHomeEndKeysDriveSelectionWhenFocused) {
const auto items = BuildListItems();
UISelectionModel selectionModel = {};
selectionModel.SetSelection("material");
UIEditorListViewInteractionState state = {};
state.listViewState.focused = true;
auto frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{ MakeKeyDown(KeyCode::Down) });
EXPECT_TRUE(frame.result.keyboardNavigated);
EXPECT_EQ(frame.result.selectedItemId, "script");
EXPECT_TRUE(selectionModel.IsSelected("script"));
frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{ MakeKeyDown(KeyCode::Home) });
EXPECT_TRUE(frame.result.keyboardNavigated);
EXPECT_EQ(frame.result.selectedItemId, "scene");
EXPECT_TRUE(selectionModel.IsSelected("scene"));
frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{ MakeKeyDown(KeyCode::End) });
EXPECT_TRUE(frame.result.keyboardNavigated);
EXPECT_EQ(frame.result.selectedItemId, "texture");
EXPECT_TRUE(selectionModel.IsSelected("texture"));
}
TEST(UIEditorListViewInteractionTest, OutsideClickAndFocusLostClearFocusAndHoverButKeepSelection) {
const auto items = BuildListItems();
UISelectionModel selectionModel = {};
selectionModel.SetSelection("material");
UIEditorListViewInteractionState state = {};
state.listViewState.focused = true;
state.listViewState.hoveredItemId = "material";
auto frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{ MakePointerDown(360.0f, 260.0f), MakePointerUp(360.0f, 260.0f) });
EXPECT_FALSE(state.listViewState.focused);
EXPECT_EQ(frame.result.hitTarget.kind, UIEditorListViewHitTargetKind::None);
EXPECT_TRUE(selectionModel.IsSelected("material"));
frame = UpdateUIEditorListViewInteraction(
state,
selectionModel,
UIRect(0.0f, 0.0f, 320.0f, 240.0f),
items,
{ MakePointerLeave(), MakeFocusLost() });
EXPECT_FALSE(state.listViewState.focused);
EXPECT_TRUE(state.listViewState.hoveredItemId.empty());
EXPECT_FALSE(state.hasPointerPosition);
EXPECT_TRUE(selectionModel.IsSelected("material"));
}

View File

@@ -0,0 +1,189 @@
#include <gtest/gtest.h>
#include <XCEditor/Widgets/UIEditorPropertyGrid.h>
namespace {
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIRect;
using XCEngine::UI::Widgets::UIExpansionModel;
using XCEngine::UI::Widgets::UIPropertyEditModel;
using XCEngine::UI::Widgets::UISelectionModel;
using XCEngine::UI::Editor::Widgets::AppendUIEditorPropertyGridBackground;
using XCEngine::UI::Editor::Widgets::AppendUIEditorPropertyGridForeground;
using XCEngine::UI::Editor::Widgets::BuildUIEditorPropertyGridLayout;
using XCEngine::UI::Editor::Widgets::FindUIEditorPropertyGridFieldLocation;
using XCEngine::UI::Editor::Widgets::FindUIEditorPropertyGridSectionIndex;
using XCEngine::UI::Editor::Widgets::FindUIEditorPropertyGridVisibleFieldIndex;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorPropertyGrid;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridField;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridSection;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridState;
bool ContainsTextCommand(
const XCEngine::UI::UIDrawData& drawData,
std::string_view text) {
for (const auto& drawList : drawData.GetDrawLists()) {
for (const auto& command : drawList.GetCommands()) {
if (command.type == XCEngine::UI::UIDrawCommandType::Text &&
command.text == text) {
return true;
}
}
}
return false;
}
std::vector<UIEditorPropertyGridSection> BuildSections() {
return {
{
"transform",
"Transform",
{
{ "position", "Position", "0, 0, 0", false, 0.0f },
{ "rotation", "Rotation", "0, 45, 0", false, 0.0f }
},
0.0f
},
{
"rendering",
"Rendering",
{
{ "material", "Material", "Metal", false, 0.0f },
{ "guid", "GUID", "asset-guid-001", true, 0.0f }
},
0.0f
}
};
}
UIPoint RectCenter(const XCEngine::UI::UIRect& rect) {
return UIPoint(rect.x + rect.width * 0.5f, rect.y + rect.height * 0.5f);
}
} // namespace
TEST(UIEditorPropertyGridTest, FindSectionAndFieldLocationReturnStableIndices) {
const auto sections = BuildSections();
EXPECT_EQ(FindUIEditorPropertyGridSectionIndex(sections, "transform"), 0u);
EXPECT_EQ(FindUIEditorPropertyGridSectionIndex(sections, "rendering"), 1u);
EXPECT_EQ(
FindUIEditorPropertyGridSectionIndex(sections, "missing"),
static_cast<std::size_t>(-1));
const auto materialLocation =
FindUIEditorPropertyGridFieldLocation(sections, "material");
EXPECT_TRUE(materialLocation.IsValid());
EXPECT_EQ(materialLocation.sectionIndex, 1u);
EXPECT_EQ(materialLocation.fieldIndex, 0u);
const auto missingLocation =
FindUIEditorPropertyGridFieldLocation(sections, "unknown");
EXPECT_FALSE(missingLocation.IsValid());
}
TEST(UIEditorPropertyGridTest, LayoutBuildsSectionHeadersAndVisibleFieldRects) {
const auto sections = BuildSections();
UIExpansionModel expansionModel = {};
expansionModel.Expand("transform");
const auto layout = BuildUIEditorPropertyGridLayout(
UIRect(10.0f, 20.0f, 420.0f, 240.0f),
sections,
expansionModel);
ASSERT_EQ(layout.sectionHeaderRects.size(), sections.size());
EXPECT_EQ(layout.visibleFieldIndices.size(), 2u);
EXPECT_EQ(layout.visibleFieldSectionIndices[0], 0u);
EXPECT_EQ(layout.visibleFieldIndices[1], 1u);
EXPECT_FLOAT_EQ(layout.sectionHeaderRects[0].x, 18.0f);
EXPECT_FLOAT_EQ(layout.sectionHeaderRects[0].y, 28.0f);
EXPECT_FLOAT_EQ(layout.fieldValueRects[0].x, 254.0f);
EXPECT_GT(layout.fieldValueRects[0].width, 0.0f);
EXPECT_EQ(
FindUIEditorPropertyGridVisibleFieldIndex(layout, "position", sections),
0u);
EXPECT_EQ(
FindUIEditorPropertyGridVisibleFieldIndex(layout, "material", sections),
static_cast<std::size_t>(-1));
}
TEST(UIEditorPropertyGridTest, HitTestResolvesHeaderRowAndValueBox) {
const auto sections = BuildSections();
UIExpansionModel expansionModel = {};
expansionModel.Expand("transform");
expansionModel.Expand("rendering");
const auto layout = BuildUIEditorPropertyGridLayout(
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
expansionModel);
const auto headerHit =
HitTestUIEditorPropertyGrid(layout, RectCenter(layout.sectionHeaderRects[0]));
EXPECT_EQ(headerHit.kind, UIEditorPropertyGridHitTargetKind::SectionHeader);
EXPECT_EQ(headerHit.sectionIndex, 0u);
const auto rowHit = HitTestUIEditorPropertyGrid(
layout,
UIPoint(
layout.fieldRowRects[1].x + 16.0f,
layout.fieldRowRects[1].y + layout.fieldRowRects[1].height * 0.5f));
EXPECT_EQ(rowHit.kind, UIEditorPropertyGridHitTargetKind::FieldRow);
EXPECT_EQ(rowHit.sectionIndex, 0u);
EXPECT_EQ(rowHit.fieldIndex, 1u);
const auto valueHit =
HitTestUIEditorPropertyGrid(layout, RectCenter(layout.fieldValueRects[2]));
EXPECT_EQ(valueHit.kind, UIEditorPropertyGridHitTargetKind::ValueBox);
EXPECT_EQ(valueHit.sectionIndex, 1u);
EXPECT_EQ(valueHit.fieldIndex, 0u);
}
TEST(UIEditorPropertyGridTest, BackgroundAndForegroundEmitStableCommands) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
selectionModel.SetSelection("rotation");
UIExpansionModel expansionModel = {};
expansionModel.Expand("transform");
expansionModel.Expand("rendering");
UIPropertyEditModel propertyEditModel = {};
propertyEditModel.BeginEdit("material", "Metal");
propertyEditModel.UpdateStagedValue("Mat_Inst");
UIEditorPropertyGridState state = {};
state.focused = true;
state.hoveredFieldId = "position";
const auto layout = BuildUIEditorPropertyGridLayout(
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
expansionModel);
XCEngine::UI::UIDrawData drawData = {};
auto& drawList = drawData.EmplaceDrawList("PropertyGrid");
AppendUIEditorPropertyGridBackground(
drawList,
layout,
sections,
selectionModel,
propertyEditModel,
state);
AppendUIEditorPropertyGridForeground(
drawList,
layout,
sections,
propertyEditModel);
const auto& commands = drawList.GetCommands();
ASSERT_GE(commands.size(), 12u);
EXPECT_EQ(commands[0].type, XCEngine::UI::UIDrawCommandType::FilledRect);
EXPECT_EQ(commands[1].type, XCEngine::UI::UIDrawCommandType::RectOutline);
EXPECT_TRUE(ContainsTextCommand(drawData, "Transform"));
EXPECT_TRUE(ContainsTextCommand(drawData, "Position"));
EXPECT_TRUE(ContainsTextCommand(drawData, "Mat_Inst"));
EXPECT_TRUE(ContainsTextCommand(drawData, "EDIT"));
}

View File

@@ -0,0 +1,368 @@
#include <gtest/gtest.h>
#include <XCEditor/Core/UIEditorPropertyGridInteraction.h>
#include <XCEngine/Input/InputTypes.h>
namespace {
using XCEngine::Input::KeyCode;
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Widgets::UIExpansionModel;
using XCEngine::UI::Widgets::UIPropertyEditModel;
using XCEngine::UI::Widgets::UISelectionModel;
using XCEngine::UI::Editor::UIEditorPropertyGridInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorPropertyGridInteraction;
using XCEngine::UI::Editor::Widgets::UIEditorPropertyGridSection;
std::vector<UIEditorPropertyGridSection> BuildSections() {
return {
{
"transform",
"Transform",
{
{ "position", "Position", "10", false, 0.0f },
{ "rotation", "Rotation", "0", false, 0.0f }
},
0.0f
},
{
"metadata",
"Metadata",
{
{ "tag", "Tag", "", false, 0.0f },
{ "guid", "GUID", "asset-guid-001", true, 0.0f }
},
0.0f
}
};
}
UIInputEvent MakePointerMove(float x, float y) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerMove;
event.position = UIPoint(x, y);
return event;
}
UIInputEvent MakePointerDown(float x, float y, UIPointerButton button = UIPointerButton::Left) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerButtonDown;
event.position = UIPoint(x, y);
event.pointerButton = button;
return event;
}
UIInputEvent MakePointerUp(float x, float y, UIPointerButton button = UIPointerButton::Left) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerButtonUp;
event.position = UIPoint(x, y);
event.pointerButton = button;
return event;
}
UIInputEvent MakeKeyDown(KeyCode keyCode) {
UIInputEvent event = {};
event.type = UIInputEventType::KeyDown;
event.keyCode = static_cast<std::int32_t>(keyCode);
return event;
}
UIInputEvent MakeCharacter(char character) {
UIInputEvent event = {};
event.type = UIInputEventType::Character;
event.character = static_cast<std::uint32_t>(character);
return event;
}
UIPoint RectCenter(const XCEngine::UI::UIRect& rect) {
return UIPoint(rect.x + rect.width * 0.5f, rect.y + rect.height * 0.5f);
}
void ExpandAll(UIExpansionModel& expansionModel) {
expansionModel.Expand("transform");
expansionModel.Expand("metadata");
}
} // namespace
TEST(UIEditorPropertyGridInteractionTest, PointerMoveUpdatesHoveredSectionAndField) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
UIExpansionModel expansionModel = {};
ExpandAll(expansionModel);
UIPropertyEditModel propertyEditModel = {};
UIEditorPropertyGridInteractionState state = {};
const auto initialFrame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{});
auto frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{ MakePointerMove(
initialFrame.layout.sectionHeaderRects[0].x + 24.0f,
initialFrame.layout.sectionHeaderRects[0].y + 16.0f) });
EXPECT_EQ(state.propertyGridState.hoveredSectionId, "transform");
EXPECT_TRUE(state.propertyGridState.hoveredFieldId.empty());
frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{ MakePointerMove(
initialFrame.layout.fieldRowRects[1].x + 16.0f,
initialFrame.layout.fieldRowRects[1].y + 16.0f) });
EXPECT_EQ(frame.result.hitTarget.fieldIndex, 1u);
EXPECT_EQ(state.propertyGridState.hoveredFieldId, "rotation");
}
TEST(UIEditorPropertyGridInteractionTest, LeftClickSectionHeaderTogglesExpansion) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
UIExpansionModel expansionModel = {};
expansionModel.Expand("transform");
UIPropertyEditModel propertyEditModel = {};
UIEditorPropertyGridInteractionState state = {};
const auto initialFrame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{});
const UIPoint metadataHeaderCenter = RectCenter(initialFrame.layout.sectionHeaderRects[1]);
const auto frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakePointerDown(metadataHeaderCenter.x, metadataHeaderCenter.y),
MakePointerUp(metadataHeaderCenter.x, metadataHeaderCenter.y)
});
EXPECT_TRUE(frame.result.sectionToggled);
EXPECT_EQ(frame.result.toggledSectionId, "metadata");
EXPECT_TRUE(expansionModel.IsExpanded("metadata"));
EXPECT_TRUE(state.propertyGridState.focused);
}
TEST(UIEditorPropertyGridInteractionTest, LeftClickFieldRowSelectsFieldAndFocusesGrid) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
UIExpansionModel expansionModel = {};
ExpandAll(expansionModel);
UIPropertyEditModel propertyEditModel = {};
UIEditorPropertyGridInteractionState state = {};
const auto initialFrame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{});
const UIPoint rowCenter = RectCenter(initialFrame.layout.fieldRowRects[1]);
const auto frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakePointerDown(rowCenter.x, rowCenter.y),
MakePointerUp(rowCenter.x, rowCenter.y)
});
EXPECT_TRUE(frame.result.consumed);
EXPECT_TRUE(frame.result.selectionChanged);
EXPECT_EQ(frame.result.selectedFieldId, "rotation");
EXPECT_TRUE(selectionModel.IsSelected("rotation"));
EXPECT_TRUE(state.propertyGridState.focused);
}
TEST(UIEditorPropertyGridInteractionTest, ValueBoxEditCanCommitWithEnter) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
UIExpansionModel expansionModel = {};
ExpandAll(expansionModel);
UIPropertyEditModel propertyEditModel = {};
UIEditorPropertyGridInteractionState state = {};
const auto initialFrame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{});
const UIPoint tagValueCenter = RectCenter(initialFrame.layout.fieldValueRects[2]);
auto frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakePointerDown(tagValueCenter.x, tagValueCenter.y),
MakePointerUp(tagValueCenter.x, tagValueCenter.y)
});
EXPECT_TRUE(frame.result.consumed);
EXPECT_EQ(frame.result.selectedFieldId, "tag");
EXPECT_EQ(frame.result.activeFieldId, "tag");
EXPECT_TRUE(propertyEditModel.HasActiveEdit());
frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakeCharacter('A'),
MakeCharacter('B'),
MakeKeyDown(KeyCode::Enter)
});
EXPECT_TRUE(frame.result.editCommitted);
EXPECT_EQ(frame.result.committedFieldId, "tag");
EXPECT_EQ(frame.result.committedValue, "AB");
EXPECT_FALSE(propertyEditModel.HasActiveEdit());
}
TEST(UIEditorPropertyGridInteractionTest, EscapeCancelsEditAndOutsideClickClearsFocus) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
UIExpansionModel expansionModel = {};
ExpandAll(expansionModel);
UIPropertyEditModel propertyEditModel = {};
UIEditorPropertyGridInteractionState state = {};
const auto initialFrame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{});
const UIPoint tagValueCenter = RectCenter(initialFrame.layout.fieldValueRects[2]);
UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakePointerDown(tagValueCenter.x, tagValueCenter.y),
MakePointerUp(tagValueCenter.x, tagValueCenter.y)
});
auto frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakeCharacter('A'),
MakeKeyDown(KeyCode::Escape)
});
EXPECT_TRUE(frame.result.editCanceled);
EXPECT_FALSE(propertyEditModel.HasActiveEdit());
EXPECT_TRUE(selectionModel.IsSelected("tag"));
frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{
MakePointerDown(520.0f, 360.0f),
MakePointerUp(520.0f, 360.0f)
});
EXPECT_FALSE(state.propertyGridState.focused);
EXPECT_TRUE(selectionModel.IsSelected("tag"));
}
TEST(UIEditorPropertyGridInteractionTest, ArrowAndHomeEndKeysNavigateVisibleFields) {
const auto sections = BuildSections();
UISelectionModel selectionModel = {};
selectionModel.SetSelection("rotation");
UIExpansionModel expansionModel = {};
ExpandAll(expansionModel);
UIPropertyEditModel propertyEditModel = {};
UIEditorPropertyGridInteractionState state = {};
state.propertyGridState.focused = true;
auto frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{ MakeKeyDown(KeyCode::Down) });
EXPECT_TRUE(frame.result.keyboardNavigated);
EXPECT_EQ(frame.result.selectedFieldId, "tag");
EXPECT_TRUE(selectionModel.IsSelected("tag"));
frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{ MakeKeyDown(KeyCode::Home) });
EXPECT_TRUE(frame.result.keyboardNavigated);
EXPECT_EQ(frame.result.selectedFieldId, "position");
EXPECT_TRUE(selectionModel.IsSelected("position"));
frame = UpdateUIEditorPropertyGridInteraction(
state,
selectionModel,
expansionModel,
propertyEditModel,
UIRect(0.0f, 0.0f, 420.0f, 320.0f),
sections,
{ MakeKeyDown(KeyCode::End) });
EXPECT_TRUE(frame.result.keyboardNavigated);
EXPECT_EQ(frame.result.selectedFieldId, "guid");
EXPECT_TRUE(selectionModel.IsSelected("guid"));
}

View File

@@ -0,0 +1,101 @@
#include <gtest/gtest.h>
#include <XCEngine/UI/DrawData.h>
#include <XCEditor/Widgets/UIEditorScrollView.h>
namespace {
using XCEngine::UI::UIDrawCommandType;
using XCEngine::UI::UIDrawList;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIRect;
using XCEngine::UI::Editor::Widgets::AppendUIEditorScrollViewBackground;
using XCEngine::UI::Editor::Widgets::BuildUIEditorScrollViewLayout;
using XCEngine::UI::Editor::Widgets::ClampUIEditorScrollViewOffset;
using XCEngine::UI::Editor::Widgets::HitTestUIEditorScrollView;
using XCEngine::UI::Editor::Widgets::ResolveUIEditorScrollViewContentOrigin;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewHitTargetKind;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewLayout;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewMetrics;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewState;
TEST(UIEditorScrollViewTest, LayoutReservesScrollbarTrackAndThumbFromOverflow) {
UIEditorScrollViewMetrics metrics = {};
metrics.scrollbarWidth = 12.0f;
metrics.scrollbarInset = 4.0f;
metrics.minThumbHeight = 24.0f;
const UIEditorScrollViewLayout layout =
BuildUIEditorScrollViewLayout(UIRect(10.0f, 20.0f, 240.0f, 120.0f), 360.0f, 60.0f, metrics);
EXPECT_TRUE(layout.hasScrollbar);
EXPECT_FLOAT_EQ(layout.maxOffset, 240.0f);
EXPECT_FLOAT_EQ(layout.verticalOffset, 60.0f);
EXPECT_FLOAT_EQ(layout.contentRect.width, 220.0f);
EXPECT_FLOAT_EQ(layout.scrollbarTrackRect.x, 234.0f);
EXPECT_FLOAT_EQ(layout.scrollbarTrackRect.height, 112.0f);
EXPECT_NEAR(layout.scrollbarThumbRect.y, 42.666f, 0.01f);
EXPECT_NEAR(layout.scrollbarThumbRect.height, 37.333f, 0.01f);
const UIPoint contentOrigin = ResolveUIEditorScrollViewContentOrigin(layout);
EXPECT_FLOAT_EQ(contentOrigin.x, layout.contentRect.x);
EXPECT_FLOAT_EQ(contentOrigin.y, layout.contentRect.y - 60.0f);
}
TEST(UIEditorScrollViewTest, ClampAndNoScrollbarWhenContentFitsViewport) {
const UIEditorScrollViewLayout layout =
BuildUIEditorScrollViewLayout(UIRect(0.0f, 0.0f, 200.0f, 100.0f), 80.0f, 42.0f);
EXPECT_FALSE(layout.hasScrollbar);
EXPECT_FLOAT_EQ(layout.maxOffset, 0.0f);
EXPECT_FLOAT_EQ(layout.verticalOffset, 0.0f);
EXPECT_FLOAT_EQ(layout.contentRect.width, 200.0f);
EXPECT_FLOAT_EQ(
ClampUIEditorScrollViewOffset(UIRect(0.0f, 0.0f, 200.0f, 100.0f), 320.0f, 400.0f),
220.0f);
}
TEST(UIEditorScrollViewTest, HitTestPrioritizesThumbThenTrackThenContent) {
const UIEditorScrollViewLayout layout =
BuildUIEditorScrollViewLayout(UIRect(10.0f, 20.0f, 240.0f, 120.0f), 360.0f, 60.0f);
EXPECT_EQ(
HitTestUIEditorScrollView(
layout,
UIPoint(
layout.scrollbarThumbRect.x + 4.0f,
layout.scrollbarThumbRect.y + 4.0f)).kind,
UIEditorScrollViewHitTargetKind::ScrollbarThumb);
EXPECT_EQ(
HitTestUIEditorScrollView(
layout,
UIPoint(
layout.scrollbarTrackRect.x + 4.0f,
layout.scrollbarTrackRect.y + layout.scrollbarTrackRect.height - 4.0f)).kind,
UIEditorScrollViewHitTargetKind::ScrollbarTrack);
EXPECT_EQ(
HitTestUIEditorScrollView(layout, UIPoint(layout.contentRect.x + 12.0f, layout.contentRect.y + 12.0f)).kind,
UIEditorScrollViewHitTargetKind::Content);
EXPECT_EQ(
HitTestUIEditorScrollView(layout, UIPoint(4.0f, 8.0f)).kind,
UIEditorScrollViewHitTargetKind::None);
}
TEST(UIEditorScrollViewTest, BackgroundCommandsEmitStableChrome) {
const UIEditorScrollViewLayout layout =
BuildUIEditorScrollViewLayout(UIRect(0.0f, 0.0f, 220.0f, 120.0f), 320.0f, 80.0f);
UIEditorScrollViewState state = {};
state.focused = true;
state.scrollbarHovered = true;
UIDrawList drawList("ScrollView");
AppendUIEditorScrollViewBackground(drawList, layout, state);
ASSERT_EQ(drawList.GetCommandCount(), 4u);
EXPECT_EQ(drawList.GetCommands()[0].type, UIDrawCommandType::FilledRect);
EXPECT_EQ(drawList.GetCommands()[1].type, UIDrawCommandType::RectOutline);
EXPECT_EQ(drawList.GetCommands()[2].type, UIDrawCommandType::FilledRect);
EXPECT_EQ(drawList.GetCommands()[3].type, UIDrawCommandType::FilledRect);
}
} // namespace

View File

@@ -0,0 +1,176 @@
#include <gtest/gtest.h>
#include <XCEditor/Core/UIEditorScrollViewInteraction.h>
namespace {
using XCEngine::UI::UIInputEvent;
using XCEngine::UI::UIInputEventType;
using XCEngine::UI::UIPoint;
using XCEngine::UI::UIPointerButton;
using XCEngine::UI::UIRect;
using XCEngine::UI::Editor::UIEditorScrollViewInteractionState;
using XCEngine::UI::Editor::UpdateUIEditorScrollViewInteraction;
using XCEngine::UI::Editor::Widgets::UIEditorScrollViewHitTargetKind;
UIInputEvent MakePointerMove(float x, float y) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerMove;
event.position = UIPoint(x, y);
return event;
}
UIInputEvent MakePointerDown(float x, float y) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerButtonDown;
event.position = UIPoint(x, y);
event.pointerButton = UIPointerButton::Left;
return event;
}
UIInputEvent MakePointerUp(float x, float y) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerButtonUp;
event.position = UIPoint(x, y);
event.pointerButton = UIPointerButton::Left;
return event;
}
UIInputEvent MakePointerWheel(float x, float y, float wheelDelta) {
UIInputEvent event = {};
event.type = UIInputEventType::PointerWheel;
event.position = UIPoint(x, y);
event.wheelDelta = wheelDelta;
return event;
}
UIInputEvent MakeFocusLost() {
UIInputEvent event = {};
event.type = UIInputEventType::FocusLost;
return event;
}
TEST(UIEditorScrollViewInteractionTest, WheelInsideViewportUpdatesOffsetAndClamps) {
UIEditorScrollViewInteractionState state = {};
float verticalOffset = 0.0f;
auto frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 200.0f, 100.0f),
360.0f,
{ MakePointerWheel(24.0f, 32.0f, -1.0f) });
EXPECT_TRUE(frame.result.consumed);
EXPECT_TRUE(frame.result.offsetChanged);
EXPECT_FLOAT_EQ(verticalOffset, 48.0f);
EXPECT_EQ(frame.result.hitTarget.kind, UIEditorScrollViewHitTargetKind::Content);
frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 200.0f, 100.0f),
360.0f,
{ MakePointerWheel(24.0f, 32.0f, -20.0f) });
EXPECT_TRUE(frame.result.offsetChanged);
EXPECT_FLOAT_EQ(verticalOffset, 260.0f);
}
TEST(UIEditorScrollViewInteractionTest, ThumbDragUpdatesOffsetAcrossPointerMoves) {
UIEditorScrollViewInteractionState state = {};
float verticalOffset = 0.0f;
const auto initialFrame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{});
const float thumbX = initialFrame.layout.scrollbarThumbRect.x + 4.0f;
const float thumbY = initialFrame.layout.scrollbarThumbRect.y + 8.0f;
auto frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{ MakePointerDown(thumbX, thumbY) });
EXPECT_TRUE(frame.result.startedThumbDrag);
EXPECT_TRUE(state.scrollViewState.draggingScrollbarThumb);
frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{ MakePointerMove(thumbX, thumbY + 40.0f) });
EXPECT_TRUE(frame.result.offsetChanged);
EXPECT_GT(verticalOffset, 0.0f);
frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{ MakePointerUp(thumbX, thumbY + 40.0f) });
EXPECT_TRUE(frame.result.endedThumbDrag);
EXPECT_FALSE(state.scrollViewState.draggingScrollbarThumb);
}
TEST(UIEditorScrollViewInteractionTest, ContentClickFocusesAndOutsideClickClearsFocus) {
UIEditorScrollViewInteractionState state = {};
float verticalOffset = 0.0f;
auto frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(10.0f, 20.0f, 220.0f, 120.0f),
420.0f,
{ MakePointerDown(36.0f, 48.0f) });
EXPECT_TRUE(frame.result.focusChanged);
EXPECT_TRUE(state.scrollViewState.focused);
EXPECT_EQ(frame.result.hitTarget.kind, UIEditorScrollViewHitTargetKind::Content);
frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(10.0f, 20.0f, 220.0f, 120.0f),
420.0f,
{ MakePointerDown(300.0f, 220.0f) });
EXPECT_TRUE(frame.result.focusChanged);
EXPECT_FALSE(state.scrollViewState.focused);
}
TEST(UIEditorScrollViewInteractionTest, FocusLostClearsHoverAndThumbDragState) {
UIEditorScrollViewInteractionState state = {};
float verticalOffset = 0.0f;
const auto initialFrame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{});
const float thumbX = initialFrame.layout.scrollbarThumbRect.x + 4.0f;
const float thumbY = initialFrame.layout.scrollbarThumbRect.y + 8.0f;
UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{ MakePointerDown(thumbX, thumbY) });
ASSERT_TRUE(state.scrollViewState.draggingScrollbarThumb);
const auto frame = UpdateUIEditorScrollViewInteraction(
state,
verticalOffset,
UIRect(0.0f, 0.0f, 220.0f, 120.0f),
420.0f,
{ MakeFocusLost() });
EXPECT_TRUE(frame.result.focusChanged);
EXPECT_FALSE(state.scrollViewState.focused);
EXPECT_FALSE(state.scrollViewState.draggingScrollbarThumb);
EXPECT_FALSE(state.hasPointerPosition);
}
} // namespace