Skip to content

system-context-menu prevents the default behavior from being invalid #26726

Closed

Description

@Linkontoask

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 11.0.3
  • Operating System:
    • Windows 10 (1803)
  • Last Known Working Electron version:

Expected Behavior

Prevent the default menu context from being displayed

Actual Behavior

image

To Reproduce

  • Set frame to true
  • Set -webkit-app-region: drag
  • Finally write the following code
win.on('system-context-menu', e => {
    e.preventDefault()
})

Screenshots

image

Additional Information

Activity

  1. MarshallOfSound commented on Nov 29, 2020

    @MarshallOfSound
    Member

    To Reproduce

    Can you provide complete repro steps, the current steps won't open that menu so it's unclear how you're opening it.

  2. added
    blocked/need-repro
    on Nov 29, 2020
  3. added
    has-repro-gist
    and removed
    blocked/need-repro
    on Nov 29, 2020
  4. ckerr commented on Nov 29, 2020

    @ckerr
    Member

    The Electron code that delegates to NativeWindowObserver looks fine, but for some reason NativeWindowViews::PreHandleMSG isn't getting any WM_CONTEXTMENU messages, so that event handler + delegation code is never being reached

  5. added
    status/confirmed
    on Nov 29, 2020
  6. ckerr commented on Nov 29, 2020

    @ckerr
    Member

    frame: false is a prerequisite for triggering this bug.

    Electron never sees the WM_CONTEXTMENU message because hwnd_message_handler.cc uses its own custom event handling for WM_RBUTTONUP here. The invocation of SendMessage() in that paragraph returns HTCAPTION, so ShowSystemMenuAtScreenPixelLocation() is called.

    That block of code is reached in the first place because is_right_mouse_pressed_on_caption_ is set to true here.

  7. fabiospampinato commented on Dec 7, 2020

    @fabiospampinato
    Contributor

    The "system-context-menu" event is never triggered for me under Electron 11.0.3 and Windows 10, I'm using frame: false.

  8. ericblade commented on Mar 3, 2021

    @ericblade

    frame false system-context-menu is not triggered in 12.0.0, either, when right clicking on the draggable region, opposed to the documentation on the pull request for the feature.

    I cannot speak for any other configurations at this time.

  9. 15 remaining items

  10. fabiospampinato commented on Jan 10, 2023

    @fabiospampinato
    Contributor

    Still interested in this

  11. ferreira-tb commented on Mar 17, 2023

    @ferreira-tb
    Contributor

    Still persists in 23.1.4 when frame: false.

  12. stalker8610 commented on Apr 19, 2023

    @stalker8610

    Still interested in this

  13. endlesszorn commented on Jun 28, 2023

    @endlesszorn

    any fix ?

    and i find a sideway , it works for me (just works and i didn't know why )

    const WM_INITMENU = 0x0116;
    win.hookWindowMessage(WM_INITMENU, () => {
        win.setEnabled(false);
        win.setEnabled(true);
    });
    

    refers: https://github.com/electron/electron/issues/24893#issuecomment-1109262719

    This setting masks the system menu, but the custom right-click menu is also masked. Is there a way to block only Windows system menu operations because I want to customize the right-click menu

  14. whzard commented on Jun 28, 2023

    @whzard

    @yangsen9608, you're supposed to add the custom menu popup code inside the event handler, after the win.setEnabled(true); line. This works for me on the latest Electron version.

  15. endlesszorn commented on Jun 28, 2023

    @endlesszorn

    Thank you for your help, I have completed the effect I want, thank you!

  16. aronCiucu commented on Sep 8, 2023

    @aronCiucu

    Bump

  17. pagoru commented on Sep 16, 2023

    @pagoru

    Fix this

  18. thisisrandomguys commented on Nov 13, 2023

    @thisisrandomguys

    Bump

  19. boiboif commented on Dec 27, 2023

    @boiboif

    Bump

  20. eXhumer commented on Apr 16, 2024

    @eXhumer

    frame: false is a prerequisite for triggering this bug.

    Electron never sees the WM_CONTEXTMENU message because hwnd_message_handler.cc uses its own custom event handling for WM_RBUTTONUP here. The invocation of SendMessage() in that paragraph returns HTCAPTION, so ShowSystemMenuAtScreenPixelLocation() is called.

    That block of code is reached in the first place because is_right_mouse_pressed_on_caption_ is set to true here.

    Can't the NativeWindowViews::PreHandleMSG be used to intercept this behaviour for WM_RBUTTONUP message and check if the current view is frameless and considering that as a system context menu message? This comment seems to indicate this method is called before HWNDMessageHandler to handle messages when possible.

    // Catch-all message handling and filtering. Called before
    // HWNDMessageHandler's built-in handling, which may pre-empt some
    // expectations in Views/Aura if messages are consumed. Returns true if the
    // message was consumed by the delegate and should not be processed further
    // by the HWNDMessageHandler. In this case, |result| is returned. |result| is
    // not modified otherwise.

    I don't have access to any Windows machine to test this on, but I would appreciate if someone who can build Electron on Windows can add this switch case inside NativeWindowViews::PreHandleMSG and try checking if you can console.log inside the system-context-menu event.

        case WM_RBUTTONUP: {
          if (!has_frame()) {
            bool prevent_default = false;
            NotifyWindowSystemContextMenu(GET_X_LPARAM(l_param),
                                          GET_Y_LPARAM(l_param), &prevent_default);
            return prevent_default;
          }
          return false;
        }
  21. xXTurnerLP commented on Jul 6, 2024

    @xXTurnerLP

    v31.1.0
    i have the issue as well

  22. self-assigned this
    on Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

11-x-ybug 🪲component/menuhas-repro-gistplatform/windowsstatus/confirmed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions