test_text_input_key_handler.dart 11.5 KB
Newer Older
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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'binding.dart';

/// Processes text input events that were not handled by the framework.
abstract class TestTextInputKeyHandler {
  /// Process key down event that was not handled by the framework.
  Future<void> handleKeyDownEvent(LogicalKeyboardKey key);

  /// Process key up event that was not handled by the framework.
  Future<void> handleKeyUpEvent(LogicalKeyboardKey key);
}

/// MacOS specific key input handler. This class translates standard macOS text editing shortcuts
/// into appropriate selectors similarly to what NSTextInputContext does in Flutter Engine.
class MacOSTestTextInputKeyHandler extends TestTextInputKeyHandler {
  /// Create a new macOS specific text input handler.
  MacOSTestTextInputKeyHandler(this.client);

  /// ClientId of TextInput
  final int client;

  Future<void> _sendSelectors(List<String> selectors) async {
29
    await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        .handlePlatformMessage(
      SystemChannels.textInput.name,
      SystemChannels.textInput.codec.encodeMethodCall(
        MethodCall(
            'TextInputClient.performSelectors', <dynamic>[client, selectors]),
      ),
      (ByteData? data) {/* response from framework is discarded */},
    );
  }

  // These combinations must match NSStandardKeyBindingResponding.
  static final Map<SingleActivator, List<String>> _macOSActivatorToSelectors =
      <SingleActivator, List<String>>{
    for (final bool pressShift in const <bool>[
      true,
      false
    ]) ...<SingleActivator, List<String>>{
      SingleActivator(LogicalKeyboardKey.backspace, shift: pressShift):
          <String>['deleteBackward:'],
      SingleActivator(LogicalKeyboardKey.backspace,
          alt: true, shift: pressShift): <String>['deleteWordBackward:'],
      SingleActivator(LogicalKeyboardKey.backspace,
          meta: true, shift: pressShift): <String>['deleteToBeginningOfLine:'],
53 54
      SingleActivator(LogicalKeyboardKey.backspace, control: true, shift: pressShift):
          <String>['deleteBackwardByDecomposingPreviousCharacter:'],
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
      SingleActivator(LogicalKeyboardKey.delete, shift: pressShift): <String>[
        'deleteForward:'
      ],
      SingleActivator(LogicalKeyboardKey.delete, alt: true, shift: pressShift):
          <String>['deleteWordForward:'],
      SingleActivator(LogicalKeyboardKey.delete, meta: true, shift: pressShift):
          <String>['deleteToEndOfLine:'],
    },
    const SingleActivator(LogicalKeyboardKey.arrowLeft): <String>['moveLeft:'],
    const SingleActivator(LogicalKeyboardKey.arrowRight): <String>[
      'moveRight:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowUp): <String>['moveUp:'],
    const SingleActivator(LogicalKeyboardKey.arrowDown): <String>['moveDown:'],
    const SingleActivator(LogicalKeyboardKey.arrowLeft, shift: true): <String>[
      'moveLeftAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowRight, shift: true): <String>[
      'moveRightAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowUp, shift: true): <String>[
      'moveUpAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowDown, shift: true): <String>[
      'moveDownAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true): <String>[
      'moveWordLeft:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowRight, alt: true): <String>[
      'moveWordRight:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): <String>[
      'moveBackward:',
      'moveToBeginningOfParagraph:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowDown, alt: true): <String>[
      'moveForward:',
      'moveToEndOfParagraph:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowLeft, alt: true, shift: true):
        <String>['moveWordLeftAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowRight,
        alt: true, shift: true): <String>['moveWordRightAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowUp, alt: true, shift: true):
        <String>['moveParagraphBackwardAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowDown, alt: true, shift: true):
        <String>['moveParagraphForwardAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowLeft, meta: true): <String>[
      'moveToLeftEndOfLine:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowRight, meta: true): <String>[
      'moveToRightEndOfLine:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowUp, meta: true): <String>[
      'moveToBeginningOfDocument:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowDown, meta: true): <String>[
      'moveToEndOfDocument:'
    ],
    const SingleActivator(LogicalKeyboardKey.arrowLeft,
        meta: true,
        shift: true): <String>['moveToLeftEndOfLineAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowRight,
        meta: true,
        shift: true): <String>['moveToRightEndOfLineAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowUp, meta: true, shift: true):
        <String>['moveToBeginningOfDocumentAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.arrowDown,
        meta: true,
        shift: true): <String>['moveToEndOfDocumentAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyA, control: true, shift: true):
        <String>['moveToBeginningOfParagraphAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyA, control: true): <String>[
      'moveToBeginningOfParagraph:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyB, control: true, shift: true):
        <String>['moveBackwardAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyB, control: true): <String>[
      'moveBackward:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyE, control: true, shift: true):
        <String>['moveToEndOfParagraphAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyE, control: true): <String>[
      'moveToEndOfParagraph:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyF, control: true, shift: true):
        <String>['moveForwardAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyF, control: true): <String>[
      'moveForward:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyK, control: true): <String>[
      'deleteToEndOfParagraph'
    ],
    const SingleActivator(LogicalKeyboardKey.keyL, control: true): <String>[
      'centerSelectionInVisibleArea'
    ],
    const SingleActivator(LogicalKeyboardKey.keyN, control: true): <String>[
      'moveDown:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyN, control: true, shift: true):
        <String>['moveDownAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyO, control: true): <String>[
      'insertNewlineIgnoringFieldEditor:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyP, control: true): <String>[
      'moveUp:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyP, control: true, shift: true):
        <String>['moveUpAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyT, control: true): <String>[
      'transpose:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyV, control: true): <String>[
      'pageDown:'
    ],
    const SingleActivator(LogicalKeyboardKey.keyV, control: true, shift: true):
        <String>['pageDownAndModifySelection:'],
    const SingleActivator(LogicalKeyboardKey.keyY, control: true): <String>[
      'yank:'
    ],
    const SingleActivator(LogicalKeyboardKey.quoteSingle, control: true):
        <String>['insertSingleQuoteIgnoringSubstitution:'],
    const SingleActivator(LogicalKeyboardKey.quote, control: true): <String>[
      'insertDoubleQuoteIgnoringSubstitution:'
    ],
    const SingleActivator(LogicalKeyboardKey.home): <String>[
      'scrollToBeginningOfDocument:'
    ],
    const SingleActivator(LogicalKeyboardKey.end): <String>[
      'scrollToEndOfDocument:'
    ],
    const SingleActivator(LogicalKeyboardKey.home, shift: true): <String>[
      'moveToBeginningOfDocumentAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.end, shift: true): <String>[
      'moveToEndOfDocumentAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.pageUp): <String>['scrollPageUp:'],
    const SingleActivator(LogicalKeyboardKey.pageDown): <String>[
      'scrollPageDown:'
    ],
    const SingleActivator(LogicalKeyboardKey.pageUp, shift: true): <String>[
      'pageUpAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.pageDown, shift: true): <String>[
      'pageDownAndModifySelection:'
    ],
    const SingleActivator(LogicalKeyboardKey.escape): <String>[
      'cancelOperation:'
    ],
    const SingleActivator(LogicalKeyboardKey.enter): <String>['insertNewline:'],
    const SingleActivator(LogicalKeyboardKey.enter, alt: true): <String>[
      'insertNewlineIgnoringFieldEditor:'
    ],
    const SingleActivator(LogicalKeyboardKey.enter, control: true): <String>[
      'insertLineBreak:'
    ],
    const SingleActivator(LogicalKeyboardKey.tab): <String>['insertTab:'],
    const SingleActivator(LogicalKeyboardKey.tab, alt: true): <String>[
      'insertTabIgnoringFieldEditor:'
    ],
    const SingleActivator(LogicalKeyboardKey.tab, shift: true): <String>[
      'insertBacktab:'
    ],
  };

  @override
  Future<void> handleKeyDownEvent(LogicalKeyboardKey key) async {
    if (key == LogicalKeyboardKey.shift ||
        key == LogicalKeyboardKey.shiftLeft ||
        key == LogicalKeyboardKey.shiftRight) {
      _shift = true;
    } else if (key == LogicalKeyboardKey.alt ||
        key == LogicalKeyboardKey.altLeft ||
        key == LogicalKeyboardKey.altRight) {
      _alt = true;
    } else if (key == LogicalKeyboardKey.meta ||
        key == LogicalKeyboardKey.metaLeft ||
        key == LogicalKeyboardKey.metaRight) {
      _meta = true;
    } else if (key == LogicalKeyboardKey.control ||
        key == LogicalKeyboardKey.controlLeft ||
        key == LogicalKeyboardKey.controlRight) {
      _control = true;
    } else {
      for (final MapEntry<SingleActivator, List<String>> entry
          in _macOSActivatorToSelectors.entries) {
        final SingleActivator activator = entry.key;
        if (activator.triggers.first == key &&
            activator.shift == _shift &&
            activator.alt == _alt &&
            activator.meta == _meta &&
            activator.control == _control) {
          await _sendSelectors(entry.value);
          return;
        }
      }
    }
  }

  @override
  Future<void> handleKeyUpEvent(LogicalKeyboardKey key) async {
    if (key == LogicalKeyboardKey.shift ||
        key == LogicalKeyboardKey.shiftLeft ||
        key == LogicalKeyboardKey.shiftRight) {
      _shift = false;
    } else if (key == LogicalKeyboardKey.alt ||
        key == LogicalKeyboardKey.altLeft ||
        key == LogicalKeyboardKey.altRight) {
      _alt = false;
    } else if (key == LogicalKeyboardKey.meta ||
        key == LogicalKeyboardKey.metaLeft ||
        key == LogicalKeyboardKey.metaRight) {
      _meta = false;
    } else if (key == LogicalKeyboardKey.control ||
        key == LogicalKeyboardKey.controlLeft ||
        key == LogicalKeyboardKey.controlRight) {
      _control = false;
    }
  }

  bool _shift = false;
  bool _alt = false;
  bool _meta = false;
  bool _control = false;
}