Merge lp:~hloeung/indicator-power/show-percentage into lp:indicator-power/13.10

Proposed by Haw Loeung
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: lp:~hloeung/indicator-power/show-percentage
Merge into: lp:indicator-power/13.10
Diff against target: 373 lines (+130/-27)
4 files modified
data/com.canonical.indicator.power.gschema.xml.in (+6/-1)
src/device.c (+4/-4)
src/service.c (+107/-9)
tests/test-device.cc (+13/-13)
To merge this branch: bzr merge lp:~hloeung/indicator-power/show-percentage
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Matthew Paul Thomas (community) design Needs Fixing
Charles Kerr (community) Needs Fixing
Review via email: mp+179635@code.launchpad.net

Commit message

Add option to display battery percentage in both menu bar and display menu (LP: #811777).

Description of the change

Add option to display battery percentage in both menu bar and display menu (LP: #811777).

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Charles Kerr (charlesk) wrote :

The code is fine; thank you for writing this patch.

One issue is that now half of indicator-power's menu is filled with display toggles. Maybe it would make more sense in the panel.

Most importantly, though, the feature's placement (and existence) needs to be signed-off by Design. This change isn't in mpt's spec <https://wiki.ubuntu.com/Power> so I'm not approving this patch as-is. Please ping mpt and ask him to re-engage on this ticket.

One final option would be to make this an "invisible" feature accessible through dconf-editor, much like indicator-bluetooth's visibility toggle or indicator-datetime's custom-time-format string.

review: Needs Fixing
Revision history for this message
Matthew Paul Thomas (mpt) wrote :

Thanks Haw. I've updated the design spec to include the "Show Percentage in Menu Bar" item. <https://wiki.ubuntu.com/Power?action=diff&rev2=37&rev1=36>

It's not clear to me from the code, but I think you might be altering the text of the menu items and not the menu title. I think we want the other way around: alter the menu title, not the individual menu items.

review: Needs Fixing (design)
189. By Haw Loeung

Fixed as per updated design spec - https://wiki.ubuntu.com/Power.

190. By Haw Loeung

Pull in changes from upstream and resolve conflicts.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
191. By Haw Loeung

Don't use deprecated g_simple_action_group_insert(), use g_action_map_add_action() instead.

192. By Haw Loeung

Fixed up test cases so package now builds successfully.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
193. By Haw Loeung

Fixed showing two brackets when charging.

194. By Haw Loeung

As per updated design spec, 'Show Time in Menu Bar' should be enabled by default.

Revision history for this message
Haw Loeung (hloeung) wrote :
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Charles Kerr (charlesk) wrote :

mpt,thanks for looking into this.

I've got a patch based on Haw's that follows the new spec; MP forthcoming.

Revision history for this message
Charles Kerr (charlesk) wrote :

Oh! we were working on the same thing. I guess I should have commented sooner :)

Revision history for this message
Charles Kerr (charlesk) wrote :

Haw, there are still a couple of issues here, the menuitem's labels are still getting changed. Also there are fringe cases for showing the % in the header when we shouldn't, such as when the primary device is UP_DEVICE_KIND_LINE_POWER.

When we were accidentally coding in parallel a few hours ago, I worked on a branch based off your earlier one, which fixes these and also adds unit tests for how header's label will look for all combinations of the time & percentage flag settings.

If you're agreeable, I'll MP it (lp:~charlesk/indicator-power/lp-811777) and we can go from there. On the other hand, you were here first and I don't want to step on your toes -- if you want to continue on with this current MP that's great too. :-)

Revision history for this message
Haw Loeung (hloeung) wrote :

Unmerged revisions

194. By Haw Loeung

As per updated design spec, 'Show Time in Menu Bar' should be enabled by default.

193. By Haw Loeung

Fixed showing two brackets when charging.

192. By Haw Loeung

Fixed up test cases so package now builds successfully.

191. By Haw Loeung

Don't use deprecated g_simple_action_group_insert(), use g_action_map_add_action() instead.

190. By Haw Loeung

Pull in changes from upstream and resolve conflicts.

189. By Haw Loeung

Fixed as per updated design spec - https://wiki.ubuntu.com/Power.

188. By Haw Loeung

Fixed percentages as I think it looks nicer outside of parentheses.

187. By Haw Loeung

Fixed so that both time and percentage could be shown.

186. By Haw Loeung

Show percentage in drop-down menu and added option to show percentage in menu bar - LP:811777.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/com.canonical.indicator.power.gschema.xml.in'
2--- data/com.canonical.indicator.power.gschema.xml.in 2012-02-02 05:00:03 +0000
3+++ data/com.canonical.indicator.power.gschema.xml.in 2013-08-23 01:04:40 +0000
4@@ -6,10 +6,15 @@
5 </enum>
6 <schema gettext-domain="@GETTEXT_PACKAGE@" id="com.canonical.indicator.power" path="/com/canonical/indicator/power/">
7 <key name="show-time" type="b">
8- <default>false</default>
9+ <default>true</default>
10 <_summary>Show time in Menu Bar</_summary>
11 <_description>Whether or not to show the time in the menu bar.</_description>
12 </key>
13+ <key name="show-percentage" type="b">
14+ <default>false</default>
15+ <_summary>Show percentage in Menu Bar</_summary>
16+ <_description>Whether or not to show the percentage in the menu bar.</_description>
17+ </key>
18 <key enum="icon-policy-enum" name="icon-policy">
19 <default>"present"</default>
20 <_summary>When to show the battery status in the menu bar.</_summary>
21
22=== modified file 'src/device.c'
23--- src/device.c 2013-06-17 04:16:19 +0000
24+++ src/device.c 2013-08-23 01:04:40 +0000
25@@ -484,7 +484,7 @@
26
27 if (minutes == 0)
28 {
29- *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
30+ *detailed_timestring = g_strdup_printf (g_dngettext (GETTEXT_PACKAGE,
31 "%i hour",
32 "%i hours",
33 hours), hours);
34@@ -595,8 +595,8 @@
35 {
36 /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
37 *accessible_name = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), device_name, detailed_timestring, percentage);
38- *details = g_strdup_printf (_("%s (%s to charge)"), device_name, short_timestring);
39- *short_details = g_strdup_printf ("(%s)", short_timestring);
40+ *details = g_strdup_printf (_("%s (%s to charge (%.0lf%%))"), device_name, short_timestring, percentage);
41+ *short_details = g_strdup (short_timestring);
42 }
43 else if ((state == UP_DEVICE_STATE_DISCHARGING) && (time > (60*60*12)))
44 {
45@@ -608,7 +608,7 @@
46 {
47 /* TRANSLATORS: %2 is a time string, e.g. "1 hour 5 minutes" */
48 *accessible_name = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, detailed_timestring, percentage);
49- *details = g_strdup_printf (_("%s (%s left)"), device_name, short_timestring);
50+ *details = g_strdup_printf (_("%s (%s left (%.0lf%%))"), device_name, short_timestring, percentage);
51 *short_details = g_strdup (short_timestring);
52 }
53
54
55=== modified file 'src/service.c'
56--- src/service.c 2013-08-20 03:47:23 +0000
57+++ src/service.c 2013-08-23 01:04:40 +0000
58@@ -32,6 +32,7 @@
59 #define BUS_PATH "/com/canonical/indicator/power"
60
61 #define SETTINGS_SHOW_TIME_S "show-time"
62+#define SETTINGS_SHOW_PERCENTAGE_S "show-percentage"
63 #define SETTINGS_ICON_POLICY_S "icon-policy"
64
65 G_DEFINE_TYPE (IndicatorPowerService,
66@@ -112,6 +113,7 @@
67 GSimpleActionGroup * actions;
68 GSimpleAction * header_action;
69 GSimpleAction * show_time_action;
70+ GSimpleAction * show_percentage_action;
71 GSimpleAction * battery_level_action;
72 GSimpleAction * brightness_action;
73
74@@ -311,6 +313,8 @@
75 create_header_state (IndicatorPowerService * self)
76 {
77 GVariantBuilder b;
78+ gchar * short_time = NULL;
79+ gdouble percentage = 0;
80 gchar * label = NULL;
81 gchar * a11y = NULL;
82 GIcon * icon = NULL;
83@@ -321,10 +325,12 @@
84 gchar * details;
85
86 indicator_power_device_get_time_details (p->primary_device,
87- &label,
88+ &short_time,
89 &details,
90 &a11y);
91
92+ percentage = indicator_power_device_get_percentage (p->primary_device);
93+
94 icon = indicator_power_device_get_gicon (p->primary_device);
95
96 g_free (details);
97@@ -335,11 +341,26 @@
98 g_variant_builder_add (&b, "{sv}", "visible",
99 g_variant_new_boolean (should_be_visible (self)));
100
101+ /* Show both time and percentage */
102+ if ((short_time != NULL) &&
103+ g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S) &&
104+ g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S))
105+ label = g_strdup_printf (_("(%s %.0lf%%)"), short_time, percentage);
106+ /* Show only time */
107+ else if ((short_time != NULL) &&
108+ g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S))
109+ label = g_strdup_printf (_("(%s)"), short_time);
110+ /* Show only percentage */
111+ else if (g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S))
112+ label = g_strdup_printf (_("(%.0lf%%)"), percentage);
113+
114+ if (short_time != NULL)
115+ g_free (short_time);
116+
117 if (label != NULL)
118 {
119- if (g_settings_get_boolean (p->settings, SETTINGS_SHOW_TIME_S))
120- g_variant_builder_add (&b, "{sv}", "label",
121- g_variant_new_string (label));
122+ g_variant_builder_add (&b, "{sv}", "label",
123+ g_variant_new_string (label));
124
125 g_free (label);
126 }
127@@ -521,6 +542,10 @@
128 "indicator.show-time");
129
130 g_menu_append (menu,
131+ _("Show Percentage in Menu Bar"),
132+ "indicator.show-percentage");
133+
134+ g_menu_append (menu,
135 _("Power Settingsā€¦"),
136 "indicator.activate-settings");
137
138@@ -744,6 +769,45 @@
139 g_variant_unref (v);
140 }
141
142+static void
143+set_show_percentage_flag (IndicatorPowerService * self, gboolean b)
144+{
145+ GVariant * v;
146+ priv_t * p = self->priv;
147+
148+ /* update the settings */
149+ if (b != g_settings_get_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S))
150+ g_settings_set_boolean (p->settings, SETTINGS_SHOW_PERCENTAGE_S, b);
151+
152+ /* update the action state */
153+ v = g_action_get_state (G_ACTION(p->show_percentage_action));
154+ if (b != g_variant_get_boolean (v))
155+ g_simple_action_set_state (p->show_percentage_action,
156+ g_variant_new_boolean (b));
157+ g_variant_unref (v);
158+
159+ rebuild_header_now (self);
160+}
161+static void
162+on_show_percentage_setting_changed (GSettings * settings,
163+ gchar * key,
164+ gpointer gself)
165+{
166+ set_show_percentage_flag (INDICATOR_POWER_SERVICE(gself),
167+ g_settings_get_boolean (settings, key));
168+}
169+
170+static void
171+on_show_percentage_action_state_changed (GAction * action,
172+ GParamSpec * pspec G_GNUC_UNUSED,
173+ gpointer gself)
174+{
175+ GVariant * v = g_action_get_state (action);
176+ set_show_percentage_flag (INDICATOR_POWER_SERVICE(gself),
177+ g_variant_get_boolean (v));
178+ g_variant_unref (v);
179+}
180+
181 /* toggles the state */
182 static void
183 on_show_time_action_activated (GSimpleAction * simple,
184@@ -755,12 +819,23 @@
185 g_simple_action_set_state (simple, g_variant_new_boolean (!flag));
186 g_variant_unref (v);
187 }
188+static void
189+on_show_percentage_action_activated (GSimpleAction * simple,
190+ GVariant * parameter G_GNUC_UNUSED,
191+ gpointer unused G_GNUC_UNUSED)
192+{
193+ GVariant * v = g_action_get_state (G_ACTION (simple));
194+ gboolean flag = g_variant_get_boolean (v);
195+ g_simple_action_set_state (simple, g_variant_new_boolean (!flag));
196+ g_variant_unref (v);
197+}
198
199 static void
200 init_gactions (IndicatorPowerService * self)
201 {
202 GSimpleAction * a;
203 gboolean show_time;
204+ gboolean show_percentage;
205 priv_t * p = self->priv;
206
207 GActionEntry entries[] = {
208@@ -777,18 +852,18 @@
209
210 /* add the header action */
211 a = g_simple_action_new_stateful ("_header", NULL, create_header_state (self));
212- g_simple_action_group_insert (p->actions, G_ACTION(a));
213+ g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
214 p->header_action = a;
215
216 /* add the power-level action */
217 a = g_simple_action_new_stateful ("battery-level", NULL, g_variant_new_uint32(0));
218 g_simple_action_set_enabled (a, FALSE);
219- g_simple_action_group_insert (p->actions, G_ACTION(a));
220+ g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
221 p->battery_level_action = a;
222
223 /* add the brightness action */
224 a = g_simple_action_new_stateful ("brightness", NULL, action_state_for_brightness (self));
225- g_simple_action_group_insert (p->actions, G_ACTION(a));
226+ g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
227 g_signal_connect (a, "change-state", G_CALLBACK(on_brightness_change_requested), self);
228 p->brightness_action = a;
229
230@@ -801,9 +876,22 @@
231 G_CALLBACK(on_show_time_action_activated), self);
232 g_signal_connect (a, "notify",
233 G_CALLBACK(on_show_time_action_state_changed), self);
234- g_simple_action_group_insert (p->actions, G_ACTION(a));
235+ g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
236 p->show_time_action = a;
237
238+ /* add the show-percentage action */
239+ show_percentage = g_settings_get_boolean (p->settings,
240+ SETTINGS_SHOW_PERCENTAGE_S);
241+ a = g_simple_action_new_stateful ("show-percentage",
242+ NULL,
243+ g_variant_new_boolean(show_percentage));
244+ g_signal_connect (a, "activate",
245+ G_CALLBACK(on_show_percentage_action_activated), self);
246+ g_signal_connect (a, "notify",
247+ G_CALLBACK(on_show_percentage_action_state_changed), self);
248+ g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
249+ p->show_percentage_action = a;
250+
251 rebuild_header_now (self);
252 }
253
254@@ -949,7 +1037,7 @@
255 {
256 IndicatorPowerService * self = INDICATOR_POWER_SERVICE (o);
257 priv_t * p = self->priv;
258-
259+
260 switch (property_id)
261 {
262 case PROP_DEVICE_PROVIDER:
263@@ -1014,10 +1102,18 @@
264 g_clear_object (&p->show_time_action);
265 }
266
267+ if (p->show_percentage_action != NULL)
268+ {
269+ g_signal_handlers_disconnect_by_data (p->show_percentage_action, self);
270+
271+ g_clear_object (&p->show_percentage_action);
272+ }
273+
274 g_clear_object (&p->brightness_action);
275 g_clear_object (&p->battery_level_action);
276 g_clear_object (&p->header_action);
277 g_clear_object (&p->show_time_action);
278+ g_clear_object (&p->show_percentage_action);
279 g_clear_object (&p->actions);
280
281 g_clear_object (&p->conn);
282@@ -1053,6 +1149,8 @@
283 G_CALLBACK(rebuild_header_now), self);
284 g_signal_connect (p->settings, "changed::" SETTINGS_SHOW_TIME_S,
285 G_CALLBACK(on_show_time_setting_changed), self);
286+ g_signal_connect (p->settings, "changed::" SETTINGS_SHOW_PERCENTAGE_S,
287+ G_CALLBACK(on_show_percentage_setting_changed), self);
288
289 p->own_id = g_bus_own_name (G_BUS_TYPE_SESSION,
290 BUS_NAME,
291
292=== modified file 'tests/test-device.cc'
293--- tests/test-device.cc 2013-06-17 23:01:51 +0000
294+++ tests/test-device.cc 2013-08-23 01:04:40 +0000
295@@ -4,16 +4,16 @@
296 Authors:
297 Charles Kerr <charles.kerr@canonical.com>
298
299-This program is free software: you can redistribute it and/or modify it
300-under the terms of the GNU General Public License version 3, as published
301+This program is free software: you can redistribute it and/or modify it
302+under the terms of the GNU General Public License version 3, as published
303 by the Free Software Foundation.
304
305-This program is distributed in the hope that it will be useful, but
306-WITHOUT ANY WARRANTY; without even the implied warranties of
307-MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
308+This program is distributed in the hope that it will be useful, but
309+WITHOUT ANY WARRANTY; without even the implied warranties of
310+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
311 PURPOSE. See the GNU General Public License for more details.
312
313-You should have received a copy of the GNU General Public License along
314+You should have received a copy of the GNU General Public License along
315 with this program. If not, see <http://www.gnu.org/licenses/>.
316 */
317
318@@ -266,7 +266,7 @@
319 g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
320 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY,
321 NULL);
322-
323+
324 g_string_append_printf (expected, "%s-empty-symbolic;", kind_str);
325 g_string_append_printf (expected, "gpm-%s-empty;", kind_str);
326 g_string_append_printf (expected, "gpm-%s-000;", kind_str);
327@@ -434,7 +434,7 @@
328
329 // state unknown
330 g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
331- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
332+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
333 NULL);
334 g_string_append_printf (expected, "%s-missing-symbolic;", kind_str);
335 g_string_append_printf (expected, "gpm-%s-missing;", kind_str);
336@@ -468,7 +468,7 @@
337 /**
338 ***
339 **/
340-
341+
342 IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
343 o = G_OBJECT(device);
344
345@@ -478,8 +478,8 @@
346 INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
347 INDICATOR_POWER_DEVICE_TIME, guint64(60*61),
348 NULL);
349- check_strings (device, "(1:01)",
350- "Battery (1:01 to charge)",
351+ check_strings (device, "1:01",
352+ "Battery (1:01 to charge (50%))",
353 "Battery (1 hour 1 minute to charge (50%))");
354
355 // discharging, < 12 hours left
356@@ -489,7 +489,7 @@
357 INDICATOR_POWER_DEVICE_TIME, guint64(60*61),
358 NULL);
359 check_strings (device, "1:01",
360- "Battery (1:01 left)",
361+ "Battery (1:01 left (50%))",
362 "Battery (1 hour 1 minute left (50%))");
363
364 // discharging, > 12 hours left
365@@ -605,7 +605,7 @@
366 ASSERT_EQ (a, indicator_power_service_choose_primary_device(device_list));
367 }
368 }
369-
370+
371 // cleanup
372 g_list_free_full (device_list, g_object_unref);
373 }

Subscribers

People subscribed via source and target branches