summaryrefslogtreecommitdiff
path: root/mozilla-1423598-popup.patch
blob: fc07bd8728ef1364e34d9ed825b3548106a31254 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
diff --git a/widget/gtk/nsWindow.h b/widget/gtk/nsWindow.h
--- a/widget/gtk/nsWindow.h
+++ b/widget/gtk/nsWindow.h
@@ -457,6 +457,10 @@
   nsWindow* GetTransientForWindowIfPopup();
   bool IsHandlingTouchSequence(GdkEventSequence* aSequence);
 
+  void NativeMoveResizeWaylandPopup(GdkPoint* aPosition, GdkRectangle* aSize);
+
+  GtkTextDirection GetTextDirection();
+
 #ifdef MOZ_X11
   typedef enum {GTK_WIDGET_COMPOSIDED_DEFAULT = 0,
                 GTK_WIDGET_COMPOSIDED_DISABLED = 1,
diff --git a/widget/gtk/nsWindow.cpp b/widget/gtk/nsWindow.cpp
--- a/widget/gtk/nsWindow.cpp
+++ b/widget/gtk/nsWindow.cpp
@@ -1109,13 +1109,89 @@
   NotifyRollupGeometryChange();
 }
 
+#ifdef DEBUG
+static void NativeMoveResizeWaylandPopupCallback(GdkWindow *window,
+  const GdkRectangle *flipped_rect, const GdkRectangle *final_rect,
+  gboolean flipped_x, gboolean flipped_y, void *unused)
+{
+  LOG(("NativeMoveResizeWaylandPopupCallback flipped %d %d\n",
+    flipped_rect->x, flipped_rect->y));
+  LOG(("NativeMoveResizeWaylandPopupCallback final %d %d\n",
+    final_rect->x, final_rect->y));
+}
+#endif
+
+void nsWindow::NativeMoveResizeWaylandPopup(GdkPoint* aPosition, GdkRectangle* aSize) {
+  // Available as of GTK 3.24+
+  static auto sGdkWindowMoveToRect =
+      (void (*)(GdkWindow *, const GdkRectangle *, GdkGravity, GdkGravity,
+                GdkAnchorHints, gint, gint))
+                dlsym(RTLD_DEFAULT, "gdk_window_move_to_rect");
+
+  if (!sGdkWindowMoveToRect) {
+    gtk_window_move(GTK_WINDOW(mShell), aPosition->x, aPosition->y);
+    if (aSize) {
+      gtk_window_resize(GTK_WINDOW(mShell), aSize->width, aSize->height);
+    }
+    return;
+  }
+
+  GdkWindow *gdkWindow = gtk_widget_get_window(GTK_WIDGET(mShell));
+  if (!gdkWindow) {
+    return;
+  }
+
+  GtkWidget* parentWidget =
+    GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(mShell)));
+
+  int x_parent, y_parent;
+  gdk_window_get_origin(gtk_widget_get_window(parentWidget), &x_parent, &y_parent);
+
+  GdkRectangle rect = { aPosition->x - x_parent,
+                        aPosition->y - y_parent,
+                        1, 1};
+  if (aSize) {
+    rect.width = aSize->width;
+    rect.height = aSize->height;
+  }
+
+#ifdef DEBUG
+  LOG(("NativeMoveResizeWaylandPopup request position %d,%d\n",
+    aPosition->x, aPosition->y));
+  if (aSize) {
+    LOG(("NativeMoveResizeWaylandPopup request size %d,%d\n",
+      aSize->width, aSize->height));
+  }
+  LOG(("NativeMoveResizeWaylandPopup result %d %d\n", rect.x, rect.y));
+  g_signal_connect(gdkWindow, "moved-to-rect",
+    G_CALLBACK(NativeMoveResizeWaylandPopupCallback), this);
+#endif
+
+  GdkGravity rectAnchor = GDK_GRAVITY_NORTH_WEST;
+  GdkGravity menuAnchor = GDK_GRAVITY_NORTH_WEST;
+  if (GetTextDirection() == GTK_TEXT_DIR_RTL) {
+    rectAnchor = GDK_GRAVITY_NORTH_EAST;
+    menuAnchor = GDK_GRAVITY_NORTH_EAST;
+  }
+
+  GdkAnchorHints hints = GdkAnchorHints(GDK_ANCHOR_SLIDE | GDK_ANCHOR_FLIP);
+  if (aSize) {
+    hints = GdkAnchorHints(hints|GDK_ANCHOR_RESIZE);
+  }
+
+  sGdkWindowMoveToRect(gdkWindow, &rect, rectAnchor, menuAnchor, hints, 0, 0);
+}
+
 void nsWindow::NativeMove() {
   GdkPoint point = DevicePixelsToGdkPointRoundDown(mBounds.TopLeft());
-
-  if (mIsTopLevel) {
-    gtk_window_move(GTK_WINDOW(mShell), point.x, point.y);
-  } else if (mGdkWindow) {
-    gdk_window_move(mGdkWindow, point.x, point.y);
+  if (!mIsX11Display && mIsTopLevel && mWindowType == eWindowType_popup) {
+    NativeMoveResizeWaylandPopup(&point, nullptr);
+  } else {
+    if (mIsTopLevel) {
+      gtk_window_move(GTK_WINDOW(mShell), point.x, point.y);
+    } else if (mGdkWindow) {
+      gdk_window_move(mGdkWindow, point.x, point.y);
+    }
   }
 }
 
@@ -3397,11 +3473,6 @@
                                  GDK_WINDOW_TYPE_HINT_DIALOG);
         gtk_window_set_transient_for(GTK_WINDOW(mShell), topLevelParent);
       } else if (mWindowType == eWindowType_popup) {
-        // With popup windows, we want to control their position, so don't
-        // wait for the window manager to place them (which wouldn't
-        // happen with override-redirect windows anyway).
-        NativeMove();
-
         gtk_window_set_wmclass(GTK_WINDOW(mShell), "Popup",
                                gdk_get_program_class());
 
@@ -3456,6 +3527,14 @@
         if (topLevelParent) {
           gtk_window_set_transient_for(GTK_WINDOW(mShell), topLevelParent);
         }
+
+        // We need realized mShell at NativeMove().
+        gtk_widget_realize(mShell);
+
+        // With popup windows, we want to control their position, so don't
+        // wait for the window manager to place them (which wouldn't
+        // happen with override-redirect windows anyway).
+        NativeMove();
       } else {  // must be eWindowType_toplevel
         SetDefaultIcon();
         gtk_window_set_wmclass(GTK_WINDOW(mShell), "Toplevel",
@@ -3895,23 +3974,27 @@
   LOG(("nsWindow::NativeMoveResize [%p] %d %d %d %d\n", (void *)this, topLeft.x,
        topLeft.y, size.width, size.height));
 
-  if (mIsTopLevel) {
-    // x and y give the position of the window manager frame top-left.
-    gtk_window_move(GTK_WINDOW(mShell), topLeft.x, topLeft.y);
-    // This sets the client window size.
-    MOZ_ASSERT(size.width > 0 && size.height > 0,
-               "Can't resize window smaller than 1x1.");
-    gtk_window_resize(GTK_WINDOW(mShell), size.width, size.height);
-  } else if (mContainer) {
-    GtkAllocation allocation;
-    allocation.x = topLeft.x;
-    allocation.y = topLeft.y;
-    allocation.width = size.width;
-    allocation.height = size.height;
-    gtk_widget_size_allocate(GTK_WIDGET(mContainer), &allocation);
-  } else if (mGdkWindow) {
-    gdk_window_move_resize(mGdkWindow, topLeft.x, topLeft.y, size.width,
-                           size.height);
+  if (!mIsX11Display && mIsTopLevel && mWindowType == eWindowType_popup) {
+    NativeMoveResizeWaylandPopup(&topLeft, &size);
+  } else {
+    if (mIsTopLevel) {
+      // x and y give the position of the window manager frame top-left.
+      gtk_window_move(GTK_WINDOW(mShell), topLeft.x, topLeft.y);
+      // This sets the client window size.
+      MOZ_ASSERT(size.width > 0 && size.height > 0,
+                 "Can't resize window smaller than 1x1.");
+      gtk_window_resize(GTK_WINDOW(mShell), size.width, size.height);
+    } else if (mContainer) {
+      GtkAllocation allocation;
+      allocation.x = topLeft.x;
+      allocation.y = topLeft.y;
+      allocation.width = size.width;
+      allocation.height = size.height;
+      gtk_widget_size_allocate(GTK_WIDGET(mContainer), &allocation);
+    } else if (mGdkWindow) {
+      gdk_window_move_resize(mGdkWindow, topLeft.x, topLeft.y, size.width,
+                             size.height);
+    }
   }
 
 #ifdef MOZ_X11
@@ -6791,3 +6874,18 @@
                                     nsChangeHint_RepaintFrame);
   }
 }
+
+GtkTextDirection nsWindow::GetTextDirection() {
+  nsView *view = nsView::GetViewFor(this);
+  if (!view) {
+    return GTK_TEXT_DIR_LTR;
+  }
+  nsIFrame *frame = view->GetFrame();
+  if (!frame) {
+    return GTK_TEXT_DIR_LTR;
+  }
+
+  WritingMode wm = frame->GetWritingMode();
+  bool isFrameRTL = !(wm.IsVertical() ? wm.IsVerticalLR() : wm.IsBidiLTR());
+  return isFrameRTL ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
+}

bgstack15