tauri-plugin-device-info-api 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.md +63 -12
- package/dist-js/index.cjs +55 -1
- package/dist-js/index.d.ts +30 -2
- package/dist-js/index.js +53 -2
- package/dist-js/type.d.ts +68 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,52 @@ All notable changes to this project are documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.2.0] - 2026-11-17
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **Adaptive Apps** — signals that turn raw readings into decisions.
|
|
13
|
+
- `getPowerProfile()` / `watchPowerProfile()` — one `full | balanced | saver`
|
|
14
|
+
recommendation combining battery level, charging state, OS power-saving mode
|
|
15
|
+
(Low Power Mode, Battery Saver, Linux `low-power` profile) and thermal
|
|
16
|
+
pressure, plus the `reasons` behind it. Battery thresholds apply only on
|
|
17
|
+
battery — not while charging or on external power (`onExternalPower`:
|
|
18
|
+
AC/USB-C adapter detected on macOS, Windows, Linux, iOS and Android, so a
|
|
19
|
+
laptop on AC that is full or held at a charge limit is not "low") — and an
|
|
20
|
+
unknown battery level (iOS Simulator, unsupported Android capacity) is
|
|
21
|
+
ignored rather than read as empty. On Linux only CPU/SoC thermal zones are
|
|
22
|
+
considered.
|
|
23
|
+
- `getDeviceTier()` — a `low | mid | high` capability class and 0-100 score
|
|
24
|
+
from memory, CPU cores and peak clock, scored against the device's form
|
|
25
|
+
factor; honours Android's low-RAM flag and media performance class.
|
|
26
|
+
- `get_power_profile` / `get_device_tier` commands with matching
|
|
27
|
+
`allow-get-power-profile` / `allow-get-device-tier` permissions, included in
|
|
28
|
+
`device-info:default`.
|
|
29
|
+
- New `power` watch kind (polled, default 5 s).
|
|
30
|
+
- Rust: `get_power_profile()`, `get_device_tier()`, `get_power_state()` and
|
|
31
|
+
`get_hardware_info()` on `DeviceInfo`, and the pure
|
|
32
|
+
`adaptive::power_profile` / `adaptive::device_tier` functions.
|
|
33
|
+
- New types: `PowerProfile`, `PowerProfileKind`, `PowerProfileReason`,
|
|
34
|
+
`ThermalState`, `DeviceTier`, `DeviceTierKind`, `HardwareInfo`.
|
|
35
|
+
|
|
36
|
+
### Fixed
|
|
37
|
+
|
|
38
|
+
- **Watch API: late subscribers now get the current value.** A `watch*` call
|
|
39
|
+
for a kind that was already being watched (a second component, or the same
|
|
40
|
+
page after a webview reload) only received *future* changes, because the
|
|
41
|
+
running monitor emits on change. `start_watching` now returns the current
|
|
42
|
+
value, which the `watch*` call delivers to its own callback only; monitors
|
|
43
|
+
emit only on change, so existing subscribers no longer see duplicates and
|
|
44
|
+
the first subscriber gets the initial value exactly once.
|
|
45
|
+
- **iOS / Android: unknown battery level reported as 0%.** iOS sent `0` when
|
|
46
|
+
`batteryLevel` is unknown (always on the Simulator) and Android passed through
|
|
47
|
+
an unsupported `BATTERY_PROPERTY_CAPACITY`; `level` is now omitted instead
|
|
48
|
+
(Android falls back to the battery intent's level/scale first).
|
|
49
|
+
- **iOS release builds failed to link** with `Undefined symbols:
|
|
50
|
+
_init_plugin_device_info` on Xcode 27 / Swift 6.4, which hides `internal`
|
|
51
|
+
symbols when merging a package's objects in release. The Swift entry point is
|
|
52
|
+
now `public`.
|
|
53
|
+
|
|
8
54
|
## [1.1.1] - 2026-11-17
|
|
9
55
|
|
|
10
56
|
### Fixed
|
|
@@ -76,6 +122,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
76
122
|
`getStorageInfo`, and `getDisplayInfo` across Windows, macOS, Linux, iOS, and
|
|
77
123
|
Android.
|
|
78
124
|
|
|
125
|
+
[1.2.0]: https://github.com/edisdev/tauri-plugin-device-info/releases/tag/v1.2.0
|
|
79
126
|
[1.1.1]: https://github.com/edisdev/tauri-plugin-device-info/releases/tag/v1.1.1
|
|
80
127
|
[1.1.0]: https://github.com/edisdev/tauri-plugin-device-info/releases/tag/v1.1.0
|
|
81
128
|
[1.0.1]: https://github.com/edisdev/tauri-plugin-device-info/releases/tag/v1.0.1
|
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ A comprehensive Tauri plugin to access device information including Battery, Net
|
|
|
26
26
|
- 💾 **Storage** — total/free space and storage type
|
|
27
27
|
- 🖥️ **Display** — resolution, scale factor, and refresh rate
|
|
28
28
|
- 🆔 **Device** — manufacturer, model, serial, and device name
|
|
29
|
+
- ⚡ **Adaptive** — one `full | balanced | saver` power signal and a `low | mid | high` device tier, so your app can adjust itself
|
|
29
30
|
- 🔔 **Reactive** — `watch*` APIs push updates on change (native event-driven on macOS, polling fallback elsewhere)
|
|
30
31
|
- 📱 **Truly cross-platform** — Windows, macOS, Linux, iOS, and Android
|
|
31
32
|
- 🛡️ **Type-safe** — full TypeScript types and granular Tauri permissions
|
|
@@ -33,15 +34,13 @@ A comprehensive Tauri plugin to access device information including Battery, Net
|
|
|
33
34
|
|
|
34
35
|
## 📸 Preview
|
|
35
36
|
|
|
36
|
-
<div>
|
|
37
|
-
<img src="./docs/public/
|
|
38
|
-
<br>
|
|
39
|
-
<img src="./docs/public/
|
|
40
|
-
<img src="./docs/public/
|
|
41
|
-
<br>
|
|
42
|
-
|
|
43
|
-
<img src="./docs/public/DeviceInfo_iOS.png" alt="iOS Preview" width="250">
|
|
44
|
-
<p><i>Example Dashboard Application across platforms</i></p>
|
|
37
|
+
<div align="center">
|
|
38
|
+
<img src="./docs/public/DeviceInfo_Promo.gif" alt="The plugin in the example app: live signals on macOS and iOS, then the power profile adapting on Windows and Android" width="900">
|
|
39
|
+
<br><br>
|
|
40
|
+
<img src="./docs/public/DeviceInfo_macOS.gif" alt="Vitals on macOS: switching pages, simulating the Saver profile and changing themes" width="560">
|
|
41
|
+
<img src="./docs/public/DeviceInfo_iOS.gif" alt="Vitals on iPhone: live battery and thermal readings, the Adaptive page and themes" width="230">
|
|
42
|
+
<p><i><b>Vitals</b> — the example app in <a href="./examples/tauri-app">examples/tauri-app</a>, built on this plugin.<br>
|
|
43
|
+
Real readings on macOS, iOS, Windows 10 and Android 16.</i></p>
|
|
45
44
|
</div>
|
|
46
45
|
|
|
47
46
|
## Platform Support
|
|
@@ -70,7 +69,7 @@ yarn tauri add device-info
|
|
|
70
69
|
|
|
71
70
|
```toml
|
|
72
71
|
[dependencies]
|
|
73
|
-
tauri-plugin-device-info = "1.
|
|
72
|
+
tauri-plugin-device-info = "1.2" # Check crates.io for latest version
|
|
74
73
|
# or from git
|
|
75
74
|
tauri-plugin-device-info = { git = "https://github.com/edisdev/tauri-plugin-device-info" }
|
|
76
75
|
```
|
|
@@ -80,7 +79,7 @@ tauri-plugin-device-info = { git = "https://github.com/edisdev/tauri-plugin-devi
|
|
|
80
79
|
```json
|
|
81
80
|
{
|
|
82
81
|
"dependencies": {
|
|
83
|
-
"tauri-plugin-device-info-api": "^1.
|
|
82
|
+
"tauri-plugin-device-info-api": "^1.2.0"
|
|
84
83
|
}
|
|
85
84
|
}
|
|
86
85
|
```
|
|
@@ -234,9 +233,32 @@ await Promise.all(unwatchers.map((unwatch) => unwatch()));
|
|
|
234
233
|
`watch*` accepts an optional `{ intervalMs }` that applies **only to polled
|
|
235
234
|
kinds** (native kinds ignore it). Defaults and minimums are per kind: `device`
|
|
236
235
|
defaults to 60000 ms (floored at 10000 ms), `storage` to 10000 ms (floored at
|
|
237
|
-
1000 ms), and others to 2000 ms (floored at 250 ms). Subscribers to the same
|
|
236
|
+
1000 ms), `power` to 5000 ms (floored at 1000 ms), and others to 2000 ms (floored at 250 ms). Subscribers to the same
|
|
238
237
|
kind share one monitor, so only the first subscriber's interval takes effect.
|
|
239
238
|
|
|
239
|
+
### Adaptive Apps
|
|
240
|
+
|
|
241
|
+
Raw readings tell you what the device is doing; these tell you what to do about
|
|
242
|
+
it. `getPowerProfile()` folds battery, Low Power Mode / Battery Saver and thermal
|
|
243
|
+
pressure into one recommendation, and `getDeviceTier()` classifies the hardware.
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { watchPowerProfile, getDeviceTier } from "tauri-plugin-device-info-api";
|
|
247
|
+
|
|
248
|
+
// Back off when the device is hot, in Low Power Mode, or low on battery.
|
|
249
|
+
await watchPowerProfile(({ profile, reasons }) => {
|
|
250
|
+
document.documentElement.dataset.power = profile; // "full" | "balanced" | "saver"
|
|
251
|
+
console.log(reasons); // e.g. ["thermalSerious", "batteryLow"]
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// Pick a quality level the hardware can sustain.
|
|
255
|
+
const { tier, score } = await getDeviceTier(); // "low" | "mid" | "high", 0-100
|
|
256
|
+
const particles = { low: 0, mid: 200, high: 1000 }[tier];
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
See [Adaptive Apps](https://edisdev.github.io/tauri-plugin-device-info/api/adaptive)
|
|
260
|
+
for the exact rules, thresholds and per-platform sources.
|
|
261
|
+
|
|
240
262
|
## API Reference
|
|
241
263
|
|
|
242
264
|
### getDeviceInfo()
|
|
@@ -293,6 +315,29 @@ Returns display/screen information.
|
|
|
293
315
|
| `scaleFactor` | `number?` | Display scale factor (e.g., 2.0 for Retina) |
|
|
294
316
|
| `refreshRate` | `number?` | Screen refresh rate in Hz |
|
|
295
317
|
|
|
318
|
+
### getPowerProfile()
|
|
319
|
+
|
|
320
|
+
Returns a recommendation for how much work the app should do right now.
|
|
321
|
+
|
|
322
|
+
| Field | Type | Description |
|
|
323
|
+
| -------------- | ------------------------------------------------ | ----------------------------------------------- |
|
|
324
|
+
| `profile` | `"full" \| "balanced" \| "saver"` | Most restrictive profile implied by any reason |
|
|
325
|
+
| `reasons` | `PowerProfileReason[]` | Why it isn't `full` (empty when it is) |
|
|
326
|
+
| `batteryLevel` | `number?` | 0-100; absent without a battery |
|
|
327
|
+
| `isCharging` | `boolean?` | Whether the device is charging |
|
|
328
|
+
| `lowPowerMode` | `boolean?` | Low Power Mode / Battery Saver / `low-power` |
|
|
329
|
+
| `thermalState` | `"nominal" \| "fair" \| "serious" \| "critical"?` | Thermal pressure (not available on Windows) |
|
|
330
|
+
|
|
331
|
+
### getDeviceTier()
|
|
332
|
+
|
|
333
|
+
Classifies the hardware relative to devices of the same form factor.
|
|
334
|
+
|
|
335
|
+
| Field | Type | Description |
|
|
336
|
+
| ---------- | ---------------------------- | ---------------------------------------------- |
|
|
337
|
+
| `tier` | `"low" \| "mid" \| "high"` | Capability class |
|
|
338
|
+
| `score` | `number` | 0-100 (`low` < 30 ≤ `mid` < 65 ≤ `high`) |
|
|
339
|
+
| `hardware` | `HardwareInfo` | CPU cores, CPU model, peak clock, total memory |
|
|
340
|
+
|
|
296
341
|
### watch\*(callback, options?)
|
|
297
342
|
|
|
298
343
|
Subscribes to a kind and invokes `callback` with the current value immediately,
|
|
@@ -306,6 +351,7 @@ to stop watching and remove the listener.
|
|
|
306
351
|
| `watchNetwork()` | `NetworkInfo` |
|
|
307
352
|
| `watchStorage()` | `StorageInfo` |
|
|
308
353
|
| `watchDisplay()` | `DisplayInfo` |
|
|
354
|
+
| `watchPowerProfile()` | `PowerProfile` |
|
|
309
355
|
|
|
310
356
|
**Options**
|
|
311
357
|
|
|
@@ -324,6 +370,9 @@ import type {
|
|
|
324
370
|
NetworkInfo,
|
|
325
371
|
StorageInfo,
|
|
326
372
|
DisplayInfo,
|
|
373
|
+
PowerProfile,
|
|
374
|
+
DeviceTier,
|
|
375
|
+
HardwareInfo,
|
|
327
376
|
WatchOptions,
|
|
328
377
|
UnwatchFn,
|
|
329
378
|
} from "tauri-plugin-device-info-api";
|
|
@@ -355,6 +404,8 @@ Add the required permissions in your `capabilities` configuration.
|
|
|
355
404
|
| `device-info:allow-get-network-info` | Allows getting network details |
|
|
356
405
|
| `device-info:allow-get-storage-info` | Allows getting storage info |
|
|
357
406
|
| `device-info:allow-get-display-info` | Allows getting display info |
|
|
407
|
+
| `device-info:allow-get-power-profile` | Allows getting the power profile |
|
|
408
|
+
| `device-info:allow-get-device-tier` | Allows getting the device tier |
|
|
358
409
|
| `device-info:allow-start-watching` | Allows starting a reactive watch |
|
|
359
410
|
| `device-info:allow-stop-watching` | Allows stopping a reactive watch |
|
|
360
411
|
|
package/dist-js/index.cjs
CHANGED
|
@@ -33,23 +33,62 @@ async function getStorageInfo() {
|
|
|
33
33
|
async function getDisplayInfo() {
|
|
34
34
|
return await core.invoke('plugin:device-info|get_display_info');
|
|
35
35
|
}
|
|
36
|
+
// ============================================================================
|
|
37
|
+
// Adaptive signals
|
|
38
|
+
// ============================================================================
|
|
39
|
+
/**
|
|
40
|
+
* Get a single `full | balanced | saver` recommendation that combines battery
|
|
41
|
+
* level, charging state, OS power-saving mode and thermal pressure.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const { profile } = await getPowerProfile()
|
|
45
|
+
* if (profile === 'saver') disableBackgroundSync()
|
|
46
|
+
*/
|
|
47
|
+
async function getPowerProfile() {
|
|
48
|
+
return await core.invoke('plugin:device-info|get_power_profile');
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Classify the device as `low | mid | high` from its CPU and memory, scored
|
|
52
|
+
* against devices of the same form factor (desktop vs. mobile).
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* const { tier } = await getDeviceTier()
|
|
56
|
+
* document.body.dataset.tier = tier // style with [data-tier="low"] { ... }
|
|
57
|
+
*/
|
|
58
|
+
async function getDeviceTier() {
|
|
59
|
+
return await core.invoke('plugin:device-info|get_device_tier');
|
|
60
|
+
}
|
|
36
61
|
/**
|
|
37
62
|
* Subscribes to a device-info kind and invokes `callback` whenever the value changes.
|
|
38
63
|
* The current value is delivered immediately, then on every change.
|
|
39
64
|
* Returns a function that stops watching and removes the listener.
|
|
40
65
|
*/
|
|
41
66
|
async function watch(kind, callback, options) {
|
|
67
|
+
// Listen before starting so no change emitted after the start is missed.
|
|
68
|
+
let changed = false;
|
|
42
69
|
const unlisten = await event.listen(`device-info://${kind}-changed`, (event) => {
|
|
70
|
+
changed = true;
|
|
43
71
|
callback(event.payload);
|
|
44
72
|
});
|
|
73
|
+
let current;
|
|
45
74
|
try {
|
|
46
|
-
|
|
75
|
+
// Returns the current value for this subscriber only; the backend emits
|
|
76
|
+
// events only on change, so other subscribers don't get a duplicate.
|
|
77
|
+
current = await core.invoke('plugin:device-info|start_watching', {
|
|
78
|
+
kind,
|
|
79
|
+
intervalMs: options?.intervalMs
|
|
80
|
+
});
|
|
47
81
|
}
|
|
48
82
|
catch (error) {
|
|
49
83
|
// Don't leak the event listener if the backend failed to start the watcher.
|
|
50
84
|
unlisten();
|
|
51
85
|
throw error;
|
|
52
86
|
}
|
|
87
|
+
// A change event that already arrived is at least as new as `current`;
|
|
88
|
+
// delivering `current` after it would roll the caller back to a stale value.
|
|
89
|
+
if (current !== null && current !== undefined && !changed) {
|
|
90
|
+
callback(current);
|
|
91
|
+
}
|
|
53
92
|
return async () => {
|
|
54
93
|
unlisten();
|
|
55
94
|
await core.invoke('plugin:device-info|stop_watching', { kind });
|
|
@@ -75,14 +114,29 @@ function watchDisplay(callback, options) {
|
|
|
75
114
|
function watchDevice(callback, options) {
|
|
76
115
|
return watch('device', callback, options);
|
|
77
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Watch the power profile — fires when the profile, any reason, or the
|
|
119
|
+
* underlying battery/thermal/low-power readings change.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* const unwatch = await watchPowerProfile(({ profile }) => {
|
|
123
|
+
* animations.enabled = profile === 'full'
|
|
124
|
+
* })
|
|
125
|
+
*/
|
|
126
|
+
function watchPowerProfile(callback, options) {
|
|
127
|
+
return watch('power', callback, options);
|
|
128
|
+
}
|
|
78
129
|
|
|
79
130
|
exports.getBatteryInfo = getBatteryInfo;
|
|
80
131
|
exports.getDeviceInfo = getDeviceInfo;
|
|
132
|
+
exports.getDeviceTier = getDeviceTier;
|
|
81
133
|
exports.getDisplayInfo = getDisplayInfo;
|
|
82
134
|
exports.getNetworkInfo = getNetworkInfo;
|
|
135
|
+
exports.getPowerProfile = getPowerProfile;
|
|
83
136
|
exports.getStorageInfo = getStorageInfo;
|
|
84
137
|
exports.watchBattery = watchBattery;
|
|
85
138
|
exports.watchDevice = watchDevice;
|
|
86
139
|
exports.watchDisplay = watchDisplay;
|
|
87
140
|
exports.watchNetwork = watchNetwork;
|
|
141
|
+
exports.watchPowerProfile = watchPowerProfile;
|
|
88
142
|
exports.watchStorage = watchStorage;
|
package/dist-js/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DeviceInfoResponse, BatteryInfo, NetworkInfo, StorageInfo, DisplayInfo, WatchOptions, UnwatchFn } from './type';
|
|
2
|
-
export type { DeviceInfoResponse, DeviceInfoResponse as DeviceInfo, BatteryInfo, NetworkInfo, StorageInfo, DisplayInfo, WatchOptions, UnwatchFn } from './type';
|
|
1
|
+
import type { DeviceInfoResponse, BatteryInfo, NetworkInfo, StorageInfo, DisplayInfo, PowerProfile, DeviceTier, WatchOptions, UnwatchFn } from './type';
|
|
2
|
+
export type { DeviceInfoResponse, DeviceInfoResponse as DeviceInfo, BatteryInfo, NetworkInfo, StorageInfo, DisplayInfo, ThermalState, PowerProfile, PowerProfileKind, PowerProfileReason, HardwareInfo, DeviceTier, DeviceTierKind, WatchOptions, UnwatchFn } from './type';
|
|
3
3
|
/**
|
|
4
4
|
* Get comprehensive device information including UUID, manufacturer, model, etc.
|
|
5
5
|
*/
|
|
@@ -20,6 +20,24 @@ export declare function getStorageInfo(): Promise<StorageInfo>;
|
|
|
20
20
|
* Get display information including resolution, scale factor, and refresh rate.
|
|
21
21
|
*/
|
|
22
22
|
export declare function getDisplayInfo(): Promise<DisplayInfo>;
|
|
23
|
+
/**
|
|
24
|
+
* Get a single `full | balanced | saver` recommendation that combines battery
|
|
25
|
+
* level, charging state, OS power-saving mode and thermal pressure.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const { profile } = await getPowerProfile()
|
|
29
|
+
* if (profile === 'saver') disableBackgroundSync()
|
|
30
|
+
*/
|
|
31
|
+
export declare function getPowerProfile(): Promise<PowerProfile>;
|
|
32
|
+
/**
|
|
33
|
+
* Classify the device as `low | mid | high` from its CPU and memory, scored
|
|
34
|
+
* against devices of the same form factor (desktop vs. mobile).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const { tier } = await getDeviceTier()
|
|
38
|
+
* document.body.dataset.tier = tier // style with [data-tier="low"] { ... }
|
|
39
|
+
*/
|
|
40
|
+
export declare function getDeviceTier(): Promise<DeviceTier>;
|
|
23
41
|
/** Watch battery status (level, charging state, health) for changes. */
|
|
24
42
|
export declare function watchBattery(callback: (info: BatteryInfo) => void, options?: WatchOptions): Promise<UnwatchFn>;
|
|
25
43
|
/** Watch network connectivity (IP, type, MAC) for changes — e.g. Wi-Fi ↔ offline. */
|
|
@@ -30,3 +48,13 @@ export declare function watchStorage(callback: (info: StorageInfo) => void, opti
|
|
|
30
48
|
export declare function watchDisplay(callback: (info: DisplayInfo) => void, options?: WatchOptions): Promise<UnwatchFn>;
|
|
31
49
|
/** Watch device identity fields for changes (rarely changes; useful on mobile). */
|
|
32
50
|
export declare function watchDevice(callback: (info: DeviceInfoResponse) => void, options?: WatchOptions): Promise<UnwatchFn>;
|
|
51
|
+
/**
|
|
52
|
+
* Watch the power profile — fires when the profile, any reason, or the
|
|
53
|
+
* underlying battery/thermal/low-power readings change.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const unwatch = await watchPowerProfile(({ profile }) => {
|
|
57
|
+
* animations.enabled = profile === 'full'
|
|
58
|
+
* })
|
|
59
|
+
*/
|
|
60
|
+
export declare function watchPowerProfile(callback: (profile: PowerProfile) => void, options?: WatchOptions): Promise<UnwatchFn>;
|
package/dist-js/index.js
CHANGED
|
@@ -31,23 +31,62 @@ async function getStorageInfo() {
|
|
|
31
31
|
async function getDisplayInfo() {
|
|
32
32
|
return await invoke('plugin:device-info|get_display_info');
|
|
33
33
|
}
|
|
34
|
+
// ============================================================================
|
|
35
|
+
// Adaptive signals
|
|
36
|
+
// ============================================================================
|
|
37
|
+
/**
|
|
38
|
+
* Get a single `full | balanced | saver` recommendation that combines battery
|
|
39
|
+
* level, charging state, OS power-saving mode and thermal pressure.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const { profile } = await getPowerProfile()
|
|
43
|
+
* if (profile === 'saver') disableBackgroundSync()
|
|
44
|
+
*/
|
|
45
|
+
async function getPowerProfile() {
|
|
46
|
+
return await invoke('plugin:device-info|get_power_profile');
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Classify the device as `low | mid | high` from its CPU and memory, scored
|
|
50
|
+
* against devices of the same form factor (desktop vs. mobile).
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* const { tier } = await getDeviceTier()
|
|
54
|
+
* document.body.dataset.tier = tier // style with [data-tier="low"] { ... }
|
|
55
|
+
*/
|
|
56
|
+
async function getDeviceTier() {
|
|
57
|
+
return await invoke('plugin:device-info|get_device_tier');
|
|
58
|
+
}
|
|
34
59
|
/**
|
|
35
60
|
* Subscribes to a device-info kind and invokes `callback` whenever the value changes.
|
|
36
61
|
* The current value is delivered immediately, then on every change.
|
|
37
62
|
* Returns a function that stops watching and removes the listener.
|
|
38
63
|
*/
|
|
39
64
|
async function watch(kind, callback, options) {
|
|
65
|
+
// Listen before starting so no change emitted after the start is missed.
|
|
66
|
+
let changed = false;
|
|
40
67
|
const unlisten = await listen(`device-info://${kind}-changed`, (event) => {
|
|
68
|
+
changed = true;
|
|
41
69
|
callback(event.payload);
|
|
42
70
|
});
|
|
71
|
+
let current;
|
|
43
72
|
try {
|
|
44
|
-
|
|
73
|
+
// Returns the current value for this subscriber only; the backend emits
|
|
74
|
+
// events only on change, so other subscribers don't get a duplicate.
|
|
75
|
+
current = await invoke('plugin:device-info|start_watching', {
|
|
76
|
+
kind,
|
|
77
|
+
intervalMs: options?.intervalMs
|
|
78
|
+
});
|
|
45
79
|
}
|
|
46
80
|
catch (error) {
|
|
47
81
|
// Don't leak the event listener if the backend failed to start the watcher.
|
|
48
82
|
unlisten();
|
|
49
83
|
throw error;
|
|
50
84
|
}
|
|
85
|
+
// A change event that already arrived is at least as new as `current`;
|
|
86
|
+
// delivering `current` after it would roll the caller back to a stale value.
|
|
87
|
+
if (current !== null && current !== undefined && !changed) {
|
|
88
|
+
callback(current);
|
|
89
|
+
}
|
|
51
90
|
return async () => {
|
|
52
91
|
unlisten();
|
|
53
92
|
await invoke('plugin:device-info|stop_watching', { kind });
|
|
@@ -73,5 +112,17 @@ function watchDisplay(callback, options) {
|
|
|
73
112
|
function watchDevice(callback, options) {
|
|
74
113
|
return watch('device', callback, options);
|
|
75
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Watch the power profile — fires when the profile, any reason, or the
|
|
117
|
+
* underlying battery/thermal/low-power readings change.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* const unwatch = await watchPowerProfile(({ profile }) => {
|
|
121
|
+
* animations.enabled = profile === 'full'
|
|
122
|
+
* })
|
|
123
|
+
*/
|
|
124
|
+
function watchPowerProfile(callback, options) {
|
|
125
|
+
return watch('power', callback, options);
|
|
126
|
+
}
|
|
76
127
|
|
|
77
|
-
export { getBatteryInfo, getDeviceInfo, getDisplayInfo, getNetworkInfo, getStorageInfo, watchBattery, watchDevice, watchDisplay, watchNetwork, watchStorage };
|
|
128
|
+
export { getBatteryInfo, getDeviceInfo, getDeviceTier, getDisplayInfo, getNetworkInfo, getPowerProfile, getStorageInfo, watchBattery, watchDevice, watchDisplay, watchNetwork, watchPowerProfile, watchStorage };
|
package/dist-js/type.d.ts
CHANGED
|
@@ -27,6 +27,73 @@ export interface DisplayInfo {
|
|
|
27
27
|
scaleFactor?: number;
|
|
28
28
|
refreshRate?: number;
|
|
29
29
|
}
|
|
30
|
+
/** Thermal pressure on Apple's four-level scale (other platforms are mapped onto it). */
|
|
31
|
+
export type ThermalState = 'nominal' | 'fair' | 'serious' | 'critical';
|
|
32
|
+
/**
|
|
33
|
+
* Recommended amount of work for the app right now:
|
|
34
|
+
* - `full` — no constraints, run at full quality
|
|
35
|
+
* - `balanced` — some pressure; skip non-essential work, lower animation/refresh rates
|
|
36
|
+
* - `saver` — heavy pressure; do the minimum, pause background work and effects
|
|
37
|
+
*/
|
|
38
|
+
export type PowerProfileKind = 'full' | 'balanced' | 'saver';
|
|
39
|
+
/**
|
|
40
|
+
* Why the profile is not `full`:
|
|
41
|
+
* - `lowPowerMode` — OS power saving is on (→ saver)
|
|
42
|
+
* - `thermalCritical` / `thermalSerious` — device is hot (→ saver)
|
|
43
|
+
* - `thermalFair` — device is warm (→ balanced)
|
|
44
|
+
* - `batteryCritical` — on battery at ≤ 20% (→ saver)
|
|
45
|
+
* - `batteryLow` — on battery at ≤ 40% (→ balanced)
|
|
46
|
+
*
|
|
47
|
+
* "On battery" means not charging and not on external power; an unknown or
|
|
48
|
+
* implausible battery level never triggers a battery reason.
|
|
49
|
+
*/
|
|
50
|
+
export type PowerProfileReason = 'lowPowerMode' | 'thermalCritical' | 'thermalSerious' | 'thermalFair' | 'batteryCritical' | 'batteryLow';
|
|
51
|
+
export interface PowerProfile {
|
|
52
|
+
/** The recommended profile — the most restrictive one implied by any reason. */
|
|
53
|
+
profile: PowerProfileKind;
|
|
54
|
+
/** Every condition pushing the profile below `full` (empty when `full`). */
|
|
55
|
+
reasons: PowerProfileReason[];
|
|
56
|
+
/** Battery level 0-100; absent on devices without a battery or when the level is unknown. */
|
|
57
|
+
batteryLevel?: number;
|
|
58
|
+
isCharging?: boolean;
|
|
59
|
+
/** Low Power Mode / Battery Saver / `low-power` profile; absent if the OS doesn't expose it. */
|
|
60
|
+
lowPowerMode?: boolean;
|
|
61
|
+
/** Absent where the OS has no thermal API (Windows). */
|
|
62
|
+
thermalState?: ThermalState;
|
|
63
|
+
/**
|
|
64
|
+
* Running on external power (AC adapter, USB-C charger), even when the
|
|
65
|
+
* battery isn't charging — e.g. full or held at a charge limit. Battery
|
|
66
|
+
* reasons never apply while this is `true`. Absent when the OS doesn't say.
|
|
67
|
+
*/
|
|
68
|
+
onExternalPower?: boolean;
|
|
69
|
+
}
|
|
70
|
+
export interface HardwareInfo {
|
|
71
|
+
/** Logical CPU cores. */
|
|
72
|
+
cpuCores?: number;
|
|
73
|
+
/** Physical CPU cores (desktop only). */
|
|
74
|
+
physicalCores?: number;
|
|
75
|
+
/** CPU / SoC name, e.g. "Apple M2" or "iPhone15,2". */
|
|
76
|
+
cpuModel?: string;
|
|
77
|
+
/** Peak CPU clock in MHz, where available (not on iOS or Apple Silicon). */
|
|
78
|
+
cpuMaxFrequencyMhz?: number;
|
|
79
|
+
/** Total physical memory in bytes. */
|
|
80
|
+
totalMemory?: number;
|
|
81
|
+
/** Whether the tier was scored against mobile hardware. */
|
|
82
|
+
isMobile: boolean;
|
|
83
|
+
/** Android only: the OS flags this as a low-RAM device (forces tier `low`). */
|
|
84
|
+
isLowRamDevice?: boolean;
|
|
85
|
+
/** Android 12+ only: declared media performance class (31+ → at least `mid`, 33+ → `high`). */
|
|
86
|
+
mediaPerformanceClass?: number;
|
|
87
|
+
}
|
|
88
|
+
export type DeviceTierKind = 'low' | 'mid' | 'high';
|
|
89
|
+
export interface DeviceTier {
|
|
90
|
+
/** Capability class relative to devices of the same form factor. */
|
|
91
|
+
tier: DeviceTierKind;
|
|
92
|
+
/** 0 (weakest) – 100 (strongest). `low` < 30 ≤ `mid` < 65 ≤ `high`. */
|
|
93
|
+
score: number;
|
|
94
|
+
/** The hardware signals the score was computed from. */
|
|
95
|
+
hardware: HardwareInfo;
|
|
96
|
+
}
|
|
30
97
|
/** Options for the reactive `watch*` APIs. */
|
|
31
98
|
export interface WatchOptions {
|
|
32
99
|
/**
|
|
@@ -37,6 +104,7 @@ export interface WatchOptions {
|
|
|
37
104
|
* some values rarely change:
|
|
38
105
|
* - `device`: defaults to 60000ms, floored at 10000ms (reads are costly and it barely changes)
|
|
39
106
|
* - `storage`: defaults to 10000ms, floored at 1000ms
|
|
107
|
+
* - `power`: defaults to 5000ms, floored at 1000ms
|
|
40
108
|
* - others: default 2000ms, floored at 250ms
|
|
41
109
|
*
|
|
42
110
|
* Only the first subscriber for a given kind sets the interval.
|