Improve blockquote / pre layout.

This commit is contained in:
John Preston 2023-10-13 15:14:58 +04:00
parent 7ed87d049f
commit 6e7c4c1c4d
6 changed files with 41 additions and 17 deletions

View file

@ -1225,8 +1225,8 @@ bool String::hasNotEmojiAndSpaces() const {
return _hasNotEmojiAndSpaces; return _hasNotEmojiAndSpaces;
} }
const base::flat_map<int, Deltas> &String::modifications() const { const std::vector<Modification> &String::modifications() const {
static const auto kEmpty = base::flat_map<int, Deltas>(); static const auto kEmpty = std::vector<Modification>();
return _extended ? _extended->modifications : kEmpty; return _extended ? _extended->modifications : kEmpty;
} }

View file

@ -83,9 +83,10 @@ struct SpoilerData;
struct ParagraphDetails; struct ParagraphDetails;
struct ExtendedData; struct ExtendedData;
struct Deltas { struct Modification {
uint16 added = 0; int position = 0;
uint16 removed = 0; uint16 skipped = 0;
bool added = false;
}; };
struct StateRequest { struct StateRequest {
@ -341,7 +342,7 @@ public:
[[nodiscard]] OnlyCustomEmoji toOnlyCustomEmoji() const; [[nodiscard]] OnlyCustomEmoji toOnlyCustomEmoji() const;
[[nodiscard]] bool hasNotEmojiAndSpaces() const; [[nodiscard]] bool hasNotEmojiAndSpaces() const;
[[nodiscard]] const base::flat_map<int, Deltas> &modifications() const; [[nodiscard]] const std::vector<Modification> &modifications() const;
[[nodiscard]] const style::TextStyle *style() const { [[nodiscard]] const style::TextStyle *style() const {
return _st; return _st;

View file

@ -388,9 +388,7 @@ style::font WithFlags(
|| (fontFlags & FontSemibold)) { || (fontFlags & FontSemibold)) {
result = result->semibold(); result = result->semibold();
} }
if ((flags & TextBlockFlag::Italic) if ((flags & TextBlockFlag::Italic) || (fontFlags & FontItalic)) {
|| (fontFlags & FontItalic)
|| (flags & TextBlockFlag::Blockquote)) {
result = result->italic(); result = result->italic();
} }
if ((flags & TextBlockFlag::Underline) || (fontFlags & FontUnderline)) { if ((flags & TextBlockFlag::Underline) || (fontFlags & FontUnderline)) {

View file

@ -12,8 +12,7 @@
namespace Ui::Text { namespace Ui::Text {
struct Deltas; struct Modification;
class String; class String;
class SpoilerClickHandler final : public ClickHandler { class SpoilerClickHandler final : public ClickHandler {
@ -59,7 +58,7 @@ struct ExtendedData {
std::vector<ClickHandlerPtr> links; std::vector<ClickHandlerPtr> links;
std::vector<ParagraphDetails> paragraphs; std::vector<ParagraphDetails> paragraphs;
std::unique_ptr<SpoilerData> spoiler; std::unique_ptr<SpoilerData> spoiler;
base::flat_map<int, Deltas> modifications; std::vector<Modification> modifications;
}; };
} // namespace Ui::Text } // namespace Ui::Text

View file

@ -603,11 +603,25 @@ bool Parser::isLinkEntity(const EntityInText &entity) const {
} }
void Parser::updateModifications(int index, int delta) { void Parser::updateModifications(int index, int delta) {
auto &deltas = _t->ensureExtended()->modifications[index]; auto &modifications = _t->ensureExtended()->modifications;
if (delta > 0) { auto i = end(modifications);
deltas.added += delta; while (i != begin(modifications) && (--i)->position >= index) {
if (i->position < index) {
break;
} else if (delta > 0) {
++i->position;
} else if (i->position == index) {
break;
}
}
if (i != end(modifications) && i->position == index) {
++i->skipped;
} else { } else {
deltas.removed -= delta; modifications.insert(i, {
.position = index,
.skipped = uint16(delta < 0 ? 1 : 0),
.added = (delta > 0),
});
} }
} }

View file

@ -433,6 +433,18 @@ void Renderer::fillParagraphBg(int paddingBottom) {
.skipTop = !isTop, .skipTop = !isTop,
.skipBottom = !isBottom, .skipBottom = !isBottom,
}); });
if (isTop && cache->withHeader) {
const auto font = _t->_st->font->monospace();
const auto topleft = rect.topLeft();
const auto position = topleft + _t->_st->blockHeaderPosition;
const auto baseline = position + QPoint(0, font->ascent);
_p->setFont(font);
_p->setPen(_palette->monoFg->p);
_p->drawText(baseline, _paragraph->language.isEmpty()
? u"code"_q
: _paragraph->language);
}
} }
_blockLineTop = _y + _lineHeight + paddingBottom; _blockLineTop = _y + _lineHeight + paddingBottom;
} }
@ -2094,7 +2106,7 @@ void Renderer::applyBlockProperties(const AbstractBlock *block) {
_currentPen = &_originalPen; _currentPen = &_originalPen;
_currentPenSelected = &_originalPenSelected; _currentPenSelected = &_originalPenSelected;
} }
} else if (isMono || (flags & TextBlockFlag::Blockquote)) { } else if (isMono) {
_currentPen = &_palette->monoFg->p; _currentPen = &_palette->monoFg->p;
_currentPenSelected = &_palette->selectMonoFg->p; _currentPenSelected = &_palette->selectMonoFg->p;
} else if (block->linkIndex()) { } else if (block->linkIndex()) {