veilidchat/lib/keyboard_shortcuts.dart
Christien Rioux 57ac0e5c4d oops
2025-03-30 11:07:36 -04:00

99 lines
3.4 KiB
Dart

import 'package:animated_theme_switcher/animated_theme_switcher.dart';
import 'package:async_tools/async_tools.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:veilid_support/veilid_support.dart';
import 'init.dart';
import 'router/router.dart';
import 'settings/settings.dart';
import 'theme/theme.dart';
import 'tools/tools.dart';
import 'veilid_processor/veilid_processor.dart';
class ReloadThemeIntent extends Intent {
const ReloadThemeIntent();
}
class AttachDetachIntent extends Intent {
const AttachDetachIntent();
}
class DeveloperPageIntent extends Intent {
const DeveloperPageIntent();
}
class KeyboardShortcuts extends StatelessWidget {
const KeyboardShortcuts({required this.child, super.key});
void reloadTheme(BuildContext context) {
singleFuture(this, () async {
log.info('Reloading theme');
await VeilidChatGlobalInit.loadAssetManifest();
final theme =
PreferencesRepository.instance.value.themePreference.themeData();
if (context.mounted) {
ThemeSwitcher.of(context).changeTheme(theme: theme);
// Hack to reload translations
final localizationDelegate = LocalizedApp.of(context).delegate;
await LocalizationDelegate.create(
fallbackLocale: localizationDelegate.fallbackLocale.toString(),
supportedLocales: localizationDelegate.supportedLocales
.map((x) => x.toString())
.toList());
}
});
}
void _attachDetach(BuildContext context) {
singleFuture(this, () async {
if (ProcessorRepository.instance.processorConnectionState.isAttached) {
log.info('Detaching');
await Veilid.instance.detach();
} else if (ProcessorRepository
.instance.processorConnectionState.isDetached) {
log.info('Attaching');
await Veilid.instance.attach();
}
});
}
void _developerPage(BuildContext context) {
singleFuture(this, () async {
final path = GoRouter.of(context).location();
if (path != '/developer') {
await GoRouterHelper(context).push('/developer');
}
});
}
@override
Widget build(BuildContext context) => ThemeSwitcher(
builder: (context) => Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.alt, LogicalKeyboardKey.keyR):
const ReloadThemeIntent(),
LogicalKeySet(LogicalKeyboardKey.alt, LogicalKeyboardKey.keyD):
const AttachDetachIntent(),
LogicalKeySet(
LogicalKeyboardKey.alt, LogicalKeyboardKey.backquote):
const DeveloperPageIntent(),
},
child: Actions(actions: <Type, Action<Intent>>{
ReloadThemeIntent: CallbackAction<ReloadThemeIntent>(
onInvoke: (intent) => reloadTheme(context)),
AttachDetachIntent: CallbackAction<AttachDetachIntent>(
onInvoke: (intent) => _attachDetach(context)),
DeveloperPageIntent: CallbackAction<DeveloperPageIntent>(
onInvoke: (intent) => _developerPage(context)),
}, child: Focus(autofocus: true, child: child))));
/////////////////////////////////////////////////////////
final Widget child;
}