2022-01-27 14:34:29 +03:00
|
|
|
import 'dart:async';
|
2022-02-25 15:17:52 +03:00
|
|
|
import 'dart:io';
|
2022-01-27 14:34:29 +03:00
|
|
|
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-03-21 11:34:45 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
|
2022-03-21 11:34:45 +03:00
|
|
|
import '../app/state.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
import '../core/state.dart';
|
|
|
|
import '../app/models.dart';
|
|
|
|
import 'models.dart';
|
|
|
|
import 'rpc.dart';
|
|
|
|
|
2022-02-21 11:38:09 +03:00
|
|
|
final _log = Logger('state');
|
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
// This must be initialized before use in initialize.dart.
|
|
|
|
final rpcProvider = Provider<RpcSession>((ref) {
|
|
|
|
throw UnimplementedError();
|
|
|
|
});
|
|
|
|
|
|
|
|
final rpcStateProvider = StateNotifierProvider<_RpcStateNotifier, RpcState>(
|
2022-03-25 10:36:29 +03:00
|
|
|
(ref) => _RpcStateNotifier(ref.watch(rpcProvider)),
|
2022-01-27 14:34:29 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
class _RpcStateNotifier extends StateNotifier<RpcState> {
|
|
|
|
final RpcSession rpc;
|
2022-03-16 16:55:21 +03:00
|
|
|
_RpcStateNotifier(this.rpc) : super(const RpcState('unknown', false)) {
|
2022-01-27 14:34:29 +03:00
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
_init() async {
|
|
|
|
final response = await rpc.command('get', []);
|
|
|
|
if (mounted) {
|
2022-03-16 16:55:21 +03:00
|
|
|
state = RpcState.fromJson(response['data']);
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final _windowStateProvider =
|
|
|
|
StateNotifierProvider<_WindowStateNotifier, WindowState>(
|
|
|
|
(ref) => _WindowStateNotifier());
|
|
|
|
|
|
|
|
final desktopWindowStateProvider = Provider<WindowState>(
|
|
|
|
(ref) => ref.watch(_windowStateProvider),
|
|
|
|
);
|
|
|
|
|
|
|
|
class _WindowStateNotifier extends StateNotifier<WindowState>
|
|
|
|
with WindowListener {
|
|
|
|
Timer? _idleTimer;
|
|
|
|
_WindowStateNotifier()
|
|
|
|
: super(WindowState(focused: true, visible: true, active: true)) {
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init() async {
|
|
|
|
windowManager.addListener(this);
|
2022-02-25 15:17:52 +03:00
|
|
|
// isFocused is not supported on Linux, assume focused
|
|
|
|
if (!Platform.isLinux) {
|
|
|
|
_idleTimer = Timer(const Duration(seconds: 5), () async {
|
|
|
|
final focused = await windowManager.isFocused();
|
|
|
|
if (mounted && !focused) {
|
|
|
|
state = state.copyWith(active: false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
windowManager.removeListener(this);
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
set state(WindowState value) {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('Window state changed: $value');
|
2022-01-27 14:34:29 +03:00
|
|
|
super.state = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onWindowEvent(String eventName) {
|
|
|
|
if (mounted) {
|
|
|
|
switch (eventName) {
|
|
|
|
case 'blur':
|
|
|
|
state = state.copyWith(focused: false);
|
|
|
|
_idleTimer?.cancel();
|
2022-02-25 12:14:25 +03:00
|
|
|
_idleTimer = Timer(const Duration(seconds: 5), () async {
|
2022-02-25 15:17:52 +03:00
|
|
|
if (mounted) {
|
2022-01-27 14:34:29 +03:00
|
|
|
state = state.copyWith(active: false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'focus':
|
|
|
|
state = state.copyWith(focused: true, active: true);
|
|
|
|
_idleTimer?.cancel();
|
|
|
|
break;
|
|
|
|
case 'minimize':
|
|
|
|
state = state.copyWith(visible: false, active: false);
|
|
|
|
_idleTimer?.cancel();
|
|
|
|
break;
|
|
|
|
case 'restore':
|
|
|
|
state = state.copyWith(visible: true, active: true);
|
2022-02-25 15:17:52 +03:00
|
|
|
_idleTimer?.cancel();
|
2022-01-27 14:34:29 +03:00
|
|
|
break;
|
|
|
|
default:
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.fine('Window event ignored: $eventName');
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 11:34:45 +03:00
|
|
|
|
|
|
|
final desktopCurrentDeviceProvider =
|
|
|
|
StateNotifierProvider<CurrentDeviceNotifier, DeviceNode?>((ref) {
|
|
|
|
final provider = _DesktopCurrentDeviceNotifier(ref.watch(prefProvider));
|
|
|
|
ref.listen(attachedDevicesProvider, provider._updateAttachedDevices);
|
|
|
|
return provider;
|
|
|
|
});
|
|
|
|
|
|
|
|
class _DesktopCurrentDeviceNotifier extends CurrentDeviceNotifier {
|
|
|
|
static const String _lastDevice = 'APP_STATE_LAST_DEVICE';
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
_DesktopCurrentDeviceNotifier(this._prefs) : super(null);
|
|
|
|
|
|
|
|
_updateAttachedDevices(List<DeviceNode>? previous, List<DeviceNode> devices) {
|
|
|
|
if (!devices.contains(state)) {
|
|
|
|
final lastDevice = _prefs.getString(_lastDevice) ?? '';
|
|
|
|
try {
|
2022-03-18 13:22:24 +03:00
|
|
|
state = devices.firstWhere((dev) => dev.path.key == lastDevice,
|
2022-03-21 11:34:45 +03:00
|
|
|
orElse: () => devices.whereType<UsbYubiKeyNode>().first);
|
|
|
|
} on StateError {
|
|
|
|
state = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
setCurrentDevice(DeviceNode device) {
|
|
|
|
state = device;
|
2022-03-18 13:22:24 +03:00
|
|
|
_prefs.setString(_lastDevice, device.path.key);
|
2022-03-21 11:34:45 +03:00
|
|
|
}
|
|
|
|
}
|