Skip to content

Commit

Permalink
U auto_theme.py: code refactor & fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
yurenchen000 committed Sep 28, 2024
1 parent 11659f8 commit 3278559
Showing 1 changed file with 38 additions and 53 deletions.
91 changes: 38 additions & 53 deletions auto_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class AutoTheme(plugin.MenuItem):
"""Add custom commands to the terminal menu"""
capabilities = ['terminal_menu']

dark = ''
light = ''
dark = ''
mode = ''
list = []

Expand All @@ -40,7 +40,7 @@ def __init__(self):
self.__class__.load_config()
self.setup_theme_monitor()

## on menu_show // it not really need context-menu
## on menu_show
def callback(self, menuitems, menu, terminal):
item = Gtk.CheckMenuItem(_(' - AutoTheme'))
# item.set_active(True)
Expand All @@ -59,79 +59,64 @@ def setup_theme_monitor():
def _on_theme_name_changed(settings, gparam):
theme_name = settings.get_property('gtk-theme-name')
theme_variant = settings.get_property('gtk-application-prefer-dark-theme')
print('--on_theme_name change:', theme_name, theme_variant)
print('== on_theme_name change:', theme_name, theme_variant)
is_dark = 'dark' in theme_name
AutoTheme.change_theme(is_dark)

Gtk.Settings.get_default().connect("notify::gtk-theme-name", _on_theme_name_changed)


@staticmethod
def change_theme(isdark):
def apply_theme(name):
terminator = plugin.Terminator()
ts = terminator.terminals
# theme = '_light2' if isdark else '_light_day'
theme = AutoTheme.dark if isdark else AutoTheme.light

print('--change_theme:', isdark, theme)
# print('---terminals:', len(ts), ts)
for term in terminator.terminals:
# print('term:', term)
term.set_profile(term.get_vte(), theme)

term.set_profile(term.get_vte(), name)

@classmethod
def save_config(cls, light_sel, dark_sel, mode_sel):
cfg = {}
cfg['light'] = light_sel
cfg['dark'] = dark_sel
cfg['mode'] = mode_sel
@staticmethod
def change_theme(isdark):
# theme = '_light2' if isdark else '_light_day'
theme = AutoTheme.dark if isdark else AutoTheme.light
print('--change_theme:', isdark, theme)
AutoTheme.apply_theme(theme)

cls.light = light_sel
cls.dark = dark_sel
cls.mode = mode_sel
@classmethod
def save_config(cls, light, dark, mode):
print('=== save_config:', light, dark, mode)
cls.light = light
cls.dark = dark
cls.mode = mode

cfg = {'light': light, 'dark': dark, 'mode': mode}
config = Config()
config.plugin_set_config(cls.__name__, cfg)
config.save()

@classmethod
def load_config(cls):
config = Config()
cfg = config.plugin_get_config(cls.__name__)
print('--- config:', cfg)

if cfg:
light_sel = cfg.get('light', '')
dark_sel = cfg.get('dark', '')
model_sel = cfg.get('mode', 'Auto')
print('==cur sel:', light_sel, dark_sel, model_sel)

cls.dark = dark_sel
cls.light = light_sel
cls.mode = model_sel
cur_profile = plugin.Terminator().terminals[0].get_profile()
cfg = Config().plugin_get_config(cls.__name__) or {}
print('--- load_config:', cfg)
cls.light = cfg.get('light', cur_profile)
cls.dark = cfg.get('dark', cur_profile)
cls.mode = cfg.get('mode', 'Auto')
print('=== load_config:', cls.light, cls.dark, cls.mode)

@classmethod
def do_menu_toggle(cls, _widget, terminal):
terminator = plugin.Terminator()
profiles = terminator.config.list_profiles()
cls.list = profiles
print('profiles:', profiles)
cls.list = terminator.config.list_profiles()
print('profiles:', cls.list)
cls.load_config()

dialog = MySettingDialog(None)
dialog.set_list(profiles)

# dialog.set_list_sel(1,2)
# dialog.set_list_sel(0,1)
dialog.set_list(cls.list)
dialog.set_list_sel(cls.light, cls.dark)
dialog.set_mode_sel(cls.mode)

response = dialog.run()

if response == Gtk.ResponseType.OK:
print('Light sel:', dialog.light_sel)
print('Dark sel:', dialog.dark_sel)
print('Mode sel:', dialog.mode_sel)
cls.save_config(dialog.light_sel, dialog.dark_sel, dialog.mode_sel)

dialog.destroy()
Expand Down Expand Up @@ -169,14 +154,14 @@ def __init__(self, parent):

## --------- col0: plain label
light_label = Gtk.Label(label="Light")
dark_label = Gtk.Label(label="Dark")
dark_label = Gtk.Label(label="Dark")
grid.attach(dark_label, 0, 1, 1, 1)
grid.attach(light_label, 0, 0, 1, 1)

light_label.set_name('light_label')
dark_label.set_name('dark_label')
self.light_label = light_label
self.dark_label = dark_label
# self.light_label = light_label
# self.dark_label = dark_label

## --------- col1: profile combo
# Light combo box
Expand Down Expand Up @@ -264,6 +249,7 @@ def set_mode_sel(self, val1):
self.radio_light.set_active(True)
else:
self.radio_dark.set_active(True)
# self.radio_dark.active = True

def add_css(self):
css = b"""
Expand All @@ -288,16 +274,15 @@ def add_css(self):

def on_radio_button_toggled(self, widget):
if widget.get_active():
print('-- mode selected:', widget.get_label())
# unsel = 'Light' if self.mode_sel=='Dark' else 'Dark'
self.mode_sel = widget.get_label()
print(f"- radio selected: {widget.get_label()}")
unsel = 'Light' if self.mode_sel=='Dark' else 'Dark'
self.box.set_name(self.mode_sel)

self.box.set_name(self.mode_sel) # css active

def on_dialog_response(self, dialog, response_id):
print('== on response:', response_id)
# print('== on response:', response_id)
self.light_sel = self.light_combo.get_active_text()
self.dark_sel = self.dark_combo.get_active_text()
self.dark_sel = self.dark_combo.get_active_text()
if response_id == Gtk.ResponseType.OK:
pass
# elif response_id == Gtk.ResponseType.CANCEL:
Expand Down

0 comments on commit 3278559

Please sign in to comment.