superlistapp / super_native_extensions

Native drag & drop, clipboard access and context menu.
MIT License
364 stars 56 forks source link

[super_context_menu] Open menu with a tap #347

Open nelsoncampos-cloudwalk opened 1 month ago

nelsoncampos-cloudwalk commented 1 month ago

GM @knopp, I'd like to see the possibility to add the option of open the context menu trough a tap too, like the iMessage does when you send a phone number.

if possible to indicate where I can change it in the code and open a PR to contribute with the package. I was reading but didn't understood if I need to change in flutter, or in rust.

Preview from iMessage https://github.com/superlistapp/super_native_extensions/assets/60667230/76523efb-e006-49d2-996d-5831129a237a

Overall thank for this package, amazing delivery for the flutter community.

AnAlpaca commented 1 month ago

HI there, I am no expert here and I am not involved in the project. But I did manage to get this to work by editting the following:

In the _ContextMenuDetector class, which is found here: Usr dir/.pub-cache/hosted/pub.dev/super_context_menu-0.8.5/lib/src/desktop.dart

bool _acceptPrimaryButton() {
    final keys = RawKeyboard.instance.keysPressed;
    return defaultTargetPlatform == TargetPlatform.macOS &&
        keys.length == 1 &&
        keys.contains(LogicalKeyboardKey.controlLeft);
  }

  bool _canAcceptEvent(PointerDownEvent event) {
    if (event.kind != PointerDeviceKind.mouse) {
      return false;
    }
    if (event.buttons == kSecondaryButton ||
        event.buttons == kPrimaryButton && _acceptPrimaryButton()) {
      return widget.contextMenuIsAllowed(event.position);
    }

    return false;
  }

From here you can edit till your hearts content, for me I just added another condition for the _acceptPrimaryButton method, where I checked if windows, and then created an _acceptSecondaryButton with the appropraite conditions to disable right click for that use case. Hope this helps. Obviously for any of this to work you would need to fork the repo or somehow get creative with extensions of the appropriate classes.

nelsoncampos-cloudwalk commented 1 month ago

Hey @AnAlpaca Thanks for your reply, that can be helpful for desktop features, I really appreciate.

I'm looking for a solution for mobile, more specific iOS implementation

AnAlpaca commented 1 month ago

Sorry you did mention that! I do see the place on mobile to make the change usr_dir/.pub-cache/hosted/pub.dev/super_context_menu-0.8.5/lib/src/mobile.dart

In there in the build method is a Listener widget, from what I can see this is where the action is registered. I am so used to to GestureDetector, so I am not as familiar with how to edit it. If you come right please let me know, as I would like this for similar reasons.

AnAlpaca commented 1 month ago

Also just wanted to say that I really appreciate this package! Its amazing, and the design is fantastic!

knopp commented 1 month ago

I don't think this is possible. On iOS the event is triggered from a system gesture recognizer. From the top of my head I'm not aware of any public API on iOS that can programatically show the context menu.

nelsoncampos-cloudwalk commented 1 month ago

Yes! I tried to do an example app in native code to see if it was possible and I didn't find any solution to it. just a menu button that has some differences from the context menu.

I created a plugin to show a UIButton with a menu on top of FlutterViewController as a subview, and did all the component lifecycle, but it being very tricky, to render and update the native element to be anchored in the flutter widget.

How would be your approach to implement the Menu Button @knopp? I saw an a open issue about that.

https://github.com/superlistapp/super_native_extensions/issues/189