← Back to team overview

elementaryart team mailing list archive

[Merge] lp:~victored/granite/mode-button-cleanup into lp:granite

 

Victor Eduardo has proposed merging lp:~victored/granite/mode-button-cleanup into lp:granite.

Requested reviews:
  xapantu (xapantu)

For more details, see:
https://code.launchpad.net/~victored/granite/mode-button-cleanup/+merge/89970

See lp:914502

Granite.Widgets.ModeButton
* Code cleanup (this includes coding style fixes)
* Reworked symbolic icon support to use GTK's built-in feature

Granite Demo:
* Added symbolic icons to the toolbar's mode button
* Updated welcome screen text
-- 
https://code.launchpad.net/~victored/granite/mode-button-cleanup/+merge/89970
Your team elementaryart (old) is subscribed to branch lp:granite.
=== modified file 'demo/main.vala'
--- demo/main.vala	2012-01-11 20:08:44 +0000
+++ demo/main.vala	2012-01-24 18:29:25 +0000
@@ -69,7 +69,7 @@
         /* welcome */
 
         // These strings will be automatically corrected by the widget
-        var welcome = new Welcome("welcome widget", "description text");
+        var welcome = new Welcome("Granite's Welcome Screen", "This is Granite's Welcome widget.");
         notebook.append_page(welcome, new Gtk.Label("Welcome"));
 
         Gdk.Pixbuf? pixbuf = null;
@@ -84,10 +84,10 @@
         Gtk.Image? image = new Gtk.Image.from_icon_name("document-open", Gtk.IconSize.DIALOG);
 
         // Adding elements. Use the most convenient function to add an icon
-        welcome.append_with_pixbuf(pixbuf, "create", "write a new document");
-        welcome.append_with_image(image, "open", "select a file");
-        welcome.append("document-save", "save", "with a much longer description");
-        welcome.append("help-info", "Discover", "Learn more about this app");
+        welcome.append_with_pixbuf(pixbuf, "Create", "Write a new document.");
+        welcome.append_with_image(image, "Open", "select a file.");
+        welcome.append("document-save", "Save", "With a much longer description.");
+        welcome.append("help-info", "Discover", "Learn more about this application.");
 
         /* modebutton */
         var mode_button = new ModeButton();
@@ -104,10 +104,10 @@
         toolbar.get_style_context().add_class("primary-toolbar");
         var toolbutton = new Gtk.ToolItem();
         var tool_mode = new ModeButton();
-        tool_mode.append(new Gtk.Label("1"));
-        tool_mode.append(new Gtk.Label("2"));
-        tool_mode.append(new Gtk.Label("3"));
-        tool_mode.append(new Gtk.Label("4"));
+        tool_mode.append_icon ("view-list-column-symbolic", Gtk.IconSize.MENU);
+        tool_mode.append_icon ("view-list-details-symbolic", Gtk.IconSize.MENU);
+        tool_mode.append_icon ("view-list-icons-symbolic", Gtk.IconSize.MENU);
+        tool_mode.append_icon ("view-list-video-symbolic", Gtk.IconSize.MENU);
         toolbutton.add(tool_mode);
         toolbar.insert(toolbutton, -1);
         toolbar.insert(create_appmenu(new Gtk.Menu()), -1);

=== modified file 'lib/Widgets/ModeButton.vala'
--- lib/Widgets/ModeButton.vala	2012-01-22 04:01:12 +0000
+++ lib/Widgets/ModeButton.vala	2012-01-24 18:29:25 +0000
@@ -27,13 +27,10 @@
         public signal void mode_removed (int index, Gtk.Widget widget);
         public signal void mode_changed (Gtk.Widget widget);
 
-        /* Style properties. Please note that style class names are for internal
-           usage only. Theme developers should use GraniteWidgetsModeButton instead.
-         */
-
+        // Style properties. Please note that style class names are for internal
+        // use only. Theme developers should use GraniteWidgetsModeButton instead.
         internal static CssProvider style_provider;
         internal static StyleContext widget_style;
