Skip to content

Commit 21c818c

Browse files
committed
Improved prompts factory.
1 parent e3feaee commit 21c818c

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

src/ui/Prompts.js

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,94 @@ var Prompts = (() => {
3737
async open(data) {
3838
promptData = data;
3939
this.close();
40+
41+
let url = browser.runtime.getURL("ui/prompt.html");
4042
let {width, height, left, top, parent } = data.features;
4143
let options = {
42-
url: browser.runtime.getURL("ui/prompt.html"),
44+
url,
4345
type: "popup",
44-
width,
45-
height,
46-
};
46+
}
47+
48+
if (!parent) {
49+
parent = await browser.windows.getCurrent();
50+
}
51+
4752
if (UA.isMozilla) {
4853
options.allowScriptsToClose = true;
4954
}
55+
5056
if (!("windows" in browser)) {
5157
// Android, most likely
52-
this.currentTab = await browser.tabs.create({url: options.url});
58+
this.currentTab = await browser.tabs.create({url});
5359
return;
5460
}
55-
if (!parent) parent = await browser.windows.getCurrent()
56-
let popup = this.currentWindow = await browser.windows.create(options);
61+
62+
const centerOnParent = (dim) => {
63+
const { width, height } = dim;
64+
dim.left =
65+
left === undefined
66+
? Math.round(parent.left + (parent.width - width) / 2)
67+
: left;
68+
dim.top =
69+
top === undefined
70+
? Math.round(parent.top + (parent.height - height) / 2)
71+
: top;
72+
return dim;
73+
};
74+
75+
if (width && height) {
76+
let size = { width, height };
77+
url += `?size=${JSON.stringify(size)}`;
78+
if (parent) {
79+
({ left, top } = Object.assign(options, centerOnParent(size)));
80+
}
81+
}
82+
debug("Prompt pre-opening options", options, left, top, width, height); // DEV_ONLY
83+
let popup = (this.currentWindow = await browser.windows.create(options));
5784

5885
if (parent) {
59-
// center to the given parent window (default last focused browser tab)
60-
if (left === undefined) left = Math.round(parent.left + (parent.width - popup.width) / 2);
61-
if (top === undefined) top = Math.round(parent.top + (parent.height - popup.height) / 2);
86+
({ left, top } = centerOnParent({
87+
width: width || popup.width,
88+
height: height || popup.height,
89+
}));
6290
} else {
63-
// features.parent explicitly nulled: use given left & top or default to auto-centering on main screen
64-
if (left === undefined) ({left} = popup);
65-
if (top === undefined) ({top} = popup);
91+
// use given left & top or default to auto-centering on main screen
92+
if (left === undefined) ({ left } = popup);
93+
if (top === undefined) ({ top } = popup);
6694
}
6795

68-
// work around for letterboxing changes (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882)
69-
let {width: popupWidth, height: popupHeight} = popup;
70-
if (width && height && (popupWidth !== width || popupHeight !== height)) {
71-
left += Math.round((popupWidth - width) / 2);
72-
top += Math.round((popupHeight - height) / 2);
73-
await browser.windows.update(popup.id,
74-
{left, top, width, height, focused: false});
75-
}
96+
debug("Prompt post-opening options", popup, options, left, top, width, height);
7697

77-
for (let attempts = 2; attempts-- > 0;) {
78-
// position gets set only 2nd time, moz bug?
79-
await browser.windows.update(popup.id,
80-
{left, top, focused: false});
81-
}
82-
if (parent) {
83-
await browser.windows.update(parent.id, {focused: true});
98+
// work around for resistFingerprinting new window rounding (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882)
99+
if (
100+
width &&
101+
height &&
102+
(popup.width !== width ||
103+
popup.height !== height ||
104+
popup.left !== left ||
105+
popup.top !== top)
106+
) {
107+
popup = await browser.windows.update(popup.id, {
108+
left,
109+
top,
110+
width,
111+
height,
112+
});
113+
for (let attempts = 2; attempts-- > 0; ) {
114+
debug("Resizing", popup, { left, top, width, height }); // DEV_ONY
115+
popup = await browser.windows.update(popup.id, { width, height });
116+
if (popup.width == width || popup.height == height) {
117+
break;
118+
}
119+
}
84120
}
85121
}
122+
86123
async close() {
87124
if (this.currentWindow) {
88125
try {
89126
await browser.windows.remove(this.currentWindow.id);
90127
} catch (e) {
91-
debug(e);
92128
}
93129
this.currentWindow = null;
94130
} else if (this.currentTab) {

src/ui/prompt.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
*/
2020

2121
(async () => {
22-
document.documentElement.classList.toggle("mobile", !!UA.mobile);
2322
let data = await Messages.send("getPromptData");
24-
debug(data);
23+
debug("Prompt data", data);
2524
if (!data) {
2625
error("Missing promptData");
2726
window.close();

src/ui/resize_hack.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ if ("windows" in browser) document.addEventListener("DOMContentLoaded", async e
2424
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1402110
2525
let win = await browser.windows.getCurrent({populate: true});
2626
if (win.tabs[0].url === document.URL) {
27-
debug("Resize hack");
27+
let size = decodeURIComponent(location.search).match(/\bsize=(\{\s*"width":[^}]+\})/);
28+
try {
29+
size = size && JSON.parse(size[1]);
30+
} catch (e) {
31+
size = null;
32+
}
33+
let {width, height} = size || win;
34+
debug("Resize hack", win, size, width, height); // DEV_ONLY
2835
await browser.windows.update(win.id, {
29-
width: win.width + 1
36+
width: width + 1, height
3037
});
3138
await browser.windows.update(win.id, {
32-
width: win.width
39+
width, height
3340
});
3441
}
3542
});

0 commit comments

Comments
 (0)