This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
119 lines (99 loc) · 2.74 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const electron = require('electron')
const {
app,
BrowserWindow,
globalShortcut,
ipcMain,
Tray,
Menu
} = require('electron')
const path = require('path')
const url = require('url')
const ClipboardWatcher = require('./lib/ClipboardWatcher')
const { RELOAD_ENTRIES, SUBMIT_ENTRY, FIX_ENTRY } = require('./lib/EventTypes')
const SUBMIT_TIMEOUT = 1500
let tray
let mainWindow
const clipboardWatcher = new ClipboardWatcher()
let submitTimeoutId
function createWindow () {
const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize
const windowWidth = Math.floor(width * 0.8)
const windowHeight = 400
// Create the browser window.
mainWindow = new BrowserWindow({
width: windowWidth,
height: windowHeight,
center: true,
show: false,
frame: false,
transparent: true
})
mainWindow.setPosition(Math.floor((width - windowWidth) / 2), Math.floor(height * 0.4))
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools()
}
}
function createTray () {
tray = new Tray(path.join(__dirname, 'assets/icon20.png'))
const contextMenu = Menu.buildFromTemplate([
{label: 'Quit CycleClipboard', role: 'quit'}
])
tray.setContextMenu(contextMenu)
}
app.on('ready', () => {
createWindow()
createTray()
if (app.dock) app.dock.hide() // Don't show on dock
registerGlobalShortcut()
clipboardWatcher.startPolling()
})
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
clipboardWatcher.destroy()
})
// submit when press Enter key on mainWindow
ipcMain.on(SUBMIT_ENTRY, (_event) => {
submit()
})
function registerGlobalShortcut () {
globalShortcut.register('Command+Control+V', () => {
resetAutoSubmit()
const nextEntries = clipboardWatcher.getNextEntries()
mainWindow.webContents.send(RELOAD_ENTRIES, nextEntries)
if (!mainWindow.isVisible()) mainWindow.show()
mainWindow.focus()
// auto submit
submitTimeoutId = setTimeout(() => {
submit()
}, SUBMIT_TIMEOUT)
})
}
function submit () {
resetAutoSubmit()
clipboardWatcher.writeFirstEntry()
clipboardWatcher.resetIndex()
startFixEntryAnimation()
// wait for animation
.then(() => {
mainWindow.hide()
Menu.sendActionToFirstResponder('hide:') // 前のアプリにフォーカスを戻す
})
}
function resetAutoSubmit () {
if (submitTimeoutId) clearTimeout(submitTimeoutId)
}
function startFixEntryAnimation () {
return new Promise((resolve) => {
mainWindow.webContents.send(FIX_ENTRY)
setTimeout(resolve, 500)
})
}