new_editor: unify shared UI text measurement semantics

This commit is contained in:
2026-04-22 01:50:00 +08:00
parent 26e75e093e
commit 0ec0eff90c
16 changed files with 284 additions and 86 deletions

View File

@@ -0,0 +1,39 @@
#pragma once
#include <XCEditor/Foundation/UIEditorTextMeasurement.h>
#include <algorithm>
#include <string_view>
namespace XCEngine::UI::Editor {
inline float ClampUIEditorNonNegative(float value) {
return (std::max)(value, 0.0f);
}
inline float EstimateUIEditorSimpleTextWidth(
std::string_view text,
float estimatedGlyphWidth) {
return static_cast<float>(text.size()) * ClampUIEditorNonNegative(estimatedGlyphWidth);
}
inline float ResolveUIEditorMeasuredTextWidth(
std::string_view text,
float measuredWidth,
float fontSize,
float estimatedGlyphWidth,
const UIEditorTextMeasurer* textMeasurer = nullptr) {
if (measuredWidth > 0.0f) {
return measuredWidth;
}
if (textMeasurer != nullptr &&
!text.empty() &&
fontSize > 0.0f) {
return textMeasurer->MeasureTextWidth(UIEditorTextMeasureRequest { text, fontSize });
}
return EstimateUIEditorSimpleTextWidth(text, estimatedGlyphWidth);
}
} // namespace XCEngine::UI::Editor

View File

