forked from AntoineMarcel/keyboard_shortcuts
-
Notifications
You must be signed in to change notification settings - Fork 6
/
keyboard_shortcuts.dart
294 lines (263 loc) · 10.6 KB
/
keyboard_shortcuts.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
library keyboard_shortcuts;
import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tuple/tuple.dart';
import 'package:visibility_detector/visibility_detector.dart';
Widget? _homeWidget;
List<_KeyBoardShortcuts> _keyBoardShortcuts = [];
Widget? _customGlobal;
String? _customTitle;
IconData? _customIcon;
bool _helperIsOpen = false;
List<Tuple3<Set<LogicalKeyboardKey>, Function(BuildContext context), String>> _newGlobal = [];
enum BasicShortCuts {
creation,
previousPage,
nextPage,
save,
}
void initShortCuts(
Widget homePage, {
Set<Set<LogicalKeyboardKey>>? keysToPress,
Set<Function(BuildContext context)>? onKeysPressed,
Set<String>? helpLabel,
Widget? helpGlobal,
String? helpTitle,
IconData? helpIcon,
}) async {
if (keysToPress != null &&
onKeysPressed != null &&
helpLabel != null &&
keysToPress.length == onKeysPressed.length &&
onKeysPressed.length == helpLabel.length) {
_newGlobal = [];
for (var i = 0; i < keysToPress.length; i++) {
_newGlobal.add(Tuple3(keysToPress.elementAt(i), onKeysPressed.elementAt(i), helpLabel.elementAt(i)));
}
}
_homeWidget = homePage;
_customGlobal = helpGlobal;
_customTitle = helpTitle;
_customIcon = helpIcon;
}
bool _isPressed(Set<LogicalKeyboardKey> keysPressed, Set<LogicalKeyboardKey> keysToPress) {
//when we type shift on chrome flutter's core return two pressed keys : Shift Left && Shift Right. So we need to delete one on the set to run the action
var rights = keysPressed.where((element) => element.debugName!.contains("Right"));
var lefts = keysPressed.where((element) => element.debugName!.contains("Left"));
var toRemove = [];
for (final rightElement in rights) {
var leftElement =
lefts.firstWhereOrNull((element) => element.debugName!.split(" ")[0] == rightElement.debugName!.split(" ")[0]);
if (leftElement != null) {
var actualKey = keysToPress.where((element) => element.debugName!.split(" ")[0] == rightElement.debugName!.split(" ")[0]);
if (actualKey.length > 0 && actualKey.first.debugName!.isNotEmpty)
actualKey.first.debugName!.contains("Right") ? toRemove.add(leftElement) : toRemove.add(rightElement);
}
}
keysPressed.removeWhere((e) => toRemove.contains(e));
return keysPressed.containsAll(keysToPress) && keysPressed.length == keysToPress.length;
}
class KeyBoardShortcuts extends StatefulWidget {
final Widget child;
/// You can use shortCut function with BasicShortCuts to avoid write data by yourself
final Set<LogicalKeyboardKey>? keysToPress;
/// Function when keys are pressed
final VoidCallback? onKeysPressed;
/// Label who will be displayed in helper
final String? helpLabel;
/// Activate when this widget is the first of the page
final bool globalShortcuts;
KeyBoardShortcuts({this.keysToPress, this.onKeysPressed, this.helpLabel, this.globalShortcuts = false, required this.child, Key? key})
: super(key: key);
@override
_KeyBoardShortcuts createState() => _KeyBoardShortcuts();
}
class _KeyBoardShortcuts extends State<KeyBoardShortcuts> {
FocusScopeNode? focusScopeNode;
ScrollController _controller = ScrollController();
bool controllerIsReady = false;
bool listening = false;
late Key key;
@override
void initState() {
_controller.addListener(() {
if (_controller.hasClients) setState(() => controllerIsReady = true);
});
_attachKeyboardIfDetached();
key = widget.key ?? UniqueKey();
super.initState();
}
@override
void dispose() {
super.dispose();
_controller.dispose();
_detachKeyboardIfAttached();
}
void _attachKeyboardIfDetached() {
if (listening) return;
_keyBoardShortcuts.add(this);
RawKeyboard.instance.addListener(listener);
listening = true;
}
void _detachKeyboardIfAttached() {
if (!listening) return;
_keyBoardShortcuts.remove(this);
RawKeyboard.instance.removeListener(listener);
listening = false;
}
void listener(RawKeyEvent v) async {
if (!mounted || _helperIsOpen) return;
Set<LogicalKeyboardKey> keysPressed = RawKeyboard.instance.keysPressed;
if (v.runtimeType == RawKeyDownEvent) {
// when user type keysToPress
if (widget.keysToPress != null && widget.onKeysPressed != null && _isPressed(keysPressed, widget.keysToPress!)) {
widget.onKeysPressed!();
}
// when user request help menu
else if (_isPressed(keysPressed, {LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyH})) {
List<Widget> activeHelp = [];
//verify if element is visible or not
List<_KeyBoardShortcuts> toRemove = [];
_keyBoardShortcuts.forEach((element) {
if (VisibilityDetectorController.instance.widgetBoundsFor(element.key) == null) {
element.listening = false;
toRemove.add(element);
}
});
_keyBoardShortcuts.removeWhere((element) => toRemove.contains(element));
_keyBoardShortcuts.forEach((element) {
Widget? elementWidget = _helpWidget(element);
if (elementWidget != null) activeHelp.add(elementWidget);
}); // get all custom shortcuts
bool showGlobalShort = _keyBoardShortcuts.any((element) => element.widget.globalShortcuts);
if (!_helperIsOpen && (activeHelp.isNotEmpty || showGlobalShort)) {
_helperIsOpen = true;
await showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog(
key: UniqueKey(),
title: Text(_customTitle ?? 'Keyboard Shortcuts'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
if (activeHelp.isNotEmpty)
ListBody(
children: [
for (final i in activeHelp) i,
Divider(),
],
),
if (showGlobalShort)
_customGlobal != null
? _customGlobal!
: ListBody(
children: [
for (final newElement in _newGlobal)
ListTile(
leading: Icon(_customIcon ?? Icons.settings),
title: Text(newElement.item3),
subtitle: Text(_getKeysToPress(newElement.item1)),
),
ListTile(
leading: Icon(Icons.home),
title: Text("Go on Home page"),
subtitle: Text(LogicalKeyboardKey.home.debugName!),
),
ListTile(
leading: Icon(Icons.subdirectory_arrow_left),
title: Text("Go on last page"),
subtitle: Text(LogicalKeyboardKey.escape.debugName!),
),
ListTile(
leading: Icon(Icons.keyboard_arrow_up),
title: Text("Scroll to top"),
subtitle: Text(LogicalKeyboardKey.pageUp.debugName!),
),
ListTile(
leading: Icon(Icons.keyboard_arrow_down),
title: Text("Scroll to bottom"),
subtitle: Text(LogicalKeyboardKey.pageDown.debugName!),
),
],
),
],
),
),
),
).then((value) => _helperIsOpen = false);
}
} else if (widget.globalShortcuts) {
if (_homeWidget != null && _isPressed(keysPressed, {LogicalKeyboardKey.home})) {
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => _homeWidget!), (_) => false);
} else if (_isPressed(keysPressed, {LogicalKeyboardKey.escape})) {
Navigator.maybePop(context);
} else if (controllerIsReady && keysPressed.containsAll({LogicalKeyboardKey.pageDown}) ||
keysPressed.first.keyId == 0x10700000022) {
_controller.animateTo(
_controller.position.maxScrollExtent,
duration: new Duration(milliseconds: 50),
curve: Curves.easeOut,
);
} else if (controllerIsReady && keysPressed.containsAll({LogicalKeyboardKey.pageUp}) || keysPressed.first.keyId == 0x10700000021) {
_controller.animateTo(
_controller.position.minScrollExtent,
duration: new Duration(milliseconds: 50),
curve: Curves.easeOut,
);
}
for (final newElement in _newGlobal) {
if (_isPressed(keysPressed, newElement.item1)) {
newElement.item2(context);
return;
}
}
}
}
}
@override
Widget build(BuildContext context) {
return VisibilityDetector(
key: key,
child: PrimaryScrollController(controller: _controller, child: widget.child),
onVisibilityChanged: (visibilityInfo) {
if (visibilityInfo.visibleFraction == 1)
_attachKeyboardIfDetached();
else
_detachKeyboardIfAttached();
},
);
}
}
String _getKeysToPress(Set<LogicalKeyboardKey>? keysToPress) {
String text = "";
if (keysToPress != null) {
for (final i in keysToPress) text += i.debugName! + " + ";
text = text.substring(0, text.lastIndexOf(" + "));
}
return text;
}
Widget? _helpWidget(_KeyBoardShortcuts widget) {
String text = _getKeysToPress(widget.widget.keysToPress);
if (widget.widget.helpLabel != null && text != "")
return ListTile(
leading: Icon(_customIcon ?? Icons.settings),
title: Text(widget.widget.helpLabel!),
subtitle: Text(text),
);
return null;
}
Set<LogicalKeyboardKey> shortCut(BasicShortCuts basicShortCuts) {
switch (basicShortCuts) {
case BasicShortCuts.creation:
return {LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyN};
case BasicShortCuts.previousPage:
return {LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.arrowLeft};
case BasicShortCuts.nextPage:
return {LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.arrowRight};
case BasicShortCuts.save:
return {LogicalKeyboardKey.controlLeft, LogicalKeyboardKey.keyS};
default:
return {};
}
}