[gnome-tweak-tool] Make the TweakGroup, TWEAK_GROUPS interface optional



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

    Make the TweakGroup, TWEAK_GROUPS interface optional
    
    Tweaks (tweak_foo.py) should now return a list of
    Tweak objects and mark which group the tweak belongs
    to using a new group_name constructor argument, e.g;
    
    TWEAKS = (
        FooTweak('summary', 'description', group_name='bar')
    )
    
    The old TweakGroup interface is still present.
    
    Port all tweaks to the new interface.

 gtweak/tweakmodel.py                    |   55 +++++++++++++++++++++++++++---
 gtweak/tweaks/tweak_font.py             |   28 +++++++---------
 gtweak/tweaks/tweak_interface.py        |   22 +++++-------
 gtweak/tweaks/tweak_nautilus.py         |   19 ++++------
 gtweak/tweaks/tweak_shell_extensions.py |    4 ++-
 gtweak/tweaks/tweak_test.py             |   28 +++++++++-------
 gtweak/tweaks/tweak_windows.py          |   20 ++++-------
 gtweak/widgets.py                       |    3 +-
 po/POTFILES.in                          |    1 +
 9 files changed, 108 insertions(+), 72 deletions(-)
---
diff --git a/gtweak/tweakmodel.py b/gtweak/tweakmodel.py
index d3e7f12..b3f688e 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 = options.get("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..7af313f 100644
--- a/gtweak/tweaks/tweak_font.py
+++ b/gtweak/tweaks/tweak_font.py
@@ -17,21 +17,17 @@
 
 from gi.repository import Gtk
 
-from gtweak.tweakmodel import Tweak, TweakGroup
-from gtweak.widgets import GSettingsRangeTweak, GSettingsFontButtonTweak, GConfFontButtonTweak, GSettingsComboTweak, build_horizontal_sizegroup
+from gtweak.tweakmodel import Tweak, TWEAK_GROUP_FONTS
+from gtweak.widgets import GSettingsRangeTweak, GSettingsFontButtonTweak, GConfFontButtonTweak, GSettingsComboTweak
 
-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, group_name=TWEAK_GROUP_FONTS),
+    GSettingsFontButtonTweak("org.gnome.desktop.interface", "font-name", group_name=TWEAK_GROUP_FONTS),
+    GSettingsFontButtonTweak("org.gnome.desktop.interface", "document-font-name", group_name=TWEAK_GROUP_FONTS),
+    GSettingsFontButtonTweak("org.gnome.desktop.interface", "monospace-font-name", group_name=TWEAK_GROUP_FONTS),
+    GConfFontButtonTweak("/apps/metacity/general/titlebar_font", str, group_name=TWEAK_GROUP_FONTS),
+    GSettingsComboTweak("org.gnome.settings-daemon.plugins.xsettings", "hinting",
+        [(i, i.title()) for i in ("none", "slight", "medium", "full")], group_name=TWEAK_GROUP_FONTS),
+    GSettingsComboTweak("org.gnome.settings-daemon.plugins.xsettings", "antialiasing",
+        [(i, i.title()) for i in ("none", "grayscale", "rgba")], group_name=TWEAK_GROUP_FONTS),
 )
diff --git a/gtweak/tweaks/tweak_interface.py b/gtweak/tweaks/tweak_interface.py
index 31f778f..d2034bf 100644
--- a/gtweak/tweaks/tweak_interface.py
+++ b/gtweak/tweaks/tweak_interface.py
@@ -21,8 +21,8 @@ from gi.repository import Gtk
 
 import gtweak
 from gtweak.utils import walk_directories, make_combo_list_with_default
-from gtweak.tweakmodel import TweakGroup
-from gtweak.widgets import GSettingsSwitchTweak, GSettingsComboTweak, build_horizontal_sizegroup
+from gtweak.tweakmodel import TWEAK_GROUP_INTERFACE
+from gtweak.widgets import GSettingsSwitchTweak, GSettingsComboTweak
 
 class GtkThemeSwitcher(GSettingsComboTweak):
     def __init__(self, **options):
@@ -92,15 +92,11 @@ class KeyThemeSwitcher(GSettingsComboTweak):
                     os.path.isfile(os.path.join(d, "gtk-2.0-key", "gtkrc")))
         return valid
 
-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", group_name=TWEAK_GROUP_INTERFACE),
+    GSettingsSwitchTweak("org.gnome.desktop.interface", "buttons-have-icons", group_name=TWEAK_GROUP_INTERFACE),
+    GtkThemeSwitcher(group_name=TWEAK_GROUP_INTERFACE),
+    KeyThemeSwitcher(group_name=TWEAK_GROUP_INTERFACE),
+    IconThemeSwitcher(group_name=TWEAK_GROUP_INTERFACE),
+    CursorThemeSwitcher(group_name=TWEAK_GROUP_INTERFACE),
 )
diff --git a/gtweak/tweaks/tweak_nautilus.py b/gtweak/tweaks/tweak_nautilus.py
index 22bcd89..ccbf2c5 100644
--- a/gtweak/tweaks/tweak_nautilus.py
+++ b/gtweak/tweaks/tweak_nautilus.py
@@ -19,7 +19,7 @@ from gi.repository import Gtk
 
 import gtweak
 from gtweak.utils import AutostartManager
