Cache Widget shaped text in new Widget::textBlob() field

This is to avoid reshaping the widget text each time we have to paint
or calculate the widget size. When we change the widget text we
invalidate this blob and recalculate it when it's needed.
This commit is contained in:
David Capello 2024-05-03 16:46:33 -03:00
parent 7de9efcc60
commit d4e6efcb02
3 changed files with 30 additions and 5 deletions

2
laf

@ -1 +1 @@
Subproject commit 9e67773f44d3c138318d91a4c66129c14ceac966
Subproject commit d590eec74a0358678f25b6d72c4fc97eb542069d

View File

@ -20,6 +20,7 @@
#include "os/system.h"
#include "os/window.h"
#include "text/font.h"
#include "text/font_mgr.h"
#include "ui/app_state.h"
#include "ui/init_theme_event.h"
#include "ui/intern.h"
@ -159,6 +160,10 @@ void Widget::setTextQuiet(const std::string& text)
{
assert_ui_thread();
// Reset blob
if (m_text != text)
m_blob.reset();
m_text = text;
enableFlags(HAS_TEXT);
}
@ -914,14 +919,29 @@ void Widget::getDrawableRegion(gfx::Region& region, DrawableRegionFlags flags)
region &= Region(cpos);
}
text::TextBlobRef Widget::textBlob() const
{
if (!m_blob) {
m_blob = text::TextBlob::MakeWithShaper(
theme()->fontMgr(),
AddRef(font()),
m_text);
}
return m_blob;
}
int Widget::textWidth() const
{
return Graphics::measureUITextLength(text().c_str(), font());
if (auto blob = textBlob())
return blob->bounds().w;
return 0;
}
int Widget::textHeight() const
{
return font()->height();
auto blob = textBlob();
return std::max<int>(font()->height(),
(blob ? blob->bounds().h: 0));
}
void Widget::getTextIconInfo(
@ -1693,8 +1713,10 @@ void Widget::onBroadcastMouseMessage(const gfx::Point& screenPos,
void Widget::onInitTheme(InitThemeEvent& ev)
{
// Reset cached font
m_font = nullptr;
// Reset cached font and TextBlob
m_font.reset();
m_blob.reset();
// Create a copy of the children list and iterate it, just in case a
// initTheme() modifies this list (e.g. this can happen in some
// strange cases with viewports, where scrollbars are added/removed

View File

@ -17,6 +17,7 @@
#include "gfx/size.h"
#include "obs/signal.h"
#include "text/font.h"
#include "text/text_blob.h"
#include "ui/base.h"
#include "ui/component.h"
#include "ui/graphics.h"
@ -88,6 +89,7 @@ namespace ui {
void setTextf(const char* text, ...);
void setTextQuiet(const std::string& text);
text::TextBlobRef textBlob() const;
int textWidth() const;
int textHeight() const;
@ -455,6 +457,7 @@ namespace ui {
Style* m_style;
std::string m_text; // Widget text
mutable text::FontRef m_font; // Cached font returned by the theme
mutable text::TextBlobRef m_blob; // Cached TextBlob
gfx::Color m_bgColor; // Background color
gfx::Rect m_bounds;
gfx::Region m_updateRegion; // Region to be redrawed.