This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
134 lines (112 loc) · 3.17 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
const { app, BrowserWindow, Menu, dialog, shell, Tray } = require('electron')
const path = require('path')
const url = require('url')
const os = require('os')
const settings = require('./settings')
const TrayInit = require('./tray')
const menuTemplate = require('./menu')
var browserWindow
function createWindow () {
browserWindow = new BrowserWindow({
height: 600,
icon: 'assets/icon.ico',
kiosk: false, // TODO: Reimplement
title: 'Home Assistant',
titleBarStyle: 'hidden',
width: 800
})
browserWindow.os = os.platform()
browserWindow.settings = settings
loadHomeAssistantOrLoginPage()
// Change window dimensions if provided
if (settings.hasDimensions()) {
browserWindow.setContentSize(settings.width(), settings.height(), false)
}
// Change window position if provided
if (settings.hasPosition()) {
browserWindow.setPosition(settings.xpos(), settings.ypos())
}
createMenu()
// Connect to home assistant
browserWindow.connect = (url, password) => {
browserWindow.url = url
browserWindow.password = password
settings.setUrlAndPassword(url, password)
loadHomeAssistantOrLoginPage()
}
// Save settings and reconnect to home assistant
browserWindow.reload = () => {
loadHomeAssistantOrLoginPage()
}
// Notifiy UI that the theme color has changed
browserWindow.setColor = (color) => {
browserWindow.webContents.send('colorChange', {color})
}
// Reset EVERYTHING
browserWindow.reset = () => {
settings.deleteAll()
app.quit()
}
browserWindow.log = (msg) => console.log(msg)
browserWindow.on('closed', () => {
browserWindow = null
})
// Save window bounds on close
browserWindow.on('close', () => {
settings.saveWindowBounds(browserWindow.getPosition()[0], browserWindow.getPosition()[1],
browserWindow.getBounds().width, browserWindow.getBounds().height)
})
}
app.on('ready', createWindow)
// Just OSX-Stuff
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (browserWindow === null) {
createWindow()
}
})
/**
* Fake basic-auth
*/
app.on('login', function (event, webContents, request, authInfo, callback) {
event.preventDefault()
let credentials = settings.configuratorCredentials()
callback(credentials.username, credentials.password)
})
/**
* Create the application menu
*/
function createMenu () {
const menu = Menu.buildFromTemplate(menuTemplate(browserWindow, os.platform() === 'darwin', load, setPage))
Menu.setApplicationMenu(menu)
}
// Load a HTML page
function load (html) {
browserWindow.loadURL(url.format({
pathname: path.join(__dirname, 'src', html),
protocol: 'file:',
slashes: true
}))
}
// Switch HomeAssistant page (eg. State)
function setPage (page) {
browserWindow.webContents.send('change', { page })
}
// Load HomeAssistant or configuration view
function loadHomeAssistantOrLoginPage () {
if (settings.kiosk()) {
browserWindow.setFullScreen(true)
}
if (settings.hasUrl()) {
load('index.html')
if (settings.tray()) {
TrayInit(settings.url(), settings.password(), settings.icon())
}
} else {
load('connect.html')
}
}