window.py 3.37 KB
# MIT License
#
# Copyright (c) 2024 Unknown
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# SPDX-License-Identifier: MIT
from .utils import write_config, configure_cr_widgets
from .utils import make_gtk3_themes_list, make_kv_themes_list, separate_themes

from gi.repository import Adw
from gi.repository import Gtk, Gio

@Gtk.Template(resource_path='/ru/ximperlinux/UnifiedThemeSwitcher/window.ui')
class XimperUnifiedThemeSwitcherGuiGtk4Window(Adw.ApplicationWindow):
    __gtype_name__ = 'XimperUnifiedThemeSwitcherGuiGtk4Window'
    
    kv_light_cr = Gtk.Template.Child()
    kv_dark_cr = Gtk.Template.Child()
    
    gtk3_light_cr = Gtk.Template.Child()
    gtk3_dark_cr = Gtk.Template.Child()
    
    change_style_btn = Gtk.Template.Child()
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        
        self.change_style_btn.connect('clicked', self.change_style)
        
        kv_themes_list = make_kv_themes_list()
        gtk3_themes_list = make_gtk3_themes_list()
        
        kv_light_themes_list, kv_dark_themes_list, gtk3_light_themes_list, gtk3_dark_themes_list = separate_themes(kv_themes_list, gtk3_themes_list)

        crs_configurations = [
            (self.kv_light_cr, kv_light_themes_list, "KV_LIGHT_THEME"),
            (self.kv_dark_cr, kv_dark_themes_list, "KV_DARK_THEME"),
            (self.gtk3_light_cr, gtk3_light_themes_list, "GTK3_LIGHT_THEME"),
            (self.gtk3_dark_cr, gtk3_dark_themes_list, "GTK3_DARK_THEME")
        ]
        configure_cr_widgets(crs_configurations, self.cr_theme_selected)
 
    def cr_theme_selected(self, cr, _pspec, cfg_key):
        cfg_new_value = cr.props.selected_item.props.string
        
        if cfg_new_value is not None and cfg_key is not None:
            print(f"For {cfg_key} selected {cfg_new_value}")
            write_config(cfg_key, cfg_new_value)
    
    def change_style(self, _button):
        schema = 'org.gnome.desktop.interface'
        key = 'color-scheme'

        # Получить текущее значение color-scheme
        settings = Gio.Settings.new(schema)
        current_theme = settings.get_string(key)

        # Установить новое значение color-scheme
        if current_theme == 'default':
            style = 'prefer-dark'
        else:
            style = 'default'

        settings.set_string(key, style)
        print(f"Color scheme changed to: {style}")