forked from mike-schultz/materialette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (142 loc) · 3.51 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const electron = require('electron');
const { Tray, BrowserWindow, app } = electron;
// App Variables
const App = {
name: 'Materialette',
width: 370,
height: 410,
icon: __dirname + '/assets/Icon.png',
entry: __dirname + '/index.html'
};
const contextMenu = electron.Menu.buildFromTemplate([
{
label: 'About',
click() {
electron.dialog.showMessageBox({
title: "Materialette",
type: "info",
message: "Material Color Palette for macOS, Windows, and Linux",
buttons: ["Close"]
});
}
},
{
label: 'Website',
click() {
electron.shell.openExternal("https://github.com/mike-schultz/materialette");
}
},
{
type: 'separator'
},
{
label: 'Quit',
click() {
app.quit();
}
}
]);
let _tray;
let _window;
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
function createWindow() {
if (app.dock) {
app.dock.hide()
}
// Tray
_tray = new Tray(App.icon);
_tray.setToolTip(App.name);
// This will add a context menu on the
// app icon on right click for Windows
if (process.platform == 'win32') {
_tray.setContextMenu(contextMenu);
}
// Tray Events
_tray.on('click', toggleWindow);
_tray.on('double-click', toggleWindow);
// Window
_window = new BrowserWindow({
width: App.width,
height: App.height,
show: false,
frame: false,
alwaysOnTop: true
});
//Global shared object
global.sharedObj = {
hide: () => {
_window.hide()
},
quit: app.quit,
pinned: false
};
// Window Events
_window.on('show', () => {
_tray.setHighlightMode('always');
});
_window.on('hide', () => {
_tray.setHighlightMode('never');
});
_window.on('blur', () => {
if (!global.sharedObj.pinned) {
_window.hide();
}
});
_window.loadURL(`file://${App.entry}`);
_window.on('closed', () => {
delete _window;
});
}
function toggleWindow() {
moveWindow();
_window.isVisible() ? _window.hide() : _window.show();
}
function moveWindow() {
// Determine orientation.
let orientation = 'top-right';
let x = 0;
let y = 0;
const screen = (electron.screen.getDisplayNearestPoint(electron.screen.getCursorScreenPoint())).bounds;
const trayBounds = _tray.getBounds();
// Orientation is either not on top or OS is windows.
if (process.platform === 'win32') {
if (trayBounds.y > screen.height / 2) {
orientation = (trayBounds.x > screen.width / 2) ? 'bottom-right' : 'bottom-left';
} else {
orientation = (trayBounds.x > screen.width / 2) ? 'top-right' : 'top-left';
}
} else if (process.platform === 'darwin') {
orientation = 'top';
}
switch (orientation) {
case 'top':
x = Math.floor(trayBounds.x - App.width / 2 + trayBounds.width / 2);
y = trayBounds.y + trayBounds.height;
break;
case 'top-right':
x = screen.width - App.width;
break;
case 'bottom-left':
y = screen.height - App.height;
break;
case 'bottom-right':
y = screen.height - App.height;
x = screen.width - App.width;
break;
case 'top-left':
default:
x = 0;
y = 0;
}
// Normalize any out of bounds
// maxX accounts for multi-screen setups where x is the coordinate across multiple screens.
const maxX = screen.width + screen.x;
x = (x > maxX ) ? maxX - App.width : (x < 0) ? 0 : x;
y = (y > screen.height) ? screen.height - App.height : (y < 0) ? 0 : y;
_window.setPosition(x, y);
}