Stabilize XCEditor shell foundation widgets
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
add_executable(editor_ui_tab_strip_basic_validation WIN32
|
||||
main.cpp
|
||||
)
|
||||
|
||||
target_include_directories(editor_ui_tab_strip_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_tab_strip_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_tab_strip_basic_validation PRIVATE /utf-8 /FS)
|
||||
set_property(TARGET editor_ui_tab_strip_basic_validation PROPERTY
|
||||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
|
||||
endif()
|
||||
|
||||
target_link_libraries(editor_ui_tab_strip_basic_validation PRIVATE
|
||||
XCUIEditorLib
|
||||
XCUIEditorHost
|
||||
)
|
||||
|
||||
set_target_properties(editor_ui_tab_strip_basic_validation PROPERTIES
|
||||
OUTPUT_NAME "XCUIEditorTabStripBasicValidation"
|
||||
)
|
||||
740
tests/UI/Editor/integration/shell/tab_strip_basic/main.cpp
Normal file
740
tests/UI/Editor/integration/shell/tab_strip_basic/main.cpp
Normal file
@@ -0,0 +1,740 @@
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#include <XCEditor/Core/UIEditorPanelRegistry.h>
|
||||
#include <XCEditor/Core/UIEditorWorkspaceController.h>
|
||||
#include <XCEditor/Widgets/UIEditorTabStrip.h>
|
||||
#include "Host/AutoScreenshot.h"
|
||||
#include "Host/NativeRenderer.h"
|
||||
|
||||
#include <XCEngine/UI/DrawData.h>
|
||||
#include <XCEngine/UI/Widgets/UITabStripModel.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <windowsx.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#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::UIPoint;
|
||||
using XCEngine::UI::UIRect;
|
||||
using XCEngine::UI::Widgets::UITabStripModel;
|
||||
using XCEngine::UI::Editor::BuildDefaultUIEditorWorkspaceController;
|
||||
using XCEngine::UI::Editor::BuildUIEditorWorkspacePanel;
|
||||
using XCEngine::UI::Editor::BuildUIEditorWorkspaceTabStack;
|
||||
using XCEngine::UI::Editor::FindUIEditorPanelDescriptor;
|
||||
using XCEngine::UI::Editor::FindUIEditorPanelSessionState;
|
||||
using XCEngine::UI::Editor::GetUIEditorWorkspaceCommandStatusName;
|
||||
using XCEngine::UI::Editor::Host::AutoScreenshotController;
|
||||
using XCEngine::UI::Editor::Host::NativeRenderer;
|
||||
using XCEngine::UI::Editor::UIEditorPanelDescriptor;
|
||||
using XCEngine::UI::Editor::UIEditorPanelRegistry;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceCommand;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceCommandKind;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceCommandResult;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceController;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceModel;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceNode;
|
||||
using XCEngine::UI::Editor::UIEditorWorkspaceNodeKind;
|
||||
using XCEngine::UI::Editor::Widgets::AppendUIEditorTabStripBackground;
|
||||
using XCEngine::UI::Editor::Widgets::AppendUIEditorTabStripForeground;
|
||||
using XCEngine::UI::Editor::Widgets::BuildUIEditorTabStripLayout;
|
||||
using XCEngine::UI::Editor::Widgets::HitTestUIEditorTabStrip;
|
||||
using XCEngine::UI::Editor::Widgets::ResolveUIEditorTabStripSelectedIndex;
|
||||
using XCEngine::UI::Editor::Widgets::UIEditorTabStripHitTarget;
|
||||
using XCEngine::UI::Editor::Widgets::UIEditorTabStripHitTargetKind;
|
||||
using XCEngine::UI::Editor::Widgets::UIEditorTabStripInvalidIndex;
|
||||
using XCEngine::UI::Editor::Widgets::UIEditorTabStripItem;
|
||||
using XCEngine::UI::Editor::Widgets::UIEditorTabStripLayout;
|
||||
using XCEngine::UI::Editor::Widgets::UIEditorTabStripState;
|
||||
|
||||
constexpr const wchar_t* kWindowClassName = L"XCUIEditorTabStripBasicValidation";
|
||||
constexpr const wchar_t* kWindowTitle = L"XCUI Editor | TabStrip Basic";
|
||||
|
||||
constexpr UIColor kWindowBg(0.15f, 0.15f, 0.15f, 1.0f);
|
||||
constexpr UIColor kCardBg(0.19f, 0.19f, 0.19f, 1.0f);
|
||||
constexpr UIColor kCardBorder(0.30f, 0.30f, 0.30f, 1.0f);
|
||||
constexpr UIColor kTextPrimary(0.94f, 0.94f, 0.94f, 1.0f);
|
||||
constexpr UIColor kTextMuted(0.73f, 0.73f, 0.73f, 1.0f);
|
||||
constexpr UIColor kTextWeak(0.58f, 0.58f, 0.58f, 1.0f);
|
||||
constexpr UIColor kSuccess(0.62f, 0.74f, 0.62f, 1.0f);
|
||||
constexpr UIColor kWarning(0.78f, 0.70f, 0.46f, 1.0f);
|
||||
constexpr UIColor kDanger(0.82f, 0.48f, 0.48f, 1.0f);
|
||||
constexpr UIColor kButtonBg(0.25f, 0.25f, 0.25f, 1.0f);
|
||||
constexpr UIColor kButtonHoveredBg(0.31f, 0.31f, 0.31f, 1.0f);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 UIRect& rect,
|
||||
std::string_view label,
|
||||
bool hovered) {
|
||||
drawList.AddFilledRect(rect, hovered ? kButtonHoveredBg : kButtonBg, 8.0f);
|
||||
drawList.AddRectOutline(rect, kCardBorder, 1.0f, 8.0f);
|
||||
drawList.AddText(UIPoint(rect.x + 14.0f, rect.y + 11.0f), std::string(label), kTextPrimary, 13.0f);
|
||||
}
|
||||
|
||||
UIEditorPanelRegistry BuildPanelRegistry() {
|
||||
UIEditorPanelRegistry registry = {};
|
||||
registry.panels = {
|
||||
{ "doc-a", "Document A", {}, true, true, true },
|
||||
{ "doc-b", "Document B", {}, true, true, true },
|
||||
{ "doc-c", "Document C", {}, true, true, false }
|
||||
};
|
||||
return registry;
|
||||
}
|
||||
|
||||
UIEditorWorkspaceModel BuildWorkspace() {
|
||||
UIEditorWorkspaceModel workspace = {};
|
||||
workspace.root = BuildUIEditorWorkspaceTabStack(
|
||||
"document-tabs",
|
||||
{
|
||||
BuildUIEditorWorkspacePanel("doc-a-node", "doc-a", "Document A", true),
|
||||
BuildUIEditorWorkspacePanel("doc-b-node", "doc-b", "Document B", true),
|
||||
BuildUIEditorWorkspacePanel("doc-c-node", "doc-c", "Document C", true)
|
||||
},
|
||||
0u);
|
||||
workspace.activePanelId = "doc-a";
|
||||
return workspace;
|
||||
}
|
||||
|
||||
const UIEditorWorkspaceNode* GetRootTabStack(const UIEditorWorkspaceModel& workspace) {
|
||||
return workspace.root.kind == UIEditorWorkspaceNodeKind::TabStack ? &workspace.root : nullptr;
|
||||
}
|
||||
|
||||
std::string DescribeHitTarget(
|
||||
const UIEditorTabStripHitTarget& target,
|
||||
const std::vector<UIEditorTabStripItem>& items) {
|
||||
switch (target.kind) {
|
||||
case UIEditorTabStripHitTargetKind::HeaderBackground:
|
||||
return "HeaderBackground";
|
||||
case UIEditorTabStripHitTargetKind::Content:
|
||||
return "Content";
|
||||
case UIEditorTabStripHitTargetKind::Tab:
|
||||
if (target.index < items.size()) {
|
||||
return "Tab: " + items[target.index].title;
|
||||
}
|
||||
return "Tab";
|
||||
case UIEditorTabStripHitTargetKind::CloseButton:
|
||||
if (target.index < items.size()) {
|
||||
return "CloseButton: " + items[target.index].title;
|
||||
}
|
||||
return "CloseButton";
|
||||
case UIEditorTabStripHitTargetKind::None:
|
||||
default:
|
||||
return "None";
|
||||
}
|
||||
}
|
||||
|
||||
std::string JoinTabTitles(const std::vector<UIEditorTabStripItem>& items) {
|
||||
if (items.empty()) {
|
||||
return "(none)";
|
||||
}
|
||||
|
||||
std::ostringstream stream;
|
||||
for (std::size_t index = 0; index < items.size(); ++index) {
|
||||
if (index > 0u) {
|
||||
stream << " | ";
|
||||
}
|
||||
stream << items[index].title;
|
||||
if (!items[index].closable) {
|
||||
stream << " (locked)";
|
||||
}
|
||||
}
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
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_LBUTTONUP:
|
||||
if (app != nullptr) {
|
||||
app->HandleClick(
|
||||
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");
|
||||
} else {
|
||||
app->HandleKeyDown(static_cast<UINT>(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) {
|
||||
m_captureRoot =
|
||||
ResolveRepoRootPath() / "tests/UI/Editor/integration/shell/tab_strip_basic/captures";
|
||||
m_autoScreenshot.Initialize(m_captureRoot);
|
||||
|
||||
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,
|
||||
1440,
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void ResetScenario() {
|
||||
m_controller =
|
||||
BuildDefaultUIEditorWorkspaceController(BuildPanelRegistry(), BuildWorkspace());
|
||||
m_tabStripState = {};
|
||||
m_layout = {};
|
||||
m_tabItems.clear();
|
||||
m_hoverTarget = {};
|
||||
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
|
||||
m_lastResult = "等待操作";
|
||||
m_navigationModel = {};
|
||||
}
|
||||
|
||||
void OnResize(UINT width, UINT height) {
|
||||
if (width == 0u || height == 0u) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_renderer.Resize(width, height);
|
||||
}
|
||||
|
||||
void HandleMouseMove(float x, float y) {
|
||||
m_mousePosition = UIPoint(x, y);
|
||||
TRACKMOUSEEVENT event = {};
|
||||
event.cbSize = sizeof(event);
|
||||
event.dwFlags = TME_LEAVE;
|
||||
event.hwndTrack = m_hwnd;
|
||||
TrackMouseEvent(&event);
|
||||
|
||||
m_resetButtonHovered = ContainsPoint(m_resetButtonRect, x, y);
|
||||
RefreshHoverTarget();
|
||||
InvalidateRect(m_hwnd, nullptr, FALSE);
|
||||
}
|
||||
|
||||
void HandleMouseLeave() {
|
||||
m_mousePosition = UIPoint(-1000.0f, -1000.0f);
|
||||
m_resetButtonHovered = false;
|
||||
m_hoverTarget = {};
|
||||
m_tabStripState.hoveredIndex = UIEditorTabStripInvalidIndex;
|
||||
m_tabStripState.closeHoveredIndex = UIEditorTabStripInvalidIndex;
|
||||
InvalidateRect(m_hwnd, nullptr, FALSE);
|
||||
}
|
||||
|
||||
void HandleClick(float x, float y) {
|
||||
if (ContainsPoint(m_resetButtonRect, x, y)) {
|
||||
ResetScenario();
|
||||
InvalidateRect(m_hwnd, nullptr, FALSE);
|
||||
return;
|
||||
}
|
||||
|
||||
const UIEditorTabStripHitTarget hit =
|
||||
HitTestUIEditorTabStrip(m_layout, m_tabStripState, UIPoint(x, y));
|
||||
switch (hit.kind) {
|
||||
case UIEditorTabStripHitTargetKind::CloseButton:
|
||||
if (hit.index < m_tabItems.size()) {
|
||||
DispatchCommand(
|
||||
UIEditorWorkspaceCommandKind::ClosePanel,
|
||||
m_tabItems[hit.index].tabId,
|
||||
"Close " + m_tabItems[hit.index].title);
|
||||
m_tabStripState.focused = true;
|
||||
}
|
||||
break;
|
||||
case UIEditorTabStripHitTargetKind::Tab:
|
||||
if (hit.index < m_tabItems.size()) {
|
||||
DispatchCommand(
|
||||
UIEditorWorkspaceCommandKind::ActivatePanel,
|
||||
m_tabItems[hit.index].tabId,
|
||||
"Activate " + m_tabItems[hit.index].title);
|
||||
m_tabStripState.focused = true;
|
||||
}
|
||||
break;
|
||||
case UIEditorTabStripHitTargetKind::HeaderBackground:
|
||||
case UIEditorTabStripHitTargetKind::Content:
|
||||
m_tabStripState.focused = true;
|
||||
m_lastResult = "TabStrip 获得 focus";
|
||||
break;
|
||||
case UIEditorTabStripHitTargetKind::None:
|
||||
default:
|
||||
m_tabStripState.focused = false;
|
||||
m_lastResult = "Focus cleared";
|
||||
break;
|
||||
}
|
||||
|
||||
RefreshHoverTarget();
|
||||
InvalidateRect(m_hwnd, nullptr, FALSE);
|
||||
}
|
||||
|
||||
void HandleKeyDown(UINT keyCode) {
|
||||
if (!m_tabStripState.focused) {
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshTabItems();
|
||||
bool handled = true;
|
||||
bool changed = false;
|
||||
std::string action = {};
|
||||
|
||||
switch (keyCode) {
|
||||
case VK_LEFT:
|
||||
action = "Keyboard Left";
|
||||
changed = m_navigationModel.SelectPrevious();
|
||||
break;
|
||||
case VK_RIGHT:
|
||||
action = "Keyboard Right";
|
||||
changed = m_navigationModel.SelectNext();
|
||||
break;
|
||||
case VK_HOME:
|
||||
action = "Keyboard Home";
|
||||
changed = m_navigationModel.SelectFirst();
|
||||
break;
|
||||
case VK_END:
|
||||
action = "Keyboard End";
|
||||
changed = m_navigationModel.SelectLast();
|
||||
break;
|
||||
default:
|
||||
handled = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!handled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!changed || !m_navigationModel.HasSelection()) {
|
||||
m_lastResult = action + " -> NoOp";
|
||||
InvalidateRect(m_hwnd, nullptr, FALSE);
|
||||
return;
|
||||
}
|
||||
|
||||
const std::size_t selectedIndex = m_navigationModel.GetSelectedIndex();
|
||||
if (selectedIndex < m_tabItems.size()) {
|
||||
DispatchCommand(
|
||||
UIEditorWorkspaceCommandKind::ActivatePanel,
|
||||
m_tabItems[selectedIndex].tabId,
|
||||
action + " -> " + m_tabItems[selectedIndex].title);
|
||||
}
|
||||
|
||||
InvalidateRect(m_hwnd, nullptr, FALSE);
|
||||
}
|
||||
|
||||
void DispatchCommand(
|
||||
UIEditorWorkspaceCommandKind kind,
|
||||
std::string_view panelId,
|
||||
std::string label) {
|
||||
UIEditorWorkspaceCommand command = {};
|
||||
command.kind = kind;
|
||||
command.panelId = std::string(panelId);
|
||||
|
||||
const UIEditorWorkspaceCommandResult result = m_controller.Dispatch(command);
|
||||
m_lastResult =
|
||||
std::move(label) + " -> " +
|
||||
std::string(GetUIEditorWorkspaceCommandStatusName(result.status)) +
|
||||
" | " +
|
||||
result.message;
|
||||
}
|
||||
|
||||
void RefreshTabItems() {
|
||||
const UIEditorWorkspaceModel& workspace = m_controller.GetWorkspace();
|
||||
const UIEditorPanelRegistry& registry = m_controller.GetPanelRegistry();
|
||||
const auto* tabStack = GetRootTabStack(workspace);
|
||||
|
||||
m_tabItems.clear();
|
||||
if (tabStack != nullptr) {
|
||||
for (const UIEditorWorkspaceNode& child : tabStack->children) {
|
||||
if (child.kind != UIEditorWorkspaceNodeKind::Panel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto* sessionState =
|
||||
FindUIEditorPanelSessionState(m_controller.GetSession(), child.panel.panelId);
|
||||
if (sessionState == nullptr || !sessionState->open || !sessionState->visible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const UIEditorPanelDescriptor* descriptor =
|
||||
FindUIEditorPanelDescriptor(registry, child.panel.panelId);
|
||||
UIEditorTabStripItem item = {};
|
||||
item.tabId = child.panel.panelId;
|
||||
item.title = child.panel.title;
|
||||
item.closable = descriptor != nullptr ? descriptor->canClose : true;
|
||||
m_tabItems.push_back(std::move(item));
|
||||
}
|
||||
}
|
||||
|
||||
m_tabStripState.selectedIndex =
|
||||
ResolveUIEditorTabStripSelectedIndex(m_tabItems, workspace.activePanelId);
|
||||
|
||||
m_navigationModel.SetItemCount(m_tabItems.size());
|
||||
if (m_tabStripState.selectedIndex != UIEditorTabStripInvalidIndex) {
|
||||
m_navigationModel.SetSelectedIndex(m_tabStripState.selectedIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void RefreshHoverTarget() {
|
||||
m_hoverTarget = HitTestUIEditorTabStrip(m_layout, m_tabStripState, m_mousePosition);
|
||||
m_tabStripState.hoveredIndex = UIEditorTabStripInvalidIndex;
|
||||
m_tabStripState.closeHoveredIndex = UIEditorTabStripInvalidIndex;
|
||||
|
||||
if (m_hoverTarget.kind == UIEditorTabStripHitTargetKind::CloseButton &&
|
||||
m_hoverTarget.index < m_tabItems.size()) {
|
||||
m_tabStripState.hoveredIndex = m_hoverTarget.index;
|
||||
m_tabStripState.closeHoveredIndex = m_hoverTarget.index;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_hoverTarget.kind == UIEditorTabStripHitTargetKind::Tab &&
|
||||
m_hoverTarget.index < m_tabItems.size()) {
|
||||
m_tabStripState.hoveredIndex = m_hoverTarget.index;
|
||||
}
|
||||
}
|
||||
|
||||
void RenderFrame() {
|
||||
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 float outerPadding = 20.0f;
|
||||
const float leftColumnWidth = 360.0f;
|
||||
const UIRect introRect(outerPadding, outerPadding, leftColumnWidth, 176.0f);
|
||||
const UIRect stateRect(outerPadding, 216.0f, leftColumnWidth, height - 236.0f);
|
||||
const UIRect previewCardRect(
|
||||
leftColumnWidth + outerPadding * 2.0f,
|
||||
outerPadding,
|
||||
width - leftColumnWidth - outerPadding * 3.0f,
|
||||
height - outerPadding * 2.0f);
|
||||
const UIRect tabStripRect(
|
||||
previewCardRect.x + 20.0f,
|
||||
previewCardRect.y + 20.0f,
|
||||
previewCardRect.width - 40.0f,
|
||||
previewCardRect.height - 40.0f);
|
||||
|
||||
RefreshTabItems();
|
||||
m_layout = BuildUIEditorTabStripLayout(tabStripRect, m_tabItems, m_tabStripState);
|
||||
RefreshHoverTarget();
|
||||
|
||||
m_resetButtonRect = UIRect(
|
||||
stateRect.x + 16.0f,
|
||||
stateRect.y + 194.0f,
|
||||
stateRect.width - 32.0f,
|
||||
38.0f);
|
||||
|
||||
UIDrawData drawData = {};
|
||||
UIDrawList& drawList = drawData.EmplaceDrawList("EditorTabStripBasic");
|
||||
drawList.AddFilledRect(UIRect(0.0f, 0.0f, width, height), kWindowBg);
|
||||
|
||||
DrawCard(
|
||||
drawList,
|
||||
introRect,
|
||||
"测试功能:Editor TabStrip 基础层",
|
||||
"只验证 TabStrip header / hit-test / selection / close / keyboard,不包含任何业务面板。");
|
||||
drawList.AddText(
|
||||
UIPoint(introRect.x + 16.0f, introRect.y + 68.0f),
|
||||
"重点检查:tab 布局是否整齐;selected / hover / focus 是否正确;Close 后 active panel 是否回退正确。",
|
||||
kTextPrimary,
|
||||
12.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(introRect.x + 16.0f, introRect.y + 92.0f),
|
||||
"操作:点击 tab 切换;点击 X 关闭;Document C 没有 X;点击内容区后按 Left / Right / Home / End;Reset 恢复;F12 截图。",
|
||||
kTextMuted,
|
||||
12.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(introRect.x + 16.0f, introRect.y + 116.0f),
|
||||
"预期:Close 命中优先于 tab;键盘导航只在 focused 时生效;Document C 始终保留。",
|
||||
kTextWeak,
|
||||
12.0f);
|
||||
|
||||
DrawCard(
|
||||
drawList,
|
||||
stateRect,
|
||||
"状态回显",
|
||||
"这里直接回显 hover / focus / active panel / result,方便人工检查。");
|
||||
DrawButton(drawList, m_resetButtonRect, "Reset", m_resetButtonHovered);
|
||||
|
||||
const std::size_t selectedIndex =
|
||||
ResolveUIEditorTabStripSelectedIndex(m_tabItems, m_controller.GetWorkspace().activePanelId);
|
||||
const std::string selectedIndexText =
|
||||
selectedIndex == UIEditorTabStripInvalidIndex ? "(none)" : std::to_string(selectedIndex);
|
||||
const auto validation = m_controller.ValidateState();
|
||||
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 70.0f),
|
||||
"Hover: " + DescribeHitTarget(m_hoverTarget, m_tabItems),
|
||||
kTextPrimary,
|
||||
13.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 96.0f),
|
||||
std::string("Focused: ") + (m_tabStripState.focused ? "On" : "Off"),
|
||||
kTextPrimary,
|
||||
13.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 122.0f),
|
||||
"Active Panel: " +
|
||||
(m_controller.GetWorkspace().activePanelId.empty()
|
||||
? std::string("(none)")
|
||||
: m_controller.GetWorkspace().activePanelId),
|
||||
kTextPrimary,
|
||||
13.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 148.0f),
|
||||
"Selected Index: " + selectedIndexText,
|
||||
kTextPrimary,
|
||||
13.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 174.0f),
|
||||
"Tabs: " + JoinTabTitles(m_tabItems),
|
||||
kTextMuted,
|
||||
12.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 250.0f),
|
||||
"Result: " + m_lastResult,
|
||||
kTextMuted,
|
||||
12.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 276.0f),
|
||||
validation.IsValid() ? "Validation: OK" : "Validation: " + validation.message,
|
||||
validation.IsValid() ? kSuccess : kDanger,
|
||||
12.0f);
|
||||
|
||||
const std::string captureSummary =
|
||||
m_autoScreenshot.HasPendingCapture()
|
||||
? "截图排队中..."
|
||||
: (m_autoScreenshot.GetLastCaptureSummary().empty()
|
||||
? std::string("F12 -> tests/UI/Editor/integration/shell/tab_strip_basic/captures/")
|
||||
: m_autoScreenshot.GetLastCaptureSummary());
|
||||
drawList.AddText(
|
||||
UIPoint(stateRect.x + 16.0f, stateRect.y + 302.0f),
|
||||
captureSummary,
|
||||
kTextWeak,
|
||||
12.0f);
|
||||
|
||||
DrawCard(
|
||||
drawList,
|
||||
previewCardRect,
|
||||
"预览区",
|
||||
"这里只放一个 TabStrip 和一个 content placeholder,避免试验面板过杂。");
|
||||
|
||||
AppendUIEditorTabStripBackground(drawList, m_layout, m_tabStripState);
|
||||
AppendUIEditorTabStripForeground(drawList, m_layout, m_tabItems, m_tabStripState);
|
||||
|
||||
drawList.AddText(
|
||||
UIPoint(m_layout.contentRect.x + 20.0f, m_layout.contentRect.y + 22.0f),
|
||||
"Content Placeholder",
|
||||
kTextPrimary,
|
||||
18.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(m_layout.contentRect.x + 20.0f, m_layout.contentRect.y + 52.0f),
|
||||
"当前 active panel: " +
|
||||
(m_controller.GetWorkspace().activePanelId.empty()
|
||||
? std::string("(none)")
|
||||
: m_controller.GetWorkspace().activePanelId),
|
||||
kTextMuted,
|
||||
13.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(m_layout.contentRect.x + 20.0f, m_layout.contentRect.y + 76.0f),
|
||||
"这个区域只用于验证 TabStrip content frame 与 focus,不承载业务内容。",
|
||||
kTextWeak,
|
||||
12.0f);
|
||||
drawList.AddText(
|
||||
UIPoint(m_layout.contentRect.x + 20.0f, m_layout.contentRect.y + 100.0f),
|
||||
"检查点:关闭 Document B 后,应自动回退到相邻 tab;Document C 无法关闭。",
|
||||
kTextWeak,
|
||||
12.0f);
|
||||
|
||||
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 = {};
|
||||
UIPoint m_mousePosition = UIPoint(-1000.0f, -1000.0f);
|
||||
UIEditorWorkspaceController m_controller = {};
|
||||
UIEditorTabStripState m_tabStripState = {};
|
||||
UIEditorTabStripLayout m_layout = {};
|
||||
std::vector<UIEditorTabStripItem> m_tabItems = {};
|
||||
UITabStripModel m_navigationModel = {};
|
||||
UIEditorTabStripHitTarget m_hoverTarget = {};
|
||||
UIRect m_resetButtonRect = {};
|
||||
bool m_resetButtonHovered = false;
|
||||
std::string m_lastResult = {};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int nCmdShow) {
|
||||
return ScenarioApp().Run(hInstance, nCmdShow);
|
||||
}
|
||||
Reference in New Issue
Block a user