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