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']; }, |
| 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)', | 88 chrome.send('updateBatteryPercent', [parseInt(percent)]); |
64 'timeUntilEmptyChanged(timeUntilEmpty)', | 89 }, |
65 'timeUntilFullChanged(timeUntilFull)', | |
66 ], | |
67 | 90 |
68 batteryPercentChanged: function(percent) { | 91 batteryStateChanged: function(state) { |
69 chrome.send('updateBatteryPercent', [parseInt(percent)]); | 92 // Find the index of the selected battery state. |
| 93 var index = this.batteryStateOptions.indexOf(state); |
| 94 if (index >= 0) |
| 95 chrome.send('updateBatteryState', [index]); |
70 }, | 96 }, |
71 | 97 |
72 externalPowerChanged: function(source) { | 98 externalPowerChanged: function(source) { |
73 var index = -1; | |
74 | |
75 // Find the index of the selected power source. | 99 // Find the index of the selected power source. |
76 for (var i = 0; i < this.externalPowerOptions.length; i++) { | 100 var index = this.externalPowerOptions.indexOf(source); |
77 if (this.externalPowerOptions[i] == source) { | |
78 index = i; | |
79 break; | |
80 } | |
81 } | |
82 | |
83 if (index >= 0) | 101 if (index >= 0) |
84 chrome.send('updateExternalPower', [index]); | 102 chrome.send('updateExternalPower', [index]); |
85 }, | 103 }, |
86 | 104 |
87 timeUntilEmptyChanged: function(time) { | 105 timeUntilEmptyChanged: function(time, oldTime) { |
88 chrome.send('updateTimeToEmpty', [parseInt(time)]); | 106 if (oldTime != undefined) |
| 107 chrome.send('updateTimeToEmpty', [parseInt(time)]); |
89 }, | 108 }, |
90 | 109 |
91 timeUntilFullChanged: function(time) { | 110 timeUntilFullChanged: function(time, oldTime) { |
92 chrome.send('updateTimeToFull', [parseInt(time)]); | 111 if (oldTime != undefined) |
| 112 chrome.send('updateTimeToFull', [parseInt(time)]); |
93 }, | 113 }, |
94 | 114 |
95 updatePowerProperties: function(percent, external_power, empty, full) { | 115 updatePowerProperties: function(power_properties) { |
96 this.batteryPercent = percent; | 116 this.batteryPercent = power_properties.battery_percent; |
97 this.externalPower = this.externalPowerOptions[external_power]; | 117 this.batteryState = |
98 this.timeUntilEmpty = empty; | 118 this.batteryStateOptions[power_properties.battery_state]; |
99 this.timeUntilFull = full; | 119 this.externalPower = |
| 120 this.externalPowerOptions[power_properties.external_power]; |
| 121 this.timeUntilEmpty = power_properties.battery_time_to_empty_sec; |
| 122 this.timeUntilFull = power_properties.battery_time_to_full_sec; |
100 } | 123 } |
101 }); | 124 }); |
OLD | NEW |