This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
/
abr-window.js
137 lines (121 loc) · 4.22 KB
/
abr-window.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
/*
* Abricotine - Markdown Editor
* Copyright (c) 2015 Thomas Brouard
* Licensed under GNU-GPLv3 <http://www.gnu.org/licenses/gpl.html>
*/
var AbrMenu = require.main.require("./abr-menu.js"),
BrowserWindow = require("browser-window"),
constants = require.main.require("./constants.js"),
contextMenuTemplate = require.main.require("./menu-context.json"),
defaultConfig = require.main.require("../default/config.json"),
menuTemplate = require.main.require("./menu-window.json"),
nconf = require('nconf'),
windowStateKeeper = require('electron-window-state');
function alreadyOpen (abrApp, path) {
if (!path || !abrApp) {
return;
}
var windows = abrApp.windows;
for (var i=0; i<windows.length; i++) {
if (!windows[i]) continue;
if (windows[i].path === path) {
return windows[i];
}
}
return false;
}
// Config creation (config is specific to the window)
function createConfig () {
var config = new nconf.Provider(); // https://github.com/indexzero/nconf/issues/39
config.overrides({})
.argv()
.file(constants.path.userConfig)
.defaults(defaultConfig);
return config;
}
function AbrWindow (abrApp, path) {
this.abrApp = abrApp;
if (path) {
this.path = path;
// Check if the called doc is already open
var docWin = alreadyOpen(abrApp, path);
if (docWin && docWin.browserWindow) {
docWin.browserWindow.show();
return;
}
}
this.config = createConfig();
var cloneTemplate = JSON.parse(JSON.stringify(menuTemplate)); // Electron modifies the template while building the menu so we need to clone it before
this.menu = new AbrMenu(this, cloneTemplate, this.config);
// Context
this.contextMenu = new AbrMenu(this, contextMenuTemplate, this.config);
this.open();
}
AbrWindow.prototype = {
// Send and exec a command in window
execCommand: function (command, parameters) {
this.browserWindow.webContents.send("command", command, parameters);
},
// Open a new window
open: function () {
var abrWin = this,
abrApp = this.abrApp,
config = this.config,
win = null;
// Update application menu
this.menu.attach();
// Create and open window
var showMenubar = config.get("window:showMenuBar"),
mainWindowState = windowStateKeeper({
path: constants.path.userData,
defaultWidth: 800,
defaultHeight: 600
});
win = new BrowserWindow({
title: constants.appName || "Abricotine",
icon: constants.path.icon,
"min-width": 100,
"min-height": 100,
"x": mainWindowState.x,
"y": mainWindowState.y,
"width": mainWindowState.width,
"height": mainWindowState.height,
"auto-hide-menu-bar": typeof showMenubar !== "undefined" ? !showMenubar : false
});
mainWindowState.manage(win);
// Register window in abrWin
this.browserWindow = win;
this.id = win.id;
// Register this in abrApp
abrApp.windows[win.id] = this;
// Prepare startup commands
var execStartupCommands = function () {
var startupCommands = abrWin.config.get("startup-commands");
for (var cmdName in startupCommands) {
if (startupCommands[cmdName]) {
abrWin.execCommand(cmdName);
}
}
};
// Set event handlers
win.webContents.on("dom-ready", function () {
execStartupCommands();
win.show(); // NOTE: win.show() and win.focus() seems to be broken in Linux
});
win.on("focus", function () {
abrWin.menu.attach();
});
win.on("closed", function () {
// Destroy the window
abrApp.windows[win.id] = null;
win = null;
});
// Load window
win.loadUrl("file://" + constants.path.window);
// Open devtools on debug mode
if (this.config.get("debug")) {
win.openDevTools();
}
}
};
module.exports = AbrWindow;