-
         private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
 
         private const string STYLESHEET = """
@@ -80,7 +77,7 @@
 
         public uint n_items {
             get {
-                return get_children().length();
+                return get_children ().length ();
             }
         }
 
@@ -102,84 +99,90 @@
             homogeneous = true;
             spacing = 0;
             app_paintable = true;
-            set_visual (get_screen().get_rgba_visual());
+            set_visual (get_screen ().get_rgba_visual ());
 
             can_focus = true;
         }
 
         public void append_pixbuf (Gdk.Pixbuf? pixbuf) {
-            if (pixbuf == null)
+            if (pixbuf == null) {
+                warning ("GraniteWidgetsModeButton: Attempt to add null pixbuf failed.");
                 return;
+            }
 
             var image = new Image.from_pixbuf (pixbuf);
             append (image);
         }
 
         public void append_text (string? text) {
-            if (text == null)
+            if (text == null) {
+                warning ("GraniteWidgetsModeButton: Attempt to add null text string failed.");
                 return;
+            }
 
             append (new Gtk.Label(text));
         }
 
         /**
-         * This is the recommended function for adding icons to the ModeButton widget.
-         * If you pass the name of a symbolic icon, it will be properly themed for
-         * every state of the widget. That is, it will match the foreground color
+         * This is the recommended method for adding icons to the ModeButton widget.
+         * If the name of a symbolic icon is passed, it will be properly themed for
+         * each state of the widget. That is, it will match the foreground color
          * defined by the theme for each state (active, prelight, insensitive, etc.)
          */
         public void append_icon (string icon_name, Gtk.IconSize size) {
-            append_mode_button_item (null, icon_name, size);
+            append (new Image.from_icon_name (icon_name, size));
         }
 
         public void append (Gtk.Widget w) {
-            append_mode_button_item (w, null, null);
-        }
-
-        /**
-         * This function adds the foreground style properties of the given style
-         * context to the widget's icons. This is useful when you want to make the widget
-         * adapt its symbolic icon color to that of the parent in case the GTK+
-         * theme has not set them correctly. This function only affects the behavior
-         * of icons added with append_icon().
-         */
-        public void set_icon_foreground_style (Gtk.StyleContext icon_style) {
-            foreach (weak Widget button in get_children ()) {
-                (button as ModeButtonItem).set_icon_foreground_style (icon_style);
+            if (w == null) {
+                warning ("GraniteWidgetsModeButton: Attempt to add null widget failed.");
+                return;
             }
+
+            var button = new ModeButtonItem ();
+
+            button.add (w);
+
+            button.button_press_event.connect (() => {
+                int selected = get_children().index (button);
+                set_active (selected);
+                return true;
+            });
+
+            add (button);
+            button.show_all ();
+
+            mode_added ((int)get_children ().length (), w);
         }
 
         public void set_active (int new_active_index) {
-
-            if (new_active_index >= get_children().length () || _selected == new_active_index)
+            if (new_active_index >= get_children ().length () || _selected == new_active_index)
                 return;
 
             if (_selected >= 0)
-                ((ToggleButton) get_children().nth_data(_selected)).set_active (false);
+                ((ToggleButton) get_children ().nth_data (_selected)).set_active (false);
 
             _selected = new_active_index;
-            ((ToggleButton) get_children().nth_data(_selected)).set_active (true);
+            ((ToggleButton) get_children ().nth_data (_selected)).set_active (true);
 
-            mode_changed(((ToggleButton) get_children().nth_data(_selected)).get_child());
+            mode_changed (((ToggleButton) get_children ().nth_data (_selected)).get_child ());
         }
 
-        public void set_item_visible(int index, bool val) {
-            var item = get_children().nth_data(index);
-            if(item == null)
+        public void set_item_visible (int index, bool val) {
+            var item = get_children ().nth_data (index);
+            if (item == null)
                 return;
 
-            item.set_no_show_all(!val);
-            item.set_visible(val);
+            item.set_no_show_all (!val);
+            item.set_visible (val);
         }
 
-        public new void remove(int index)
-        {
-            mode_removed(index, (get_children().nth_data(index) as Gtk.Bin).get_child ());
-            get_children().nth_data(index).destroy();
+        public new void remove (int index) {
+            mode_removed (index, (get_children ().nth_data (index) as Gtk.Bin).get_child ());
+            get_children ().nth_data (index).destroy ();
         }
 
         public void clear_children () {
-
             foreach (weak Widget button in get_children ()) {
                 button.hide ();
                 if (button.get_parent () != null)
@@ -190,7 +193,7 @@
         }
 
         protected override bool scroll_event (EventScroll ev) {
-            if(ev.direction == Gdk.ScrollDirection.DOWN) {
+            if (ev.direction == Gdk.ScrollDirection.DOWN) {
                 selected ++;
             }
             else if (ev.direction == Gdk.ScrollDirection.UP) {
@@ -199,110 +202,16 @@
 
             return false;
         }
-
-        private void append_mode_button_item (Gtk.Widget? w, string? icon_name, Gtk.IconSize? size) {
-            var button = new ModeButtonItem ();
-
-            /* Modifying properties */
-            if (icon_name != null && size != null && w == null) {
-                button.set_icon (icon_name, size);
-            } else {
-                button.add(w);
-            }
-
-            button.button_press_event.connect (() => {
-                int selected = get_children().index (button);
-                set_active (selected);
-                return true;
-            });
-
-            add(button);
-            button.show_all ();
-
-            mode_added((int)get_children().length(), w);
-        }
-
     }
 
     private class ModeButtonItem : Gtk.ToggleButton {
-
-        /* The main purpose of this class is handling icon theming */
-
-        private bool has_themed_icon;
-        private StyleContext? icon_style;
-
-        private string icon_name = "";
-        private Gtk.IconSize? icon_size = null;
-
-        private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
-
         public ModeButtonItem () {
             can_focus = false;
-            has_themed_icon = false;
-
-            icon_style = null;
-
-            get_style_context().add_class ("button");
-            get_style_context().add_class ("raised");
-            get_style_context().add_provider (ModeButton.style_provider, style_priority);
-
-            /* We need to track state changes in order to modify the icon */
-            state_flags_changed.connect ( () => {
-                if (has_themed_icon)
-                    load_icon ();
-            });
-        }
-
-        public void set_icon_foreground_style (StyleContext? icon_style) {
-            this.icon_style = icon_style;
-        }
-
-        public new void set_icon (string name, Gtk.IconSize size) {
-            icon_name = name;
-            icon_size = size;
-
-            has_themed_icon = true;
-
-            load_icon ();
-        }
-
-        public new void set_image (Gtk.Image? image) {
-            if (image == null)
-                return;
-
-            /* Remove previous images */
-            foreach (weak Widget _image in get_children ()) {
-                if (this.get_parent () != null && _image is Gtk.Image)
-                    _image.destroy();
-            }
-
-            /* Add new image */
-            add (image);
-
-            show_all ();
-        }
-
-        private void load_icon () {
-            set_image (new Image.from_pixbuf (render_themed_icon()));
-        }
-
-        private Gdk.Pixbuf? render_themed_icon () {
-            Gdk.Pixbuf? rv = null;
-
-            int width = 0, height = 0;
-            icon_size_lookup (icon_size, out width, out height);
-
-            try {
-                var themed_icon = new GLib.ThemedIcon.with_default_fallbacks (icon_name);
-                Gtk.IconInfo? icon_info = IconTheme.get_default().lookup_by_gicon (themed_icon as GLib.Icon, height, Gtk.IconLookupFlags.GENERIC_FALLBACK);
-                if (icon_info != null)
-                    rv = icon_info.load_symbolic_for_context (icon_style ?? ModeButton.widget_style);
-            }
-            catch (Error err) {
-                warning ("%s", err.message);
-            }
-
-            return rv;
+
+            const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
+
+            get_style_context ().add_class ("raised");
+            get_style_context ().add_provider (ModeButton.style_provider, style_priority);
         }
     }
 }


Follow ups