[gnome-tweak-tool/decouple-tweakgroups: 2/2] Port tweaks to the new group-less interface



commit 231c9423fad28398b71147eec24c2336f97fd9cc
Author: John Stowers <john stowers gmail com>
Date:   Sat Aug 6 11:09:25 2011 +1200

    Port tweaks to the new group-less interface

 gtweak/tweakmodel.py                    |   55 +++++++++++++++++++++++++++---
 gtweak/tweaks/tweak_font.py             |   24 ++++++-------
 gtweak/tweaks/tweak_interface.py        |   16 ++++-----
 gtweak/tweaks/tweak_nautilus.py         |   17 ++++------
 gtweak/tweaks/tweak_shell_extensions.py |    4 ++-
 gtweak/tweaks/tweak_windows.py          |   14 +++----
 gtweak/widgets.py                       |    3 +-
 7 files changed, 85 insertions(+), 48 deletions(-)
---
diff --git a/gtweak/tweakmodel.py b/gtweak/tweakmodel.py
index d3e7f12..531d45d 100644
--- a/gtweak/tweakmodel.py
+++ b/gtweak/tweakmodel.py
@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU General Public License
 # along with gnome-tweak-tool.  If not, see <http://www.gnu.org/licenses/>.
 
+import logging
 import glob
 import os.path
 
@@ -22,11 +23,18 @@ import gtweak
 
 from gi.repository import Gtk
 
+TWEAK_GROUP_FONTS = _("Fonts")
+TWEAK_GROUP_INTERFACE = _("Interface")
+TWEAK_GROUP_FILE_MANAGER = _("File Manager")
+TWEAK_GROUP_WINDOWS = _("Windows")
+
+LOG = logging.getLogger(__name__)
+
 class Tweak:
     def __init__(self, name, description, **options):
         self.name = name
         self.description = description
-        self.size_group = options.get('size_group')
+        self.group_name = _("Miscellaneous")
 
         #FIXME: I would have rather done this as a GObject signal, but it
         #would prohibit other tweaks from inheriting from GtkWidgets
@@ -61,11 +69,19 @@ class Tweak:
 class TweakGroup:
     def __init__(self, name, *tweaks):
         self.name = name
-        self.tweaks = [t for t in tweaks]
+        self.tweaks = []
+
+        self._sg = Gtk.SizeGroup(mode=Gtk.SizeGroupMode.HORIZONTAL)
+        self._sg.props.ignore_hidden = True
+
+        self.set_tweaks(*tweaks)
+
+    def set_tweaks(self, *tweaks):
+        self.tweaks += [t for t in tweaks]
 
         for t in tweaks:
