-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (65 loc) · 2.6 KB
/
app.js
File metadata and controls
76 lines (65 loc) · 2.6 KB
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
const { app, Tray } = require('electron');
const path = require('path');
const Store = require('./store.js');
const {ipcMain} = require('electron');
const Window = require('./Window');
let mainWindow; //do this so that the window object doesn't get GC'd
require('electron-reload')(__dirname);
// First instantiate the class
const store = new Store({
// We'll call our data file 'user-preferences'
configName: 'user-preferences',
defaults: {
// 800x600 is the default size of our window
windowBounds: { width: 800, height: 600 }
}
});
const bookStore = new Store({
configName: 'books',
defaults: {
books: []
}
});
let books = []
// When our app is ready, we'll create our BrowserWindow
app.on('ready', function() {
// First we'll get our height and width. This will be the defaults if there wasn't anything saved
let { width, height } = store.get('windowBounds');
const appIcon = new Tray('images/logo.png');
mainWindow = new Window({
file: path.join('views', 'index.html'),
windowSettings: { width, height },
icon: 'images/logo.png'
});
mainWindow.webContents.on('did-finish-load', (event, url) => {
event.preventDefault()
books = bookStore.get('books');
mainWindow.webContents.send('displayBook', books);
})
// delete book
ipcMain.on('changePage', (event, file) => {
books = bookStore.get('books');
mainWindow.loadURL('file://' + path.join(__dirname, 'views/' + file + '.html'));
mainWindow.webContents.send('displayBook', books);
})
// delete book
ipcMain.on('deleteBook', (event, book) => {
const updatedBooks = bookStore.delete(book);
mainWindow.webContents.send('displayBook', updatedBooks);
})
// add book
ipcMain.on('addBook', (event, book) => {
bookStore.add('books', book);
mainWindow.loadURL('file://' + path.join(__dirname, 'views/index.html'));
})
// The BrowserWindow class extends the node.js core EventEmitter class, so we use that API
// to listen to events on the BrowserWindow. The resize event is emitted when the window size changes.
mainWindow.on('resize', () => {
// The event doesn't pass us the window size, so we call the `getBounds` method which returns an object with
// the height, width, and x and y coordinates.
let { width, height } = mainWindow.getBounds();
// Now that we have them, save them using the `set` method.
store.set('windowBounds', { width, height });
});
mainWindow.loadURL('file://' + path.join(__dirname, 'views/splash.html'));
});