From 06563efc20d046691225a232ea47df9869af235b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Br=C3=B6tzmann?= <mailtrash@posteo.de>
Date: Tue, 17 Mar 2020 13:15:34 +0100
Subject: [PATCH] Tooltips: Use css colors

---
 gajim/common/config.py       |  6 ------
 gajim/data/style/default.css | 12 ++++++++++++
 gajim/gtk/tooltips.py        | 32 ++++++++++----------------------
 gajim/gtk/util.py            | 13 +++++++++++++
 4 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/gajim/common/config.py b/gajim/common/config.py
index 63ead1963f..92c9a96c88 100644
--- a/gajim/common/config.py
+++ b/gajim/common/config.py
@@ -174,12 +174,6 @@ class Config:
         'tabs_always_visible': [opt_bool, False, _('Show tab when only one conversation?')],
         'tabs_border': [opt_bool, False, _('Show tabbed notebook border in chat windows?')],
         'tabs_close_button': [opt_bool, True, _('Show close button in tab?')],
-        'tooltip_status_online_color': [opt_color, '#73D216'],
-        'tooltip_status_free_for_chat_color': [opt_color, '#3465A4'],
-        'tooltip_status_away_color': [opt_color, '#EDD400'],
-        'tooltip_status_busy_color': [opt_color, '#F57900'],
-        'tooltip_status_na_color': [opt_color, '#CC0000'],
-        'tooltip_status_offline_color': [opt_color, '#555753'],
         'tooltip_account_name_color': [opt_color, '#888A85'],
         'tooltip_idle_color': [opt_color, '#888A85'],
         'notification_preview_message': [opt_bool, True, _('Preview new messages in notification popup?')],
diff --git a/gajim/data/style/default.css b/gajim/data/style/default.css
index f7f8909818..5d4606143f 100644
--- a/gajim/data/style/default.css
+++ b/gajim/data/style/default.css
@@ -77,3 +77,15 @@ .gajim-notify-ft-complete {
 .gajim-notify-other {
 	color: rgb(255, 255, 255);
 }
+.gajim-status-online {
+    color: rgb(102, 191, 16);
+}
+.gajim-status-away {
+    color: rgb(255, 133, 51);
+}
+.gajim-status-dnd {
+    color: rgb(230, 46, 0);
+}
+.gajim-status-offline {
+    color: rgb(154, 154, 154);
+}
diff --git a/gajim/gtk/tooltips.py b/gajim/gtk/tooltips.py
index 8e7165c36d..8fcd58de7d 100644
--- a/gajim/gtk/tooltips.py
+++ b/gajim/gtk/tooltips.py
@@ -39,6 +39,7 @@
 from gajim.common.const import PEPEventType
 from gajim.common.i18n import Q_
 from gajim.common.i18n import _
+from gajim.gtkgui_helpers import add_css_class
 
 from gajim.gtk.util import get_builder
 from gajim.gtk.util import get_icon_name
@@ -46,6 +47,7 @@
 from gajim.gtk.util import format_activity
 from gajim.gtk.util import format_tune
 from gajim.gtk.util import format_location
+from gajim.gtk.util import get_css_show_class
 
 
 log = logging.getLogger('gajim.gtk.tooltips')
@@ -206,7 +208,8 @@ def _populate_grid(self, contact):
 
         # Status
         show = helpers.get_uf_show(contact.show.value)
-        self._ui.user_show.set_markup(colorize_status(show))
+        self._ui.user_show.set_text(show)
+        colorize_status(self._ui.user_show, contact.show)
         self._ui.user_show.show()
 
         # JID
@@ -487,7 +490,8 @@ def _set_idle_time(self, contact):
                 else:
                     show = _('Disconnected')
 
-            self._ui.user_show.set_markup(colorize_status(show))
+            colorize_status(self._ui.user_show, contact.show)
+            self._ui.user_show.set_text(show)
             self._ui.user_show.show()
 
     @staticmethod
@@ -605,25 +609,9 @@ def _get_current_status(file_props):
         return Q_('?transfer status:Not started')
 
 
-def colorize_status(status):
+def colorize_status(widget, show):
     """
-    Colorize the status message inside the tooltip by it's
-    semantics. Color palette is the Tango.
+    Colorize the status message inside the tooltip by it's semantics.
     """
-    formatted = "<span foreground='%s'>%s</span>"
-    color = None
-    if status.startswith(Q_("?user status:Available")):
-        color = app.config.get('tooltip_status_online_color')
-    elif status.startswith(_("Free for Chat")):
-        color = app.config.get('tooltip_status_free_for_chat_color')
-    elif status.startswith(_("Away")):
-        color = app.config.get('tooltip_status_away_color')
-    elif status.startswith(_("Busy")):
-        color = app.config.get('tooltip_status_busy_color')
-    elif status.startswith(_("Not Available")):
-        color = app.config.get('tooltip_status_na_color')
-    elif status.startswith(_("Offline")):
-        color = app.config.get('tooltip_status_offline_color')
-    if color:
-        status = formatted % (color, status)
-    return status
+    css_class = get_css_show_class(show)[14:]
+    add_css_class(widget, css_class, prefix='gajim-status-')
diff --git a/gajim/gtk/util.py b/gajim/gtk/util.py
index 5480a01821..0f7d1e0c33 100644
--- a/gajim/gtk/util.py
+++ b/gajim/gtk/util.py
@@ -28,6 +28,7 @@
 import xml.etree.ElementTree as ET
 from pathlib import Path
 from functools import wraps
+from functools import lru_cache
 
 try:
     from PIL import Image
@@ -652,6 +653,18 @@ def get_color_for_account(account: str) -> str:
     return rgba.to_string()
 
 
+@lru_cache(maxsize=16)
+def get_css_show_class(show):
+    if show in ('online', 'chat'):
+        return '.gajim-status-online'
+    if show in ('away', 'xa'):
+        return '.gajim-status-away'
+    if show == 'dnd':
+        return '.gajim-status-dnd'
+    # 'offline', 'not in roster', 'requested'
+    return '.gajim-status-offline'
+
+
 def scale_with_ratio(size, width, height):
     if height == width:
         return size, size
-- 
GitLab