Fix Dark Theme switch

- Move css_config to gtk module
- Add Combobox for overriding the current session setting
- Move theme methods to css_config
This commit is contained in:
Philipp Hörist 2018-09-26 20:17:13 +02:00
parent ec76d8ac3b
commit cb1be1621d
7 changed files with 103 additions and 67 deletions

View File

@ -46,7 +46,6 @@ from gajim.common import configpaths
from gajim.common import ged as ged_module from gajim.common import ged as ged_module
from gajim.common.contacts import LegacyContactsAPI from gajim.common.contacts import LegacyContactsAPI
from gajim.common.events import Events from gajim.common.events import Events
from gajim.common.css_config import CSSConfig
from gajim.common.types import NetworkEventsControllerT # pylint: disable=unused-import from gajim.common.types import NetworkEventsControllerT # pylint: disable=unused-import
from gajim.common.types import InterfaceT # pylint: disable=unused-import from gajim.common.types import InterfaceT # pylint: disable=unused-import
from gajim.common.types import LoggerT # pylint: disable=unused-import from gajim.common.types import LoggerT # pylint: disable=unused-import
@ -645,6 +644,7 @@ def get_app_window(cls, account=None):
def load_css_config(): def load_css_config():
global css_config global css_config
from gajim.gtk.css_config import CSSConfig
css_config = CSSConfig() css_config = CSSConfig()
def set_win_debug_mode(enable: bool) -> None: def set_win_debug_mode(enable: bool) -> None:

View File

