forked from xvrh/chrome_extension.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidle.dart
74 lines (59 loc) · 2.55 KB
/
idle.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ignore_for_file: unnecessary_parenthesis
library;
import 'dart:js_util';
import 'src/internal_helpers.dart';
import 'src/js/idle.dart' as $js;
export 'src/chrome.dart' show chrome, EventStream;
final _idle = ChromeIdle._();
extension ChromeIdleExtension on Chrome {
/// Use the `chrome.idle` API to detect when the machine's idle state changes.
ChromeIdle get idle => _idle;
}
class ChromeIdle {
ChromeIdle._();
bool get isAvailable => $js.chrome.idleNullable != null && alwaysTrue;
/// Returns "locked" if the system is locked, "idle" if the user has not
/// generated any input for a specified number of seconds, or "active"
/// otherwise.
/// [detectionIntervalInSeconds] The system is considered idle if
/// detectionIntervalInSeconds seconds have elapsed since the last user
/// input detected.
Future<IdleState> queryState(int detectionIntervalInSeconds) async {
var $res = await promiseToFuture<$js.IdleState>(
$js.chrome.idle.queryState(detectionIntervalInSeconds));
return IdleState.fromJS($res);
}
/// Sets the interval, in seconds, used to determine when the system is in an
/// idle state for onStateChanged events. The default interval is 60 seconds.
/// [intervalInSeconds] Threshold, in seconds, used to determine when the
/// system is in an idle state.
void setDetectionInterval(int intervalInSeconds) {
$js.chrome.idle.setDetectionInterval(intervalInSeconds);
}
/// Gets the time, in seconds, it takes until the screen is locked
/// automatically while idle. Returns a zero duration if the screen is never
/// locked automatically. Currently supported on Chrome OS only.
Future<int> getAutoLockDelay() async {
var $res = await promiseToFuture<int>($js.chrome.idle.getAutoLockDelay());
return $res;
}
/// Fired when the system changes to an active, idle or locked state. The
/// event fires with "locked" if the screen is locked or the screensaver
/// activates, "idle" if the system is unlocked and the user has not generated
/// any input for a specified number of seconds, and "active" when the user
/// generates input on an idle system.
EventStream<IdleState> get onStateChanged =>
$js.chrome.idle.onStateChanged.asStream(($c) => ($js.IdleState newState) {
return $c(IdleState.fromJS(newState));
}.toJS);
}
enum IdleState {
active('active'),
idle('idle'),
locked('locked');
const IdleState(this.value);
final String value;
String get toJS => value;
static IdleState fromJS(String value) =>
values.firstWhere((e) => e.value == value);
}