From 621304168edc83453e0f60a7c4a459401f68f822 Mon Sep 17 00:00:00 2001 From: Przemyslaw Brudny Date: Thu, 22 Jul 2021 00:36:39 +0200 Subject: [PATCH] [EGD-7203] Added extra space removal in Text draw line ending Added extra space removal in Text draw line ending. Fixed crash in Notes. --- .../style/NotesListStyle.hpp | 2 +- module-gui/gui/widgets/TextCursor.cpp | 2 +- module-gui/gui/widgets/TextLine.cpp | 44 +++++++++++++++++-- module-gui/gui/widgets/TextLine.hpp | 15 ++----- .../test-gui-TextFixedSize.cpp | 4 +- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/module-apps/application-notes/style/NotesListStyle.hpp b/module-apps/application-notes/style/NotesListStyle.hpp index 19ae44c572bb8a43ef3738323d7fac2385504aed..6f4d685816285b09900b70a9e5de88857c005518 100644 --- a/module-apps/application-notes/style/NotesListStyle.hpp +++ b/module-apps/application-notes/style/NotesListStyle.hpp @@ -9,7 +9,7 @@ namespace app::notes::style::list { constexpr inline auto X = ::style::window::default_left_margin; constexpr inline auto Y = ::style::window::default_vertical_pos + ::style::margins::small - 1; - constexpr inline auto Width = ::style::window::default_body_width; + constexpr inline auto Width = ::style::listview::body_width_with_scroll; constexpr inline auto Height = ::style::window::default_body_height - ::style::margins::small; constexpr inline auto PenWidth = 0; diff --git a/module-gui/gui/widgets/TextCursor.cpp b/module-gui/gui/widgets/TextCursor.cpp index 077ecbfa091eb2eef499fb45ea628f2913f3d095..819c8cd3a0fd57a1c4f47282d643bb9002aa9254 100644 --- a/module-gui/gui/widgets/TextCursor.cpp +++ b/module-gui/gui/widgets/TextCursor.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "TextCursor.hpp" diff --git a/module-gui/gui/widgets/TextLine.cpp b/module-gui/gui/widgets/TextLine.cpp index 18c5d7ccffe07b56b6a1c4423825bd10f4aad888..d7cddb2fe7c95a841cd6ef2414b005c54448849d 100644 --- a/module-gui/gui/widgets/TextLine.cpp +++ b/module-gui/gui/widgets/TextLine.cpp @@ -78,8 +78,7 @@ namespace gui } // create item for show and update Line data - auto item = - buildUITextPart(text.substr(0, signsCountToShow) + (breakLineDashAddition ? "-" : ""), textFormat); + auto item = buildUITextPart(textToPrint(signsCountToShow, text), textFormat); shownLetterCount += signsCountToShow; widthUsed += item->getTextNeedSpace(); heightUsed = std::max(heightUsed, item->getTextHeight()); @@ -89,6 +88,11 @@ namespace gui localCursor += signsCountToShow; + if (removeTrailingSpace) { + end = TextBlock::End::Newline; + return; + } + if (localCursor.checkAndInvalidateBlockChanged() && localCursor.checkPreviousBlockNewLine()) { end = TextBlock::End::Newline; return; @@ -124,8 +128,14 @@ namespace gui // check if space found and no newline at end if (spacePos != UTF8::npos && newlinePos == UTF8::npos) { + // if line ends on space remove it when drawing + if (spacePos >= signsCountToShow) { + removeTrailingSpace = true; + } + // add one to include space in the line - signsCountToShow = spacePos + 1; + signsCountToShow = spacePos + 1; + breakLineDashAddition = false; } // if sings still left in text add dash sign @@ -138,6 +148,21 @@ namespace gui return signsCountToShow; } + UTF8 TextLine::textToPrint(unsigned int signsCountToShow, UTF8 &text) + { + auto textToPrint = text.substr(0, signsCountToShow); + + if (removeTrailingSpace) { + textToPrint = text.substr(0, signsCountToShow - 1); + } + + if (breakLineDashAddition) { + textToPrint = textToPrint + "-"; + } + + return textToPrint; + } + TextLine::TextLine(TextLine &&from) noexcept { lineContent = std::move(from.lineContent); @@ -153,6 +178,7 @@ namespace gui end = from.end; maxWidth = from.maxWidth; breakLineDashAddition = from.breakLineDashAddition; + removeTrailingSpace = from.removeTrailingSpace; lineStartBlockNumber = from.lineStartBlockNumber; lineStartBlockPosition = from.lineStartBlockPosition; lineVisible = from.lineVisible; @@ -260,6 +286,18 @@ namespace gui return width; } + const Item *TextLine::getElement(unsigned int pos) const noexcept + { + unsigned int local_pos = 0; + for (auto &el : lineContent) { + local_pos += el->getTextLength(); + if (local_pos >= pos) { + return el; + } + } + return nullptr; + } + UTF8 TextLine::getText(unsigned int pos) const { UTF8 text; diff --git a/module-gui/gui/widgets/TextLine.hpp b/module-gui/gui/widgets/TextLine.hpp index cc4899864126f4217a26bdd8e8019bdee903b579..0302f3d24ef3ea08e4daa0daf84009371bb49a62 100644 --- a/module-gui/gui/widgets/TextLine.hpp +++ b/module-gui/gui/widgets/TextLine.hpp @@ -37,10 +37,12 @@ namespace gui bool lineEnd = false; bool lineVisible = true; bool breakLineDashAddition = false; + bool removeTrailingSpace = false; unsigned int lineStartBlockNumber = text::npos; unsigned int lineStartBlockPosition = text::npos; unsigned int calculateSignsToShow(BlockCursor &localCursor, UTF8 &text, unsigned int space); + UTF8 textToPrint(unsigned int signsCountToShow, UTF8 &text); void createUnderline(unsigned int max_w, unsigned int max_height); void updateUnderline(const short &x, const short &y); void setLineStartConditions(unsigned int startBlockNumber, unsigned int startBlockPosition); @@ -126,18 +128,6 @@ namespace gui return lineStartBlockPosition; } - [[nodiscard]] const Item *getElement(unsigned int pos) const noexcept - { - unsigned int local_pos = 0; - for (auto &el : lineContent) { - local_pos += el->getTextLength(); - if (local_pos >= pos) { - return el; - } - } - return nullptr; - } - [[nodiscard]] int32_t getX() const noexcept { return lineContent.front()->area().pos(Axis::X); @@ -153,6 +143,7 @@ namespace gui /// moves Text parts in Text. To not call n times callbacks on resize, call prior to setting parent void alignH(Alignment align, Length parent_length) const; void alignV(Alignment align, Length parent_length, Length lines_height); + [[nodiscard]] const Item *getElement(unsigned int pos) const noexcept; [[nodiscard]] auto getText(unsigned int pos) const -> UTF8; }; } // namespace gui diff --git a/module-gui/test/test-catch-text/test-gui-TextFixedSize.cpp b/module-gui/test/test-catch-text/test-gui-TextFixedSize.cpp index dac82651d064ac8a6676bb88cd7243c06d062405..e5b1ddf6fcb20b6ea1f709706bfa3bd9ddded3da 100644 --- a/module-gui/test/test-catch-text/test-gui-TextFixedSize.cpp +++ b/module-gui/test/test-catch-text/test-gui-TextFixedSize.cpp @@ -165,7 +165,7 @@ TEST_CASE("TextFixedSize remove Char") SECTION("Remove char from last line on size limited space") { auto text = TestTextFixedSize(); - text.setSize(210, 30); + text.setSize(220, 30); text.setFont(style::window::font::medium); text.setText(testStringLongLine); @@ -181,7 +181,7 @@ TEST_CASE("TextFixedSize remove Char") SECTION("Remove char from last line on line limited space") { auto text = TestTextFixedSize(); - text.setSize(210, 120); + text.setSize(220, 120); text.setFont(style::window::font::medium); text.setLines(1);