From d4100786737579736a6b28aa788d3564ff9b0d9d Mon Sep 17 00:00:00 2001 From: BruebachL <44814898+BruebachL@users.noreply.github.com> Date: Wed, 28 Jan 2026 21:54:11 +0100 Subject: [PATCH] Refresh chat view colors on theme changed. (#6581) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Took 35 minutes Co-authored-by: Lukas BrĂ¼bach --- .../widgets/server/chat_view/chat_view.cpp | 65 ++++++++++++++----- .../widgets/server/chat_view/chat_view.h | 2 + 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/cockatrice/src/interface/widgets/server/chat_view/chat_view.cpp b/cockatrice/src/interface/widgets/server/chat_view/chat_view.cpp index 50375c936..adbae45c5 100644 --- a/cockatrice/src/interface/widgets/server/chat_view/chat_view.cpp +++ b/cockatrice/src/interface/widgets/server/chat_view/chat_view.cpp @@ -28,21 +28,9 @@ ChatView::ChatView(TabSupervisor *_tabSupervisor, AbstractGame *_game, bool _sho userListProxy(_tabSupervisor->getUserListManager()), evenNumber(true), showTimestamps(_showTimestamps), hoveredItemType(HoveredNothing) { - if (palette().windowText().color().lightness() > 200) { - document()->setDefaultStyleSheet(R"( - a { text-decoration: none; color: rgb(71,158,252); } - .blue { color: rgb(71,158,252); } - )"); - serverMessageColor = QColor(0xFF, 0x73, 0x83); - otherUserColor = otherUserColor.lighter(150); - linkColor = QColor(71, 158, 252); - } else { - document()->setDefaultStyleSheet(R"( - a { text-decoration: none; color: blue; } - .blue { color: blue } - )"); - linkColor = palette().link().color(); - } + adjustColorsToPalette(); + + connect(&SettingsCache::instance(), &SettingsCache::themeChanged, this, &ChatView::adjustColorsToPalette); userContextMenu = new UserContextMenu(tabSupervisor, this, game); connect(userContextMenu, SIGNAL(openMessageDialog(QString, bool)), this, SIGNAL(openMessageDialog(QString, bool))); @@ -63,6 +51,53 @@ ChatView::ChatView(TabSupervisor *_tabSupervisor, AbstractGame *_game, bool _sho connect(this, &ChatView::anchorClicked, this, &ChatView::openLink); } +void ChatView::adjustColorsToPalette() +{ + if (palette().windowText().color().lightness() > 200) { + document()->setDefaultStyleSheet(R"( + a { text-decoration: none; color: rgb(71,158,252); } + .blue { color: rgb(71,158,252); } + )"); + serverMessageColor = QColor(0xFF, 0x73, 0x83); + otherUserColor = otherUserColor.lighter(150); + linkColor = QColor(71, 158, 252); + } else { + document()->setDefaultStyleSheet(R"( + a { text-decoration: none; color: blue; } + .blue { color: blue } + )"); + linkColor = palette().link().color(); + } + + QTimer::singleShot(0, this, &ChatView::refreshBlockColors); +} + +void ChatView::refreshBlockColors() +{ + QTextDocument *doc = document(); + QTextCursor cursor(doc); + + bool even = true; // start fresh + + for (QTextBlock block = doc->begin(); block.isValid(); block = block.next()) { + QTextBlockFormat fmt = block.blockFormat(); + + if (even) + fmt.setBackground(palette().window()); + else + fmt.setBackground(palette().base()); + + fmt.setForeground(palette().text()); + + cursor.setPosition(block.position()); + cursor.setBlockFormat(fmt); + + even = !even; + } + + evenNumber = even; // keep future rows consistent +} + void ChatView::retranslateUi() { userContextMenu->retranslateUi(); diff --git a/cockatrice/src/interface/widgets/server/chat_view/chat_view.h b/cockatrice/src/interface/widgets/server/chat_view/chat_view.h index 6cf8370ed..d1939fbea 100644 --- a/cockatrice/src/interface/widgets/server/chat_view/chat_view.h +++ b/cockatrice/src/interface/widgets/server/chat_view/chat_view.h @@ -85,6 +85,8 @@ private: private slots: void openLink(const QUrl &link); void actMessageClicked(); + void adjustColorsToPalette(); + void refreshBlockColors(); public: ChatView(TabSupervisor *_tabSupervisor, AbstractGame *_game, bool _showTimestamps, QWidget *parent = nullptr);