yubioath-flutter/lib/desktop/state.dart

279 lines
8.3 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:async';
2022-02-25 15:17:52 +03:00
import 'dart:io';
import 'package:collection/collection.dart';
2022-09-23 11:17:28 +03:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
2023-02-24 16:10:39 +03:00
import '../app/logging.dart';
import '../app/models.dart';
import '../app/state.dart';
import '../core/state.dart';
import 'models.dart';
import 'rpc.dart';
final _log = Logger('state');
// This must be initialized before use in initialize.dart.
2022-12-20 16:14:14 +03:00
final rpcProvider = FutureProvider<RpcSession>((ref) {
throw UnimplementedError();
});
final rpcStateProvider = StateNotifierProvider<_RpcStateNotifier, RpcState>(
2022-12-20 16:14:14 +03:00
(ref) => _RpcStateNotifier(ref.watch(rpcProvider).valueOrNull),
);
class _RpcStateNotifier extends StateNotifier<RpcState> {
2022-12-20 16:14:14 +03:00
final RpcSession? _rpc;
2022-12-20 16:14:14 +03:00
_RpcStateNotifier(this._rpc) : super(const RpcState('unknown', false)) {
_init();
}
_init() async {
2022-12-20 16:14:14 +03:00
final response = await _rpc?.command('get', []);
if (mounted && response != null) {
2022-03-16 16:55:21 +03:00
state = RpcState.fromJson(response['data']);
}
}
}
2023-02-24 16:10:39 +03:00
final desktopWindowStateProvider =
StateNotifierProvider<DesktopWindowStateNotifier, WindowState>(
(ref) => DesktopWindowStateNotifier(ref.watch(prefProvider)));
2023-02-24 16:10:39 +03:00
const String windowHidden = 'DESKTOP_WINDOW_HIDDEN';
2023-02-24 16:10:39 +03:00
class DesktopWindowStateNotifier extends StateNotifier<WindowState>
with WindowListener {
2023-02-24 16:10:39 +03:00
final SharedPreferences _prefs;
Timer? _idleTimer;
2023-02-24 16:10:39 +03:00
DesktopWindowStateNotifier(this._prefs)
: super(WindowState(
focused: true,
visible: true,
active: true,
hidden: _prefs.getBool(windowHidden) ?? false)) {
_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);
}
});
}
}
2023-02-24 16:10:39 +03:00
void setWindowHidden(bool hidden) async {
if (hidden) {
await windowManager.hide();
} else {
await windowManager.show();
}
await _prefs.setBool(windowHidden, hidden);
state = state.copyWith(hidden: hidden);
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
@override
set state(WindowState value) {
2022-05-03 12:24:25 +03:00
_log.debug('Window state changed: $value');
super.state = value;
}
@override
2023-02-24 16:10:39 +03:00
@protected
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) {
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();
break;
default:
2022-05-03 12:24:25 +03:00
_log.traffic('Window event ignored: $eventName');
}
}
}
}
final desktopClipboardProvider = Provider<AppClipboard>(
(ref) => _DesktopClipboard(),
);
class _DesktopClipboard extends AppClipboard {
@override
bool platformGivesFeedback() {
return false;
}
@override
Future<void> setText(String toClipboard, {bool isSensitive = false}) async {
2024-03-18 19:26:23 +03:00
await Clipboard.setData(ClipboardData(text: toClipboard));
// Wayland may require the window to be focused to copy to clipboard
2023-02-24 16:23:17 +03:00
final needsFocus = Platform.isLinux &&
2024-03-18 19:26:23 +03:00
Platform.environment['XDG_SESSION_TYPE'] == 'wayland' &&
Platform.environment['_YA_WL_CLIPFIX'] != null;
2023-02-24 16:23:17 +03:00
var hidden = false;
try {
if (needsFocus && !await windowManager.isFocused()) {
if (!await windowManager.isVisible()) {
hidden = true;
await windowManager.setOpacity(0.0);
await windowManager.show();
}
await windowManager.focus();
// Window focus isn't immediate, wait until focused with 10s timeout
await Future.doWhile(() async => !await windowManager.isFocused())
.timeout(const Duration(seconds: 10));
}
await Clipboard.setData(ClipboardData(text: toClipboard));
} finally {
if (hidden) {
await windowManager.hide();
await windowManager.setOpacity(1.0);
}
}
}
}
2022-09-23 11:17:28 +03:00
final desktopSupportedThemesProvider = StateProvider<List<ThemeMode>>(
(ref) => ThemeMode.values,
);
class DesktopCurrentDeviceNotifier extends CurrentDeviceNotifier {
static const String _lastDevice = 'APP_STATE_LAST_DEVICE';
@override
DeviceNode? build() {
SharedPreferences prefs = ref.watch(prefProvider);
2023-01-12 15:14:31 +03:00
final devices = ref.watch(attachedDevicesProvider);
final lastDevice = prefs.getString(_lastDevice) ?? '';
var node = devices.where((dev) => dev.path.key == lastDevice).firstOrNull;
if (node == null) {
final parts = lastDevice.split('/');
if (parts.firstOrNull == 'pid') {
node = devices
.whereType<UsbYubiKeyNode>()
.where((e) => e.pid.value.toString() == parts[1])
.firstOrNull;
}
}
2023-01-12 15:14:31 +03:00
return node ?? devices.whereType<UsbYubiKeyNode>().firstOrNull;
}
@override
setCurrentDevice(DeviceNode? device) {
state = device;
ref.read(prefProvider).setString(_lastDevice, device?.path.key ?? '');
}
}
CurrentSectionNotifier desktopCurrentSectionNotifier(Ref ref) {
final notifier = DesktopCurrentSectionNotifier(
ref.watch(supportedSectionsProvider), ref.watch(prefProvider));
ref.listen<AsyncValue<YubiKeyData>>(currentDeviceDataProvider, (_, data) {
notifier._notifyDeviceChanged(data.whenOrNull(data: ((data) => data)));
}, fireImmediately: true);
return notifier;
}
class DesktopCurrentSectionNotifier extends CurrentSectionNotifier {
final List<Section> _supportedSections;
static const String _key = 'APP_STATE_LAST_SECTION';
final SharedPreferences _prefs;
DesktopCurrentSectionNotifier(this._supportedSections, this._prefs)
: super(_fromName(_prefs.getString(_key), _supportedSections));
@override
void setCurrentSection(Section section) {
state = section;
_prefs.setString(_key, section.name);
}
void _notifyDeviceChanged(YubiKeyData? data) {
if (data == null) {
state = _supportedSections.first;
return;
}
String? lastAppName = _prefs.getString(_key);
if (lastAppName != null && lastAppName != state.name) {
// Try switching to saved app
state = Section.values.firstWhere((app) => app.name == lastAppName);
}
if (state == Section.passkeys &&
state.getAvailability(data) != Availability.enabled) {
state = Section.securityKey;
}
if (state == Section.securityKey &&
state.getAvailability(data) != Availability.enabled) {
state = Section.passkeys;
}
if (state.getAvailability(data) != Availability.unsupported) {
// Keep current app
return;
}
state = _supportedSections.firstWhere(
(app) => app.getAvailability(data) == Availability.enabled,
orElse: () => _supportedSections.first,
);
}
static Section _fromName(String? name, List<Section> supportedSections) =>
supportedSections.firstWhere((element) => element.name == name,
orElse: () => supportedSections.first);
}