// SPDX-License-Identifier: GPL-3.0-or-later #include "app/editor/advanced.h" #include "app/editor/colourpicker.h" #include "app/apply.h" #include "app/session.h" #include "core/render.h" #include "core/schema.h" #include #include #include #include #include #include #include #include #include #include #include #include namespace xpl::editor { namespace { // XP's items: what each one's size is, its colours (by .theme name), its // font and the font's colour. (Empty: XP greys that control out.) struct Item { const char *label; const char *size; // a metric, below const char *colour1, *colour2; const char *font; // "caption", "smcaption", "menu", "status", "message", "icon" const char *fontColour; }; const Item kItems[] = { {"3D Objects", nullptr, "ButtonFace", nullptr, nullptr, "ButtonText"}, {"Active Title Bar", "captionHeight", "ActiveTitle", "GradientActiveTitle", "caption", "TitleText"}, {"Active Window Border", "borderWidth", "ActiveBorder", nullptr, nullptr, nullptr}, {"Application Background", nullptr, "AppWorkspace", nullptr, nullptr, nullptr}, {"Caption Buttons", "captionWidth", nullptr, nullptr, nullptr, nullptr}, {"Desktop", nullptr, "Background", nullptr, nullptr, nullptr}, {"Icon", nullptr, nullptr, nullptr, "icon", nullptr}, {"Icon Spacing (Horizontal)", "iconHorizontal", nullptr, nullptr, nullptr, nullptr}, {"Icon Spacing (Vertical)", "iconVertical", nullptr, nullptr, nullptr, nullptr}, {"Inactive Title Bar", "captionHeight", "InactiveTitle", "GradientInactiveTitle", "caption", "InactiveTitleText"}, {"Inactive Window Border", "borderWidth", "InactiveBorder", nullptr, nullptr, nullptr}, {"Menu", "menuHeight", "Menu", nullptr, "menu", "MenuText"}, {"Message Box", nullptr, nullptr, nullptr, "message", "WindowText"}, {"Palette Title", "smCaptionHeight", nullptr, nullptr, "smcaption", nullptr}, {"Scrollbar", "scrollWidth", nullptr, nullptr, nullptr, nullptr}, {"Selected Items", "menuHeight", "Hilight", nullptr, "menu", "HilightText"}, {"ToolTip", nullptr, "InfoWindow", nullptr, "status", "InfoText"}, {"Window", nullptr, "Window", nullptr, nullptr, "WindowText"}, }; int *itemMetric(ClassicScheme &s, const char *name) { NonClientMetrics &m = s.metrics; const QLatin1String n(name ? name : ""); if (n == QLatin1String("captionHeight")) return &m.captionHeight; if (n == QLatin1String("captionWidth")) return &m.captionWidth; if (n == QLatin1String("borderWidth")) return &m.borderWidth; if (n == QLatin1String("menuHeight")) return &m.menuHeight; if (n == QLatin1String("smCaptionHeight")) return &m.smCaptionHeight; if (n == QLatin1String("scrollWidth")) return &m.scrollWidth; if (n == QLatin1String("iconHorizontal")) return &s.icons.horzSpacing; if (n == QLatin1String("iconVertical")) return &s.icons.vertSpacing; return nullptr; } LogFont *itemFont(ClassicScheme &s, const char *name) { const QLatin1String n(name ? name : ""); if (n == QLatin1String("caption")) return &s.metrics.captionFont; if (n == QLatin1String("smcaption")) return &s.metrics.smCaptionFont; if (n == QLatin1String("menu")) return &s.metrics.menuFont; if (n == QLatin1String("status")) return &s.metrics.statusFont; if (n == QLatin1String("message")) return &s.metrics.messageFont; if (n == QLatin1String("icon")) return &s.icons.font; return nullptr; } // The same items on a visual style: what each one's colours, size and font // are there. A colour is either a [SysMetrics] colour, set exactly, or art -- // parts whose bitmaps turn (core/tint.h) toward the colour picked, from // their own colour (`ref`: a [SysMetrics] colour or a part's colour hint). // Caption Buttons and Scrollbar have a colour here, which XP's Classic items // hadn't: their art. const char *const kCaptionsActive[] = {"window.caption(active)", "window.smallcaption(active)", "window.maxcaption(active)", "window.smallmaxcaption(active)", "sysmetrics.activecaption", nullptr}; const char *const kCaptionsInactive[] = {"window.caption(inactive)", "window.smallcaption(inactive)", "window.maxcaption(inactive)", "window.smallmaxcaption(inactive)", "sysmetrics.inactivecaption", nullptr}; const char *const kFramesActive[] = {"window.frameleft(active)", "window.frameright(active)", "window.framebottom(active)", "window.smallframeleft(active)", "window.smallframeright(active)", "window.smallframebottom(active)", nullptr}; const char *const kFramesInactive[] = {"window.frameleft(inactive)", "window.frameright(inactive)", "window.framebottom(inactive)", "window.smallframeleft(inactive)", "window.smallframeright(inactive)", "window.smallframebottom(inactive)", nullptr}; const char *const kCaptionButtons[] = {"window.minbutton", "window.maxbutton", "window.restorebutton", "window.helpbutton", "window.mdiminbutton", "window.mdirestorebutton", "window.mdihelpbutton", nullptr}; const char *const kScrollbar[] = {"scrollbar", nullptr}; // (The taskbar, tray and Start menu: what the taskbar art is made from. The // Start button keeps its green: start::button isn't among them.) const char *const kTaskbar[] = {"taskbar", "taskbar::*", "taskbarvert::*", "taskband", "taskband::*", "taskbandgroupmenu::*", "taskbandvert::*", "traynotify", "traynotify::*", "traynotifyhoriz::*", "traynotifyhorizopen::*", "traynotifyvert::*", "traynotifyvertopen::*", "startpanel", "startmenu::*", nullptr}; // (XP's greens for beginning, which keep under the Taskbar's colour: the // Start button and All Programs' arrow.) const char *const kStartButton[] = {"start::button", "startpanel.moreprogramsarrow", nullptr}; // Their text: the task buttons', the clock's; the Start button's. const char *const kTaskbarText[] = {"taskband::toolbar", "taskbandvert::toolbar", "taskbandgroupmenu::toolbar", "taskbar::toolbar", "taskbarvert::toolbar", "traynotify::clock", nullptr}; const char *const kStartText[] = {"start::button", nullptr}; struct StyleColour { const char *sys = nullptr; // a [SysMetrics] colour (set exactly; or, with art, the reference) const char *const *art = nullptr; // selectors whose art turns toward the colour const char *refSection = nullptr; // ...from this part's colour hint, when not `sys`'s const char *refProperty = nullptr; const char *const *textOf = nullptr; // sections whose TextColor is set (an override each) }; struct StyleItem { const char *label; StyleColour colour1, colour2, fontColour; const char *size = nullptr, *size2 = nullptr; // [SysMetrics] sizes (the second kept the same) const char *font = nullptr; // a [SysMetrics] font // The item whose turn this one's art follows while synchronized: the // same turn on its own colours, so it keeps where the style has it // relative to that one (an inactive caption stays that much lighter). const char *syncWith = nullptr; // Art carrying the taskbar's colours at its edge, which the core turns // with the taskbar's tint (Theme::tintFrame) unless this section's // XPlasmaSharedEdge says "none". const char *sharedEdge = nullptr; // Sections whose Font is set (an override each), for what has a font of // its own rather than a [SysMetrics] one. const char *const *fontOf = nullptr; }; // XP's items, and a Start Button and Taskbar (in order among them), on a // visual style. const StyleItem kStyleItems[] = { {"3D Objects", {"btnface"}, {}, {"btntext"}}, // (No Color 2: a style's caption is its art, no gradient between two colours.) {"Active Title Bar", {"activecaption", kCaptionsActive}, {}, {"captiontext"}, "captionbarheight", nullptr, "captionfont"}, {"Active Window Border", {"activecaption", kFramesActive}, {}, {}, "sizingborderwidth", nullptr, nullptr, "Active Title Bar"}, {"Application Background", {"appworkspace"}}, {"Caption Buttons", {"activecaption", kCaptionButtons}, {}, {}, "captionbarwidth", nullptr, nullptr, "Active Title Bar"}, {"Desktop", {"background"}}, {"Icon", {}, {}, {}, nullptr, nullptr, "icontitlefont"}, {"Icon Spacing (Horizontal)", {}}, {"Icon Spacing (Vertical)", {}}, {"Inactive Title Bar", {"inactivecaption", kCaptionsInactive}, {}, {"inactivecaptiontext"}, "captionbarheight", nullptr, "captionfont", "Active Title Bar"}, {"Inactive Window Border", {"inactivecaption", kFramesInactive}, {}, {}, "sizingborderwidth", nullptr, nullptr, "Inactive Title Bar"}, {"Menu", {"menu"}, {}, {"menutext"}, "menubarheight", nullptr, "menufont"}, {"Message Box", {}, {}, {"windowtext"}, nullptr, nullptr, "msgboxfont"}, {"Palette Title", {}, {}, {}, "smcaptionbarheight", nullptr, "smallcaptionfont"}, {"Scrollbar", {nullptr, kScrollbar, "scrollbar.thumbbtnvert", "fillcolorhint"}, {}, {}, "scrollbarwidth", "scrollbarheight"}, {"Selected Items", {"highlight"}, {}, {"highlighttext"}, "menubarheight", nullptr, "menufont"}, {"Start Button", {nullptr, kStartButton, "start::button.pushbutton", "fillcolorhint"}, {}, {nullptr, nullptr, nullptr, nullptr, kStartText}, nullptr, nullptr, nullptr, nullptr, "start::button", kStartText}, {"Taskbar", {nullptr, kTaskbar, "taskband::toolbar.button", "fillcolorhint"}, {}, {nullptr, nullptr, nullptr, nullptr, kTaskbarText}, nullptr, nullptr, nullptr, "Active Title Bar", nullptr, kTaskbarText}, {"ToolTip", {"infobk"}, {}, {"infotext"}, nullptr, nullptr, "statusfont"}, {"Window", {"window"}, {}, {"windowtext"}}, }; const StyleItem *styleItem(const QString &label) { for (const StyleItem &i : kStyleItems) if (label == QLatin1String(i.label)) return &i; return nullptr; } // XP's own entry for an item (its scheme fields), if it has one. const Item *classicItem(const QString &label) { for (const Item &i : kItems) if (label == QLatin1String(i.label)) return &i; return nullptr; } TintRule ruleFor(const QString &selector) { const auto spec = TintSpec::parse(selector + QLatin1String(": keep")); return spec && !spec->rules.isEmpty() ? spec->rules.first() : TintRule(); } // The turn an item's art has now (its first selector's rule). Tint artTurn(const Adjustments &adjust, const StyleItem &item) { if (!item.colour1.art) return Tint(); const TintRule r = ruleFor(QLatin1String(item.colour1.art[0])); return adjust.tint.forSection(r.sub, r.cls, r.part, r.state).first; } std::optional themeColour(const Theme &theme, const QString §ion, const QString &property) { const TintRule s = ruleFor(section); QRgb c; if (const auto v = theme.property(s.cls, s.part, s.state, property, s.sub); v && parseColour(*v, &c)) return QColor(c); return std::nullopt; } // A section's Font ("Franklin Gothic Medium, 14, italic"), as the style has it. std::optional iniFont(const Theme &theme, const QString §ion) { const TintRule s = ruleFor(section); const auto v = theme.property(s.cls, s.part, s.state, u"font", s.sub); if (!v) return std::nullopt; const QStringList parts = v->split(QLatin1Char(',')); bool ok = false; const int points = parts.value(1).trimmed().toInt(&ok); if (parts.value(0).trimmed().isEmpty() || !ok) return std::nullopt; const QString flags = parts.mid(2).join(QLatin1Char(' ')).toLower(); LogFont f = LogFont::fromPoints(parts.value(0).trimmed(), points, flags.contains(QLatin1String("bold"))); f.italic = flags.contains(QLatin1String("italic")); return f; } QString fontText(const LogFont &f) { QString text = QStringLiteral("%1, %2").arg(f.face).arg(f.points()); if (f.bold()) text += QLatin1String(", bold"); if (f.italic) text += QLatin1String(", italic"); return text; } } // namespace // --- the colour button ----------------------------------------------------------------- ColourButton::ColourButton(QWidget *parent) : QWidget(parent) , m_button(new QPushButton(this)) { auto *layout = new QHBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(m_button); m_button->setFixedWidth(46); connect(m_button, &QPushButton::clicked, this, [this] { // (Each colour on the way told, for what follows the dialog live.) const auto c = ColourPicker::pick(m_colour, this, [this](const QColor &on) { Q_EMIT changing(on); }); if (c) { setColour(*c); Q_EMIT picked(); } }); setColour(Qt::black); } void ColourButton::setColour(const QColor &colour) { m_colour = colour; QPixmap swatch(28, 12); swatch.fill(colour); QPainter p(&swatch); p.setPen(Qt::black); p.drawRect(swatch.rect().adjusted(0, 0, -1, -1)); p.end(); m_button->setIcon(QIcon(swatch)); m_button->setIconSize(swatch.size()); } QSize ColourButton::sizeHint() const { return m_button->sizeHint(); } // --- the dialog ------------------------------------------------------------------------ AdvancedDialog::AdvancedDialog(const ClassicScheme &scheme, preview::Previewer *previewer, QWidget *parent) : QDialog(parent) , m_scheme(scheme) , m_previewer(previewer) { build(); } AdvancedDialog::AdvancedDialog(const Look &look, const Adjustments &adjustments, preview::Previewer *previewer, QWidget *parent) : QDialog(parent) , m_previewer(previewer) , m_styleMode(true) , m_look(look) , m_adjust(adjustments) , m_start(adjustments) , m_pending(adjustments) { // An item's art all turned as its first selector is: what an older // XPlasma saved has no rule for a selector added since (the tray's // buttons under the Taskbar). OK keeps them. for (const StyleItem &item : kStyleItems) { if (!item.colour1.art) continue; const Tint turn = artTurn(m_adjust, item); for (const char *const *s = item.colour1.art + 1; *s; ++s) { const TintRule r = ruleFor(QLatin1String(*s)); if (m_adjust.tint.forSection(r.sub, r.cls, r.part, r.state).second < 0) m_adjust.tint.setRule(QLatin1String(*s), turn); } } m_pending = m_adjust; m_scheme = m_look.adjusted(m_adjust).system; // (Edits gathered up: the windows follow at most every 30 ms.) m_live = new QTimer(this); m_live->setSingleShot(true); m_live->setInterval(30); connect(m_live, &QTimer::timeout, this, [this] { render(); if (!testAttribute(Qt::WA_DontShowOnScreen)) { sendLookPreview(m_pending); // (What the Start button is drawn from: its own item, and the // taskbar its end carries.) const QString label = m_item->currentText(); if (label == QLatin1String("Start Button") || label == QLatin1String("Taskbar")) m_liveStart->start(); } }); // The running taskbar's Start button: redrawn once edits pause. m_liveStart = new QTimer(this); m_liveStart->setSingleShot(true); m_liveStart->setInterval(250); connect(m_liveStart, &QTimer::timeout, this, [this] { LiveSession session; m_previewedStart |= previewStartButton(session, m_look.adjusted(m_pending)); }); build(); } void AdvancedDialog::build() { setWindowTitle(QStringLiteral("Advanced Appearance")); auto *layout = new QVBoxLayout(this); m_sample = new QLabel; m_sample->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); m_sample->setFixedSize(preview::kAppearanceSize + QSize(4, 4)); m_sample->setAlignment(Qt::AlignCenter); layout->addWidget(m_sample, 0, Qt::AlignHCenter); auto *blurb = new QLabel(m_styleMode ? QStringLiteral("Your changes to the following settings are drawn over the windows and buttons " "style you selected.") : QStringLiteral("If you select a windows and buttons setting other than Windows Classic, it will " "override the following settings, except in some older programs.")); blurb->setWordWrap(true); // (Room for the lines it takes at the sample's width, which the dialog is // at least: sized before it wrapped, it ran up over the sample.) blurb->setMinimumHeight(blurb->heightForWidth(m_sample->width())); layout->addWidget(blurb); auto *grid = new QGridLayout; m_item = new QComboBox; if (m_styleMode) for (const StyleItem &item : kStyleItems) m_item->addItem(QLatin1String(item.label)); else for (const Item &item : kItems) m_item->addItem(QLatin1String(item.label)); // (An item without a size or font shows them blank, as XP's does: each // field's lowest value, which no item has, reads as nothing.) m_size = new QSpinBox; m_size->setRange(0, 200); m_size->setSpecialValueText(QStringLiteral(" ")); m_colour1 = new ColourButton; m_colour2 = new ColourButton; grid->addWidget(new QLabel(QStringLiteral("Item:")), 0, 0); grid->addWidget(new QLabel(QStringLiteral("Size:")), 0, 1); grid->addWidget(new QLabel(QStringLiteral("Color 1:")), 0, 2); grid->addWidget(new QLabel(QStringLiteral("Color 2:")), 0, 3); grid->addWidget(m_item, 1, 0); grid->addWidget(m_size, 1, 1); grid->addWidget(m_colour1, 1, 2); grid->addWidget(m_colour2, 1, 3); m_fontFace = new QComboBox; m_fontFace->addItems(QFontDatabase::families()); m_fontSize = new QSpinBox; m_fontSize->setRange(5, 36); m_fontSize->setSpecialValueText(QStringLiteral(" ")); m_fontColour = new ColourButton; m_bold = new QCheckBox(QStringLiteral("B")); m_italic = new QCheckBox(QStringLiteral("I")); grid->addWidget(new QLabel(QStringLiteral("Font:")), 2, 0); grid->addWidget(new QLabel(QStringLiteral("Size:")), 2, 1); grid->addWidget(new QLabel(QStringLiteral("Color:")), 2, 2); grid->addWidget(m_fontFace, 3, 0); grid->addWidget(m_fontSize, 3, 1); grid->addWidget(m_fontColour, 3, 2); auto *styleRow = new QHBoxLayout; styleRow->addWidget(m_bold); styleRow->addWidget(m_italic); grid->addLayout(styleRow, 3, 3); grid->setColumnStretch(0, 1); layout->addLayout(grid); auto *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); // A visual style's linked items: synchronized, an item's colour is its // parent's turn on its own colours, and its own colour controls rest. m_sync = new QCheckBox; m_sync->setVisible(false); layout->insertWidget(layout->indexOf(buttons), m_sync); connect(m_sync, &QCheckBox::toggled, this, [this](bool on) { if (m_loading) return; const QString label = m_item->currentText(); if (on) { m_unsynced.remove(label); // Back in step: the parent's turn, down every synchronized link. if (const StyleItem *item = styleItem(label); item && item->syncWith) cascade(m_adjust, label, artTurn(m_adjust, *styleItem(QLatin1String(item->syncWith)))); m_scheme = m_look.adjusted(m_adjust).system; m_pending = m_adjust; if (!m_live->isActive()) m_live->start(); } else { m_unsynced.insert(label); } showItem(); }); m_overlap = new QCheckBox(QStringLiteral("Attempt to correct &taskbar overlap")); m_overlap->setVisible(false); layout->insertWidget(layout->indexOf(buttons), m_overlap); connect(m_overlap, &QCheckBox::toggled, this, [this](bool on) { const StyleItem *item = styleItem(m_item->currentText()); if (m_loading || !item || !item->sharedEdge) return; m_adjust.overrides.set(QLatin1String(item->sharedEdge), QStringLiteral("xplasmasharededge"), on ? QString() : QStringLiteral("none")); m_pending = m_adjust; if (!m_live->isActive()) m_live->start(); }); layout->addWidget(buttons); connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); if (m_styleMode) { // Every item back to what the style's own file says (live, like any // edit; Cancel still undoes it). QPushButton *reset = buttons->addButton(QStringLiteral("&Reset"), QDialogButtonBox::ResetRole); connect(reset, &QPushButton::clicked, this, [this] { m_adjust = Adjustments(); m_scheme = m_look.adjusted(m_adjust).system; m_pending = m_adjust; showItem(); if (!m_live->isActive()) m_live->start(); }); } connect(m_item, &QComboBox::currentIndexChanged, this, &AdvancedDialog::showItem); connect(m_size, &QSpinBox::valueChanged, this, &AdvancedDialog::changed); connect(m_fontSize, &QSpinBox::valueChanged, this, &AdvancedDialog::changed); connect(m_fontFace, &QComboBox::currentTextChanged, this, &AdvancedDialog::changed); for (ColourButton *b : {m_colour1, m_colour2, m_fontColour}) { connect(b, &ColourButton::picked, this, &AdvancedDialog::changed); // A visual style's windows follow the picker while it's open. if (m_styleMode) connect(b, &ColourButton::changing, this, [this, b](const QColor &c) { m_pending = styleChanged(b, c); if (!m_live->isActive()) m_live->start(); }); } for (QCheckBox *b : {m_bold, m_italic}) connect(b, &QCheckBox::toggled, this, &AdvancedDialog::changed); m_item->setCurrentIndex(m_item->findText(QStringLiteral("Desktop"))); // as XP opens on it if (m_styleMode) // Which links are off: an item whose turn isn't its parent's. for (const StyleItem &item : kStyleItems) if (item.syncWith && !(artTurn(m_adjust, item) == artTurn(m_adjust, *styleItem(QLatin1String(item.syncWith))))) m_unsynced.insert(QLatin1String(item.label)); showItem(); render(); } void AdvancedDialog::showItemNamed(const QString &label) { if (const int i = m_item->findText(label); i >= 0) m_item->setCurrentIndex(i); } void AdvancedDialog::done(int result) { // A visual style cancelled: the windows back to how they were. if (m_styleMode && result != Accepted && !testAttribute(Qt::WA_DontShowOnScreen) && m_adjust != m_start) sendLookPreview(m_start); // ...and the Start button (Accepted, the edits stand: a pending one now). const bool pending = m_liveStart && m_liveStart->isActive(); if (pending) m_liveStart->stop(); if ((pending && result == Accepted) || (m_previewedStart && result != Accepted)) { LiveSession session; m_previewedStart |= previewStartButton(session, m_look.adjusted(result == Accepted ? m_pending : m_start)); } QDialog::done(result); } void AdvancedDialog::showItem() { if (m_styleMode) { showStyleItem(); return; } const Item &item = kItems[m_item->currentIndex()]; m_loading = true; int *size = itemMetric(m_scheme, item.size); m_size->setEnabled(size); m_size->setValue(size ? *size : 0); m_colour1->setEnabled(item.colour1); m_colour1->setColour(item.colour1 ? m_scheme.colour(QString::fromLatin1(item.colour1)) : QColor(Qt::gray)); m_colour2->setEnabled(item.colour2); m_colour2->setColour(item.colour2 ? m_scheme.colour(QString::fromLatin1(item.colour2)) : QColor(Qt::gray)); LogFont *f = itemFont(m_scheme, item.font); for (QWidget *w : std::initializer_list{m_fontFace, m_fontSize, m_bold, m_italic}) w->setEnabled(f); if (f) { m_fontFace->setCurrentText(f->face); m_fontSize->setValue(f->points()); m_bold->setChecked(f->bold()); m_italic->setChecked(f->italic); } else { m_fontFace->setCurrentIndex(-1); m_fontSize->setValue(m_fontSize->minimum()); m_bold->setChecked(false); m_italic->setChecked(false); } m_fontColour->setEnabled(item.fontColour); m_fontColour->setColour(item.fontColour ? m_scheme.colour(QString::fromLatin1(item.fontColour)) : QColor(Qt::gray)); m_loading = false; } void AdvancedDialog::changed() { if (m_loading) return; if (m_styleMode) { m_adjust = styleChanged(); m_scheme = m_look.adjusted(m_adjust).system; m_pending = m_adjust; if (!m_live->isActive()) m_live->start(); return; } const Item &item = kItems[m_item->currentIndex()]; if (int *size = itemMetric(m_scheme, item.size)) *size = m_size->value(); if (item.colour1) m_scheme.setColour(QString::fromLatin1(item.colour1), m_colour1->colour()); if (item.colour2) m_scheme.setColour(QString::fromLatin1(item.colour2), m_colour2->colour()); if (item.fontColour) m_scheme.setColour(QString::fromLatin1(item.fontColour), m_fontColour->colour()); if (LogFont *f = itemFont(m_scheme, item.font)) { const bool italic = m_italic->isChecked(); *f = LogFont::fromPoints(m_fontFace->currentText(), m_fontSize->value(), m_bold->isChecked()); f->italic = italic; } // The scheme is the user's own now. if (!m_scheme.name.endsWith(QLatin1String(" (modified)"))) m_scheme.name += QLatin1String(" (modified)"); render(); } void AdvancedDialog::render() { if (m_styleMode) { const Look look = m_look.adjusted(m_pending); m_sample->setPixmap(QPixmap::fromImage(m_previewer->appearance(look, preview::kAppearanceSize, look.system.colour(u"Background")))); return; } const QImage image = m_previewer->appearance(Look::fromScheme(m_scheme), preview::kAppearanceSize, m_scheme.colour(u"Background")); m_sample->setPixmap(QPixmap::fromImage(image)); } // --- a visual style's items ---------------------------------------------------------------- namespace { // The colour a style colour has now: a [SysMetrics] colour as the adjusted // style has it; art as its own colour (from the style's file), turned by the // rule its first selector has. // The colour art has, as the style draws it: its part's opaque pixels // averaged (a style's colour hint can say otherwise -- Royale Noir's // captions are hinted blue, drawn slate). None for a selector without a part. std::optional artColour(const Look &look, const QString &selector) { const TintRule r = ruleFor(selector); const int part = r.part.isEmpty() ? 0 : schema::partId(r.cls, r.part); if (!look.base || part <= 0) return std::nullopt; const int state = r.state.isEmpty() ? 1 : schema::stateId(r.cls, r.part, r.state); const QImage art = Renderer(look.base).stateImage({r.cls, part, qMax(1, state), r.sub}); double sum[3] = {}, n = 0; for (int y = 0; y < art.height(); ++y) for (int x = 0; x < art.width(); ++x) if (const QRgb p = art.pixel(x, y); qAlpha(p) > 128) { sum[0] += qRed(p); sum[1] += qGreen(p); sum[2] += qBlue(p); ++n; } if (n == 0) return std::nullopt; return QColor(qRound(sum[0] / n), qRound(sum[1] / n), qRound(sum[2] / n)); } // What an item's art turns from: its art's own colour, else the colour the // style gives for it. QColor artReference(const Look &look, const StyleColour &c, const QColor &fallback) { if (const auto drawn = artColour(look, QLatin1String(c.refSection ? c.refSection : c.art[0]))) return *drawn; return c.refSection ? themeColour(*look.base, QLatin1String(c.refSection), QLatin1String(c.refProperty)) .value_or(themeColour(*look.base, QStringLiteral("sysmetrics"), QLatin1String(c.sys ? c.sys : "activecaption")) .value_or(fallback)) : themeColour(*look.base, QStringLiteral("sysmetrics"), QLatin1String(c.sys)).value_or(fallback); } QColor shownColour(const Look &look, const Adjustments &adjust, const StyleColour &c, const QColor &fallback) { if (!look.base) return fallback; if (c.textOf) return themeColour(*Theme::adjusted(look.base, adjust), QLatin1String(c.textOf[0]), QStringLiteral("textcolor")) .value_or(fallback); if (c.art) { const QColor ref = artReference(look, c, fallback); const TintRule r = ruleFor(QLatin1String(c.art[0])); const Tint t = adjust.tint.forSection(r.sub, r.cls, r.part, r.state).first; return t.isIdentity() ? ref : QColor(Tinter(t).apply(ref.rgb())); } const auto theme = Theme::adjusted(look.base, adjust); return themeColour(*theme, QStringLiteral("sysmetrics"), QLatin1String(c.sys)).value_or(fallback); } // `colour` written into `adjust` for `c`: a [SysMetrics] colour as an // override (none if it's the style's own); art as a turn from its own colour // (none if that's no turn), its [SysMetrics] colour following it. void setColour(const Look &look, Adjustments &adjust, const StyleColour &c, const QColor &colour) { if (!look.base || !colour.isValid()) return; const QString sysmetrics = QStringLiteral("sysmetrics"); if (c.textOf) { // Each section's TextColor, none where it's that section's own. const QString value = QStringLiteral("%1 %2 %3").arg(colour.red()).arg(colour.green()).arg(colour.blue()); for (const char *const *s = c.textOf; *s; ++s) { const auto own = themeColour(*look.base, QLatin1String(*s), QStringLiteral("textcolor")); adjust.overrides.set(QLatin1String(*s), QStringLiteral("textcolor"), own && own->rgb() == colour.rgb() ? QString() : value); } return; } if (c.art) { const QColor ref = artReference(look, c, colour); const Tint turn = Tint::toward(ref.rgb(), colour.rgb()); for (const char *const *s = c.art; *s; ++s) adjust.tint.setRule(QLatin1String(*s), turn); if (c.sys) adjust.overrides.set(sysmetrics, QLatin1String(c.sys), QString()); return; } const auto own = themeColour(*look.base, sysmetrics, QLatin1String(c.sys)); adjust.overrides.set(sysmetrics, QLatin1String(c.sys), own && *own == colour ? QString() : QStringLiteral("%1 %2 %3").arg(colour.red()).arg(colour.green()).arg(colour.blue())); } } // namespace void AdvancedDialog::showStyleItem() { const QString label = m_item->currentText(); const StyleItem *found = styleItem(label); if (!found) return; const StyleItem &item = *found; const Item *classic = classicItem(label); // (none for the Taskbar) m_loading = true; // Linked: "Synchronize with ...", on unless its box was turned off. const bool synced = item.syncWith && !m_unsynced.contains(label); m_sync->setVisible(item.syncWith); m_sync->setText(item.syncWith ? QStringLiteral("S&ynchronize with %1").arg(QLatin1String(item.syncWith)) : QString()); m_sync->setChecked(synced); m_overlap->setVisible(item.sharedEdge); m_overlap->setChecked(item.sharedEdge && m_adjust.overrides.value(QLatin1String(item.sharedEdge), QStringLiteral("xplasmasharededge")) != QLatin1String("none")); const auto colourRow = [&](ColourButton *button, const StyleColour &c, const char *classicName, bool followsLink) { const bool on = c.sys || c.art || c.textOf; // (Synchronized, the item's own colour rests: its parent decides.) button->setEnabled(on && !(followsLink && synced)); const QColor fallback = classicName ? m_scheme.colour(QString::fromLatin1(classicName)) : QColor(Qt::gray); button->setColour(on ? shownColour(m_look, m_adjust, c, fallback) : QColor(Qt::gray)); }; colourRow(m_colour1, item.colour1, classic ? classic->colour1 : nullptr, true); colourRow(m_colour2, item.colour2, classic ? classic->colour2 : nullptr, true); colourRow(m_fontColour, item.fontColour, classic ? classic->fontColour : nullptr, false); // Sizes and fonts: what the adjusted style's [SysMetrics] gives (the // scheme made from it). int *size = item.size && classic ? itemMetric(m_scheme, classic->size) : nullptr; m_size->setEnabled(size); m_size->setValue(size ? *size : 0); LogFont *f = item.font && classic ? itemFont(m_scheme, classic->font) : nullptr; // (Or a font of the item's own sections, as the adjusted style has it.) std::optional own = item.fontOf ? iniFont(*Theme::adjusted(m_look.base, m_adjust), QLatin1String(item.fontOf[0])) : std::nullopt; if (!f && own) f = &*own; for (QWidget *w : std::initializer_list{m_fontFace, m_fontSize, m_bold, m_italic}) w->setEnabled(f); if (f) { m_fontFace->setCurrentText(f->face); m_fontSize->setValue(f->points()); m_bold->setChecked(f->bold()); m_italic->setChecked(f->italic); } else { m_fontFace->setCurrentIndex(-1); m_fontSize->setValue(m_fontSize->minimum()); m_bold->setChecked(false); m_italic->setChecked(false); } m_loading = false; } void AdvancedDialog::cascade(Adjustments &adjust, const QString &label, const Tint &turn) const { const StyleItem *item = styleItem(label); if (!item || !item->colour1.art) return; for (const char *const *s = item->colour1.art; *s; ++s) adjust.tint.setRule(QLatin1String(*s), turn); for (const StyleItem &child : kStyleItems) if (child.syncWith && label == QLatin1String(child.syncWith) && !m_unsynced.contains(QLatin1String(child.label))) cascade(adjust, QLatin1String(child.label), turn); } Adjustments AdvancedDialog::styleChanged(ColourButton *liveButton, const QColor &liveColour) const { const QString label = m_item->currentText(); const StyleItem *found = styleItem(label); if (!found) return m_adjust; const StyleItem &item = *found; const Item *classic = classicItem(label); Adjustments a = m_adjust; auto colourOf = [&](ColourButton *b) { return b == liveButton ? liveColour : b->colour(); }; for (const auto &[button, c] : {std::pair{m_colour1, item.colour1}, std::pair{m_colour2, item.colour2}, std::pair{m_fontColour, item.fontColour}}) if (button->isEnabled() && (c.sys || c.art || c.textOf)) { const QColor now = colourOf(button); // (Only what was changed: an untouched swatch writes nothing.) if (now != shownColour(m_look, m_adjust, c, now)) setColour(m_look, a, c, now); } // The art's new turn, down to what's synchronized with it. if (item.colour1.art) cascade(a, label, artTurn(a, item)); // Sizes and fonts, against what the style's own file has. const ClassicScheme own = m_look.adjusted({}).system; ClassicScheme ownCopy = own; const QString sysmetrics = QStringLiteral("sysmetrics"); if (item.size && classic) if (int *ownSize = itemMetric(ownCopy, classic->size)) { const QString value = m_size->value() == *ownSize ? QString() : QString::number(m_size->value()); a.overrides.set(sysmetrics, QLatin1String(item.size), value); if (item.size2) a.overrides.set(sysmetrics, QLatin1String(item.size2), value); } if (item.font && classic) if (LogFont *ownFont = itemFont(ownCopy, classic->font)) { LogFont f = LogFont::fromPoints(m_fontFace->currentText(), m_fontSize->value(), m_bold->isChecked()); f.italic = m_italic->isChecked(); const QString text = fontText(f); a.overrides.set(sysmetrics, QLatin1String(item.font), text == fontText(*ownFont) ? QString() : text); } if (item.fontOf && m_fontFace->isEnabled()) { LogFont f = LogFont::fromPoints(m_fontFace->currentText(), m_fontSize->value(), m_bold->isChecked()); f.italic = m_italic->isChecked(); const QString text = fontText(f); // (Only once the font shown is changed: each section keeps its own till then.) const auto shown = iniFont(*Theme::adjusted(m_look.base, m_adjust), QLatin1String(item.fontOf[0])); for (const char *const *s = item.fontOf; *s && !(shown && fontText(*shown) == text); ++s) { const auto ownFont = iniFont(*m_look.base, QLatin1String(*s)); a.overrides.set(QLatin1String(*s), QStringLiteral("font"), ownFont && fontText(*ownFont) == text ? QString() : text); } } return a; } } // namespace xpl::editor