Move window centering to utils

This commit is contained in:
hensm
2019-02-15 08:21:54 +00:00
parent 005de9cf91
commit 48c5a21e98
3 changed files with 21 additions and 37 deletions

View File

@@ -8,3 +8,14 @@ export function getNextEllipsis (ellipsis) {
else if (ellipsis === "...") "";
};
}
export function getWindowCenteredProps (refWin, width, height) {
const centerX = refWin.left + (refWin.width / 2);
const centerY = refWin.top + (refWin.height / 3);
return {
width, height
, left: Math.floor(centerX - width / 2)
, top: Math.floor(centerY - height / 2)
};
}

View File

@@ -4,6 +4,8 @@ import defaultOptions from "./options/defaultOptions";
import getBridgeInfo from "./lib/getBridgeInfo";
import messageRouter from "./lib/messageRouter";
import { getWindowCenteredProps } from "./lib/utils";
import semver from "semver";
@@ -335,26 +337,14 @@ let popupPort;
* to close and returns an API error.
*/
async function openPopup (shimId) {
const width = 350;
const height = 200;
// Current window to base centered position on
const win = await browser.windows.getCurrent();
// Top(mid)-center position
const centerX = win.left + (win.width / 2);
const centerY = win.top + (win.height / 3);
const left = Math.floor(centerX - (width / 2));
const top = Math.floor(centerY - (height / 2));
const centeredProps = getWindowCenteredProps(win, 350, 200);
const popup = await browser.windows.create({
url: "popup/index.html"
, type: "popup"
, width
, height
, left
, top
, ...centeredProps
});
// Store popup details for message forwarding
@@ -363,10 +353,7 @@ async function openPopup (shimId) {
// Size/position not set correctly on creation (bug?)
await browser.windows.update(popup.id, {
width
, height
, left
, top
...centeredProps
});
// Close popup on other browser window focus

View File

@@ -1,7 +1,8 @@
import React, { Component } from "react";
import semver from "semver";
import { getNextEllipsis } from "../lib/utils";
import { getNextEllipsis
, getWindowCenteredProps } from "../lib/utils";
const _ = browser.i18n.getMessage;
@@ -146,34 +147,19 @@ export default class Bridge extends Component {
}
async onUpdate () {
const width = 400;
const height = 150;
// Current window to base centered position on
const win = await browser.windows.getCurrent();
// Top(mid)-center position
const centerX = win.left + (win.width / 2);
const centerY = win.top + (win.height / 3);
const left = Math.floor(centerX - (width / 2));
const top = Math.floor(centerY - (height / 2));
const centeredProps = getWindowCenteredProps(win, 400, 150);
const updaterPopup = await browser.windows.create({
url: "../updater/index.html"
, type: "popup"
, width
, height
, left
, top
, ...centeredProps
});
// Size/position not set correctly on creation (bug?)
await browser.windows.update(updaterPopup.id, {
width
, height
, left
, top
...centeredProps
});
browser.runtime.onConnect.addListener(port => {