2024-09-26 14:29:04 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2025-12-16 11:23:09 +08:00
|
|
|
import 'package:fl_clash/controller.dart';
|
2024-09-08 21:21:21 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
|
|
|
|
import 'package:fl_clash/models/common.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/providers/config.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-09-08 21:21:21 +08:00
|
|
|
import 'package:flutter/services.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-09-08 21:21:21 +08:00
|
|
|
import 'package:hotkey_manager/hotkey_manager.dart';
|
|
|
|
|
|
2025-05-02 02:24:12 +08:00
|
|
|
class HotKeyManager extends ConsumerStatefulWidget {
|
2024-09-08 21:21:21 +08:00
|
|
|
final Widget child;
|
|
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
const HotKeyManager({super.key, required this.child});
|
2024-09-08 21:21:21 +08:00
|
|
|
|
2025-05-02 02:24:12 +08:00
|
|
|
@override
|
|
|
|
|
ConsumerState<HotKeyManager> createState() => _HotKeyManagerState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _HotKeyManagerState extends ConsumerState<HotKeyManager> {
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2025-12-16 11:23:09 +08:00
|
|
|
ref.listenManual(hotKeyActionsProvider, (prev, next) {
|
|
|
|
|
if (!hotKeyActionListEquality.equals(prev, next)) {
|
|
|
|
|
_updateHotKeys(hotKeyActions: next);
|
|
|
|
|
}
|
|
|
|
|
}, fireImmediately: true);
|
2025-05-02 02:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> _handleHotKeyAction(HotAction action) async {
|
2024-09-08 21:21:21 +08:00
|
|
|
switch (action) {
|
|
|
|
|
case HotAction.mode:
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateMode();
|
2024-09-08 21:21:21 +08:00
|
|
|
case HotAction.start:
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateStart();
|
2024-09-08 21:21:21 +08:00
|
|
|
case HotAction.view:
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateVisible();
|
2024-09-08 21:21:21 +08:00
|
|
|
case HotAction.proxy:
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateSystemProxy();
|
2024-09-08 21:21:21 +08:00
|
|
|
case HotAction.tun:
|
2025-12-16 11:23:09 +08:00
|
|
|
appController.updateTun();
|
2024-09-08 21:21:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> _updateHotKeys({
|
2024-09-08 21:21:21 +08:00
|
|
|
required List<HotKeyAction> hotKeyActions,
|
|
|
|
|
}) async {
|
|
|
|
|
await hotKeyManager.unregisterAll();
|
2025-12-16 11:23:09 +08:00
|
|
|
final hotkeyActionHandles = hotKeyActions
|
|
|
|
|
.where((hotKeyAction) {
|
|
|
|
|
return hotKeyAction.key != null && hotKeyAction.modifiers.isNotEmpty;
|
|
|
|
|
})
|
|
|
|
|
.map<Future>((hotKeyAction) async {
|
|
|
|
|
final modifiers = hotKeyAction.modifiers
|
|
|
|
|
.map((item) => item.toHotKeyModifier())
|
|
|
|
|
.toList();
|
|
|
|
|
final hotKey = HotKey(
|
|
|
|
|
key: PhysicalKeyboardKey(hotKeyAction.key!),
|
|
|
|
|
modifiers: modifiers,
|
|
|
|
|
);
|
|
|
|
|
return await hotKeyManager.register(
|
|
|
|
|
hotKey,
|
|
|
|
|
keyDownHandler: (_) {
|
|
|
|
|
_handleHotKeyAction(hotKeyAction.action);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
2024-09-08 21:21:21 +08:00
|
|
|
await Future.wait(hotkeyActionHandles);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Shortcuts _buildShortcuts(Widget child) {
|
2025-05-02 02:24:12 +08:00
|
|
|
return Shortcuts(
|
|
|
|
|
shortcuts: {
|
|
|
|
|
utils.controlSingleActivator(LogicalKeyboardKey.keyW):
|
|
|
|
|
CloseWindowIntent(),
|
|
|
|
|
},
|
|
|
|
|
child: Actions(
|
|
|
|
|
actions: {
|
|
|
|
|
CloseWindowIntent: CallbackAction<CloseWindowIntent>(
|
2025-12-16 11:23:09 +08:00
|
|
|
onInvoke: (_) => appController.handleBackOrExit(),
|
2025-05-02 02:24:12 +08:00
|
|
|
),
|
|
|
|
|
DoNothingIntent: CallbackAction<DoNothingIntent>(
|
|
|
|
|
onInvoke: (_) => null,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
child: child,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-08 21:21:21 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-12-16 11:23:09 +08:00
|
|
|
return _buildShortcuts(widget.child);
|
2024-09-08 21:21:21 +08:00
|
|
|
}
|
|
|
|
|
}
|