OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var BatterySettings = Polymer({ | 5 var BatterySettings = Polymer({ |
6 is: 'battery-settings', | 6 is: 'battery-settings', |
7 | 7 |
8 properties: { | 8 properties: { |
9 /** | 9 /** |
10 * The system's battery percentage. | 10 * The system's battery percentage. |
11 */ | 11 */ |
12 batteryPercent: { | 12 batteryPercent: { |
13 type: Number, | 13 type: Number, |
14 observer: 'batteryPercentChanged', | |
14 }, | 15 }, |
15 | 16 |
16 /** | 17 /** |
17 * A string representing the value of an | 18 * A string representing a value in the |
19 * PowerSupplyProperties_BatteryState enumeration. | |
20 */ | |
21 batteryState: { | |
22 type: String, | |
23 observer: 'batteryStateChanged', | |
24 }, | |
25 | |
26 /** | |
27 * An array representing the battery state options. | |
28 * The names are ordered based on the | |
29 * PowerSupplyProperties_BatteryState enumeration. These values must be | |
30 * in sync. | |
31 */ | |
32 batteryStateOptions: { | |
33 type: Array, | |
34 value: function() { return ['Full', 'Charging', 'Disharging', | |
35 'Not Present']; }, | |
michaelpg
2015/07/30 03:02:09
nit: indent (preferably to align with 'Full')
mozartalouis
2015/07/30 21:18:18
Done.
| |
36 }, | |
37 | |
38 /** | |
39 * A string representing a value in the | |
18 * PowerSupplyProperties_ExternalPower enumeration. | 40 * PowerSupplyProperties_ExternalPower enumeration. |
19 */ | 41 */ |
20 externalPower: { | 42 externalPower: { |
21 type: String, | 43 type: String, |
44 observer: 'externalPowerChanged', | |
22 }, | 45 }, |
23 | 46 |
24 /** | 47 /** |
25 * An array representing the external power options. | 48 * An array representing the external power options. |
26 * The names are ordered based on the | 49 * The names are ordered based on the |
27 * PowerSupplyProperties_ExternalPower enumeration. These values must be | 50 * PowerSupplyProperties_ExternalPower enumeration. These values must be |
28 * in sync. | 51 * in sync. |
29 */ | 52 */ |
30 externalPowerOptions: { | 53 externalPowerOptions: { |
31 type: Array, | 54 type: Array, |
32 value: function() { return ['AC', 'USB (Low Power)', 'Disconnected']; } | 55 value: function() { return ['AC', 'USB (Low Power)', 'Disconnected']; } |
33 }, | 56 }, |
34 | 57 |
35 /** | 58 /** |
36 * A string representing the time left until the battery is discharged. | 59 * A string representing the time left until the battery is discharged. |
37 */ | 60 */ |
38 timeUntilEmpty: { | 61 timeUntilEmpty: { |
39 type: String, | 62 type: String, |
63 observer: 'timeUntilEmptyChanged', | |
40 }, | 64 }, |
41 | 65 |
42 /** | 66 /** |
43 * A string representing the time left until the battery is at 100%. | 67 * A string representing the time left until the battery is at 100%. |
44 */ | 68 */ |
45 timeUntilFull: { | 69 timeUntilFull: { |
46 type: String, | 70 type: String, |
71 observer: 'timeUntilFullChanged', | |
47 }, | 72 }, |
48 | 73 |
49 /** | 74 /** |
50 * The title for the settings section. | 75 * The title for the settings section. |
51 */ | 76 */ |
52 title: { | 77 title: { |
53 type: String, | 78 type: String, |
54 }, | 79 }, |
55 }, | 80 }, |
56 | 81 |
57 ready: function() { | 82 ready: function() { |
58 this.title = 'Power Settings'; | 83 this.title = 'Power Settings'; |
59 }, | 84 }, |
60 | 85 |
61 observers: [ | 86 batteryPercentChanged: function(percent, oldPercent) { |
62 'batteryPercentChanged(batteryPercent)', | 87 if (oldPercent != undefined) |
63 'externalPowerChanged(externalPower)', | |
64 'timeUntilEmptyChanged(timeUntilEmpty)', | |
65 'timeUntilFullChanged(timeUntilFull)', | |
66 ], | |
67 | |
68 batteryPercentChanged: function(percent) { | |
69 chrome.send('updateBatteryPercent', [parseInt(percent)]); | 88 chrome.send('updateBatteryPercent', [parseInt(percent)]); |
michaelpg
2015/07/30 03:02:09
indent
| |
70 }, | 89 }, |
71 | 90 |
72 externalPowerChanged: function(source) { | 91 batteryStateChanged: function(state, oldState) { |
73 var index = -1; | 92 // Find the index of the selected battery state. |
74 | 93 if (oldState != undefined) { |
michaelpg
2015/07/30 03:02:09
This check is to ignore default values set by pape
mozartalouis
2015/07/30 21:18:18
Done.
| |
75 // Find the index of the selected power source. | 94 var index = this.batteryStateOptions.indexOf(state); |
76 for (var i = 0; i < this.externalPowerOptions.length; i++) { | 95 if (index >= 0) |
77 if (this.externalPowerOptions[i] == source) { | 96 chrome.send('updateBatteryState', [index]); |
78 index = i; | |
79 break; | |
80 } | |
81 } | 97 } |
82 | |
83 if (index >= 0) | |
84 chrome.send('updateExternalPower', [index]); | |
85 }, | 98 }, |
86 | 99 |
87 timeUntilEmptyChanged: function(time) { | 100 externalPowerChanged: function(source, oldSource) { |
88 chrome.send('updateTimeToEmpty', [parseInt(time)]); | 101 // Find the index of the selected power source. |
102 if (oldSource != undefined) { | |
103 var index = this.externalPowerOptions.indexOf(source); | |
104 if (index >= 0) | |
105 chrome.send('updateExternalPower', [index]); | |
106 } | |
89 }, | 107 }, |
90 | 108 |
91 timeUntilFullChanged: function(time) { | 109 timeUntilEmptyChanged: function(time, oldTime) { |
92 chrome.send('updateTimeToFull', [parseInt(time)]); | 110 if (oldTime != undefined) |
111 chrome.send('updateTimeToEmpty', [parseInt(time)]); | |
93 }, | 112 }, |
94 | 113 |
95 updatePowerProperties: function(percent, external_power, empty, full) { | 114 timeUntilFullChanged: function(time, oldTime) { |
96 this.batteryPercent = percent; | 115 if (oldTime != undefined) |
97 this.externalPower = this.externalPowerOptions[external_power]; | 116 chrome.send('updateTimeToFull', [parseInt(time)]); |
98 this.timeUntilEmpty = empty; | 117 }, |
99 this.timeUntilFull = full; | 118 |
119 updatePowerProperties: function(power_properties) { | |
120 this.batteryPercent = power_properties.battery_percent; | |
121 this.batteryState = | |
122 this.batteryStateOptions[power_properties.battery_state]; | |
michaelpg
2015/07/30 03:02:09
indent 4 spaces
mozartalouis
2015/07/30 21:18:18
Done.
| |
123 this.externalPower = | |
124 this.externalPowerOptions[power_properties.external_power]; | |
michaelpg
2015/07/30 03:02:09
same
mozartalouis
2015/07/30 21:18:18
Done.
| |
125 this.timeUntilEmpty = power_properties.battery_time_to_empty_sec; | |
126 this.timeUntilFull = power_properties.battery_time_to_full_sec; | |
100 } | 127 } |
101 }); | 128 }); |
OLD | NEW |