From 518b0cd9a5dfe883048d1e460ed4b6ce750b8879 Mon Sep 17 00:00:00 2001 From: James Hall Date: Sat, 6 Aug 2016 11:58:57 +0100 Subject: [PATCH] Open new window offset from last focused window (Credit: albinekb) (#581) * Open new window offset from last focused window * Make sure a newly opened window is seen to now have focus * Instead of setting focus time, simply emit the event first * Add comment explaining why we emit the focus event * Don't fire the event, as that can make it fire twice --- app/index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/index.js b/app/index.js index e4dda1f4..c42e2736 100644 --- a/app/index.js +++ b/app/index.js @@ -60,9 +60,9 @@ app.on('ready', () => { // Open the new window roughly the height of the header away from the // previous window. This also ensures in multi monitor setups that the // new terminal is on the correct screen. - if (BrowserWindow.getFocusedWindow() !== null) { - const currentWindow = BrowserWindow.getFocusedWindow(); - const points = currentWindow.getPosition(); + const focusedWindow = BrowserWindow.getFocusedWindow() || app.getLastFocusedWindow(); + if (focusedWindow) { + const points = focusedWindow.getPosition(); const currentScreen = screen.getDisplayNearestPoint({ x: points[0], y: points[1] }); const biggestX = ((points[0] + 100 + width) - currentScreen.bounds.x); @@ -280,9 +280,15 @@ app.on('ready', () => { // Keep track of focus time of every window, to figure out // which one of the existing window is the last focused. // Works nicely even if a window is closed and removed. - win.on('focus', () => { + const updateFocusTime = () => { win.focusTime = process.uptime(); + }; + win.on('focus', () => { + updateFocusTime(); }); + // Ensure focusTime is set on window open. The focus event doesn't + // fire from the dock (see bug #583) + updateFocusTime(); // the window can be closed by the browser process itself win.on('close', () => {