From d8cab18eb32457036cf202ac2aa703d2f39d7693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 29 Apr 2020 10:16:47 +0200 Subject: [PATCH] [apps/code] Do not display varbox source name if it does not fit --- apps/code/script_node_cell.cpp | 11 ++++++++++- apps/code/script_node_cell.h | 15 +++++++++------ apps/code/variable_box_controller.cpp | 4 ++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/apps/code/script_node_cell.cpp b/apps/code/script_node_cell.cpp index e76c46274..e0b42b68e 100644 --- a/apps/code/script_node_cell.cpp +++ b/apps/code/script_node_cell.cpp @@ -41,10 +41,19 @@ KDSize ScriptNodeCell::ScriptNodeView::minimalSizeForOptimalDisplay() const { return KDSizeZero; } return KDSize( - Ion::Display::Width - Metric::PopUpLeftMargin - Metric::PopUpRightMargin, + k_optimalWidth, m_scriptNode->description() == nullptr ? k_simpleItemHeight : k_complexItemHeight); } +bool ScriptNodeCell::CanDisplayNameAndSource(int nameLength, const char * source) { + if (source == nullptr) { + return true; + } + assert(nameLength > 0); + const KDFont * font = ScriptNodeView::k_font; + return font->glyphSize().width()*(nameLength + 1) + font->stringSize(source).width() <= ScriptNodeView::k_optimalWidth; // + 1 for the separating space +} + void ScriptNodeCell::setScriptNode(ScriptNode * scriptNode) { m_scriptNodeView.setScriptNode(scriptNode); reloadCell(); diff --git a/apps/code/script_node_cell.h b/apps/code/script_node_cell.h index 12c1b7a7b..672a9fb44 100644 --- a/apps/code/script_node_cell.h +++ b/apps/code/script_node_cell.h @@ -10,11 +10,18 @@ namespace Code { class ScriptNodeCell : public TableCell { public: + static_assert('\x11' == UCodePointEmpty, "Unicode error"); + constexpr static char k_parentheses[] = "()"; + constexpr static char k_parenthesesWithEmpty[] = "(\x11)"; + constexpr static KDCoordinate k_simpleItemHeight = 27; + constexpr static KDCoordinate k_complexItemHeight = 42; + ScriptNodeCell() : TableCell(), m_scriptNodeView() {} void setScriptNode(ScriptNode * node); + static bool CanDisplayNameAndSource(int nameLength, const char * source); /* TableCell */ View * labelView() const override { return const_cast(static_cast(&m_scriptNodeView)); } @@ -24,14 +31,11 @@ public: void reloadCell() override; const char * text() const override { return m_scriptNodeView.text(); } - static_assert('\x11' == UCodePointEmpty, "Unicode error"); - constexpr static char k_parentheses[] = "()"; - constexpr static char k_parenthesesWithEmpty[] = "(\x11)"; - constexpr static KDCoordinate k_simpleItemHeight = 27; - constexpr static KDCoordinate k_complexItemHeight = 42; protected: class ScriptNodeView : public HighlightCell { public: + constexpr static const KDFont * k_font = KDFont::SmallFont; + constexpr static KDCoordinate k_optimalWidth = Ion::Display::Width - Metric::PopUpLeftMargin - Metric::PopUpRightMargin; ScriptNodeView() : HighlightCell(), m_scriptNode(nullptr) @@ -43,7 +47,6 @@ protected: return m_scriptNode->description(); } private: - constexpr static const KDFont * k_font = KDFont::SmallFont; constexpr static KDCoordinate k_bottomMargin = 5; constexpr static KDCoordinate k_topMargin = k_bottomMargin + k_separatorThickness; ScriptNode * m_scriptNode; diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index a69fa535a..7d8769bb3 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -936,6 +936,10 @@ void VariableBoxController::addNode(ScriptNode::Type type, NodeOrigin origin, co for (int i = *currentNodeCount - 1; i >= insertionIndex; i--) { nodes[i+1] = nodes[i]; } + // Check if the node source name fits, if not, do not use it + if (!ScriptNodeCell::CanDisplayNameAndSource(nameLength, nodeSourceName)) { + nodeSourceName = nullptr; + } // Add the node nodes[insertionIndex] = ScriptNode(type, name, nameLength, nodeSourceName, description); // Increase the node count