-from gtweak.tweakmodel import TweakGroup
+from gtweak.tweakmodel import TWEAK_GROUP_FILE_MANAGER
 from gtweak.widgets import GSettingsSwitchTweak
 
 class DesktopIconTweak(GSettingsSwitchTweak):
@@ -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(group_name=TWEAK_GROUP_FILE_MANAGER),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "computer-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml",group_name=TWEAK_GROUP_FILE_MANAGER),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "home-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml",group_name=TWEAK_GROUP_FILE_MANAGER),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "network-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml",group_name=TWEAK_GROUP_FILE_MANAGER),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "trash-icon-visible", schema_filename="org.gnome.nautilus.gschema.xml",group_name=TWEAK_GROUP_FILE_MANAGER),
+    GSettingsSwitchTweak("org.gnome.nautilus.desktop", "volumes-visible", schema_filename="org.gnome.nautilus.gschema.xml",group_name=TWEAK_GROUP_FILE_MANAGER),
 )
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_test.py b/gtweak/tweaks/tweak_test.py
index 14203ca..77af18d 100644
--- a/gtweak/tweaks/tweak_test.py
+++ b/gtweak/tweaks/tweak_test.py
@@ -58,16 +58,20 @@ class _TestButtonTweak(Tweak):
                 self.notify_info(self.name)
 
 TWEAK_GROUPS = (
-        TweakGroup(
-            "Test Foo Bar",
-            _TestTweak("foo bar", "does foo bar"),
-            _TestTweak("foo baz", "does foo baz"),
-            _TestInfoTweak("foo info", "info widget", tweak_info="Information"),
-            _TestInfoTweak("foo warning", "info widget", tweak_warning="Warning"),
-            _TestButtonTweak("Need Action", "foo bar", need_action=True),
-            _TestButtonTweak("Report Error", "foo baz", action_error=True),
-            _TestButtonTweak("Report Info", "foo bob", action_error=False)),
-        TweakGroup(
-            "Test Many Settings",
-            *[_TestTweak("name: " + str(d), "desc: " + str(d)) for d in range(50)]),
+    TweakGroup(
+        "Test Settings Group",
+        *[_TestTweak("name: " + str(d), "desc: " + str(d)) for d in range(50)]),
 )
+
+group_name = "Test Settings"
+
+TWEAKS = (
+    _TestTweak("foo bar", "does foo bar", group_name=group_name),
+    _TestTweak("foo baz", "does foo baz", group_name=group_name),
+    _TestInfoTweak("foo info", "info widget", tweak_info="Information", group_name=group_name),
+    _TestInfoTweak("foo warning", "info widget", tweak_warning="Warning", group_name=group_name),
+    _TestButtonTweak("Need Action", "foo bar", need_action=True, group_name=group_name),
+    _TestButtonTweak("Report Error", "foo baz", action_error=True, group_name=group_name),
+    _TestButtonTweak("Report Info", "foo bob", action_error=False, group_name=group_name),
+)
+
diff --git a/gtweak/tweaks/tweak_windows.py b/gtweak/tweaks/tweak_windows.py
index faee610..0a63ccd 100644
--- a/gtweak/tweaks/tweak_windows.py
+++ b/gtweak/tweaks/tweak_windows.py
@@ -19,8 +19,8 @@ import os.path
 
 import gtweak
 from gtweak.utils import walk_directories, make_combo_list_with_default
-from gtweak.tweakmodel import TweakGroup
-from gtweak.widgets import GConfComboTweak, build_horizontal_sizegroup
+from gtweak.tweakmodel import TWEAK_GROUP_WINDOWS, TWEAK_GROUP_INTERFACE
+from gtweak.widgets import GConfComboTweak
 from gtweak.gconf import GConfSetting
 
 class ActionClickTitlebarTweak(GConfComboTweak):
@@ -72,14 +72,10 @@ class WindowThemeSwitcher(GConfComboTweak):
             self.gconf.set_value(value)
             self.gconf_metacity.set_value(value)
 
-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(group_name=TWEAK_GROUP_INTERFACE),
+    ActionClickTitlebarTweak("/apps/metacity/general/action_double_click_titlebar", group_name=TWEAK_GROUP_WINDOWS),
+    ActionClickTitlebarTweak("/apps/metacity/general/action_middle_click_titlebar", group_name=TWEAK_GROUP_WINDOWS),
+    ActionClickTitlebarTweak("/apps/metacity/general/action_right_click_titlebar", group_name=TWEAK_GROUP_WINDOWS),
+    FocusModeTweak(group_name=TWEAK_GROUP_WINDOWS),
 )
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):
diff --git a/po/POTFILES.in b/po/POTFILES.in
index beb9380..8fc363c 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -9,3 +9,4 @@ gtweak/tweaks/tweak_shell.py
 gtweak/tweaks/tweak_shell_extensions.py
 gtweak/tweaks/tweak_windows.py
 gtweak/utils.py
+gtweak/tweakmodel.py



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