@@ -1,5 +1,7 @@
#pragma once
#include <XCEditor/Foundation/UIEditorTextMeasurement.h>
#include <XCEngine/UI/DrawData.h>
#include <cstddef>
@@ -16,7 +18,8 @@ struct UIEditorMenuBarItem {
std::string menuId = {};
std::string label = {};
bool enabled = true;
float desiredLabelWidth = 0.0f;
// Pure measured label width. Button padding is applied only in layout.
float measuredLabelWidth = 0.0f;
};
struct UIEditorMenuBarState {
@@ -81,6 +84,11 @@ struct UIEditorMenuBarHitTarget {
std::size_t index = UIEditorMenuBarInvalidIndex;
};
float ResolveUIEditorMenuBarMeasuredLabelWidth(
const UIEditorMenuBarItem& item,
const UIEditorMenuBarMetrics& metrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
float ResolveUIEditorMenuBarDesiredButtonWidth(
const UIEditorMenuBarItem& item,
const UIEditorMenuBarMetrics& metrics = {});

View File

@@ -1,5 +1,6 @@
#pragma once
#include <XCEditor/Foundation/UIEditorTextMeasurement.h>
#include <XCEditor/Menu/UIEditorMenuModel.h>
#include <XCEngine/UI/DrawData.h>
@@ -23,8 +24,9 @@ struct UIEditorMenuPopupItem {
bool enabled = true;
bool checked = false;
bool hasSubmenu = false;
float desiredLabelWidth = 0.0f;
float desiredShortcutWidth = 0.0f;
// Pure measured text widths. Row/popup sizing is derived from them locally.
float measuredLabelWidth = 0.0f;
float measuredShortcutWidth = 0.0f;
};
struct UIEditorMenuPopupState {
@@ -91,6 +93,16 @@ struct UIEditorMenuPopupHitTarget {
std::size_t index = UIEditorMenuPopupInvalidIndex;
};
float ResolveUIEditorMenuPopupMeasuredLabelWidth(
const UIEditorMenuPopupItem& item,
const UIEditorMenuPopupMetrics& metrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
float ResolveUIEditorMenuPopupMeasuredShortcutWidth(
const UIEditorMenuPopupItem& item,
const UIEditorMenuPopupMetrics& metrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
float ResolveUIEditorMenuPopupDesiredWidth(
const std::vector<UIEditorMenuPopupItem>& items,
const UIEditorMenuPopupMetrics& metrics = {});

View File

@@ -1,5 +1,7 @@
#pragma once
#include <XCEditor/Foundation/UIEditorTextMeasurement.h>
#include <XCEngine/UI/DrawData.h>
#include <cstddef>
@@ -38,7 +40,8 @@ struct UIEditorStatusBarSegment {
UIEditorStatusBarTextTone tone = UIEditorStatusBarTextTone::Primary;
bool interactive = true;
bool showSeparator = false;
float desiredWidth = 0.0f;
// Pure measured label width. Segment padding is applied only in layout.
float measuredLabelWidth = 0.0f;
};
struct UIEditorStatusBarState {
@@ -57,6 +60,8 @@ struct UIEditorStatusBarMetrics {
float separatorInsetY = 5.0f;
float slotGapMin = 18.0f;
float cornerRounding = 0.0f;
float labelFontSize = 12.0f;
float labelInsetY = 0.0f;
float estimatedGlyphWidth = 6.5f;
float borderThickness = 1.0f;
float focusedBorderThickness = 1.0f;
@@ -100,6 +105,11 @@ struct UIEditorStatusBarHitTarget {
std::size_t index = UIEditorStatusBarInvalidIndex;
};
float ResolveUIEditorStatusBarMeasuredLabelWidth(
const UIEditorStatusBarSegment& segment,
const UIEditorStatusBarMetrics& metrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
float ResolveUIEditorStatusBarDesiredSegmentWidth(
const UIEditorStatusBarSegment& segment,
const UIEditorStatusBarMetrics& metrics = {});

View File

@@ -1,5 +1,6 @@
#pragma once
#include <XCEditor/Foundation/UIEditorTextMeasurement.h>
#include <XCEditor/Viewport/UIEditorViewportInputBridge.h>
#include <XCEditor/Viewport/UIEditorViewportSlot.h>
@@ -42,6 +43,11 @@ struct UIEditorViewportShellFrame {
::XCEngine::UI::UISize requestedViewportSize = {};
};
UIEditorViewportShellModel ResolveUIEditorViewportShellMeasuredModel(
const UIEditorViewportShellModel& model,
const Widgets::UIEditorViewportSlotMetrics& metrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
UIEditorViewportShellRequest ResolveUIEditorViewportShellRequest(
const ::XCEngine::UI::UIRect& bounds,
const UIEditorViewportShellSpec& spec,

View File

@@ -53,7 +53,8 @@ struct UIEditorViewportSlotToolItem {
UIEditorViewportSlotToolSlot slot = UIEditorViewportSlotToolSlot::Trailing;
bool enabled = true;
bool selected = false;
float desiredWidth = 0.0f;
// Pure measured tool-label width. Tool button padding is applied only in layout.
float measuredLabelWidth = 0.0f;
};
struct UIEditorViewportSlotState {
@@ -75,13 +76,18 @@ struct UIEditorViewportSlotMetrics {
float toolPaddingX = 8.0f;
float toolGap = 4.0f;
float toolCornerRounding = 2.0f;
float toolLabelFontSize = 11.0f;
float toolLabelInsetY = 3.0f;
float titleGap = 6.0f;
float titleFontSize = 13.0f;
float titleInsetY = 5.0f;
float subtitleFontSize = 10.0f;
float subtitleInsetY = 14.0f;
float estimatedGlyphWidth = 6.5f;
float surfaceInset = 0.0f;
float surfaceBorderThickness = 1.0f;
float focusedSurfaceBorderThickness = 1.0f;
UIEditorStatusBarMetrics statusBarMetrics = {};
};
struct UIEditorViewportSlotPalette {
@@ -157,6 +163,11 @@ struct UIEditorViewportSlotForegroundAppendOptions {
bool includeSurfaceTexture = true;
};
float ResolveUIEditorViewportSlotMeasuredToolLabelWidth(
const UIEditorViewportSlotToolItem& item,
const UIEditorViewportSlotMetrics& metrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
float ResolveUIEditorViewportSlotDesiredToolWidth(
const UIEditorViewportSlotToolItem& item,
const UIEditorViewportSlotMetrics& metrics = {});

View File

@@ -35,6 +35,7 @@ struct UIEditorWorkspaceComposeState {
struct UIEditorWorkspaceViewportComposeRequest {
std::string panelId = {};
::XCEngine::UI::UIRect bounds = {};
UIEditorViewportShellModel viewportShellModel = {};
UIEditorViewportShellRequest viewportShellRequest = {};
};
@@ -82,7 +83,8 @@ UIEditorWorkspaceComposeRequest ResolveUIEditorWorkspaceComposeRequest(
const Widgets::UIEditorDockHostLayout& dockHostLayout,
const UIEditorPanelRegistry& panelRegistry,
const std::vector<UIEditorWorkspacePanelPresentationModel>& presentations,
const Widgets::UIEditorViewportSlotMetrics& viewportMetrics = {});
const Widgets::UIEditorViewportSlotMetrics& viewportMetrics = {},
const UIEditorTextMeasurer* textMeasurer = nullptr);
UIEditorWorkspaceComposeRequest ResolveUIEditorWorkspaceComposeRequest(
const ::XCEngine::UI::UIRect& bounds,