-            if t.size_group and t.widget_for_size_group:
-                t.size_group.add_widget(t.widget_for_size_group)
+            if t.widget_for_size_group:
+                self._sg.add_widget(t.widget_for_size_group)
 
 class TweakModel(Gtk.ListStore):
     (COLUMN_NAME,
@@ -78,6 +94,9 @@ class TweakModel(Gtk.ListStore):
 
         self.set_sort_column_id(self.COLUMN_NAME, Gtk.SortType.ASCENDING)
 
+        # map of tweakgroup.name -> tweakgroup
+        self._tweak_group_names = {}
+
     @property
     def tweaks(self):
         return [t for row in self for t in row[TweakModel.COLUMN_TWEAK].tweaks]
@@ -100,13 +119,37 @@ class TweakModel(Gtk.ListStore):
             except ValueError:
                 pass
         
+        groups = []
+        tweaks = []
+
         mods = __import__("gtweak.tweaks", globals(), locals(), tweak_files, 0)
         for mod in [getattr(mods, file_name) for file_name in tweak_files]:
-            for group in mod.TWEAK_GROUPS:
-                self.add_tweak_group(group)
+            groups.extend( getattr(mod, "TWEAK_GROUPS", []) )
+            tweaks.extend( getattr(mod, "TWEAKS", []) )
+
+        for g in groups:
+            self.add_tweak_group(g)
+
+        for t in tweaks:
+            self.add_tweak_auto_to_group(t)
 
     def add_tweak_group(self, tweakgroup):
+        if tweakgroup.name in self._tweak_group_names:
+            LOG.critical("Tweak group named: %s already exists" % tweakgroup.name)
+            return
+
         self.append([tweakgroup.name, tweakgroup])
+        self._tweak_group_names[tweakgroup.name] = tweakgroup
+
+    def add_tweak_auto_to_group(self, tweak):
+        name = tweak.group_name
+        try:
+            group = self._tweak_group_names[name]
+        except KeyError:
+            group = TweakGroup(name)
+            self.add_tweak_group(group)
+
+        group.set_tweaks(tweak)
       
     def search_matches(self, txt):
         return [t for t in self.tweaks if t.search_matches(txt)]
diff --git a/gtweak/tweaks/tweak_font.py b/gtweak/tweaks/tweak_font.py
index a16aef6..9356508 100644
--- a/gtweak/tweaks/tweak_font.py
+++ b/gtweak/tweaks/tweak_font.py
@@ -17,21 +17,19 @@
 
 from gi.repository import Gtk
 
-from gtweak.tweakmodel import Tweak, TweakGroup
+from gtweak.tweakmodel import Tweak, TWEAK_GROUP_FONTS
 from gtweak.widgets import GSettingsRangeTweak, GSettingsFontButtonTweak, GConfFontButtonTweak, GSettingsComboTweak, build_horizontal_sizegroup
 
 sg = build_horizontal_sizegroup()
 
-TWEAK_GROUPS = (
-        TweakGroup(
-            _("Fonts"),
-            GSettingsRangeTweak("org.gnome.desktop.interface", "text-scaling-factor", adjustment_step=0.1, size_group=sg),
-            GSettingsFontButtonTweak("org.gnome.desktop.interface", "font-name", size_group=sg),
-            GSettingsFontButtonTweak("org.gnome.desktop.interface", "document-font-name", size_group=sg),
-            GSettingsFontButtonTweak("org.gnome.desktop.interface", "monospace-font-name", size_group=sg),
-            GConfFontButtonTweak("/apps/metacity/general/titlebar_font", str, size_group=sg),
-            GSettingsComboTweak("org.gnome.settings-daemon.plugins.xsettings", "hinting",
-                [(i, i.title()) for i in ("none", "slight", "medium", "full")], size_group=sg),
-            GSettingsComboTweak("org.gnome.settings-daemon.plugins.xsettings", "antialiasing",
-                [(i, i.title()) for i in ("none", "grayscale", "rgba")], size_group=sg)),
+TWEAKS = (
+    GSettingsRangeTweak("org.gnome.desktop.interface", "text-scaling-factor", adjustment_step=0.1, size_group=sg),
+    GSettingsFontButtonTweak("org.gnome.desktop.interface", "font-name", size_group=sg),
+    GSettingsFontButtonTweak("org.gnome.desktop.interface", "document-font-name", size_group=sg),
+    GSettingsFontButtonTweak("org.gnome.desktop.interface", "monospace-font-name", size_group=sg),
+    GConfFontButtonTweak("/apps/metacity/general/titlebar_font", str, size_group=sg),
+    GSettingsComboTweak("org.gnome.settings-daemon.plugins.xsettings", "hinting",
+        [(i, i.title()) for i in ("none", "slight", "medium", "full")], size_group=sg),
+    GSettingsComboTweak("org.gnome.settings-daemon.plugins.xsettings", "antialiasing",
+        [(i, i.title()) for i in ("none", "grayscale", "rgba")], size_group=sg),
 )
diff --git a/gtweak/tweaks/tweak_interface.py b/gtweak/tweaks/tweak_interface.py
index 31f778f..c258fbe 100644
--- a/gtweak/tweaks/tweak_interface.py
+++ b/gtweak/tweaks/tweak_interface.py
@@ -94,13 +94,11 @@ class KeyThemeSwitcher(GSettingsComboTweak):
 
 sg = build_horizontal_sizegroup()
 
-TWEAK_GROUPS = (
-        TweakGroup(
-            _("Interface"),
-            GSettingsSwitchTweak("org.gnome.desktop.interface", "menus-have-icons"),
-            GSettingsSwitchTweak("org.gnome.desktop.interface", "buttons-have-icons"),
-            GtkThemeSwitcher(size_group=sg),
-            KeyThemeSwitcher(size_group=sg),
-            IconThemeSwitcher(size_group=sg),
-            CursorThemeSwitcher(size_group=sg)),
+TWEAKS = (
+    GSettingsSwitchTweak("org.gnome.desktop.interface", "menus-have-icons"),
+    GSettingsSwitchTweak("org.gnome.desktop.interface", "buttons-have-icons"),
+    GtkThemeSwitcher(size_group=sg),
+    KeyThemeSwitcher(size_group=sg),
+    IconThemeSwitcher(size_group=sg),
+    CursorThemeSwitcher(size_group=sg),
 )
diff --git a/gtweak/tweaks/tweak_nautilus.py b/gtweak/tweaks/tweak_nautilus.py
index 22bcd89..3e34b25 100644
--- a/gtweak/tweaks/tweak_nautilus.py
+++ b/gtweak/tweaks/tweak_nautilus.py
@@ -43,14 +43,11 @@ class DesktopIconTweak(GSettingsSwitchTweak):
         self.nautilus.update_start_at_login(
                 self.settings.get_boolean(key))
 
-TWEAK_GROUPS = (
-        TweakGroup(
-            _("File Manager"),
-            DesktopIconTweak(),
-            GSettingsSwitchTweak("org.gnome.nautilus.desktop", "computer-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
-            GSettingsSwitchTweak("org.gnome.nautilus.desktop", "home-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
-            GSettingsSwitchTweak("org.gnome.nautilus.desktop", "network-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
-            GSettingsSwitchTweak("org.gnome.nautilus.desktop", "trash-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
-            GSettingsSwitchTweak("org.gnome.nautilus.desktop", "volumes-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
-            ),
+TWEAKS = (
+    DesktopIconTweak(),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "computer-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "home-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "network-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "trash-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "volumes-visible", schema_filename="org.gnome.nautilus.gschema.xml"),
 )
diff --git a/gtweak/tweaks/tweak_shell_extensions.py b/gtweak/tweaks/tweak_shell_extensions.py
index a212994..9f58bf1 100644
--- a/gtweak/tweaks/tweak_shell_extensions.py
+++ b/gtweak/tweaks/tweak_shell_extensions.py
@@ -141,6 +141,8 @@ class _ShellExtensionInstallerTweak(Tweak):
 
 class ShellExtensionTweakGroup(TweakGroup):
     def __init__(self):
+        TweakGroup.__init__(self, _("Shell Extensions"))
+
         extension_tweaks = []
         sg = build_horizontal_sizegroup()
 
@@ -166,7 +168,7 @@ class ShellExtensionTweakGroup(TweakGroup):
         except:
             logging.warning("Error detecting shell")
 
-        TweakGroup.__init__(self, _("Shell Extensions"), *extension_tweaks)
+        self.set_tweaks(*extension_tweaks)
 
 TWEAK_GROUPS = (
         ShellExtensionTweakGroup(),
diff --git a/gtweak/tweaks/tweak_windows.py b/gtweak/tweaks/tweak_windows.py
index faee610..1432d09 100644
--- a/gtweak/tweaks/tweak_windows.py
+++ b/gtweak/tweaks/tweak_windows.py
@@ -74,12 +74,10 @@ class WindowThemeSwitcher(GConfComboTweak):
 
 sg = build_horizontal_sizegroup()
 
-TWEAK_GROUPS = (
-        TweakGroup(
-            _("Windows"),
-            WindowThemeSwitcher(size_group=sg),
-            ActionClickTitlebarTweak("/apps/metacity/general/action_double_click_titlebar", size_group=sg),
-            ActionClickTitlebarTweak("/apps/metacity/general/action_middle_click_titlebar", size_group=sg),
-            ActionClickTitlebarTweak("/apps/metacity/general/action_right_click_titlebar", size_group=sg),
-            FocusModeTweak(size_group=sg)),
+TWEAKS = (
+    WindowThemeSwitcher(size_group=sg),
+    ActionClickTitlebarTweak("/apps/metacity/general/action_double_click_titlebar", size_group=sg),
+    ActionClickTitlebarTweak("/apps/metacity/general/action_middle_click_titlebar", size_group=sg),
+    ActionClickTitlebarTweak("/apps/metacity/general/action_right_click_titlebar", size_group=sg),
+    FocusModeTweak(size_group=sg),
 )
diff --git a/gtweak/widgets.py b/gtweak/widgets.py
index facf26b..1137a0d 100644
--- a/gtweak/widgets.py
+++ b/gtweak/widgets.py
@@ -109,7 +109,8 @@ class GSettingsSwitchTweak(_GSettingsTweak):
         w = Gtk.Switch()
         self.settings.bind(key_name, w, "active", Gio.SettingsBindFlags.DEFAULT)
         self.widget = build_label_beside_widget(self.settings.schema_get_summary(key_name), w)
-        self.widget_for_size_group = w
+        # never change the size of a switch
+        self.widget_for_size_group = None
 
 class GSettingsFontButtonTweak(_GSettingsTweak):
     def __init__(self, schema_name, key_name, **options):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]