@ -292,6 +292,7 @@ class Config:
'use_keyring': [opt_bool, True, _('If true, Gajim will use the Systems Keyring to store account passwords.')], 'use_keyring': [opt_bool, True, _('If true, Gajim will use the Systems Keyring to store account passwords.')],
'pgp_encoding': [opt_str, '', _('Sets the encoding used by python-gnupg'), True], 'pgp_encoding': [opt_str, '', _('Sets the encoding used by python-gnupg'), True],
'remote_commands': [opt_bool, False, _('If true, Gajim will execute XEP-0146 Commands.')], 'remote_commands': [opt_bool, False, _('If true, Gajim will execute XEP-0146 Commands.')],
'dark_theme': [opt_int, 2, _('2: System, 1: Enabled, 0: Disabled')],
}, {}) # type: Tuple[Dict[str, List[Any]], Dict[Any, Any]] }, {}) # type: Tuple[Dict[str, List[Any]], Dict[Any, Any]]
__options_per_key = { __options_per_key = {

View File

@ -1540,41 +1540,6 @@ $T will be replaced by auto-not-available timeout</property>
<property name="top_attach">0</property> <property name="top_attach">0</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkLabel" id="label174">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Status _iconset</property>
<property name="use_underline">True</property>
<property name="justify">right</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="transports_iconsets_checkbutton">
<property name="label" translatable="yes">Use _transports icons</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="tooltip_text" translatable="yes">If checked, Gajim will use protocol-specific status icons. (e.g. A contact from ICQ will have the equivalent ICQ icon for status online, away, busy, etc...)</property>
<property name="halign">start</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="on_transports_iconsets_checkbutton_toggled" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child> <child>
<object class="GtkComboBoxText" id="theme_combobox"> <object class="GtkComboBoxText" id="theme_combobox">
<property name="width_request">200</property> <property name="width_request">200</property>
@ -1614,21 +1579,72 @@ $T will be replaced by auto-not-available timeout</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkCheckButton" id="enable_dark_theme"> <object class="GtkCheckButton" id="transports_iconsets_checkbutton">
<property name="label" translatable="yes">Enable dark theme</property> <property name="label" translatable="yes">Use _transports icons</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="receives_default">False</property>
<property name="tooltip_text" translatable="yes">If checked, Gajim will use protocol-specific status icons. (e.g. A contact from ICQ will have the equivalent ICQ icon for status online, away, busy, etc...)</property>
<property name="halign">start</property> <property name="halign">start</property>
<property name="use_underline">True</property> <property name="use_underline">True</property>
<property name="draw_indicator">True</property> <property name="draw_indicator">True</property>
<signal name="toggled" handler="on_enable_dark_theme_toggled" swapped="no"/> <signal name="toggled" handler="on_transports_iconsets_checkbutton_toggled" swapped="no"/>
</object> </object>
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
<property name="top_attach">3</property> <property name="top_attach">3</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkLabel" id="label174">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Status _iconset</property>
<property name="use_underline">True</property>
<property name="justify">right</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">Dark Theme</property>
<property name="use_underline">True</property>
<property name="justify">right</property>
<style>
<class name="dim-label"/>
</style>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="dark_theme_combobox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<items>
<item id="2" translatable="yes">System</item>
<item id="1" translatable="yes">Enabled</item>
<item id="0" translatable="yes">Disabled</item>
</items>
<signal name="changed" handler="on_dark_theme_changed" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
</packing>
</child>
<child> <child>
<placeholder/> <placeholder/>
</child> </child>

View File

@ -15,5 +15,12 @@
# Constants for the gtk module # Constants for the gtk module
from collections import namedtuple from collections import namedtuple
from enum import IntEnum, unique
Filter = namedtuple('Filter', 'name pattern default') Filter = namedtuple('Filter', 'name pattern default')
@unique
class Theme(IntEnum):
NOT_DARK = 0
DARK = 1
SYSTEM = 2

View File

@ -28,12 +28,10 @@ from gajim.common import app
from gajim.common import configpaths from gajim.common import configpaths
from gajim.common.const import StyleAttr, CSSPriority from gajim.common.const import StyleAttr, CSSPriority
log = logging.getLogger('gajim.c.css') from gajim.gtk.const import Theme
_settings = Gtk.Settings.get_default() log = logging.getLogger('gajim.gtk.css')
PREFER_DARK = False settings = Gtk.Settings.get_default()
if _settings is not None:
PREFER_DARK = _settings.get_property('gtk-application-prefer-dark-theme')
class CSSConfig(): class CSSConfig():
@ -92,6 +90,7 @@ class CSSConfig():
# Holds all currently available themes # Holds all currently available themes
self.themes = [] self.themes = []
self.set_dark_theme()
self._load_css() self._load_css()
self._gather_available_themes() self._gather_available_themes()
self._load_default() self._load_default()
@ -102,14 +101,37 @@ class CSSConfig():
self._provider, self._provider,
CSSPriority.USER_THEME) CSSPriority.USER_THEME)
@property
def prefer_dark(self):
setting = app.config.get('dark_theme')
if setting == Theme.SYSTEM:
if settings is None:
return False
return settings.get_property('gtk-application-prefer-dark-theme')
return setting == Theme.DARK
@staticmethod
def set_dark_theme(value=None):
if value is None:
value = app.config.get('dark_theme')
else:
app.config.set('dark_theme', value)
if settings is None:
return
if value == Theme.SYSTEM:
settings.reset_property('gtk-application-prefer-dark-theme')
return
settings.set_property('gtk-application-prefer-dark-theme', bool(value))
def _load_css(self): def _load_css(self):
self._load_css_from_file('gajim.css', CSSPriority.APPLICATION) self._load_css_from_file('gajim.css', CSSPriority.APPLICATION)
if PREFER_DARK: if self.prefer_dark:
self._load_css_from_file('gajim-dark.css', self._load_css_from_file('gajim-dark.css',
CSSPriority.APPLICATION_DARK) CSSPriority.APPLICATION_DARK)
self._load_css_from_file('default.css', CSSPriority.DEFAULT_THEME) self._load_css_from_file('default.css', CSSPriority.DEFAULT_THEME)
if PREFER_DARK: if self.prefer_dark:
self._load_css_from_file('default-dark.css', self._load_css_from_file('default-dark.css',
CSSPriority.DEFAULT_THEME_DARK) CSSPriority.DEFAULT_THEME_DARK)
@ -151,9 +173,8 @@ class CSSConfig():
# Ignore user created themes that are named 'default' # Ignore user created themes that are named 'default'
self.themes.remove('default') self.themes.remove('default')
@classmethod def get_theme_path(self, theme, user=True):
def get_theme_path(cls, theme, user=True): if theme == 'default' and self.prefer_dark:
if theme == 'default' and PREFER_DARK:
theme = 'default-dark' theme = 'default-dark'
if user: if user:

