Skip to content

Commit

Permalink
Don't cache screens, but save positions by id.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaine Schmeisser committed Feb 28, 2017
1 parent 82b3470 commit 0db9cd0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 64 deletions.
8 changes: 4 additions & 4 deletions app/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ app.on('ready', function () {
},
})

let screens = new Screens({
const screens = Screens.getInstance({
windowWidth: mainWindow.getSize()[0],
})

Expand All @@ -123,18 +123,18 @@ app.on('ready', function () {
})

mainWindow.on('move', () => {
let currentWindowPosition = mainWindow.getPosition()
const currentWindowPosition = mainWindow.getPosition()
screens.saveWindowPositionOnCurrentScreen(currentWindowPosition[0], currentWindowPosition[1])
})

mainWindow.on('moved', () => {
let currentWindowPosition = mainWindow.getPosition()
const currentWindowPosition = mainWindow.getPosition()
screens.saveWindowPositionOnCurrentScreen(currentWindowPosition[0], currentWindowPosition[1])
})

globalEmitter.on('showWindow', () => {
logger.log('info', 'showing window from manual trigger')
let position = screens.getCenterPositionOnCurrentScreen()
const position = screens.getCenterPositionOnCurrentScreen()
if (position) {
mainWindow.setPosition(position.x, position.y)
}
Expand Down
2 changes: 1 addition & 1 deletion app/blocks/output/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Preview extends Block {
constructor (data) {
super(data)
this.message = data.message || '{value}'
this.screens = new Screens({
this.screens = Screens.getInstance({
windowWidth: 700,
})
}
Expand Down
122 changes: 63 additions & 59 deletions app/lib/screens.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,86 @@
const electron = require('electron')

const configuration = require('../lib/configuration')

class Screens {
constructor (options) {
this.screenModule = electron.screen
this.displayOn = configuration.displayOn
if (this.displayOn === 'detect') {
this.resetScreens(options.windowWidth)
static getInstance (options) {
if (configuration.displayOn !== 'detect') {
return new PrimaryScreens(options)
}
return new DetectScreens(options)
}
}

class PrimaryScreens {
saveWindowPositionOnCurrentScreen () {}
getCenterPositionOnCurrentScreen () {}
}

class DetectScreens {
constructor ({ windowWidth }) {
this.windowWidth = windowWidth
this.positions = {}
this.screenModule = electron.screen
this.monitorDisplays()
}

monitorDisplays () {
this.screenModule.on('display-added', () => {
this.resetScreens(options.windowWidth)
this.positions = {}
})
this.screenModule.on('display-removed', () => {
this.resetScreens(options.windowWidth)
this.positions = {}
})
this.screenModule.on('display-metrics-changed', () => {
this.resetScreens(options.windowWidth)
this.positions = {}
})
}

saveWindowPositionOnCurrentScreen (currentWindowPositionX, currentWindowPositionY, flag) {
if (this.displayOn === 'detect') {
let currentDisplay = this.getCurrentScreen()
if (currentDisplay.id === this.screenModule.getDisplayNearestPoint({x: currentWindowPositionX, y: currentWindowPositionY}).id) {
// save previous position
if (currentDisplay.customPosition) {
currentDisplay.lastPosition = currentDisplay.customPosition
}
currentDisplay.customPosition = {
x: currentWindowPositionX,
y: currentWindowPositionY,
time: Date.now(),
}
} else {
let timeDiff = (Date.now() - currentDisplay.customPosition.time)
// if second move event fired in quick succession
if (timeDiff < 50) {
// replace current position with last to fix linux duplicate 'move' event bug
currentDisplay.customPosition = currentDisplay.lastPosition
}
}
}
getCurrentScreen () {
const point = this.screenModule.getCursorScreenPoint()
return this.screenModule.getDisplayNearestPoint(point)
}

getCenterPositionOnCurrentScreen () {
if (this.displayOn === 'detect') {
let currentScreen = this.getCurrentScreen()
let centerPosition = {
x: currentScreen.customPosition.x,
y: currentScreen.customPosition.y,
saveWindowPositionOnCurrentScreen (positionX, positionY) {
const currentScreen = this.getCurrentScreen()
const closestScreen = this.screenModule.getDisplayNearestPoint({
x: positionX,
y: positionY,
})
this.positions[currentScreen.id] = this.positions[currentScreen.id] || {}
const position = this.positions[currentScreen.id]

if (currentScreen.id === closestScreen.id) {
if (position.customPosition) {
position.lastPosition = position.customPosition
}
position.customPosition = {
x: positionX,
y: positionY,
time: Date.now(),
}
} else if (position.customPosition && position.lastPosition) {
const timeDiff = (Date.now() - position.customPosition.time)
if (timeDiff < 50) {
// if second move event fired in quick succession then replace the
// current position with last to fix linux duplicate 'move' event bug
position.customPosition = position.lastPosition
}
return centerPosition
}
return null
}

getDisplayBelowCursor () {
return this.screenModule.getDisplayNearestPoint(this.screenModule.getCursorScreenPoint())
}

getCurrentScreen () {
return this.screens[this.getDisplayBelowCursor().id]
}

getAllScreens () {
return this.screens
}

resetScreens (windowWidth) {
this.screens = {}
this.screenModule.getAllDisplays().forEach((display) => {
this.screens[display.id] = display
this.screens[display.id].customPosition = {
x: Math.ceil(((display.bounds.x + (display.bounds.width / 2)) - (windowWidth / 2))),
y: Math.ceil(display.bounds.y + (display.bounds.height * 0.33)),
getCenterPositionOnCurrentScreen () {
const currentScreen = this.getCurrentScreen()
const position = this.positions[currentScreen.id] || {}
if (position.customPosition) {
return {
x: position.customPosition.x,
y: position.customPosition.y,
}
})
}
return {
x: Math.ceil(((currentScreen.bounds.x + (currentScreen.bounds.width / 2)) - (this.windowWidth / 2))),
y: Math.ceil(currentScreen.bounds.y + (currentScreen.bounds.height * 0.33)),
}
}
}

Expand Down

0 comments on commit 0db9cd0

Please sign in to comment.