Skip to content

Commit e3503a5

Browse files
committed
Add battery api pages
1 parent d8bd28d commit e3503a5

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

Writerside/boson.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<toc-element topic="data-api.md" />
4444
<toc-element topic="web-components-api.md" />
4545
<toc-element topic="security-api.md" />
46+
<toc-element topic="battery-api.md" />
4647
</toc-element>
4748
<toc-element topic="events.md" />
4849
</toc-element>

Writerside/topics/overview.topic

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
</a>
2424
</spotlight>
2525

26-
2726
<primary>
2827
<title>Core Components</title>
2928
<a href="application.md" type="start"/>
@@ -45,6 +44,7 @@
4544
<title>Other APIs</title>
4645
<card href="web-components-api.md" />
4746
<card href="security-api.md" />
47+
<card href="battery-api.md" />
4848
</cards>
4949
</misc>
5050
</section-starting-page>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Battery API
2+
3+
<show-structure for="chapter" depth="2"/>
4+
<secondary-label ref="security-limitations"/>
5+
<secondary-label ref="macos-limitations"/>
6+
7+
This API provides information about
8+
[device battery](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API).
9+
10+
<warning>
11+
MacOS/WebKit does not support this API. In the future, it
12+
may be implemented using native syscalls.
13+
</warning>
14+
15+
The API is available in the `WebView::$battery` property.
16+
17+
```php
18+
$app = new Boson\Application();
19+
20+
$app->webview->battery; // Access to Battery API
21+
```
22+
23+
## Battery Level
24+
<secondary-label ref="read-only"/>
25+
26+
To get the current battery charge level you can use the read-only
27+
`$level` property. The battery level contain a float value
28+
between `0.0` and `1.0`.
29+
30+
```php
31+
$level = $app->webview->battery->level;
32+
33+
echo 'Charge level is ' . (int) ($level * 100) . '%';
34+
//
35+
// Expects: Charge level is 100%
36+
//
37+
```
38+
39+
<note>
40+
For non-mobile devices the charge level is always <code>1.0</code>
41+
</note>
42+
43+
## Charging Status
44+
<secondary-label ref="read-only"/>
45+
46+
To get the battery charging status you can use the read-only
47+
`$isCharging` property.
48+
49+
```php
50+
$isCharging = $app->webview->battery->isCharging;
51+
52+
echo 'The battery is ' . ($isCharging ? '' : 'not ') . 'charging now';
53+
//
54+
// Expects: The battery is charging now
55+
//
56+
```
57+
58+
<note>
59+
For non-mobile devices the charging status is always <code>true</code>
60+
</note>
61+
62+
## Charging Time
63+
<secondary-label ref="read-only"/>
64+
65+
To get the time until the battery is fully charged, use the read-only
66+
`$chargingTime` property. The charging time property will contain an
67+
integer value in seconds.
68+
69+
```php
70+
$chargingTime = $app->webview->battery->chargingTime;
71+
72+
echo vsprintf('It takes another %d seconds to fully charge', [
73+
$chargingTime,
74+
]);
75+
```
76+
77+
<note>
78+
For non-mobile devices the charging time is always <code>0</code>
79+
</note>
80+
81+
## Discharging Time
82+
<secondary-label ref="read-only"/>
83+
84+
To get the time until the battery is fully discharged, use the read-only
85+
`$dischargingTime` property. The discharging time property will
86+
contain an integer value in seconds or `null` in case the
87+
discharge time is not available.
88+
89+
```php
90+
$dischargingTime = $app->webview->battery->dischargingTime;
91+
92+
if ($dischargingTime === null) {
93+
echo 'Battery is missing';
94+
95+
return;
96+
}
97+
98+
echo vsprintf('It takes another %d seconds to fully discharge', [
99+
$dischargingTime,
100+
]);
101+
```
102+
103+
<note>
104+
For non-mobile devices the discharging time is always <code>null</code>
105+
</note>

Writerside/topics/webview/security-api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ To get the current security status you can use the read-only
2828
```php
2929
$isSecure = $app->webview->security->isSecureContext;
3030

31-
echo 'Context is ' . ($isSecure ? 'secure' : 'insecure');
31+
echo 'Context is ' . ($isSecure ? 'secure' : 'insecure');
32+
//
33+
// Expects: Context is secure
34+
//
3235
```

0 commit comments

Comments
 (0)