2022-08-08 16:36:26 +08:00
|
|
|
import 'dart:io';
|
2023-07-05 10:58:36 +05:30
|
|
|
import 'package:appflowy/workspace/application/appearance.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/workspace/application/home/home_setting_bloc.dart';
|
2023-08-08 07:09:17 +02:00
|
|
|
import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart';
|
2022-08-08 16:36:26 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:hotkey_manager/hotkey_manager.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
2023-08-08 07:09:17 +02:00
|
|
|
typedef KeyDownHandler = void Function(HotKey hotKey);
|
|
|
|
|
|
|
|
/// Helper class that utilizes the global [HotKeyManager] to easily
|
|
|
|
/// add a [HotKey] with different handlers.
|
|
|
|
///
|
|
|
|
/// Makes registration of a [HotKey] simple and easy to read, and makes
|
|
|
|
/// sure the [KeyDownHandler], and other handlers, are grouped with the
|
|
|
|
/// relevant [HotKey].
|
|
|
|
///
|
|
|
|
class HotKeyItem {
|
|
|
|
final HotKey hotKey;
|
|
|
|
final KeyDownHandler? keyDownHandler;
|
|
|
|
|
|
|
|
HotKeyItem({
|
|
|
|
required this.hotKey,
|
|
|
|
this.keyDownHandler,
|
|
|
|
});
|
|
|
|
|
|
|
|
void register() =>
|
|
|
|
hotKeyManager.register(hotKey, keyDownHandler: keyDownHandler);
|
|
|
|
}
|
|
|
|
|
2022-08-08 16:36:26 +08:00
|
|
|
class HomeHotKeys extends StatelessWidget {
|
|
|
|
final Widget child;
|
2022-08-08 17:12:27 +08:00
|
|
|
const HomeHotKeys({required this.child, Key? key}) : super(key: key);
|
2022-08-08 16:36:26 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-08-08 07:09:17 +02:00
|
|
|
// Collapse sidebar menu
|
|
|
|
HotKeyItem(
|
|
|
|
hotKey: HotKey(
|
|
|
|
KeyCode.backslash,
|
|
|
|
modifiers: [Platform.isMacOS ? KeyModifier.meta : KeyModifier.control],
|
|
|
|
// Set hotkey scope (default is HotKeyScope.system)
|
|
|
|
scope: HotKeyScope.inapp, // Set as inapp-wide hotkey.
|
|
|
|
),
|
|
|
|
keyDownHandler: (_) => context
|
|
|
|
.read<HomeSettingBloc>()
|
|
|
|
.add(const HomeSettingEvent.collapseMenu()),
|
|
|
|
).register();
|
|
|
|
|
|
|
|
// Toggle theme mode light/dark
|
|
|
|
HotKeyItem(
|
|
|
|
hotKey: HotKey(
|
|
|
|
KeyCode.keyL,
|
|
|
|
modifiers: [
|
|
|
|
Platform.isMacOS ? KeyModifier.meta : KeyModifier.control,
|
|
|
|
KeyModifier.shift,
|
|
|
|
],
|
|
|
|
scope: HotKeyScope.inapp,
|
|
|
|
),
|
|
|
|
keyDownHandler: (_) =>
|
|
|
|
context.read<AppearanceSettingsCubit>().toggleThemeMode(),
|
|
|
|
).register();
|
|
|
|
|
|
|
|
// Close current tab
|
|
|
|
HotKeyItem(
|
|
|
|
hotKey: HotKey(
|
|
|
|
KeyCode.keyW,
|
|
|
|
modifiers: [Platform.isMacOS ? KeyModifier.meta : KeyModifier.control],
|
|
|
|
scope: HotKeyScope.inapp,
|
|
|
|
),
|
|
|
|
keyDownHandler: (_) =>
|
|
|
|
context.read<TabsBloc>().add(const TabsEvent.closeCurrentTab()),
|
|
|
|
).register();
|
|
|
|
|
|
|
|
// Go to previous tab
|
|
|
|
HotKeyItem(
|
|
|
|
hotKey: HotKey(
|
|
|
|
KeyCode.pageUp,
|
|
|
|
modifiers: [Platform.isMacOS ? KeyModifier.meta : KeyModifier.control],
|
|
|
|
scope: HotKeyScope.inapp,
|
|
|
|
),
|
|
|
|
keyDownHandler: (_) => _selectTab(context, -1),
|
|
|
|
).register();
|
|
|
|
|
|
|
|
// Go to next tab
|
|
|
|
HotKeyItem(
|
|
|
|
hotKey: HotKey(
|
|
|
|
KeyCode.pageDown,
|
|
|
|
modifiers: [Platform.isMacOS ? KeyModifier.meta : KeyModifier.control],
|
|
|
|
scope: HotKeyScope.inapp,
|
|
|
|
),
|
|
|
|
keyDownHandler: (_) => _selectTab(context, 1),
|
|
|
|
).register();
|
|
|
|
|
2022-08-08 16:36:26 +08:00
|
|
|
return child;
|
|
|
|
}
|
2023-08-08 07:09:17 +02:00
|
|
|
|
|
|
|
void _selectTab(BuildContext context, int change) {
|
|
|
|
final bloc = context.read<TabsBloc>();
|
|
|
|
bloc.add(TabsEvent.selectTab(bloc.state.currentIndex + change));
|
|
|
|
}
|
2022-08-08 16:36:26 +08:00
|
|
|
}
|