From 085b7643c40d8762462ce51cbb26a8df048e6546 Mon Sep 17 00:00:00 2001 From: Alex Crooks Date: Wed, 19 Dec 2018 10:40:34 +0000 Subject: [PATCH] Improved reliability of app-ready-window-show Throttle post time button to prevent duplicate time Fix for search input focus --- index.js | 222 +++++++++++----------- package-lock.json | 5 + package.json | 3 +- src/containers/search/search-container.js | 24 +-- src/containers/timer/timer-container.js | 3 +- 5 files changed, 127 insertions(+), 130 deletions(-) diff --git a/index.js b/index.js index 4c79ec7..a087fc2 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ log.info(app.getName()) log.info(app.getVersion()) console.log(app.getName(), app.getVersion()) -// resolve user $PATH env variable +// Resolve user $PATH env variable require('fix-path')() if (process.env.NODE_ENV === 'development') @@ -42,123 +42,122 @@ const installExtensions = () => { } }) } +// For copy and paste to work within the menubar we +// need to enable the OS standard Edit menus :( +const menu = Menu.buildFromTemplate([ + { + label: 'Edit', + submenu: [ + {role: 'undo'}, + {role: 'redo'}, + {type: 'separator'}, + {role: 'cut'}, + {role: 'copy'}, + {role: 'paste'}, + {role: 'pasteandmatchstyle'}, + {role: 'delete'}, + {role: 'selectall'} + ] + } +]) + +Menu.setApplicationMenu(menu) let mb = null let credentials = null let jiraUserKey = null let windowVisible = true -Worklogs.getCredentialsFromKeyChain() - .then(keyChainCredentials => { - credentials = keyChainCredentials - console.log('Credentials ready') - launchMenuBar() - }) - .catch(launchMenuBar) +app.on('ready', () => { + installExtensions() + + // Get our keychain credentials and launch menu bar on success or failure + Worklogs.getCredentialsFromKeyChain() + .then(keyChainCredentials => { + credentials = keyChainCredentials + console.log('Credentials ready') + launchMenuBar() + }) + .catch(launchMenuBar) +}) function launchMenuBar () { - app.on('ready', () => { - console.log('App is ready') - - // For copy and paste to work within the menubar we - // need to enable the OS standard Edit menus :( - const menu = Menu.buildFromTemplate([ - { - label: 'Edit', - submenu: [ - {role: 'undo'}, - {role: 'redo'}, - {type: 'separator'}, - {role: 'cut'}, - {role: 'copy'}, - {role: 'paste'}, - {role: 'pasteandmatchstyle'}, - {role: 'delete'}, - {role: 'selectall'} - ] + + // transparency workaround https://github.com/electron/electron/issues/2170 + setTimeout(() => { + + mb = menubar({ + alwaysOnTop: process.env.NODE_ENV === 'development', + icon: path.join(app.getAppPath(), '/static/tray-dark.png'), + width: 500, + minWidth: 500, + maxWidth: 500, + minHeight: 20, + hasShadow: false, + preloadWindow: true, + resizable: false, + useContentSize: false, + transparent: true, + frame: false, + toolbar: false + }) + + console.log('MB Created') + + mb.window.credentials = credentials + + // Start event handling for the auto updater + const autoUpdater = new Updater(mb.window.webContents, log) + autoUpdater.handleEvents() + + mb.on('ready', () => { + console.log('Menubar ready') + mb.tray.setTitle(' Login') + }) + + mb.on('show', () => { + windowVisible = true + + mb.tray.setImage(path.join(app.getAppPath(), '/static/tray-dark-active.png')) + + if (jiraUserKey) { + + let renderProcess = mb.window.webContents + + // Tell the main process the window is visible + renderProcess.send('windowVisible') + + Worklogs.checkLock(true) + .then(() => { + console.log('Telling render process we are fetching worklogs') + renderProcess.send('fetchingWorklogs') + + let fullWeek = false + + Worklogs.request(jiraUserKey, fullWeek, true) + .then(worklogs => { + console.log('All good', worklogs.length) + renderProcess.send('worklogs', JSON.stringify({ + fullWeek, + worklogs + })) + }) + .catch(error => { + console.error('Error fetching worklogs', error) + renderProcess.send('worklogs', JSON.stringify([])) + }) + }) + .catch(error => { + renderProcess.send('worklogs', JSON.stringify([])) + }) } - ]) - - Menu.setApplicationMenu(menu) - - installExtensions() - - // transparency workaround https://github.com/electron/electron/issues/2170 - setTimeout(() => { - - mb = menubar({ - alwaysOnTop: process.env.NODE_ENV === 'development', - icon: path.join(app.getAppPath(), '/static/tray-dark.png'), - width: 500, - minWidth: 500, - maxWidth: 500, - minHeight: 20, - hasShadow: false, - preloadWindow: true, - resizable: false, - useContentSize: false, - transparent: true, - frame: false, - toolbar: false - }) - - console.log('MB Created') - - mb.window.credentials = credentials - - // Start event handling for the auto updater - const autoUpdater = new Updater(mb.window.webContents, log) - autoUpdater.handleEvents() - - mb.on('ready', () => { - console.log('Menubar ready') - mb.tray.setTitle(' Login') - }) - - mb.on('show', () => { - windowVisible = true - - mb.tray.setImage(path.join(app.getAppPath(), '/static/tray-dark-active.png')) - - if (jiraUserKey) { - - let renderProcess = mb.window.webContents - - // Tell the main process the window is visible - renderProcess.send('windowVisible') - - Worklogs.checkLock(true) - .then(() => { - console.log('Telling render process we are fetching worklogs') - renderProcess.send('fetchingWorklogs') - - let fullWeek = false - - Worklogs.request(jiraUserKey, fullWeek, true) - .then(worklogs => { - console.log('All good', worklogs.length) - renderProcess.send('worklogs', JSON.stringify({ - fullWeek, - worklogs - })) - }) - .catch(error => { - console.error('Error fetching worklogs', error) - renderProcess.send('worklogs', JSON.stringify([])) - }) - }) - .catch(error => { - renderProcess.send('worklogs', JSON.stringify([])) - }) - } - }) - - mb.on('hide', () => { - windowVisible = false - mb.tray.setImage(path.join(app.getAppPath(), '/static/tray-dark.png')) - }) - }, 100) - }) + }) + + mb.on('hide', () => { + windowVisible = false + mb.tray.setImage(path.join(app.getAppPath(), '/static/tray-dark.png')) + }) + }, 100) } // Quit when all windows are closed. @@ -178,10 +177,8 @@ ipcMain.on('quit', () => { ipcMain.on('windowSizeChange', (event, newHeight) => { const [currentWidth, currentHeight] = mb.window.getSize() - console.log('newHeight', newHeight) - mb.window.setSize(currentWidth, newHeight) -}); +}) ipcMain.on('openDevTools', (event) => { mb.window.webContents.openDevTools({ mode: 'detach' }) @@ -206,7 +203,6 @@ ipcMain.on('deletePassword', (event) => { }) ipcMain.on('fetchWorklogs', (event, args) => { - let { userKey, fullWeek } = args jiraUserKey = userKey diff --git a/package-lock.json b/package-lock.json index 1303a27..7aca5b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8093,6 +8093,11 @@ "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=" }, + "lodash.throttle": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.throttle/-/lodash.throttle-4.1.1.tgz", + "integrity": "sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ=" + }, "log-symbols": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.2.0.tgz", diff --git a/package.json b/package.json index fa77ba7..763aaa0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jira-timer", - "version": "1.0.18", + "version": "1.0.17", "description": "Jira Timer", "productName": "Jira Timer", "main": "index.js", @@ -94,6 +94,7 @@ "lodash.orderby": "^4.6.0", "lodash.remove": "^4.7.0", "lodash.sortby": "^4.7.0", + "lodash.throttle": "^4.1.1", "menubar": "github:mantou132/menubar#master", "node-loader": "^0.6.0", "object-get": "^2.1.0", diff --git a/src/containers/search/search-container.js b/src/containers/search/search-container.js index 7914d61..698a582 100644 --- a/src/containers/search/search-container.js +++ b/src/containers/search/search-container.js @@ -31,30 +31,24 @@ class SearchContainer extends Component { this.triggerChange = this.triggerChange.bind(this) this.onAddTimer = this.onAddTimer.bind(this) this.onClearSearch = this.onClearSearch.bind(this) - } - - componentWillMount () { - this.searchTimer = null - } - componentDidMount () { // When the user opens the window lets focus the search input ipcRenderer.on('windowVisible', () => { console.log('windowVisible') - this.focusSearch() + if (this.searchInput.current) + this.searchInput.current.focus() }) } + componentWillMount () { + this.searchTimer = null + } + onAddTimer (id, key, summary) { this.props.addTimer(id, key, summary) this.onClearSearch() } - focusSearch () { - console.log('Focusing search') - this.searchInput.current.focus() - } - onClearSearch () { this.setState({ query: '', @@ -97,7 +91,7 @@ class SearchContainer extends Component { cursor: prevState.cursor - 1 })) - this.scrollActiveTaskIntoView() + this.scrollActiveTaskIntoView() break // Down arrow @@ -109,14 +103,14 @@ class SearchContainer extends Component { cursor: prevState.cursor + 1 })) - this.scrollActiveTaskIntoView() + this.scrollActiveTaskIntoView() break } } scrollActiveTaskIntoView () { let itemComponent = this.refs.activeItem - console.log({itemComponent}) + //console.log('itemComponent', itemComponent) if (itemComponent) { let domNode = ReactDOM.findDOMNode(itemComponent) domNode.scrollIntoView(true) diff --git a/src/containers/timer/timer-container.js b/src/containers/timer/timer-container.js index a08f98b..100a744 100644 --- a/src/containers/timer/timer-container.js +++ b/src/containers/timer/timer-container.js @@ -3,6 +3,7 @@ import React, { Component, Fragment } from 'react' import { connect } from 'react-redux' import styled from 'styled-components' import find from 'lodash.find' +import throttle from 'lodash.throttle' import parseDuration from 'parse-duration' import { formatSecondsToStopWatch, roundToNearestMinutes, secondsHuman } from '../../lib/time' import { openInJira } from '../../lib/jira' @@ -38,7 +39,7 @@ class TimerContainer extends Component { this.onTimeChanged = this.onTimeChanged.bind(this) this.onEditTime = this.onEditTime.bind(this) this.onResetEditTime = this.onResetEditTime.bind(this) - this.onEditComment = this.onEditComment.bind(this) + this.onEditComment = throttle(this.onEditComment.bind(this), 1000) this.onResetEditComment = this.onResetEditComment.bind(this) this.onCommentSaved = this.onCommentSaved.bind(this) this.onPlay = this.onPlay.bind(this)