diff --git a/tray/gui/devbuttonsitemdelegate.cpp b/tray/gui/devbuttonsitemdelegate.cpp index 8f5a3cd..219a7cd 100644 --- a/tray/gui/devbuttonsitemdelegate.cpp +++ b/tray/gui/devbuttonsitemdelegate.cpp @@ -45,8 +45,7 @@ void DevButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem textRect.setWidth(textRect.width() - 20); QTextOption textOption; textOption.setAlignment(opt.displayAlignment); - painter->setFont(opt.font); - painter->setPen(opt.palette.color(opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text)); + setupPainterToDrawViewItemText(painter, opt); painter->drawText(textRect, displayText(index.data(Qt::DisplayRole), option.locale), textOption); // draw buttons diff --git a/tray/gui/dirbuttonsitemdelegate.cpp b/tray/gui/dirbuttonsitemdelegate.cpp index b32c34b..8c2e992 100644 --- a/tray/gui/dirbuttonsitemdelegate.cpp +++ b/tray/gui/dirbuttonsitemdelegate.cpp @@ -44,8 +44,7 @@ void DirButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem textRect.setWidth(textRect.width() - 58); QTextOption textOption; textOption.setAlignment(opt.displayAlignment); - painter->setFont(opt.font); - painter->setPen(opt.palette.color(opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text)); + setupPainterToDrawViewItemText(painter, opt); painter->drawText(textRect, displayText(index.data(Qt::DisplayRole), option.locale), textOption); // draw buttons diff --git a/tray/gui/helper.cpp b/tray/gui/helper.cpp index a33f0c9..2e03fa8 100644 --- a/tray/gui/helper.cpp +++ b/tray/gui/helper.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -66,4 +67,31 @@ void drawBasicItemViewItem(QPainter &painter, const QStyleOptionViewItem &option } } +void setupPainterToDrawViewItemText(QPainter *painter, QStyleOptionViewItem &opt) +{ + painter->setFont(opt.font); + + if (!(opt.state & QStyle::State_Selected)) { + painter->setPen(opt.palette.color(QPalette::Text)); + return; + } + + // set pen/palette in accordance with the Windows 11 or Windows Vista style for selected items + // note: These styles unfortunately don't just use the highlighted text color and just using it would + // lead to a very bad contrast. +#if defined(Q_OS_WINDOWS) && QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) + auto *const style = opt.widget ? opt.widget->style() : nullptr; + const auto styleName = style ? style->name() : QString(); + if (styleName.compare(QLatin1String("windows11"), Qt::CaseInsensitive) == 0) { + painter->setPen(QPen(opt.palette.buttonText().color())); + return; + } else if (styleName.compare(QLatin1String("windowsvista"), Qt::CaseInsensitive) == 0) { + opt.palette.setColor(QPalette::All, QPalette::HighlightedText, opt.palette.color(QPalette::Active, QPalette::Text)); + opt.palette.setColor(QPalette::All, QPalette::Highlight, opt.palette.base().color().darker(108)); + } +#endif + + painter->setPen(opt.palette.color(QPalette::HighlightedText)); +} + } // namespace QtGui diff --git a/tray/gui/helper.h b/tray/gui/helper.h index 2efa6e6..301b25f 100644 --- a/tray/gui/helper.h +++ b/tray/gui/helper.h @@ -29,6 +29,7 @@ public: void showViewMenu(const QPoint &position, const QTreeView &view, QMenu &menu); void drawBasicItemViewItem(QPainter &painter, const QStyleOptionViewItem &option); +void setupPainterToDrawViewItemText(QPainter *painter, QStyleOptionViewItem &opt); inline auto copyToClipboard(const QString &text) {