Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tray icon to show up to 10 recently updated notes #3104

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions browser/main/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Main extends React.Component {
}

this.toggleFullScreen = () => this.handleFullScreenButton()
this.probeTrayClose = () => this.handleTrayCloseProbe()
}

getChildContext() {
Expand Down Expand Up @@ -177,6 +178,11 @@ class Main extends React.Component {
delete CodeMirror.keyMap.emacs['Ctrl-V']

eventEmitter.on('editor:fullscreen', this.toggleFullScreen)
eventEmitter.on('tray:probe-close', this.probeTrayClose)
eventEmitter.on(
'menubar:togglemenubar',
this.toggleMenuBarVisible.bind(this)
)
eventEmitter.on(
'menubar:togglemenubar',
this.toggleMenuBarVisible.bind(this)
Expand All @@ -185,6 +191,7 @@ class Main extends React.Component {

componentWillUnmount() {
eventEmitter.off('editor:fullscreen', this.toggleFullScreen)
eventEmitter.off('tray:probe-close', this.probeTrayClose)
eventEmitter.off(
'menubar:togglemenubar',
this.toggleMenuBarVisible.bind(this)
Expand Down Expand Up @@ -294,6 +301,14 @@ class Main extends React.Component {
})
}

handleTrayCloseProbe(e) {
const { config } = this.props
if (!config.ui.closeToTray) {
console.log('handleTrayClose')
eventEmitter.emitIpc('tray:quit')
}
}

hideLeftLists(noteDetail, noteList, mainBody) {
this.setState({ noteDetailWidth: noteDetail.style.left })
this.setState({ mainBodyWidth: mainBody.style.left })
Expand Down
12 changes: 11 additions & 1 deletion browser/main/NoteList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class NoteList extends React.Component {
}

componentDidUpdate(prevProps) {
const { dispatch, location } = this.props
const { data, dispatch, location } = this.props
const { selectedNoteKeys } = this.state
const visibleNoteKeys = this.notes && this.notes.map(note => note.key)
const note = this.notes && this.notes[0]
Expand All @@ -159,6 +159,16 @@ class NoteList extends React.Component {
? prevKey
: note && note.key

console.log('tray:update trigger', data)
ee.emitIpc(
'tray:update',
data.noteMap
.map(note => note)
.sort(sortByUpdatedAt)
.slice(0, 10)
.filter(note => note.title !== '' && !note.isTrashed)
)

if (note && location.search === '') {
if (!location.pathname.match(/\/searched/))
this.contextNotes = this.getContextNotes()
Expand Down
1 change: 1 addition & 0 deletions browser/main/lib/ConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const DEFAULT_CONFIG = {
ui: {
language: 'en',
theme: 'default',
closeToTray: true,
showCopyNotification: true,
disableDirectWrite: false,
defaultNote: 'ALWAYS_ASK', // 'ALWAYS_ASK', 'SNIPPET_NOTE', 'MARKDOWN_NOTE'
Expand Down
7 changes: 6 additions & 1 deletion browser/main/lib/eventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ function emit(name, ...args) {
remote.getCurrentWindow().webContents.send(name, ...args)
}

function emitIpc(name, ...args) {
ipcRenderer.send(name, ...args)
}

export default {
emit,
on,
off,
once
once,
emitIpc
}
12 changes: 12 additions & 0 deletions browser/main/modals/PreferencesModal/UiTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class UiTab extends React.Component {
const newConfig = {
ui: {
theme: this.refs.uiTheme.value,
closeToTray: this.refs.closeToTray.checked,
language: this.refs.uiLanguage.value,
defaultNote: this.refs.defaultNote.value,
tagNewNoteWithFilteringTags: this.refs.tagNewNoteWithFilteringTags
Expand Down Expand Up @@ -351,6 +352,17 @@ class UiTab extends React.Component {
</label>
</div>
) : null}
<div styleName='group-checkBoxSection'>
<label>
<input
onChange={e => this.handleUIChange(e)}
checked={this.state.config.ui.closeToTray}
ref='closeToTray'
type='checkbox'
/>
&nbsp; Close main window to tray
</label>
</div>

<div styleName='group-header2'>Tags</div>

Expand Down
89 changes: 83 additions & 6 deletions lib/main-window.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const { app, BrowserWindow, Menu, MenuItem, Tray, ipcMain } = electron
const path = require('path')
const Config = require('electron-config')
const config = new Config()
const _ = require('lodash')
const manifest = require('../package.json')
const product = `${manifest.productName} ${manifest.version}`

var menu
// set up some chrome extensions
if (process.env.NODE_ENV === 'development') {
const {
Expand Down Expand Up @@ -78,7 +80,10 @@ mainWindow.webContents.sendInputEvent({
keyCode: '\u0008'
})

if (process.platform === 'darwin') {
if (
process.platform === 'darwin' ||
process.env.DESKTOP_SESSION === 'cinnamon'
) {
mainWindow.on('close', function(e) {
e.preventDefault()
if (mainWindow.isFullScreen()) {
Expand All @@ -91,11 +96,17 @@ if (process.platform === 'darwin') {
}
})

app.on('before-quit', function(e) {
mainWindow.removeAllListeners()
})
mainWindow.webContents.send('tray:probe-close')
}

app.on('before-quit', function(e) {
mainWindow.removeAllListeners()
})

app.on('window-all-closed', function() {
app.quit()
})

mainWindow.on('resize', _.throttle(storeWindowSize, 500))
mainWindow.on('move', _.throttle(storeWindowSize, 500))

Expand All @@ -113,4 +124,70 @@ app.on('activate', function() {
mainWindow.show()
})

ipcMain.on('tray:update', handleTrayUpdate)

ipcMain.on('tray:quit', function(e, notes) {
ipcMain.removeListener('tray:update', handleTrayUpdate)
menu = null
app.quit()
})

function handleTrayUpdate(e, notes) {
updateTray(notes)
}

function updateTray(notes) {
const menu = new Menu()

menu.append(
new MenuItem({
label: `Open ${product}`,
click: function() {
mainWindow.show()
}
})
)

if (notes && notes.length) {
menu.append(new MenuItem({ type: 'separator' }))
notes.forEach(note => {
menu.append(
new MenuItem({
label: note.title,
click: function() {
mainWindow.webContents.send('list:jump', note.key)
mainWindow.show()
}
})
)
})
menu.append(new MenuItem({ type: 'separator' }))
}

menu.append(
new MenuItem({
label: 'Quit',
click: function() {
app.quit()
}
})
)

tray.setContextMenu(menu)

return menu
}

const tray = new Tray(
path.join(__dirname, '../resources/[email protected]')
)
menu = updateTray()
const displayTray = function(e) {
e.preventDefault()
tray.popUpContextMenu(menu)
}

tray.setToolTip(product)
tray.on('click', displayTray)

module.exports = mainWindow
1 change: 1 addition & 0 deletions tests/lib/boostnoterc/.boostnoterc.all
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"sortBy": "UPDATED_AT",
"sortTagsBy": "ALPHABETICAL",
"ui": {
"closeToTray": false,
"defaultNote": "ALWAYS_ASK",
"disableDirectWrite": false,
"theme": "default"
Expand Down