0001-Bug-1360278-Add-preference-to-trigger-context-menu-o.patch 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. From 05ec1aa0d5e8806dd0c5c6d08c82846a1389b599 Mon Sep 17 00:00:00 2001
  2. Message-Id: <05ec1aa0d5e8806dd0c5c6d08c82846a1389b599.1512038840.git.jan.steffens@gmail.com>
  3. From: Robin Grenet <robin.grenet@wanadoo.fr>
  4. Date: Thu, 16 Nov 2017 13:35:58 +0100
  5. Subject: [PATCH 1/2] Bug 1360278 - Add preference to trigger context menu on
  6. mouse up for GTK+ and macOS, r=mstange,smaug
  7. MozReview-Commit-ID: Bg60bD8jIg6
  8. --HG--
  9. extra : rebase_source : cc8bd5796096f49ad4fdab81885a426afd6117e4
  10. ---
  11. modules/libpref/init/all.js | 4 ++++
  12. widget/cocoa/nsChildView.mm | 23 +++++++++++++++++++++--
  13. widget/gtk/nsWindow.cpp | 27 ++++++++++++++++++++-------
  14. widget/gtk/nsWindow.h | 2 ++
  15. widget/nsBaseWidget.cpp | 16 ++++++++++++++++
  16. widget/nsBaseWidget.h | 6 ++++++
  17. 6 files changed, 69 insertions(+), 9 deletions(-)
  18. diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js
  19. index 9febead1d363d792..7a6e6a20f3cc3fd6 100644
  20. --- a/modules/libpref/init/all.js
  21. +++ b/modules/libpref/init/all.js
  22. @@ -231,6 +231,10 @@ pref("browser.sessionhistory.max_total_viewers", -1);
  23. pref("ui.use_native_colors", true);
  24. pref("ui.click_hold_context_menus", false);
  25. +
  26. +// Pop up context menu on mouseup instead of mousedown, if that's the OS default.
  27. +// Note: ignored on Windows (context menus always use mouseup)
  28. +pref("ui.context_menus.after_mouseup", false);
  29. // Duration of timeout of incremental search in menus (ms). 0 means infinite.
  30. pref("ui.menu.incremental_search.timeout", 1000);
  31. // If true, all popups won't hide automatically on blur
  32. diff --git a/widget/cocoa/nsChildView.mm b/widget/cocoa/nsChildView.mm
  33. index 25b4c1ba7a2d1207..2affd1ef386cbfd0 100644
  34. --- a/widget/cocoa/nsChildView.mm
  35. +++ b/widget/cocoa/nsChildView.mm
  36. @@ -4719,30 +4719,49 @@ NSEvent* gLastDragMouseDownEvent = nil;
  37. if (!mGeckoChild)
  38. return;
  39. - // Let the superclass do the context menu stuff.
  40. - [super rightMouseDown:theEvent];
  41. + if (!nsBaseWidget::ShowContextMenuAfterMouseUp()) {
  42. + // Let the superclass do the context menu stuff.
  43. + [super rightMouseDown:theEvent];
  44. + }
  45. NS_OBJC_END_TRY_ABORT_BLOCK;
  46. }
  47. - (void)rightMouseUp:(NSEvent *)theEvent
  48. {
  49. NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
  50. if (!mGeckoChild)
  51. return;
  52. if (mTextInputHandler->OnHandleEvent(theEvent)) {
  53. return;
  54. }
  55. WidgetMouseEvent geckoEvent(true, eMouseUp, mGeckoChild,
  56. WidgetMouseEvent::eReal);
  57. [self convertCocoaMouseEvent:theEvent toGeckoEvent:&geckoEvent];
  58. geckoEvent.button = WidgetMouseEvent::eRightButton;
  59. geckoEvent.mClickCount = [theEvent clickCount];
  60. nsAutoRetainCocoaObject kungFuDeathGrip(self);
  61. mGeckoChild->DispatchInputEvent(&geckoEvent);
  62. + if (!mGeckoChild)
  63. + return;
  64. +
  65. + if (nsBaseWidget::ShowContextMenuAfterMouseUp()) {
  66. + // Let the superclass do the context menu stuff, but pretend it's rightMouseDown.
  67. + NSEvent *dupeEvent = [NSEvent mouseEventWithType:NSRightMouseDown
  68. + location:theEvent.locationInWindow
  69. + modifierFlags:theEvent.modifierFlags
  70. + timestamp:theEvent.timestamp
  71. + windowNumber:theEvent.windowNumber
  72. + context:theEvent.context
  73. + eventNumber:theEvent.eventNumber
  74. + clickCount:theEvent.clickCount
  75. + pressure:theEvent.pressure];
  76. +
  77. + [super rightMouseDown:dupeEvent];
  78. + }
  79. NS_OBJC_END_TRY_ABORT_BLOCK;
  80. }
  81. diff --git a/widget/gtk/nsWindow.cpp b/widget/gtk/nsWindow.cpp
  82. index 37b6aae4c3d0b4e7..2b80124538c20ed6 100644
  83. --- a/widget/gtk/nsWindow.cpp
  84. +++ b/widget/gtk/nsWindow.cpp
  85. @@ -2727,6 +2727,19 @@ static guint ButtonMaskFromGDKButton(guint button)
  86. return GDK_BUTTON1_MASK << (button - 1);
  87. }
  88. +void
  89. +nsWindow::DispatchContextMenuEventFromMouseEvent(uint16_t domButton,
  90. + GdkEventButton *aEvent)
  91. +{
  92. + if (domButton == WidgetMouseEvent::eRightButton && MOZ_LIKELY(!mIsDestroyed)) {
  93. + WidgetMouseEvent contextMenuEvent(true, eContextMenu, this,
  94. + WidgetMouseEvent::eReal);
  95. + InitButtonEvent(contextMenuEvent, aEvent);
  96. + contextMenuEvent.pressure = mLastMotionPressure;
  97. + DispatchInputEvent(&contextMenuEvent);
  98. + }
  99. +}
  100. +
  101. void
  102. nsWindow::OnButtonPressEvent(GdkEventButton *aEvent)
  103. {
  104. @@ -2796,13 +2809,8 @@ nsWindow::OnButtonPressEvent(GdkEventButton *aEvent)
  105. DispatchInputEvent(&event);
  106. // right menu click on linux should also pop up a context menu
  107. - if (domButton == WidgetMouseEvent::eRightButton &&
  108. - MOZ_LIKELY(!mIsDestroyed)) {
  109. - WidgetMouseEvent contextMenuEvent(true, eContextMenu, this,
  110. - WidgetMouseEvent::eReal);
  111. - InitButtonEvent(contextMenuEvent, aEvent);
  112. - contextMenuEvent.pressure = mLastMotionPressure;
  113. - DispatchInputEvent(&contextMenuEvent);
  114. + if (!nsBaseWidget::ShowContextMenuAfterMouseUp()) {
  115. + DispatchContextMenuEventFromMouseEvent(domButton, aEvent);
  116. }
  117. }
  118. @@ -2838,6 +2846,11 @@ nsWindow::OnButtonReleaseEvent(GdkEventButton *aEvent)
  119. DispatchInputEvent(&event);
  120. mLastMotionPressure = pressure;
  121. +
  122. + // right menu click on linux should also pop up a context menu
  123. + if (nsBaseWidget::ShowContextMenuAfterMouseUp()) {
  124. + DispatchContextMenuEventFromMouseEvent(domButton, aEvent);
  125. + }
  126. }
  127. void
  128. diff --git a/widget/gtk/nsWindow.h b/widget/gtk/nsWindow.h
  129. index f7c07d57491b0b83..b969c9db4306ba6a 100644
  130. --- a/widget/gtk/nsWindow.h
  131. +++ b/widget/gtk/nsWindow.h
  132. @@ -245,6 +245,8 @@ private:
  133. void UpdateClientOffset();
  134. + void DispatchContextMenuEventFromMouseEvent(uint16_t domButton,
  135. + GdkEventButton *aEvent);
  136. public:
  137. void ThemeChanged(void);
  138. void OnDPIChanged(void);
  139. diff --git a/widget/nsBaseWidget.cpp b/widget/nsBaseWidget.cpp
  140. index 996409f45db11cc7..de73fe36d27955cd 100644
  141. --- a/widget/nsBaseWidget.cpp
  142. +++ b/widget/nsBaseWidget.cpp
  143. @@ -1222,6 +1222,22 @@ nsBaseWidget::DispatchEventToAPZOnly(mozilla::WidgetInputEvent* aEvent)
  144. }
  145. }
  146. +// static
  147. +bool
  148. +nsBaseWidget::ShowContextMenuAfterMouseUp()
  149. +{
  150. + static bool gContextMenuAfterMouseUp = false;
  151. + static bool gContextMenuAfterMouseUpCached = false;
  152. + if (!gContextMenuAfterMouseUpCached) {
  153. + Preferences::AddBoolVarCache(&gContextMenuAfterMouseUp,
  154. + "ui.context_menus.after_mouseup",
  155. + false);
  156. +
  157. + gContextMenuAfterMouseUpCached = true;
  158. + }
  159. + return gContextMenuAfterMouseUp;
  160. +}
  161. +
  162. nsIDocument*
  163. nsBaseWidget::GetDocument() const
  164. {
  165. diff --git a/widget/nsBaseWidget.h b/widget/nsBaseWidget.h
  166. index 6d6b93ea73d64b38..cdc6aa0c87279832 100644
  167. --- a/widget/nsBaseWidget.h
  168. +++ b/widget/nsBaseWidget.h
  169. @@ -418,6 +418,12 @@ public:
  170. void RecvScreenPixels(mozilla::ipc::Shmem&& aMem, const ScreenIntSize& aSize) override {};
  171. #endif
  172. + /**
  173. + * Whether context menus should only appear on mouseup instead of mousedown,
  174. + * on OSes where they normally appear on mousedown (macOS, *nix).
  175. + */
  176. + static bool ShowContextMenuAfterMouseUp();
  177. +
  178. protected:
  179. // These are methods for CompositorWidgetWrapper, and should only be
  180. // accessed from that class. Derived widgets can choose which methods to
  181. --
  182. 2.15.1