From 57ac0e5c4d373ddd40b938bb99e9f003fc59ca7a Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sun, 30 Mar 2025 11:07:36 -0400 Subject: [PATCH] oops --- lib/keyboard_shortcuts.dart | 98 +++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 lib/keyboard_shortcuts.dart diff --git a/lib/keyboard_shortcuts.dart b/lib/keyboard_shortcuts.dart new file mode 100644 index 0000000..0c531a9 --- /dev/null +++ b/lib/keyboard_shortcuts.dart @@ -0,0 +1,98 @@ +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(LogicalKeyboardKey.alt, LogicalKeyboardKey.keyR): + const ReloadThemeIntent(), + LogicalKeySet(LogicalKeyboardKey.alt, LogicalKeyboardKey.keyD): + const AttachDetachIntent(), + LogicalKeySet( + LogicalKeyboardKey.alt, LogicalKeyboardKey.backquote): + const DeveloperPageIntent(), + }, + child: Actions(actions: >{ + ReloadThemeIntent: CallbackAction( + onInvoke: (intent) => reloadTheme(context)), + AttachDetachIntent: CallbackAction( + onInvoke: (intent) => _attachDetach(context)), + DeveloperPageIntent: CallbackAction( + onInvoke: (intent) => _developerPage(context)), + }, child: Focus(autofocus: true, child: child)))); + + ///////////////////////////////////////////////////////// + + final Widget child; +}