Merge lp:~victored/granite/mode-button-cleanup into lp:~elementary-pantheon/granite/granite

Proposed by Victor Martinez
Status: Merged
Merged at revision: 169
Proposed branch: lp:~victored/granite/mode-button-cleanup
Merge into: lp:~elementary-pantheon/granite/granite
Diff against target: 322 lines (+59/-150)
2 files modified
demo/main.vala (+9/-9)
lib/Widgets/ModeButton.vala (+50/-141)
To merge this branch: bzr merge lp:~victored/granite/mode-button-cleanup
Reviewer Review Type Date Requested Status
xapantu Pending
Review via email: mp+89970@code.launchpad.net

Description of the change

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

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'demo/main.vala'
2--- demo/main.vala 2012-01-11 20:08:44 +0000
3+++ demo/main.vala 2012-01-24 18:29:25 +0000
4@@ -69,7 +69,7 @@
5 /* welcome */
6
7 // These strings will be automatically corrected by the widget
8- var welcome = new Welcome("welcome widget", "description text");
9+ var welcome = new Welcome("Granite's Welcome Screen", "This is Granite's Welcome widget.");
10 notebook.append_page(welcome, new Gtk.Label("Welcome"));
11
12 Gdk.Pixbuf? pixbuf = null;
13@@ -84,10 +84,10 @@
14 Gtk.Image? image = new Gtk.Image.from_icon_name("document-open", Gtk.IconSize.DIALOG);
15
16 // Adding elements. Use the most convenient function to add an icon
17- welcome.append_with_pixbuf(pixbuf, "create", "write a new document");
18- welcome.append_with_image(image, "open", "select a file");
19- welcome.append("document-save", "save", "with a much longer description");
20- welcome.append("help-info", "Discover", "Learn more about this app");
21+ welcome.append_with_pixbuf(pixbuf, "Create", "Write a new document.");
22+ welcome.append_with_image(image, "Open", "select a file.");
23+ welcome.append("document-save", "Save", "With a much longer description.");
24+ welcome.append("help-info", "Discover", "Learn more about this application.");
25
26 /* modebutton */
27 var mode_button = new ModeButton();
28@@ -104,10 +104,10 @@
29 toolbar.get_style_context().add_class("primary-toolbar");
30 var toolbutton = new Gtk.ToolItem();
31 var tool_mode = new ModeButton();
32- tool_mode.append(new Gtk.Label("1"));
33- tool_mode.append(new Gtk.Label("2"));
34- tool_mode.append(new Gtk.Label("3"));
35- tool_mode.append(new Gtk.Label("4"));
36+ tool_mode.append_icon ("view-list-column-symbolic", Gtk.IconSize.MENU);
37+ tool_mode.append_icon ("view-list-details-symbolic", Gtk.IconSize.MENU);
38+ tool_mode.append_icon ("view-list-icons-symbolic", Gtk.IconSize.MENU);
39+ tool_mode.append_icon ("view-list-video-symbolic", Gtk.IconSize.MENU);
40 toolbutton.add(tool_mode);
41 toolbar.insert(toolbutton, -1);
42 toolbar.insert(create_appmenu(new Gtk.Menu()), -1);
43
44=== modified file 'lib/Widgets/ModeButton.vala'
45--- lib/Widgets/ModeButton.vala 2012-01-22 04:01:12 +0000
46+++ lib/Widgets/ModeButton.vala 2012-01-24 18:29:25 +0000
47@@ -27,13 +27,10 @@
48 public signal void mode_removed (int index, Gtk.Widget widget);
49 public signal void mode_changed (Gtk.Widget widget);
50
51- /* Style properties. Please note that style class names are for internal
52- usage only. Theme developers should use GraniteWidgetsModeButton instead.
53- */
54-
55+ // Style properties. Please note that style class names are for internal
56+ // use only. Theme developers should use GraniteWidgetsModeButton instead.
57 internal static CssProvider style_provider;
58 internal static StyleContext widget_style;
59-
60 private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
61
62 private const string STYLESHEET = """
63@@ -80,7 +77,7 @@
64
65 public uint n_items {
66 get {
67- return get_children().length();
68+ return get_children ().length ();
69 }
70 }
71
72@@ -102,84 +99,90 @@
73 homogeneous = true;
74 spacing = 0;
75 app_paintable = true;
76- set_visual (get_screen().get_rgba_visual());
77+ set_visual (get_screen ().get_rgba_visual ());
78
79 can_focus = true;
80 }
81
82 public void append_pixbuf (Gdk.Pixbuf? pixbuf) {
83- if (pixbuf == null)
84+ if (pixbuf == null) {
85+ warning ("GraniteWidgetsModeButton: Attempt to add null pixbuf failed.");
86 return;
87+ }
88
89 var image = new Image.from_pixbuf (pixbuf);
90 append (image);
91 }
92
93 public void append_text (string? text) {
94- if (text == null)
95+ if (text == null) {
96+ warning ("GraniteWidgetsModeButton: Attempt to add null text string failed.");
97 return;
98+ }
99
100 append (new Gtk.Label(text));
101 }
102
103 /**
104- * This is the recommended function for adding icons to the ModeButton widget.
105- * If you pass the name of a symbolic icon, it will be properly themed for
106- * every state of the widget. That is, it will match the foreground color
107+ * This is the recommended method for adding icons to the ModeButton widget.
108+ * If the name of a symbolic icon is passed, it will be properly themed for
109+ * each state of the widget. That is, it will match the foreground color
110 * defined by the theme for each state (active, prelight, insensitive, etc.)
111 */
112 public void append_icon (string icon_name, Gtk.IconSize size) {
113- append_mode_button_item (null, icon_name, size);
114+ append (new Image.from_icon_name (icon_name, size));
115 }
116
117 public void append (Gtk.Widget w) {
118- append_mode_button_item (w, null, null);
119- }
120-
121- /**
122- * This function adds the foreground style properties of the given style
123- * context to the widget's icons. This is useful when you want to make the widget
124- * adapt its symbolic icon color to that of the parent in case the GTK+
125- * theme has not set them correctly. This function only affects the behavior
126- * of icons added with append_icon().
127- */
128- public void set_icon_foreground_style (Gtk.StyleContext icon_style) {
129- foreach (weak Widget button in get_children ()) {
130- (button as ModeButtonItem).set_icon_foreground_style (icon_style);
131+ if (w == null) {
132+ warning ("GraniteWidgetsModeButton: Attempt to add null widget failed.");
133+ return;
134 }
135+
136+ var button = new ModeButtonItem ();
137+
138+ button.add (w);
139+
140+ button.button_press_event.connect (() => {
141+ int selected = get_children().index (button);
142+ set_active (selected);
143+ return true;
144+ });
145+
146+ add (button);
147+ button.show_all ();
148+
149+ mode_added ((int)get_children ().length (), w);
150 }
151
152 public void set_active (int new_active_index) {
153-
154- if (new_active_index >= get_children().length () || _selected == new_active_index)
155+ if (new_active_index >= get_children ().length () || _selected == new_active_index)
156 return;
157
158 if (_selected >= 0)
159- ((ToggleButton) get_children().nth_data(_selected)).set_active (false);
160+ ((ToggleButton) get_children ().nth_data (_selected)).set_active (false);
161
162 _selected = new_active_index;
163- ((ToggleButton) get_children().nth_data(_selected)).set_active (true);
164+ ((ToggleButton) get_children ().nth_data (_selected)).set_active (true);
165
166- mode_changed(((ToggleButton) get_children().nth_data(_selected)).get_child());
167+ mode_changed (((ToggleButton) get_children ().nth_data (_selected)).get_child ());
168 }
169
170- public void set_item_visible(int index, bool val) {
171- var item = get_children().nth_data(index);
172- if(item == null)
173+ public void set_item_visible (int index, bool val) {
174+ var item = get_children ().nth_data (index);
175+ if (item == null)
176 return;
177
178- item.set_no_show_all(!val);
179- item.set_visible(val);
180+ item.set_no_show_all (!val);
181+ item.set_visible (val);
182 }
183
184- public new void remove(int index)
185- {
186- mode_removed(index, (get_children().nth_data(index) as Gtk.Bin).get_child ());
187- get_children().nth_data(index).destroy();
188+ public new void remove (int index) {
189+ mode_removed (index, (get_children ().nth_data (index) as Gtk.Bin).get_child ());
190+ get_children ().nth_data (index).destroy ();
191 }
192
193 public void clear_children () {
194-
195 foreach (weak Widget button in get_children ()) {
196 button.hide ();
197 if (button.get_parent () != null)
198@@ -190,7 +193,7 @@
199 }
200
201 protected override bool scroll_event (EventScroll ev) {
202- if(ev.direction == Gdk.ScrollDirection.DOWN) {
203+ if (ev.direction == Gdk.ScrollDirection.DOWN) {
204 selected ++;
205 }
206 else if (ev.direction == Gdk.ScrollDirection.UP) {
207@@ -199,110 +202,16 @@
208
209 return false;
210 }
211-
212- private void append_mode_button_item (Gtk.Widget? w, string? icon_name, Gtk.IconSize? size) {
213- var button = new ModeButtonItem ();
214-
215- /* Modifying properties */
216- if (icon_name != null && size != null && w == null) {
217- button.set_icon (icon_name, size);
218- } else {
219- button.add(w);
220- }
221-
222- button.button_press_event.connect (() => {
223- int selected = get_children().index (button);
224- set_active (selected);
225- return true;
226- });
227-
228- add(button);
229- button.show_all ();
230-
231- mode_added((int)get_children().length(), w);
232- }
233-
234 }
235
236 private class ModeButtonItem : Gtk.ToggleButton {
237-
238- /* The main purpose of this class is handling icon theming */
239-
240- private bool has_themed_icon;
241- private StyleContext? icon_style;
242-
243- private string icon_name = "";
244- private Gtk.IconSize? icon_size = null;
245-
246- private const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
247-
248 public ModeButtonItem () {
249 can_focus = false;
250- has_themed_icon = false;
251-
252- icon_style = null;
253-
254- get_style_context().add_class ("button");
255- get_style_context().add_class ("raised");
256- get_style_context().add_provider (ModeButton.style_provider, style_priority);
257-
258- /* We need to track state changes in order to modify the icon */
259- state_flags_changed.connect ( () => {
260- if (has_themed_icon)
261- load_icon ();
262- });
263- }
264-
265- public void set_icon_foreground_style (StyleContext? icon_style) {
266- this.icon_style = icon_style;
267- }
268-
269- public new void set_icon (string name, Gtk.IconSize size) {
270- icon_name = name;
271- icon_size = size;
272-
273- has_themed_icon = true;
274-
275- load_icon ();
276- }
277-
278- public new void set_image (Gtk.Image? image) {
279- if (image == null)
280- return;
281-
282- /* Remove previous images */
283- foreach (weak Widget _image in get_children ()) {
284- if (this.get_parent () != null && _image is Gtk.Image)
285- _image.destroy();
286- }
287-
288- /* Add new image */
289- add (image);
290-
291- show_all ();
292- }
293-
294- private void load_icon () {
295- set_image (new Image.from_pixbuf (render_themed_icon()));
296- }
297-
298- private Gdk.Pixbuf? render_themed_icon () {
299- Gdk.Pixbuf? rv = null;
300-
301- int width = 0, height = 0;
302- icon_size_lookup (icon_size, out width, out height);
303-
304- try {
305- var themed_icon = new GLib.ThemedIcon.with_default_fallbacks (icon_name);
306- Gtk.IconInfo? icon_info = IconTheme.get_default().lookup_by_gicon (themed_icon as GLib.Icon, height, Gtk.IconLookupFlags.GENERIC_FALLBACK);
307- if (icon_info != null)
308- rv = icon_info.load_symbolic_for_context (icon_style ?? ModeButton.widget_style);
309- }
310- catch (Error err) {
311- warning ("%s", err.message);
312- }
313-
314- return rv;
315+
316+ const int style_priority = Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION;
317+
318+ get_style_context ().add_class ("raised");
319+ get_style_context ().add_provider (ModeButton.style_provider, style_priority);
320 }
321 }
322 }

Subscribers

People subscribed via source and target branches