Skip to content

Commit

Permalink
fix: treeland font init failed
Browse files Browse the repository at this point in the history
DPlatformTheme::font() is async call in treeland and
will fallback to `QGenericUnixTheme::font` which resolveMask is
`FamiliesResolved|SizeResolved`
defaultSystemFontName: Sans Serif (FamiliesResolved)
defaultSystemFontSize: 9 (SizeResolved)
Remove the resolveMask to reset font on font changed
  • Loading branch information
kegechen committed Dec 3, 2024
1 parent 684e90b commit c7f7623
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions platformthemeplugin/qdeepintheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ const QPalette *QDeepinTheme::palette(QPlatformTheme::Palette type) const
return &palette;
}

static QFont *createUnresolveFont(const QString &family, qreal pointSize)
{
auto font = new QFont(family);
font->setPointSizeF(pointSize);
setResolveMask(*font, QFont::NoPropertiesResolved);
return font;
}

const QFont *QDeepinTheme::font(QPlatformTheme::Font type) const
{
if (!qApp->desktopSettingsAware())
Expand All @@ -667,12 +675,16 @@ const QFont *QDeepinTheme::font(QPlatformTheme::Font type) const
QByteArray font_name = theme->fontName();
qreal font_size = 0;

static QScopedPointer<QFont> sysFont;
if (font_name.isEmpty()) {
font_name = theme->gtkFontName();
int font_size_index = font_name.lastIndexOf(' ');

if (font_size_index <= 0)
break;
if (font_size_index <= 0) {
const QFont *defaultFont = QGenericUnixTheme::font(type);
sysFont.reset(createUnresolveFont(defaultFont->family(), defaultFont->pointSizeF()));
return sysFont.data();
}

font_size = font_name.mid(font_size_index + 1).toDouble();
font_name = font_name.left(font_size_index);
Expand All @@ -684,13 +696,8 @@ const QFont *QDeepinTheme::font(QPlatformTheme::Font type) const
font_size = 10.5;
}

static QScopedPointer<QFont> sysFont;

// We need to re-contruct a new QFont each time, inorder to refresh font dpi
sysFont.reset(new QFont(QString()));
sysFont->setFamily(font_name);
sysFont->setPointSizeF(font_size);
setResolveMask(*sysFont, QFont::NoPropertiesResolved);
sysFont.reset(createUnresolveFont(font_name, font_size));

return sysFont.data();
}
Expand All @@ -700,8 +707,11 @@ const QFont *QDeepinTheme::font(QPlatformTheme::Font type) const
if (DPlatformTheme *theme = appTheme()) {
QByteArray font_name = theme->monoFontName();

static QScopedPointer<QFont> fixedFont;
if (font_name.isEmpty()) {
break;
const QFont *defaultFont = QGenericUnixTheme::font(type);
fixedFont.reset(createUnresolveFont(defaultFont->family(), defaultFont->pointSizeF()));
return fixedFont.data();
}

qreal font_size = theme->fontPointSize();
Expand All @@ -710,13 +720,8 @@ const QFont *QDeepinTheme::font(QPlatformTheme::Font type) const
font_size = 10.5;
}

static QScopedPointer<QFont> fixedFont;

// We need to re-contruct a new QFont each time, inorder to refresh font dpi
fixedFont.reset(new QFont(QString()));
fixedFont->setFamily(font_name);
fixedFont->setPointSizeF(font_size);
setResolveMask(*fixedFont, QFont::NoPropertiesResolved);
fixedFont.reset(createUnresolveFont(font_name, font_size));

return fixedFont.data();
}
Expand Down

0 comments on commit c7f7623

Please sign in to comment.