From 48c5a21e98e7dbf1cbd180ba599186c46e0ce1c5 Mon Sep 17 00:00:00 2001 From: hensm Date: Fri, 15 Feb 2019 08:21:54 +0000 Subject: [PATCH] Move window centering to utils --- ext/src/lib/utils.js | 11 +++++++++++ ext/src/main.js | 23 +++++------------------ ext/src/options/Bridge.jsx | 24 +++++------------------- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/ext/src/lib/utils.js b/ext/src/lib/utils.js index c5315ef..0a814ce 100644 --- a/ext/src/lib/utils.js +++ b/ext/src/lib/utils.js @@ -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) + }; +} diff --git a/ext/src/main.js b/ext/src/main.js index 7901e29..75a8408 100755 --- a/ext/src/main.js +++ b/ext/src/main.js @@ -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 diff --git a/ext/src/options/Bridge.jsx b/ext/src/options/Bridge.jsx index 51f7ee5..309a949 100644 --- a/ext/src/options/Bridge.jsx +++ b/ext/src/options/Bridge.jsx @@ -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 => {