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.
|
|
|
|
*/
|
|
|
|
|
2022-06-10 14:55:46 +03:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-06-10 14:49:02 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2023-02-09 21:12:35 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-02-09 15:43:27 +03:00
|
|
|
import 'package:window_manager/window_manager.dart';
|
|
|
|
|
2023-02-09 21:12:35 +03:00
|
|
|
import '../about_page.dart';
|
|
|
|
import '../core/state.dart';
|
2023-02-24 16:23:43 +03:00
|
|
|
import '../desktop/state.dart';
|
2023-02-09 15:43:27 +03:00
|
|
|
import '../oath/keys.dart';
|
2023-02-09 21:12:35 +03:00
|
|
|
import 'message.dart';
|
|
|
|
import 'models.dart';
|
|
|
|
import 'state.dart';
|
2023-06-28 18:14:15 +03:00
|
|
|
import 'views/keys.dart';
|
2023-03-03 18:09:40 +03:00
|
|
|
import 'views/settings_page.dart';
|
2022-06-10 14:49:02 +03:00
|
|
|
|
2023-02-10 13:08:37 +03:00
|
|
|
class OpenIntent extends Intent {
|
|
|
|
const OpenIntent();
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:49:02 +03:00
|
|
|
class CopyIntent extends Intent {
|
|
|
|
const CopyIntent();
|
|
|
|
}
|
|
|
|
|
2023-02-09 15:43:27 +03:00
|
|
|
class CloseIntent extends Intent {
|
|
|
|
const CloseIntent();
|
|
|
|
}
|
|
|
|
|
2023-02-24 16:23:43 +03:00
|
|
|
class HideIntent extends Intent {
|
|
|
|
const HideIntent();
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:49:02 +03:00
|
|
|
class SearchIntent extends Intent {
|
|
|
|
const SearchIntent();
|
|
|
|
}
|
|
|
|
|
2023-02-09 21:12:35 +03:00
|
|
|
class NextDeviceIntent extends Intent {
|
|
|
|
const NextDeviceIntent();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SettingsIntent extends Intent {
|
|
|
|
const SettingsIntent();
|
|
|
|
}
|
2022-06-10 14:55:46 +03:00
|
|
|
|
2023-02-09 21:12:35 +03:00
|
|
|
class AboutIntent extends Intent {
|
|
|
|
const AboutIntent();
|
|
|
|
}
|
2023-02-09 15:43:27 +03:00
|
|
|
|
2023-02-10 19:37:42 +03:00
|
|
|
class EditIntent extends Intent {
|
|
|
|
const EditIntent();
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeleteIntent extends Intent {
|
|
|
|
const DeleteIntent();
|
|
|
|
}
|
|
|
|
|
2023-03-07 13:03:31 +03:00
|
|
|
/// Use cmd on macOS, use ctrl on the other platforms
|
|
|
|
SingleActivator ctrlOrCmd(LogicalKeyboardKey key) =>
|
|
|
|
SingleActivator(key, meta: Platform.isMacOS, control: !Platform.isMacOS);
|
2023-02-09 15:43:27 +03:00
|
|
|
|
2023-02-09 21:12:35 +03:00
|
|
|
Widget registerGlobalShortcuts(
|
|
|
|
{required WidgetRef ref, required Widget child}) =>
|
|
|
|
Actions(
|
|
|
|
actions: {
|
|
|
|
CloseIntent: CallbackAction<CloseIntent>(onInvoke: (_) {
|
|
|
|
windowManager.close();
|
|
|
|
return null;
|
|
|
|
}),
|
2023-02-24 16:23:43 +03:00
|
|
|
HideIntent: CallbackAction<HideIntent>(onInvoke: (_) {
|
|
|
|
if (isDesktop) {
|
|
|
|
ref.read(desktopWindowStateProvider.notifier).setWindowHidden(true);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}),
|
2023-02-09 21:12:35 +03:00
|
|
|
SearchIntent: CallbackAction<SearchIntent>(onInvoke: (intent) {
|
|
|
|
// If the OATH view doesn't have focus, but is shown, find and select the search bar.
|
|
|
|
final searchContext = searchAccountsField.currentContext;
|
|
|
|
if (searchContext != null) {
|
|
|
|
if (!Navigator.of(searchContext).canPop()) {
|
|
|
|
return Actions.maybeInvoke(searchContext, intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
NextDeviceIntent: CallbackAction<NextDeviceIntent>(onInvoke: (_) {
|
|
|
|
ref.read(withContextProvider)((context) async {
|
2023-06-28 18:14:15 +03:00
|
|
|
// Only allow switching keys if no other views are open,
|
|
|
|
// with the exception of the drawer.
|
|
|
|
if (!Navigator.of(context).canPop() ||
|
|
|
|
scaffoldGlobalKey.currentState?.isDrawerOpen == true) {
|
2023-02-09 21:12:35 +03:00
|
|
|
final attached = ref
|
|
|
|
.read(attachedDevicesProvider)
|
|
|
|
.whereType<UsbYubiKeyNode>()
|
|
|
|
.toList();
|
|
|
|
if (attached.length > 1) {
|
|
|
|
final current = ref.read(currentDeviceProvider);
|
|
|
|
if (current != null && current is UsbYubiKeyNode) {
|
|
|
|
final index = attached.indexOf(current);
|
|
|
|
ref.read(currentDeviceProvider.notifier).setCurrentDevice(
|
|
|
|
attached[(index + 1) % attached.length]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
SettingsIntent: CallbackAction<SettingsIntent>(onInvoke: (_) {
|
|
|
|
ref.read(withContextProvider)((context) async {
|
|
|
|
if (!Navigator.of(context).canPop()) {
|
|
|
|
await showBlurDialog(
|
|
|
|
context: context,
|
2023-03-03 18:09:40 +03:00
|
|
|
builder: (context) => const SettingsPage(),
|
2023-02-09 21:12:35 +03:00
|
|
|
routeSettings: const RouteSettings(name: 'settings'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
AboutIntent: CallbackAction<AboutIntent>(onInvoke: (_) {
|
|
|
|
ref.read(withContextProvider)((context) async {
|
|
|
|
if (!Navigator.of(context).canPop()) {
|
|
|
|
await showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => const AboutPage(),
|
|
|
|
routeSettings: const RouteSettings(name: 'about'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
},
|
2023-02-09 15:43:27 +03:00
|
|
|
child: Shortcuts(
|
2023-02-09 21:12:35 +03:00
|
|
|
shortcuts: {
|
2023-03-07 13:03:31 +03:00
|
|
|
ctrlOrCmd(LogicalKeyboardKey.keyC): const CopyIntent(),
|
2023-08-16 15:57:24 +03:00
|
|
|
const SingleActivator(LogicalKeyboardKey.copy): const CopyIntent(),
|
2023-03-07 13:03:31 +03:00
|
|
|
ctrlOrCmd(LogicalKeyboardKey.keyF): const SearchIntent(),
|
2023-02-09 21:12:35 +03:00
|
|
|
if (isDesktop) ...{
|
2023-03-02 16:40:47 +03:00
|
|
|
const SingleActivator(LogicalKeyboardKey.tab, control: true):
|
2023-02-09 21:12:35 +03:00
|
|
|
const NextDeviceIntent(),
|
|
|
|
},
|
|
|
|
if (Platform.isMacOS) ...{
|
2023-08-16 15:57:24 +03:00
|
|
|
const SingleActivator(LogicalKeyboardKey.keyW, meta: true):
|
|
|
|
const HideIntent(),
|
2023-03-02 16:40:47 +03:00
|
|
|
const SingleActivator(LogicalKeyboardKey.keyQ, meta: true):
|
2023-02-09 21:12:35 +03:00
|
|
|
const CloseIntent(),
|
2023-03-02 16:40:47 +03:00
|
|
|
const SingleActivator(LogicalKeyboardKey.comma, meta: true):
|
2023-02-09 21:12:35 +03:00
|
|
|
const SettingsIntent(),
|
|
|
|
},
|
2023-08-16 15:57:24 +03:00
|
|
|
if (Platform.isWindows) ...{
|
|
|
|
const SingleActivator(LogicalKeyboardKey.keyW, control: true):
|
|
|
|
const HideIntent(),
|
|
|
|
},
|
2023-05-26 09:30:21 +03:00
|
|
|
if (Platform.isLinux) ...{
|
|
|
|
const SingleActivator(LogicalKeyboardKey.keyQ, control: true):
|
|
|
|
const CloseIntent(),
|
|
|
|
},
|
2023-02-09 21:12:35 +03:00
|
|
|
},
|
2023-02-09 15:43:27 +03:00
|
|
|
child: child,
|
|
|
|
),
|
|
|
|
);
|