[UI] Ensure prompts are always centered relative to the parent window in multi-monitors setups.

This commit is contained in:
hackademix 2022-11-18 20:24:04 +01:00
parent 44eee13420
commit e62403815c
No known key found for this signature in database
GPG Key ID: 231A83AFDA9C2434
2 changed files with 37 additions and 19 deletions

View File

@ -37,12 +37,13 @@ var Prompts = (() => {
async open(data) {
promptData = data;
this.close();
let {width, height} = data.features;
let {width, height, left, top, parent = await browser.windows.getCurrent() } = data.features;
let options = {
url: browser.runtime.getURL("ui/prompt.html"),
type: "panel",
type: "popup",
width,
height,
focused: false, // initially in the background while sizing
};
if (UA.isMozilla) {
options.allowScriptsToClose = true;
@ -52,15 +53,31 @@ var Prompts = (() => {
this.currentTab = await browser.tabs.create({url: options.url});
return;
}
this.currentWindow = await browser.windows.create(options);
// work around for https://bugzilla.mozilla.org/show_bug.cgi?id=1330882
let {left, top, width: cw, height: ch} = this.currentWindow;
if (width && height && cw !== width || ch !== height) {
left += Math.round((cw - width) / 2);
top += Math.round((ch - height) / 2);
for (let attempts = 2; attempts-- > 0;) // top gets set only 2nd time, moz bug?
await browser.windows.update(this.currentWindow.id,
{left, top, width, height});
let popup = this.currentWindow = await browser.windows.create(options);
if (parent) {
// center to the given parent window (default last focused browser tab)
if (left === undefined) left = Math.round(parent.left + (parent.width - popup.width) / 2);
if (top === undefined) top = Math.round(parent.top + (parent.height - popup.height) / 2);
} else {
// features.parent explicitly nulled: use given left & top or default to auto-centering on main screen
if (left === undefined) ({left} = popup);
if (top === undefined) ({top} = popup);
}
// work around for letterboxing changes (https://bugzilla.mozilla.org/show_bug.cgi?id=1330882)
let {width: popupWidth, height: popupHeight} = popup;
if (width && height && (popupWidth !== width || popupHeight !== height)) {
left += Math.round((popupWidth - width) / 2);
top += Math.round((popupHeight - height) / 2);
}
for (let attempts = 2; attempts-- > 0;) // position gets set only 2nd time, moz bug?
await browser.windows.update(popup.id,
{left, top, width, height, focused: false});
if (parent) {
await browser.windows.update(parent.id, {focused: true});
}
}
async close() {

View File

@ -128,7 +128,7 @@
}
});
let resize = async e => {
let fitHeight = async e => {
if (!("windows" in browser)) {
// tabbed (mobile?) - ensure buttons are visible
document.querySelector("#buttons").scrollIntoView();
@ -136,15 +136,16 @@
}
let win = await browser.windows.getCurrent();
let delta = document.documentElement.offsetHeight - window.innerHeight;
let geometry = {
width: win.width, height: win.height + delta,
left: win.left, top: win.top - Math.round(delta / 2)
};
for (let j = 2; j-- > 0;) await browser.windows.update(win.id, geometry);
await browser.windows.update(win.id, {
height: win.height + delta,
top: win.top + Math.round(delta / 2),
focused: false
});
await browser.windows.update(win.id, {focused: true});
}
if (document.readyState === "complete") {
resize();
fitHeight();
} else {
window.addEventListener("load", resize);
window.addEventListener("load", fitHeight);
}
})();