View File

@ -24,11 +24,7 @@ from gajim.common import helpers
from gajim.common import configpaths from gajim.common import configpaths
from gajim.common import config as c_config from gajim.common import config as c_config
from gajim.common import idle from gajim.common import idle
from gajim.gtk.util import get_dark_theme
from gajim.gtk.util import set_dark_theme
from gajim.gtk.util import get_builder
from gajim.gtk.dialogs import AspellDictError
from gajim.gtk.themes import Themes
from gajim.advanced_configuration_window import AdvancedConfigurationWindow from gajim.advanced_configuration_window import AdvancedConfigurationWindow
from gajim.chat_control_base import ChatControlBase from gajim.chat_control_base import ChatControlBase
from gajim.config import ManageProxiesWindow, ManageSoundsWindow from gajim.config import ManageProxiesWindow, ManageSoundsWindow
@ -36,6 +32,10 @@ from gajim import message_control
from gajim import cell_renderer_image from gajim import cell_renderer_image
from gajim import gtkgui_helpers from gajim import gtkgui_helpers
from gajim.gtk.util import get_builder
from gajim.gtk.dialogs import AspellDictError
from gajim.gtk.themes import Themes
try: try:
from gajim.common.multimedia_helpers import AudioInputManager, AudioOutputManager from gajim.common.multimedia_helpers import AudioInputManager, AudioOutputManager
from gajim.common.multimedia_helpers import VideoInputManager, VideoOutputManager from gajim.common.multimedia_helpers import VideoInputManager, VideoOutputManager
@ -210,7 +210,8 @@ class Preferences(Gtk.ApplicationWindow):
self.xml.get_object('transports_iconsets_checkbutton').set_active(st) self.xml.get_object('transports_iconsets_checkbutton').set_active(st)
# Dark theme # Dark theme
self.xml.get_object('enable_dark_theme').set_active(get_dark_theme()) dark_theme_combo = self.xml.get_object('dark_theme_combobox')
dark_theme_combo.set_active_id(str(app.config.get('dark_theme')))
### Personal Events tab ### ### Personal Events tab ###
# outgoing send chat state notifications # outgoing send chat state notifications
@ -660,8 +661,8 @@ class Preferences(Gtk.ApplicationWindow):
self.on_checkbutton_toggled(widget, 'use_transports_iconsets') self.on_checkbutton_toggled(widget, 'use_transports_iconsets')
gtkgui_helpers.reload_jabber_state_images() gtkgui_helpers.reload_jabber_state_images()
def on_enable_dark_theme_toggled(self, widget): def on_dark_theme_changed(self, widget):
set_dark_theme(widget.get_active()) app.css_config.set_dark_theme(int(widget.get_active_id()))
def on_outgoing_chat_states_combobox_changed(self, widget): def on_outgoing_chat_states_combobox_changed(self, widget):
active = widget.get_active() active = widget.get_active()

View File

@ -233,13 +233,3 @@ def convert_rgb_to_hex(rgb_string: str) -> str:
green = int(rgb.green * 255) green = int(rgb.green * 255)
blue = int(rgb.blue * 255) blue = int(rgb.blue * 255)
return '#%02x%02x%02x' % (red, green, blue) return '#%02x%02x%02x' % (red, green, blue)
def set_dark_theme(enable: bool) -> None:
settings = Gtk.Settings.get_default()
settings.set_property('gtk-application-prefer-dark-theme', enable)
def get_dark_theme() -> bool:
settings = Gtk.Settings.get_default()
return settings.get_property('gtk-application-prefer-dark-theme')