Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: UI framework optimizations and DockArea min size enforcement

esengine8d6f4bfb44ca
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f54fd34a8..1fd9f012f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -65,6 +65,8 @@ set(ESENGINE_SOURCES
src/esengine/ui/widgets/ContextMenu.cpp
src/esengine/ui/widgets/Dropdown.cpp
src/esengine/ui/widgets/ClickablePanel.cpp
+ # Text utilities
+ src/esengine/ui/text/TextMeasureHelper.cpp
# Docking System
src/esengine/ui/docking/DockNode.cpp
src/esengine/ui/docking/DockPanel.cpp
@@ -148,6 +150,8 @@ set(ESENGINE_HEADERS
src/esengine/ui/widgets/ContextMenu.hpp
src/esengine/ui/widgets/Dropdown.hpp
src/esengine/ui/widgets/ClickablePanel.hpp
+ # Text utilities
+ src/esengine/ui/text/TextMeasureHelper.hpp
# Docking System
src/esengine/ui/docking/DockTypes.hpp
src/esengine/ui/docking/DockNode.hpp
diff --git a/src/esengine/ui/UIContext.cpp b/src/esengine/ui/UIContext.cpp
index 05f66c974..9fa0ea221 100644
--- a/src/esengine/ui/UIContext.cpp
+++ b/src/esengine/ui/UIContext.cpp
@@ -353,7 +353,17 @@ void UIContext::processMouseDown(MouseButton button, f32 x, f32 y) {
event.pressed = true;
event.x = x;
event.y = y;
- hit->onMouseDown(event);
+ event.ctrl = ctrlDown_;
+ event.shift = shiftDown_;
+ event.alt = altDown_;
+
+ Widget* target = hit;
+ while (target && !event.consumed && !event.propagationStopped) {
+ if (target->onMouseDown(event)) {
+ event.consume();
+ }
+ target = target->getParent();
+ }
return;
}
}
@@ -375,8 +385,17 @@ void UIContext::processMouseDown(MouseButton button, f32 x, f32 y) {
event.pressed = true;
event.x = x;
event.y = y;
+ event.ctrl = ctrlDown_;
+ event.shift = shiftDown_;
+ event.alt = altDown_;
- hoveredWidget_->onMouseDown(event);
+ Widget* target = hoveredWidget_;
+ while (target && !event.consumed && !event.propagationStopped) {
+ if (target->onMouseDown(event)) {
+ event.consume();
+ }
+ target = target->getParent();
+ }
} else {
setFocus(nullptr);
}
@@ -387,16 +406,25 @@ void UIContext::processMouseUp(MouseButton button, f32 x, f32 y) {
lastMouseX_ = x;
lastMouseY_ = y;
- Widget* targetWidget = pressedWidget_ ? pressedWidget_ : hoveredWidget_;
+ Widget* startWidget = pressedWidget_ ? pressedWidget_ : hoveredWidget_;
- if (targetWidget) {
+ if (startWidget) {
MouseButtonEvent event;
event.button = button;
event.pressed = false;
event.x = x;
event.y = y;
+ event.ctrl = ctrlDown_;
+ event.shift = shiftDown_;
+ event.alt = altDown_;
- targetWidget->onMouseUp(event);
+ Widget* target = startWidget;
+ while (target && !event.consumed && !event.propagationStopped) {
+ if (target->onMouseUp(event)) {
+ event.consume();
+ }
+ target = target->getParent();
+ }
}
if (button == MouseButton::Left) {
@@ -415,11 +443,13 @@ void UIContext::processMouseScroll(f32 deltaX, f32 deltaY, f32 x, f32 y) {
event.deltaY = deltaY;
event.x = x;
event.y = y;
+ event.ctrl = ctrlDown_;
+ event.shift = shiftDown_;
Widget* target = hoveredWidget_;
- while (target) {
+ while (target && !event.consumed && !event.propagationStopped) {
if (target->onScroll(event)) {
- break;
+ event.consume();
}
target = target->getParent();
}
@@ -439,7 +469,13 @@ void UIContext::processKeyDown(KeyCode key, bool ctrl, bool shift, bool alt) {
event.shift = shift;
event.alt = alt;
- focusedWidget_->onKeyDown(event);
+ Widget* target = focusedWidget_;
+ while (target && !event.consumed && !event.propagationStopped) {
+ if (target->onKeyDown(event)) {
+ event.consume();
+ }
+ target = target->getParent();
+ }
}
}
@@ -456,7 +492,13 @@ void UIContext::processKeyUp(KeyCode key, bool ctrl, bool shift, bool alt) {
event.shift = shift;
event.alt = alt;
- focusedWidget_->onKeyUp(event);
+ Widget* target = focusedWidget_;
+ while (target && !event.consumed && !event.propagationStopped) {
+ if (target->onKeyUp(event)) {
+ event.consume();
+ }
+ target = target->getParent();
+ }
}
}
@@ -474,6 +516,13 @@ void UIContext::processTextInput(const std::string& text) {
// Focus Management
// =============================================================================
+Widget* UIContext::getFocusedWidget() {
+ if (focusedWidget_ && focusedWidget_->getContext() != this) {
+ focusedWidget_ = nullptr;
+ }
+ return focusedWidget_;
+}
+
void UIContext::setFocus(Widget* widget) {
if (focusedWidget_ == widget) return;
@@ -506,6 +555,16 @@ void UIContext::clearWidgetReferences(Widget* widget) {
}
}
+Widget* UIContext::findWidgetById(const WidgetId& id) {
+ if (!root_) return nullptr;
+
+ if (root_->getId() == id) {
+ return root_.get();
+ }
+
+ return root_->findChild(id);
+}
+
// =============================================================================
// Internal
// =============================================================================
diff --git a/src/esengine/ui/UIContext.hpp b/src/esengine/ui/UIContext.hpp
index 3be8789d5..27e00da16 100644
--- a/src/esengine/ui/UIContext.hpp
+++ b/src/esengine/ui/UIContext.hpp
@@ -295,8 +295,9 @@ public:
/**
* @brief Gets the currently focused widget
+ * @return Focused widget or nullptr if none/invalid
*/
- Widget* getFocusedWidget() { return focusedWidget_; }
+ Widget* getFocusedWidget();
/**
* @brief Sets focus to a widget
@@ -315,6 +316,13 @@ public:
*/
void clearWidgetReferences(Widget* widget);
+ /**
+ * @brief Finds a widget by ID in the tree
+ * @param id Widget ID to find
+ * @return Widget pointer or nullptr if not found
+ */
+ Widget* findWidgetById(const WidgetId& id);
+
// =========================================================================
// Overlay Management
// =========================================================================
diff --git a/src/esengine/ui/docking/DockArea.cpp b/src/esengine/ui/docking/DockArea.cpp
index 5c388f179..11cd7bf9d 100644
--- a/src/esengine/ui/docking/DockArea.cpp
+++ b/src/esengine/ui/docking/DockArea.cpp
@@ -540,15 +540,62 @@ void DockArea::handleSplitterDrag(f32 x, f32 y) {
if (draggedSplitter_->getSplitDirection() == DockSplitDirection::Horizontal) {
f32 relX = x - bounds.x;
newRatio = relX / bounds.width;
+
+ f32 availableWidth = bounds.width - splitterThickness_;
+ f32 minFirst = getMinSizeForNode(draggedSplitter_->getFirst()).x;
+ f32 minSecond = getMinSizeForNode(draggedSplitter_->getSecond()).x;
+
+ f32 minRatio = minFirst / availableWidth;
+ f32 maxRatio = 1.0f - (minSecond / availableWidth);
+ newRatio = std::clamp(newRatio, minRatio, maxRatio);
} else {
f32 relY = y - bounds.y;
newRatio = relY / bounds.height;
+
+ f32 availableHeight = bounds.height - splitterThickness_;
+ f32 minFirst = getMinSizeForNode(draggedSplitter_->getFirst()).y;
+ f32 minSecond = getMinSizeForNode(draggedSplitter_->getSecond()).y;
+
+ f32 minRatio = minFirst / availableHeight;
+ f32 maxRatio = 1.0f - (minSecond / availableHeight);
+ newRatio = std::clamp(newRatio, minRatio, maxRatio);
}
draggedSplitter_->setSplitRatio(newRatio);
invalidateLayout();
}
+glm::vec2 DockArea::getMinSizeForNode(DockNode* node) const {
+ if (!node) return minPanelSize_;
+
+ if (node->isTabs()) {
+ glm::vec2 maxMin = minPanelSize_;
+ for (const auto& panel : node->getPanels()) {
+ if (panel) {
+ const auto& constraints = panel->getConstraints();
+ maxMin.x = std::max(maxMin.x, constraints.minWidth);
+ maxMin.y = std::max(maxMin.y, constraints.minHeight);
+ }
+ }
+ return maxMin;
+ }
+
+ if (node->isSplit()) {
+ glm::vec2 firstMin = getMinSizeForNode(node->getFirst());
+ glm::vec2 secondMin = getMinSizeForNode(node->getSecond());
+
+ if (node->getSplitDirection() == DockSplitDirection::Horizontal) {
+ return {firstMin.x + secondMin.x + splitterThickness_,
+ std::max(firstMin.y, secondMin.y)};
+ } else {
+ return {std::max(firstMin.x, secondMin.x),
+ firstMin.y + secondMin.y + splitterThickness_};
+ }
+ }
+
+ return minPanelSize_;
+}
+
void DockArea::setNodeArea(DockNode* node) {
if (!node) return;
diff --git a/src/esengine/ui/docking/DockArea.hpp b/src/esengine/ui/docking/DockArea.hpp
index ea432fe1a..c4709ccab 100644
--- a/src/esengine/ui/docking/DockArea.hpp
+++ b/src/esengine/ui/docking/DockArea.hpp
@@ -224,6 +224,7 @@ private:
DockNode* hitTestSplitter(f32 x, f32 y);
void handleSplitterDrag(f32 x, f32 y);
+ glm::vec2 getMinSizeForNode(DockNode* node) const;
void setNodeArea(DockNode* node);
void connectTabBarSignals(DockNode* node);
diff --git a/src/esengine/ui/text/TextMeasureHelper.cpp b/src/esengine/ui/text/TextMeasureHelper.cpp
new file mode 100644
index 000000000..36bc8ae91
--- /dev/null
+++ b/src/esengine/ui/text/TextMeasureHelper.cpp
@@ -0,0 +1,111 @@
+/**
+ * @file TextMeasureHelper.cpp
+ * @brief TextMeasureHelper implementation
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "TextMeasureHelper.hpp"
+#include "../UIContext.hpp"
+
+#if ES_FEATURE_SDF_FONT
+#include "../font/MSDFFont.hpp"
+#endif
+
+#if ES_FEATURE_BITMAP_FONT
+#include "../font/BitmapFont.hpp"
+#endif
+
+namespace esengine::ui {
+
+// =============================================================================
+// Font Resolution
+// =============================================================================
+
+#if ES_FEATURE_SDF_FONT
+MSDFFont* TextMeasureHelper::resolveFont(UIContext* ctx, const std::string& fontName,
+ bool useIconFont) {
+ if (!ctx) return nullptr;
+
+ if (!fontName.empty()) {
+ return ctx->getMSDFFont(fontName);
+ }
+
+ if (useIconFont) {
+ return ctx->getIconMSDFFont();
+ }
+
+ return ctx->getDefaultMSDFFont();
+}
+#endif
+
+#if ES_FEATURE_BITMAP_FONT
+BitmapFont* TextMeasureHelper::resolveBitmapFont(UIContext* ctx, const std::string& fontName) {
+ if (!ctx) return nullptr;
+
+ if (!fontName.empty()) {
+ return ctx->getBitmapFont(fontName);
+ }
+
+ return ctx->getDefaultBitmapFont();
+}
+#endif
+
+// =============================================================================
+// Icon Detection
+// =============================================================================
+
+bool TextMeasureHelper::isIconText(const std::string& text) {
+ if (text.empty()) return false;
+
+ u8 firstByte = static_cast<u8>(text[0]);
+ if ((firstByte & 0xF0) == 0xE0 && text.size() >= 3) {
+ u32 codepoint = ((firstByte & 0x0F) << 12) |
+ ((static_cast<u8>(text[1]) & 0x3F) << 6) |
+ (static_cast<u8>(text[2]) & 0x3F);
+ return (codepoint >= 0xE000 && codepoint <= 0xF8FF);
+ }
+ return false;
+}
+
+// =============================================================================
+// Text Measurement
+// =============================================================================
+
+glm::vec2 TextMeasureHelper::measureText(UIContext* ctx, const std::string& text, f32 fontSize,
+ const std::string& fontName, bool useIconFont) {
+ if (!ctx || text.empty()) return {0.0f, 0.0f};
+
+#if ES_FEATURE_SDF_FONT
+ bool isIcon = useIconFont || (fontName.empty() && isIconText(text));
+ MSDFFont* font = resolveFont(ctx, fontName, isIcon);
+ if (font) {
+ return font->measureText(text, fontSize);
+ }
+#elif ES_FEATURE_BITMAP_FONT
+ BitmapFont* font = resolveBitmapFont(ctx, fontName);
+ if (font) {
+ return font->measureText(text, fontSize);
+ }
+#endif
+
+ return {0.0f, 0.0f};
+}
+
+glm::vec2 TextMeasureHelper::measureTextCached(UIContext* ctx, const std::string& text,
+ f32 fontSize, MeasureCache& cache,
+ const std::string& fontName, bool useIconFont) {
+ if (cache.isValid(text, fontSize)) {
+ return cache.cachedSize;
+ }
+
+ glm::vec2 size = measureText(ctx, text, fontSize, fontName, useIconFont);
+ cache.update(text, fontSize, size);
+ return size;
+}
+
+} // namespace esengine::ui
diff --git a/src/esengine/ui/text/TextMeasureHelper.hpp b/src/esengine/ui/text/TextMeasureHelper.hpp
new file mode 100644
index 000000000..2d89c586d
--- /dev/null
+++ b/src/esengine/ui/text/TextMeasureHelper.hpp
@@ -0,0 +1,119 @@
+/**
+ * @file TextMeasureHelper.hpp
+ * @brief Helper utilities for text measurement and font resolution
+ * @details Provides centralized text measurement with caching and font
+ * resolution logic for UI widgets.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+#pragma once
+
+// =============================================================================
+// Includes
+// =============================================================================
+
+#include "../../core/Types.hpp"
+
+#include <glm/vec2.hpp>
+
+#include <string>
+
+namespace esengine::ui {
+
+class UIContext;
+
+#if ES_FEATURE_SDF_FONT
+class MSDFFont;
+#endif
+
+#if ES_FEATURE_BITMAP_FONT
+class BitmapFont;
+#endif
+
+// =============================================================================
+// TextMeasureHelper
+// =============================================================================
+
+/**
+ * @brief Helper for text measurement and font resolution
+ */
+class TextMeasureHelper {
+public:
+ /**
+ * @brief Resolves the appropriate font from the context
+ * @param ctx UI context
+ * @param fontName Optional font name (empty for default)
+ * @param useIconFont Whether to use icon font
+ * @return Font pointer or nullptr if not found
+ */
+#if ES_FEATURE_SDF_FONT
+ static MSDFFont* resolveFont(UIContext* ctx, const std::string& fontName = "",
+ bool useIconFont = false);
+#endif
+
+#if ES_FEATURE_BITMAP_FONT
+ static BitmapFont* resolveBitmapFont(UIContext* ctx, const std::string& fontName = "");
+#endif
+
+ /**
+ * @brief Checks if text starts with an icon codepoint (Private Use Area)
+ * @param text Text to check
+ * @return true if text starts with an icon character
+ */
+ static bool isIconText(const std::string& text);
+
+ /**
+ * @brief Measures text size using the appropriate font
+ * @param ctx UI context
+ * @param text Text to measure
+ * @param fontSize Font size
+ * @param fontName Optional font name
+ * @param useIconFont Whether to use icon font
+ * @return Measured text size
+ */
+ static glm::vec2 measureText(UIContext* ctx, const std::string& text, f32 fontSize,
+ const std::string& fontName = "", bool useIconFont = false);
+
+ /**
+ * @brief Cache for text measurement
+ */
+ struct MeasureCache {
+ std::string cachedText;
+ f32 cachedFontSize = 0.0f;
+ glm::vec2 cachedSize{0.0f};
+ bool dirty = true;
+
+ void invalidate() { dirty = true; }
+
+ bool isValid(const std::string& text, f32 fontSize) const {
+ return !dirty && cachedText == text && cachedFontSize == fontSize;
+ }
+
+ void update(const std::string& text, f32 fontSize, glm::vec2 size) {
+ cachedText = text;
+ cachedFontSize = fontSize;
+ cachedSize = size;
+ dirty = false;
+ }
+ };
+
+ /**
+ * @brief Measures text with caching support
+ * @param ctx UI context
+ * @param text Text to measure
+ * @param fontSize Font size
+ * @param cache Cache to use/update
+ * @param fontName Optional font name
+ * @param useIconFont Whether to use icon font
+ * @return Measured text size
+ */
+ static glm::vec2 measureTextCached(UIContext* ctx, const std::string& text, f32 fontSize,
+ MeasureCache& cache, const std::string& fontName = "",
+ bool useIconFont = false);
+};
+
+} // namespace esengine::ui
diff --git a/src/esengine/ui/widgets/Widget.cpp b/src/esengine/ui/widgets/Widget.cpp
index c5c316ae7..ec44f8e98 100644
--- a/src/esengine/ui/widgets/Widget.cpp
+++ b/src/esengine/ui/widgets/Widget.cpp
@@ -107,6 +107,12 @@ void Widget::setMaxSize(f32 maxWidth, f32 maxHeight) {
}
glm::vec2 Widget::measure(f32 availableWidth, f32 availableHeight) {
+ if (!measureDirty_ &&
+ lastAvailableWidth_ == availableWidth &&
+ lastAvailableHeight_ == availableHeight) {
+ return {cachedMeasureWidth_, cachedMeasureHeight_};
+ }
+
f32 contentWidth = 0.0f;
f32 contentHeight = 0.0f;
@@ -131,6 +137,12 @@ glm::vec2 Widget::measure(f32 availableWidth, f32 availableHeight) {
width = constraints_.constrainWidth(width);
height = constraints_.constrainHeight(height);
+ cachedMeasureWidth_ = width;
+ cachedMeasureHeight_ = height;
+ lastAvailableWidth_ = availableWidth;
+ lastAvailableHeight_ = availableHeight;
+ measureDirty_ = false;
+
return {width, height};
}
@@ -155,12 +167,23 @@ Rect Widget::getContentBounds() const {
}
void Widget::invalidateLayout() {
+ if (layoutDirty_) return;
layoutDirty_ = true;
+ measureDirty_ = true;
if (parent_) {
parent_->invalidateLayout();
}
}
+void Widget::invalidateMeasure() {
+ if (measureDirty_) return;
+ measureDirty_ = true;
+ layoutDirty_ = true;
+ if (parent_) {
+ parent_->invalidateMeasure();
+ }
+}
+
// =============================================================================
// State
// =============================================================================
diff --git a/src/esengine/ui/widgets/Widget.hpp b/src/esengine/ui/widgets/Widget.hpp
index e4730da0a..5d2b75595 100644
--- a/src/esengine/ui/widgets/Widget.hpp
+++ b/src/esengine/ui/widgets/Widget.hpp
@@ -195,9 +195,15 @@ public:
/** @brief Marks layout as needing recalculation */
void invalidateLayout();
+ /** @brief Marks measure cache as dirty (called when content changes) */
+ void invalidateMeasure();
+
/** @brief Returns true if layout needs recalculation */
bool isLayoutDirty() const { return layoutDirty_; }
+ /** @brief Returns true if measure cache is dirty */
+ bool isMeasureDirty() const { return measureDirty_; }
+
// =========================================================================
// State
// =========================================================================
@@ -344,6 +350,12 @@ private:
Rect bounds_;
WidgetState state_;
bool layoutDirty_ = true;
+ bool measureDirty_ = true;
+
+ f32 cachedMeasureWidth_ = 0.0f;
+ f32 cachedMeasureHeight_ = 0.0f;
+ f32 lastAvailableWidth_ = -1.0f;
+ f32 lastAvailableHeight_ = -1.0f;
UIContext* context_ = nullptr;
};