Commit b0423c32 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik Committed by Victoria Fischer
Browse files

window: Create palette only when needed



Creating a `DecorationPalete` (e.g. the `KColorScheme` inside) is
relatively expensive. Currently, this is done once a client is mapped,
regardless of whether it actually has a server-side decoration.

This change makes it create the palette only when it is actually used,
e.g. the window indeed has a server-side decoration or when the user
actions menu is invoked.

Signed-off-by: default avatarVictoria Fischer <victoria.fischer@mbition.io>
parent 8ca6170c
Pipeline #221120 failed with stage
in 15 minutes and 31 seconds
......@@ -1417,16 +1417,15 @@ void Window::doMinimize()
{
}
QPalette Window::palette() const
QPalette Window::palette()
{
if (!m_palette) {
return QPalette();
}
ensurePalette();
return m_palette->palette();
}
const Decoration::DecorationPalette *Window::decorationPalette() const
const Decoration::DecorationPalette *Window::decorationPalette()
{
ensurePalette();
return m_palette.get();
}
......@@ -1447,45 +1446,60 @@ void Window::setColorScheme(const QString &colorScheme)
requestedColorScheme = QStringLiteral("kdeglobals");
}
if (!m_palette || m_colorScheme != requestedColorScheme) {
m_colorScheme = requestedColorScheme;
if (m_colorScheme == requestedColorScheme) {
return;
}
if (m_palette) {
disconnect(m_palette.get(), &Decoration::DecorationPalette::changed, this, &Window::handlePaletteChange);
}
m_colorScheme = requestedColorScheme;
auto it = s_palettes.find(m_colorScheme);
if (m_palette) {
disconnect(m_palette.get(), &Decoration::DecorationPalette::changed, this, &Window::handlePaletteChange);
m_palette.reset();
if (it == s_palettes.end() || it->expired()) {
m_palette = std::make_shared<Decoration::DecorationPalette>(m_colorScheme);
if (m_palette->isValid()) {
s_palettes[m_colorScheme] = m_palette;
} else {
if (!s_defaultPalette) {
s_defaultPalette = std::make_shared<Decoration::DecorationPalette>(QStringLiteral("kdeglobals"));
s_palettes[QStringLiteral("kdeglobals")] = s_defaultPalette;
}
// If there already was a palette, re-create it right away
// so the signals for repainting the decoration are emitted.
ensurePalette();
}
m_palette = s_defaultPalette;
}
Q_EMIT colorSchemeChanged();
}
if (m_colorScheme == QStringLiteral("kdeglobals")) {
s_defaultPalette = m_palette;
}
void Window::updateColorScheme()
{
setColorScheme(preferredColorScheme());
}
void Window::ensurePalette()
{
if (m_palette) {
return;
}
auto it = s_palettes.find(m_colorScheme);
if (it == s_palettes.end() || it->expired()) {
m_palette = std::make_shared<Decoration::DecorationPalette>(m_colorScheme);
if (m_palette->isValid()) {
s_palettes[m_colorScheme] = m_palette;
} else {
m_palette = it->lock();
}
if (!s_defaultPalette) {
s_defaultPalette = std::make_shared<Decoration::DecorationPalette>(QStringLiteral("kdeglobals"));
s_palettes[QStringLiteral("kdeglobals")] = s_defaultPalette;
}
connect(m_palette.get(), &Decoration::DecorationPalette::changed, this, &Window::handlePaletteChange);
m_palette = s_defaultPalette;
}
Q_EMIT paletteChanged(palette());
Q_EMIT colorSchemeChanged();
if (m_colorScheme == QStringLiteral("kdeglobals")) {
s_defaultPalette = m_palette;
}
} else {
m_palette = it->lock();
}
}
void Window::updateColorScheme()
{
setColorScheme(preferredColorScheme());
connect(m_palette.get(), &Decoration::DecorationPalette::changed, this, &Window::handlePaletteChange);
handlePaletteChange();
}
void Window::handlePaletteChange()
......
......@@ -1033,8 +1033,8 @@ public:
Q_INVOKABLE void setMaximize(bool vertically, bool horizontally);
virtual bool noBorder() const;
virtual void setNoBorder(bool set);
QPalette palette() const;
const Decoration::DecorationPalette *decorationPalette() const;
QPalette palette();
const Decoration::DecorationPalette *decorationPalette();
/**
* Returns whether the window is resizable or has a fixed size.
*/
......@@ -1658,6 +1658,7 @@ protected:
void setupWindowManagementInterface();
void updateColorScheme();
void ensurePalette();
void setTransientFor(Window *transientFor);
/**
* Just removes the @p cl from the transients without any further checks.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment