Skip to content

Remove keyLabel and debugName from Logical/PhysicalKeyboardKey - #78263

Merged
dkwingsmt merged 21 commits into
flutter:masterfrom
dkwingsmt:remove-logical-keylabel
Mar 24, 2021
Merged

dkwingsmt merged 21 commits into
flutter:masterfrom
dkwingsmt:remove-logical-keylabel

Conversation

@dkwingsmt

@dkwingsmt dkwingsmt commented Mar 15, 2021 •

Copy link
Copy Markdown
Contributor

This PR makes the following changes:

  • Remove LogicalKeyboardKey.keyLabel
  • Make Physical/LogicalKeyboardKey.debugName getters
  • Make Physical/LogicalKeyboardKey() factory constructors, which cache non-predefined instances.

Reasons:

  1. It has been planned to remove keyLabel because it brings confusion to usage, and complexity to expanding code related with logical keys.
  • The keyLabel is a duplicate information to flutterId. It is possible to construct an instance with the same flutterId and different keyLabel, but what does it even mean?
  • An alternative way is to keep keyLabel as a getter, but for now it doesn't seem necessary. The keyLabel is not used anywhere in keyboard-related algorithms, nor does it seem like a necessary part for client-side processing. Since flutterId is the deciding property for a logical key, keyLabel only serves for explanation. It's like another debugName but available at run time.
  1. With debugName being turned into a getter, the Logical/PhysicalKeyboardKey classes no longer needs to override hashCode and operator ==. This allows the key instances to be used in a const context and removes a linter exception (prefer_const_literals_to_create_immutables), such as
  const <LogicalKeyboardKey>[
    LogicalKeyboardKey.keyA,
    LogicalKeyboardKey.keyX,
  ]  // This would trigger compiler error before
  <LogicalKeyboardKey>[
    const LogicalKeyboardKey.keyA,
    const LogicalKeyboardKey.keyX,
  ]  // This would trigger linter error

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard Bot added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. c: contributor-productivity Team-specific productivity, code health, technical debt. labels Mar 15, 2021
@google-cla google-cla Bot added the cla: yes label Mar 15, 2021
@dkwingsmt
dkwingsmt requested a review from gspencergoog March 16, 2021 00:01
@cungocthuong cungocthuong mentioned this pull request Mar 16, 2021

@gspencergoog gspencergoog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32384589-a60f0e74-c078-11e7-9bc1-e5b5287aea9d

Hey, this change is awesome! Much cleaner! Hopefully it won't be breaking for many folks, but I doubt it will be. About the only thing some developers might be doing is creating their own LogicalKeyboardKeys, so removing the keyLabel constructor arg might cause some breakage, but as you point out, it's not really used much.

/// } else {
/// if (kReleaseMode) {
/// _message = 'Not a Q: Key label is "${event.logicalKey.keyLabel}"';
/// _message = 'Not a Q: Pressed 0x${event.logicalKey.keyId.toRadixString(16)}';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe print the character or debugLabel here in addition? Otherwise it doesn't show which key was pressed, just a hex value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would if this is a framework code. Since it's only an example, I'm aiming for simplicity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it's exactly in an example that it should be clear that you can print those things. People aren't going to print the hex string, they're going to print the debug string so that know what key was pressed, so that's the best example of the usage.

@dkwingsmt dkwingsmt Mar 16, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the problem is that the keyId is technically not connected with the character it represents. It's entirely coincidental that the key ID mirrors the ASCII code the key might represent (without a modifier) in the ASCII printable range. How should we convert 0x01000c018a into a string that says "launch mail"? In release mode all explanatory information of a key object will and should be stripped. The only thing the key object represents then is the ID as a bare index.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if it's part of the Unicode ID plane, it could at least include that Unicode character as a label.

@fluttergithubbot

Copy link
Copy Markdown
Contributor

This pull request is not suitable for automatic merging in its current state.

  • The status or check suite Linux build_aar_module_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux build_tests_1_2 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux build_tests_2_2 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux firebase_abstract_method_smoke_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux firebase_android_embedding_v2_smoke_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux firebase_release_smoke_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux gradle_desugar_classes_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux gradle_non_android_plugin_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux gradle_plugin_bundle_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux gradle_plugin_fat_apk_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux gradle_plugin_light_apk_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux module_host_with_custom_build_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux module_custom_host_app_name_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux module_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux plugin_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Linux tool_integration_tests_2_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac build_aar_module_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac build_tests_1_4 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac build_tests_2_4 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac build_tests_3_4 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac build_tests_4_4 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac gradle_non_android_plugin_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac gradle_plugin_bundle_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac gradle_plugin_fat_apk_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac gradle_plugin_light_apk_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac module_custom_host_app_name_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac module_host_with_custom_build_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac module_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac plugin_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac tool_integration_tests_1_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Mac tool_integration_tests_3_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows build_aar_module_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows build_tests_1_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows build_tests_2_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows build_tests_3_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows gradle_non_android_plugin_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows gradle_plugin_bundle_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows gradle_plugin_light_apk_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows module_custom_host_app_name_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows module_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows module_host_with_custom_build_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows plugin_test has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite Windows tool_integration_tests_1_3 has failed. Please fix the issues identified (or deflake) before re-applying this label.

return other is LogicalKeyboardKey
&& other.keyId == keyId;
}
String? get debugName => _debugNames[keyId];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this also include a representation of the key as a Unicode character, if it is part of the Unicode ID plane?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying to include it as the release information or the debug information?

  • If as release information, isn't it redundant symbol since it's unnecessary for runtime logic?
  • If as debug information, doesn't the current debugName already convey the key name?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As debug information only. It would include a lot more keys that aren't in your _debugNames map: any Unicode-producing key would have a debugName that includes the character it represents. You could just produce a string if it doesn't exist in the map. If you do that here, then it'll also get printed as part of the debugFillProperties output.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to add a debugCharacterLabel as you describe. See dkwingsmt@c62cf08

It doesn't really work well in my opinion.

  • As you said, this is debug information only. Therefore it doesn't solve the problem mentioned in Remove keyLabel and debugName from Logical/PhysicalKeyboardKey #78263 (comment), which asks to add more information to release mode code.
  • The only "unicode" keys LogicalKeyboardKey represents are ASCII printable keys (just search for 0x000 in keyboard_key.dart). There isn't a œ logical key: despite that this chacacter can be input by a macOS keyboard with Option-Q, it's still an Option key followed by a KeyQ key. The debugCharacterLabel does not add any information besides what's already available in debugName.

@gspencergoog gspencergoog Mar 18, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of something like this:

 String? get debugName {
    String? result;
    assert(() {
	  result ??= _debugNames[keyId];
      if (result == null && keyId & ~valueMask == unicodePlane) {
        result = 'Key ${String.fromCharCode(keyId)}';
      }
      return true;
    }());
    return result;
  }

For the printable key with the key cap of "Ù" (letter U with grave) on the French keyboard, the function above will return 'Key Ù', but your current implementation would return null. It's more than just ASCII: any key that generates a Unicode character should get a Unicode plane key id. In this case the logical id for "Ù" is 0x000000D9. There is a dedicated key for this on the French keyboard, so that's exactly what it should report. Debugging will get a lot harder if all they get from debugName is null outside of the ASCII keys, and this adds more useful information to allow them to differentiate inputs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this might be even better, since it won't return null except in release mode.

 String? get debugName {
    String? result;
    assert(() {
	  result ??= _debugNames[keyId];
      if (result == null) {
        if (keyId & ~valueMask == unicodePlane) {
          result = 'Key ${String.fromCharCode(keyId)}';
        } else {
          result = 'Key with ID 0x${keyId.toRadixString(16).padLeft(digits, '0')}';
        }
      }
      return true;
    }());
    return result;
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice. I've added this one, as well as similar code to PhysicalKeyboardKey, and tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird: I see your push with the debugName changes in it (0deb173), but I don't see them here in the "Files changed" tab on GitHub. Maybe you somehow reverted them?

/// } else {
/// if (kReleaseMode) {
/// _message = 'Not a Q: Key label is "${event.logicalKey.keyLabel}"';
/// _message = 'Not a Q: Pressed 0x${event.logicalKey.keyId.toRadixString(16)}';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if it's part of the Unicode ID plane, it could at least include that Unicode character as a label.

@fluttergithubbot

Copy link
Copy Markdown
Contributor

This pull request is not suitable for automatic merging in its current state.

  • This pull request has changes requested by @gspencergoog. Please resolve those before re-applying the label.

/// _message = 'Not a Q: Key label is "${event.logicalKey.keyLabel}"';
/// _message = 'Not a Q: Pressed 0x${event.logicalKey.keyId.toRadixString(16)}';
/// } else {
/// // This will only print useful information in debug mode.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// // This will only print useful information in debug mode.
/// // debugName will only print useful information in debug mode.

return other is LogicalKeyboardKey
&& other.keyId == keyId;
}
String? get debugName => _debugNames[keyId];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird: I see your push with the debugName changes in it (0deb173), but I don't see them here in the "Files changed" tab on GitHub. Maybe you somehow reverted them?

@dkwingsmt dkwingsmt mentioned this pull request Mar 19, 2021
8 tasks done
@dkwingsmt

Copy link
Copy Markdown
Contributor Author

@gspencergoog should work now. Can you check again? :)

@gspencergoog gspencergoog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32384589-a60f0e74-c078-11e7-9bc1-e5b5287aea9d

@dkwingsmt
dkwingsmt merged commit 515ef91 into flutter:master Mar 24, 2021
@dkwingsmt
dkwingsmt deleted the remove-logical-keylabel branch March 24, 2021 12:01
dkwingsmt added a commit that referenced this pull request Mar 24, 2021
dkwingsmt added a commit that referenced this pull request Mar 24, 2021
dkwingsmt added a commit that referenced this pull request Mar 24, 2021
@dkwingsmt dkwingsmt added the a: text input Entering text in a text field or keyboard related problems label Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: tests "flutter test", flutter_test, or one of our tests a: text input Entering text in a text field or keyboard related problems c: contributor-productivity Team-specific productivity, code health, technical debt. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants