|
1 | 1 | import 'package:flutter/foundation.dart';
|
| 2 | +import 'package:flutter/material.dart'; |
2 | 3 | import 'package:flutter/services.dart';
|
3 |
| -import 'package:flutter/widgets.dart'; |
4 |
| - |
5 |
| -Map<ShortcutActivator, Intent> get defaultTerminalShortcuts { |
6 |
| - switch (defaultTargetPlatform) { |
7 |
| - case TargetPlatform.android: |
8 |
| - case TargetPlatform.fuchsia: |
9 |
| - case TargetPlatform.linux: |
10 |
| - case TargetPlatform.windows: |
11 |
| - return _defaultShortcuts; |
12 |
| - case TargetPlatform.iOS: |
13 |
| - case TargetPlatform.macOS: |
14 |
| - return _defaultAppleShortcuts; |
| 4 | + |
| 5 | +import 'package:xterm/src/core/buffer/cell_offset.dart'; |
| 6 | +import 'package:xterm/src/core/buffer/range_line.dart'; |
| 7 | +import 'package:xterm/src/terminal.dart'; |
| 8 | +import 'package:xterm/src/ui/controller.dart'; |
| 9 | + |
| 10 | +class TerminalShortcut<T extends Intent> { |
| 11 | + /// The activator which triggers the the intent. |
| 12 | + final ShortcutActivator activator; |
| 13 | + |
| 14 | + /// The intent that is triggered bt the activator. |
| 15 | + final T intent; |
| 16 | + |
| 17 | + /// The action to run when the shortcut is invoked. |
| 18 | + final Object? Function(T, Terminal, TerminalController) action; |
| 19 | + |
| 20 | + const TerminalShortcut(this.activator, this.intent, this.action); |
| 21 | + |
| 22 | + /// Use the default modifier key for the current platform so assemble a |
| 23 | + /// key combination for the shortcut. On iOS the macOS the shortcut will be |
| 24 | + /// triggered my META + [key], otherwise CTRL + [key] triggers the shortcut. |
| 25 | + factory TerminalShortcut.platformDefault( |
| 26 | + LogicalKeyboardKey key, |
| 27 | + T intent, |
| 28 | + Object? Function(T, Terminal, TerminalController) action, |
| 29 | + ) { |
| 30 | + switch (defaultTargetPlatform) { |
| 31 | + case TargetPlatform.android: |
| 32 | + case TargetPlatform.fuchsia: |
| 33 | + case TargetPlatform.linux: |
| 34 | + case TargetPlatform.windows: |
| 35 | + return TerminalShortcut( |
| 36 | + SingleActivator(key, control: true), |
| 37 | + intent, |
| 38 | + action, |
| 39 | + ); |
| 40 | + case TargetPlatform.iOS: |
| 41 | + case TargetPlatform.macOS: |
| 42 | + return TerminalShortcut( |
| 43 | + SingleActivator(key, meta: true), |
| 44 | + intent, |
| 45 | + action, |
| 46 | + ); |
| 47 | + } |
15 | 48 | }
|
16 |
| -} |
17 | 49 |
|
18 |
| -final _defaultShortcuts = { |
19 |
| - SingleActivator(LogicalKeyboardKey.keyC, control: true): |
20 |
| - CopySelectionTextIntent.copy, |
21 |
| - SingleActivator(LogicalKeyboardKey.keyV, control: true): |
22 |
| - const PasteTextIntent(SelectionChangedCause.keyboard), |
23 |
| - SingleActivator(LogicalKeyboardKey.keyA, control: true): |
24 |
| - const SelectAllTextIntent(SelectionChangedCause.keyboard), |
25 |
| -}; |
26 |
| - |
27 |
| -final _defaultAppleShortcuts = { |
28 |
| - SingleActivator(LogicalKeyboardKey.keyC, meta: true): |
29 |
| - CopySelectionTextIntent.copy, |
30 |
| - SingleActivator(LogicalKeyboardKey.keyV, meta: true): |
31 |
| - const PasteTextIntent(SelectionChangedCause.keyboard), |
32 |
| - SingleActivator(LogicalKeyboardKey.keyA, meta: true): |
33 |
| - const SelectAllTextIntent(SelectionChangedCause.keyboard), |
34 |
| -}; |
| 50 | + /// Convert the shortcut to a [MapEntry] for passing it to an [Action] Widget. |
| 51 | + MapEntry<Type, Action<T>> toActionMapEntry( |
| 52 | + Terminal terminal, |
| 53 | + TerminalController terminalController, |
| 54 | + ) { |
| 55 | + return MapEntry( |
| 56 | + intent.runtimeType, |
| 57 | + CallbackAction<T>( |
| 58 | + onInvoke: (intent) => action(intent, terminal, terminalController), |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /// Generate a list of default shortcuts for the current platform. |
| 64 | + static List<TerminalShortcut> get defaults { |
| 65 | + return <TerminalShortcut>[ |
| 66 | + TerminalShortcut<CopySelectionTextIntent>.platformDefault( |
| 67 | + LogicalKeyboardKey.keyC, |
| 68 | + CopySelectionTextIntent.copy, |
| 69 | + TerminalShortcut.defaultCopy, |
| 70 | + ), |
| 71 | + TerminalShortcut<PasteTextIntent>.platformDefault( |
| 72 | + LogicalKeyboardKey.keyV, |
| 73 | + const PasteTextIntent(SelectionChangedCause.keyboard), |
| 74 | + TerminalShortcut.defaultPaste, |
| 75 | + ), |
| 76 | + TerminalShortcut<SelectAllTextIntent>.platformDefault( |
| 77 | + LogicalKeyboardKey.keyA, |
| 78 | + const SelectAllTextIntent(SelectionChangedCause.keyboard), |
| 79 | + TerminalShortcut.defaultSelectAll, |
| 80 | + ), |
| 81 | + ]; |
| 82 | + } |
| 83 | + |
| 84 | + /// Default handler for [CopySelectionTextIntent]. |
| 85 | + static Object? defaultCopy( |
| 86 | + CopySelectionTextIntent intent, |
| 87 | + Terminal terminal, |
| 88 | + TerminalController controller, |
| 89 | + ) async { |
| 90 | + final selection = controller.selection; |
| 91 | + |
| 92 | + if (selection == null) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + final text = terminal.buffer.getText(selection); |
| 97 | + |
| 98 | + await Clipboard.setData(ClipboardData(text: text)); |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + /// Default handler for [PasteTextIntent]. |
| 104 | + static Object? defaultPaste( |
| 105 | + PasteTextIntent intent, |
| 106 | + Terminal terminal, |
| 107 | + TerminalController controller, |
| 108 | + ) async { |
| 109 | + final data = await Clipboard.getData(Clipboard.kTextPlain); |
| 110 | + final text = data?.text; |
| 111 | + if (text != null) { |
| 112 | + terminal.paste(text); |
| 113 | + controller.clearSelection(); |
| 114 | + } |
| 115 | + |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /// Default handler for [SelectAllTextIntent]. |
| 121 | + static Object? defaultSelectAll( |
| 122 | + SelectAllTextIntent intent, |
| 123 | + Terminal terminal, |
| 124 | + TerminalController controller, |
| 125 | + ) async { |
| 126 | + controller.setSelection( |
| 127 | + BufferRangeLine( |
| 128 | + CellOffset(0, terminal.buffer.height - terminal.viewHeight), |
| 129 | + CellOffset(terminal.viewWidth, terminal.buffer.height - 1), |
| 130 | + ), |
| 131 | + ); |
| 132 | + return null; |
| 133 | + } |
| 134 | +} |
0 commit comments