Refine XCEditor docking and DPI rendering

This commit is contained in:
2026-04-11 17:07:37 +08:00
parent 35d3d6328b
commit 2958dcc491
46 changed files with 4839 additions and 471 deletions

View File

@@ -1,5 +1,6 @@
#include <XCEditor/Shell/UIEditorPanelRegistry.h>
#include <XCEditor/Shell/UIEditorWorkspaceModel.h>
#include <XCEditor/Shell/UIEditorWorkspaceSession.h>
#include <cmath>
#include <unordered_set>
@@ -43,6 +44,19 @@ UIEditorWorkspaceNode WrapStandalonePanelAsTabStack(UIEditorWorkspaceNode panelN
return tabStack;
}
void CollapseSplitNodeToOnlyChild(UIEditorWorkspaceNode& node) {
if (node.kind != UIEditorWorkspaceNodeKind::Split ||
node.children.size() != 1u) {
return;
}
// Move the remaining child through a temporary object first. Assigning
// directly from node.children.front() aliases a subobject of node and can
// trigger use-after-move when the vector storage is torn down.
UIEditorWorkspaceNode remainingChild = std::move(node.children.front());
node = std::move(remainingChild);
}
void CanonicalizeNodeRecursive(
UIEditorWorkspaceNode& node,
bool allowStandalonePanelLeaf) {
@@ -67,7 +81,7 @@ void CanonicalizeNodeRecursive(
if (node.kind == UIEditorWorkspaceNodeKind::Split &&
node.children.size() == 1u) {
node = std::move(node.children.front());
CollapseSplitNodeToOnlyChild(node);
}
}
@@ -131,6 +145,256 @@ UIEditorWorkspaceNode* FindMutableNodeRecursive(
return nullptr;
}
bool FindNodePathRecursive(
const UIEditorWorkspaceNode& node,
std::string_view nodeId,
std::vector<std::size_t>& path) {
if (node.nodeId == nodeId) {
return true;
}
for (std::size_t index = 0; index < node.children.size(); ++index) {
path.push_back(index);
if (FindNodePathRecursive(node.children[index], nodeId, path)) {
return true;
}
path.pop_back();
}
return false;
}
UIEditorWorkspaceNode* ResolveMutableNodeByPath(
UIEditorWorkspaceNode& node,
const std::vector<std::size_t>& path) {
UIEditorWorkspaceNode* current = &node;
for (const std::size_t childIndex : path) {
if (childIndex >= current->children.size()) {
return nullptr;
}
current = &current->children[childIndex];
}
return current;
}
bool IsPanelOpenAndVisibleInSession(
const UIEditorWorkspaceSession& session,
std::string_view panelId) {
const UIEditorPanelSessionState* state = FindUIEditorPanelSessionState(session, panelId);
return state != nullptr && state->open && state->visible;
}
std::size_t CountVisibleChildren(
const UIEditorWorkspaceNode& node,
const UIEditorWorkspaceSession& session) {
if (node.kind != UIEditorWorkspaceNodeKind::TabStack) {
return 0u;
}
std::size_t visibleCount = 0u;
for (const UIEditorWorkspaceNode& child : node.children) {
if (child.kind == UIEditorWorkspaceNodeKind::Panel &&
IsPanelOpenAndVisibleInSession(session, child.panel.panelId)) {
++visibleCount;
}
}
return visibleCount;
}
std::size_t ResolveActualInsertionIndexForVisibleInsertion(
const UIEditorWorkspaceNode& node,
const UIEditorWorkspaceSession& session,
std::size_t targetVisibleInsertionIndex) {
std::vector<std::size_t> visibleIndices = {};
visibleIndices.reserve(node.children.size());
for (std::size_t index = 0; index < node.children.size(); ++index) {
const UIEditorWorkspaceNode& child = node.children[index];
if (child.kind == UIEditorWorkspaceNodeKind::Panel &&
IsPanelOpenAndVisibleInSession(session, child.panel.panelId)) {
visibleIndices.push_back(index);
}
}
if (targetVisibleInsertionIndex == 0u) {
return visibleIndices.empty() ? 0u : visibleIndices.front();
}
if (visibleIndices.empty()) {
return 0u;
}
if (targetVisibleInsertionIndex >= visibleIndices.size()) {
return visibleIndices.back() + 1u;
}
return visibleIndices[targetVisibleInsertionIndex];
}
void FixTabStackSelectedIndex(
UIEditorWorkspaceNode& node,
std::string_view preferredPanelId) {
if (node.kind != UIEditorWorkspaceNodeKind::TabStack || node.children.empty()) {
return;
}
for (std::size_t index = 0; index < node.children.size(); ++index) {
if (node.children[index].kind == UIEditorWorkspaceNodeKind::Panel &&
node.children[index].panel.panelId == preferredPanelId) {
node.selectedTabIndex = index;
return;
}
}
if (node.selectedTabIndex >= node.children.size()) {
node.selectedTabIndex = node.children.size() - 1u;
}
}
bool RemoveNodeByIdRecursive(
UIEditorWorkspaceNode& node,
std::string_view nodeId) {
if (node.kind != UIEditorWorkspaceNodeKind::Split) {
return false;
}
for (std::size_t index = 0; index < node.children.size(); ++index) {
if (node.children[index].nodeId == nodeId) {
node.children.erase(node.children.begin() + static_cast<std::ptrdiff_t>(index));
if (node.children.size() == 1u) {
CollapseSplitNodeToOnlyChild(node);
}
return true;
}
}
for (UIEditorWorkspaceNode& child : node.children) {
if (RemoveNodeByIdRecursive(child, nodeId)) {
if (node.kind == UIEditorWorkspaceNodeKind::Split &&
node.children.size() == 1u) {
CollapseSplitNodeToOnlyChild(node);
}
return true;
}
}
return false;
}
float ClampDockSplitRatio(float value) {
constexpr float kMinRatio = 0.1f;
constexpr float kMaxRatio = 0.9f;
return (std::min)(kMaxRatio, (std::max)(kMinRatio, value));
}
bool IsLeadingDockPlacement(UIEditorWorkspaceDockPlacement placement) {
return placement == UIEditorWorkspaceDockPlacement::Left ||
placement == UIEditorWorkspaceDockPlacement::Top;
}
UIEditorWorkspaceSplitAxis ResolveDockSplitAxis(UIEditorWorkspaceDockPlacement placement) {
return placement == UIEditorWorkspaceDockPlacement::Left ||
placement == UIEditorWorkspaceDockPlacement::Right
? UIEditorWorkspaceSplitAxis::Horizontal
: UIEditorWorkspaceSplitAxis::Vertical;
}
std::string MakeUniqueNodeId(
const UIEditorWorkspaceModel& workspace,
std::string base) {
if (base.empty()) {
base = "workspace-node";
}
if (FindUIEditorWorkspaceNode(workspace, base) == nullptr) {
return base;
}
for (std::size_t suffix = 1u; suffix < 1024u; ++suffix) {
const std::string candidate = base + "-" + std::to_string(suffix);
if (FindUIEditorWorkspaceNode(workspace, candidate) == nullptr) {
return candidate;
}
}
return base + "-overflow";
}
bool TryExtractVisiblePanelFromTabStack(
UIEditorWorkspaceModel& workspace,
const UIEditorWorkspaceSession& session,
std::string_view sourceNodeId,
std::string_view panelId,
UIEditorWorkspaceNode& extractedPanel) {
std::vector<std::size_t> sourcePath = {};
if (!FindNodePathRecursive(workspace.root, sourceNodeId, sourcePath)) {
return false;
}
UIEditorWorkspaceNode* sourceStack =
ResolveMutableNodeByPath(workspace.root, sourcePath);
if (sourceStack == nullptr ||
sourceStack->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
std::size_t panelIndex = sourceStack->children.size();
for (std::size_t index = 0; index < sourceStack->children.size(); ++index) {
const UIEditorWorkspaceNode& child = sourceStack->children[index];
if (child.kind != UIEditorWorkspaceNodeKind::Panel) {
return false;
}
if (child.panel.panelId == panelId) {
if (!IsPanelOpenAndVisibleInSession(session, panelId)) {
return false;
}
panelIndex = index;
break;
}
}
if (panelIndex >= sourceStack->children.size()) {
return false;
}
if (sourcePath.empty() && sourceStack->children.size() == 1u) {
return false;
}
std::string fallbackSelectedPanelId = {};
if (sourceStack->selectedTabIndex < sourceStack->children.size()) {
fallbackSelectedPanelId =
sourceStack->children[sourceStack->selectedTabIndex].panel.panelId;
}
extractedPanel = std::move(sourceStack->children[panelIndex]);
sourceStack->children.erase(
sourceStack->children.begin() + static_cast<std::ptrdiff_t>(panelIndex));
if (sourceStack->children.empty()) {
if (sourcePath.empty()) {
return false;
}
if (!RemoveNodeByIdRecursive(workspace.root, sourceNodeId)) {
return false;
}
} else {
if (fallbackSelectedPanelId == panelId) {
const std::size_t nextIndex =
(std::min)(panelIndex, sourceStack->children.size() - 1u);
fallbackSelectedPanelId =
sourceStack->children[nextIndex].panel.panelId;
}
FixTabStackSelectedIndex(*sourceStack, fallbackSelectedPanelId);
}
workspace = CanonicalizeUIEditorWorkspaceModel(std::move(workspace));
return true;
}
bool TryActivateRecursive(
UIEditorWorkspaceNode& node,
std::string_view panelId) {
@@ -484,4 +748,266 @@ bool TrySetUIEditorWorkspaceSplitRatio(
return true;
}
bool TryReorderUIEditorWorkspaceTab(
UIEditorWorkspaceModel& workspace,
const UIEditorWorkspaceSession& session,
std::string_view nodeId,
std::string_view panelId,
std::size_t targetVisibleInsertionIndex) {
UIEditorWorkspaceNode* node = FindMutableNodeRecursive(workspace.root, nodeId);
if (node == nullptr || node->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
std::vector<std::size_t> visibleChildIndices = {};
std::vector<UIEditorWorkspaceNode> reorderedVisibleChildren = {};
visibleChildIndices.reserve(node->children.size());
reorderedVisibleChildren.reserve(node->children.size());
std::size_t sourceVisibleIndex = node->children.size();
for (std::size_t index = 0; index < node->children.size(); ++index) {
const UIEditorWorkspaceNode& child = node->children[index];
if (child.kind != UIEditorWorkspaceNodeKind::Panel) {
return false;
}
if (!IsPanelOpenAndVisibleInSession(session, child.panel.panelId)) {
continue;
}
if (child.panel.panelId == panelId) {
sourceVisibleIndex = visibleChildIndices.size();
}
visibleChildIndices.push_back(index);
reorderedVisibleChildren.push_back(child);
}
if (sourceVisibleIndex >= reorderedVisibleChildren.size() ||
targetVisibleInsertionIndex > reorderedVisibleChildren.size()) {
return false;
}
if (targetVisibleInsertionIndex == sourceVisibleIndex ||
targetVisibleInsertionIndex == sourceVisibleIndex + 1u) {
return false;
}
UIEditorWorkspaceNode movedChild =
std::move(reorderedVisibleChildren[sourceVisibleIndex]);
reorderedVisibleChildren.erase(
reorderedVisibleChildren.begin() + static_cast<std::ptrdiff_t>(sourceVisibleIndex));
std::size_t adjustedInsertionIndex = targetVisibleInsertionIndex;
if (adjustedInsertionIndex > sourceVisibleIndex) {
--adjustedInsertionIndex;
}
if (adjustedInsertionIndex > reorderedVisibleChildren.size()) {
adjustedInsertionIndex = reorderedVisibleChildren.size();
}
reorderedVisibleChildren.insert(
reorderedVisibleChildren.begin() +
static_cast<std::ptrdiff_t>(adjustedInsertionIndex),
std::move(movedChild));
std::string selectedPanelId = {};
if (node->selectedTabIndex < node->children.size()) {
selectedPanelId = node->children[node->selectedTabIndex].panel.panelId;
}
const std::vector<UIEditorWorkspaceNode> originalChildren = node->children;
std::size_t nextVisibleIndex = 0u;
for (std::size_t index = 0; index < originalChildren.size(); ++index) {
const UIEditorWorkspaceNode& originalChild = originalChildren[index];
if (!IsPanelOpenAndVisibleInSession(session, originalChild.panel.panelId)) {
node->children[index] = originalChild;
continue;
}
node->children[index] = reorderedVisibleChildren[nextVisibleIndex];
++nextVisibleIndex;
}
for (std::size_t index = 0; index < node->children.size(); ++index) {
if (node->children[index].panel.panelId == selectedPanelId) {
node->selectedTabIndex = index;
break;
}
}
return true;
}
bool TryMoveUIEditorWorkspaceTabToStack(
UIEditorWorkspaceModel& workspace,
const UIEditorWorkspaceSession& session,
std::string_view sourceNodeId,
std::string_view panelId,
std::string_view targetNodeId,
std::size_t targetVisibleInsertionIndex) {
if (sourceNodeId.empty() ||
panelId.empty() ||
targetNodeId.empty()) {
return false;
}
if (sourceNodeId == targetNodeId) {
return TryReorderUIEditorWorkspaceTab(
workspace,
session,
sourceNodeId,
panelId,
targetVisibleInsertionIndex);
}
const UIEditorWorkspaceNode* targetNode =
FindUIEditorWorkspaceNode(workspace, targetNodeId);
if (targetNode == nullptr ||
targetNode->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
if (targetVisibleInsertionIndex > CountVisibleChildren(*targetNode, session)) {
return false;
}
UIEditorWorkspaceNode extractedPanel = {};
if (!TryExtractVisiblePanelFromTabStack(
workspace,
session,
sourceNodeId,
panelId,
extractedPanel)) {
return false;
}
UIEditorWorkspaceNode* targetStack =
FindMutableNodeRecursive(workspace.root, targetNodeId);
if (targetStack == nullptr ||
targetStack->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
const std::size_t actualInsertionIndex =
ResolveActualInsertionIndexForVisibleInsertion(
*targetStack,
session,
targetVisibleInsertionIndex);
if (actualInsertionIndex > targetStack->children.size()) {
return false;
}
targetStack->children.insert(
targetStack->children.begin() +
static_cast<std::ptrdiff_t>(actualInsertionIndex),
std::move(extractedPanel));
targetStack->selectedTabIndex = actualInsertionIndex;
workspace.activePanelId = std::string(panelId);
workspace = CanonicalizeUIEditorWorkspaceModel(std::move(workspace));
return true;
}
bool TryDockUIEditorWorkspaceTabRelative(
UIEditorWorkspaceModel& workspace,
const UIEditorWorkspaceSession& session,
std::string_view sourceNodeId,
std::string_view panelId,
std::string_view targetNodeId,
UIEditorWorkspaceDockPlacement placement,
float splitRatio) {
if (placement == UIEditorWorkspaceDockPlacement::Center) {
const UIEditorWorkspaceNode* targetNode =
FindUIEditorWorkspaceNode(workspace, targetNodeId);
if (targetNode == nullptr ||
targetNode->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
return TryMoveUIEditorWorkspaceTabToStack(
workspace,
session,
sourceNodeId,
panelId,
targetNodeId,
CountVisibleChildren(*targetNode, session));
}
if (sourceNodeId.empty() ||
panelId.empty() ||
targetNodeId.empty()) {
return false;
}
const UIEditorWorkspaceNode* sourceNode =
FindUIEditorWorkspaceNode(workspace, sourceNodeId);
const UIEditorWorkspaceNode* targetNode =
FindUIEditorWorkspaceNode(workspace, targetNodeId);
if (sourceNode == nullptr ||
targetNode == nullptr ||
sourceNode->kind != UIEditorWorkspaceNodeKind::TabStack ||
targetNode->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
if (sourceNodeId == targetNodeId &&
sourceNode->children.size() <= 1u) {
return false;
}
UIEditorWorkspaceNode extractedPanel = {};
if (!TryExtractVisiblePanelFromTabStack(
workspace,
session,
sourceNodeId,
panelId,
extractedPanel)) {
return false;
}
UIEditorWorkspaceNode* targetStack =
FindMutableNodeRecursive(workspace.root, targetNodeId);
if (targetStack == nullptr ||
targetStack->kind != UIEditorWorkspaceNodeKind::TabStack) {
return false;
}
const std::string movedStackNodeId = MakeUniqueNodeId(
workspace,
std::string(targetNodeId) + "__dock_" + std::string(panelId) + "_stack");
UIEditorWorkspaceNode movedStack = {};
movedStack.kind = UIEditorWorkspaceNodeKind::TabStack;
movedStack.nodeId = movedStackNodeId;
movedStack.selectedTabIndex = 0u;
movedStack.children.push_back(std::move(extractedPanel));
UIEditorWorkspaceNode existingTarget = std::move(*targetStack);
UIEditorWorkspaceNode primary = {};
UIEditorWorkspaceNode secondary = {};
if (IsLeadingDockPlacement(placement)) {
primary = std::move(movedStack);
secondary = std::move(existingTarget);
} else {
primary = std::move(existingTarget);
secondary = std::move(movedStack);
}
const float requestedRatio = ClampDockSplitRatio(splitRatio);
const float resolvedSplitRatio =
IsLeadingDockPlacement(placement)
? requestedRatio
: (1.0f - requestedRatio);
*targetStack = BuildUIEditorWorkspaceSplit(
MakeUniqueNodeId(
workspace,
std::string(targetNodeId) + "__dock_split"),
ResolveDockSplitAxis(placement),
resolvedSplitRatio,
std::move(primary),
std::move(secondary));
workspace.activePanelId = std::string(panelId);
workspace = CanonicalizeUIEditorWorkspaceModel(std::move(workspace));
return true;
}
} // namespace XCEngine::UI::Editor